This repository has been archived by the owner on Oct 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
5_demo_kubernetes.bal
77 lines (69 loc) · 2.16 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
75
76
77
// Add kubernetes package and annotations
// To build kubernetes artifacts:
// ballerina build 5_demo_kubernetes.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 ballerina/kubernetes;
import ballerina/stringutils;
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:latest",
name: "ballerina-demo"
}
// Pass our config file into the image.
@kubernetes:ConfigMap{
conf: "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 (!stringutils:contains(payload, "#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(<@untainted> myJson);
_ = check caller->respond(res);
return;
}
}