forked from ballerina-attic/ballerina-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
5_demo_kubernetes.bal
74 lines (66 loc) · 2.07 KB
/
5_demo_kubernetes.bal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Add kubernetes package and annotations
// To build kubernetes artifacts:
// ballerina build demo.bal
// To run it:
// kubectl apply -f kubernetes/
// To see the pod:
// kubectl get pods
// To see the service:
// kubectl get svc
// To invoke:
// curl -X POST -d "Hello from K8S" localhost:<put the port that you get from kubectl get svc>
// To clean up:
// kubectl delete -f kubernetes/
import ballerina/config;
import ballerina/http;
import wso2/twitter;
// Add kubernetes package
import ballerinax/kubernetes;
twitter:Client tw = new({
clientId: config:getAsString("clientId"),
clientSecret: config:getAsString("clientSecret"),
accessToken: config:getAsString("accessToken"),
accessTokenSecret: config:getAsString("accessTokenSecret"),
clientConfig:{}
});
// Now instead of inline {port:9090} bind we create a separate endpoint.
// We need this so we can add Kubernetes notation to it and tell the compiler
// to generate a Kubernetes services (expose it to the outside world).
@kubernetes:Service {
serviceType: "NodePort",
name: "ballerina-demo"
}
listener http:Listener cmdListener = new(9090);
// Instruct the compiler to generate Kubernetes deployment artifacts
// and a docker image out of this Ballerina service.
@kubernetes:Deployment {
image: "demo/ballerina-demo",
name: "ballerina-demo"
}
// Pass our config file into the image.
@kubernetes:ConfigMap{
ballerinaConf: "twitter.toml"
}
@http:ServiceConfig {
basePath: "/"
}
service hello on cmdListener {
@http:ResourceConfig {
path: "/",
methods: ["POST"]
}
resource function hi (http:Caller caller, http:Request request) returns error? {
string payload = check request.getTextPayload();
if (!payload.contains("#ballerina")){payload=payload+" #ballerina";}
twitter:Status st = check tw->tweet(payload);
json myJson = {
text: payload,
id: st.id,
agent: "ballerina"
};
http:Response res = new;
res.setPayload(untaint myJson);
_ = check caller->respond(res);
return;
}
}