-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.py
45 lines (39 loc) · 1.25 KB
/
sync.py
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
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import json
class Controller(BaseHTTPRequestHandler):
def sync(self, parent, children):
# Compute status based on observed state.
desired_status = {
"pods": len(children["Pod.v1"])
}
# Generate the desired child object(s).
who = parent.get("spec", {}).get("who", "World")
desired_pods = [
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": parent["metadata"]["name"]
},
"spec": {
"restartPolicy": "OnFailure",
"containers": [
{
"name": "hello",
"image": "busybox",
"command": ["echo", "Hello, %s!" % who]
}
]
}
}
]
return {"status": desired_status, "children": desired_pods}
def do_POST(self):
# Serve the sync() function as a JSON webhook.
observed = json.loads(self.rfile.read(int(self.headers.getheader("content-length"))))
desired = self.sync(observed["parent"], observed["children"])
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(desired))
HTTPServer(("", 80), Controller).serve_forever()