This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
controller.py
67 lines (60 loc) · 2.49 KB
/
controller.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import json
import yaml
from kubernetes import client, config, watch
import os
DOMAIN = "kool.karmalabs.local"
goodbrands = ['coleclark', 'fender', 'gibson', 'ibanez', 'martin', 'seagull', 'squier', 'washburn']
badbrands = ['epiphone', 'guild', 'gretsch', 'jackson', 'ovation', 'prs', 'rickenbauer', 'taylor', 'yamaha']
def review_guitar(crds, obj):
metadata = obj.get("metadata")
if not metadata:
print("No metadata in object, skipping: %s" % json.dumps(obj, indent=1))
return
name = metadata.get("name")
namespace = metadata.get("namespace")
obj["spec"]["review"] = True
brand = obj["spec"]["brand"]
if brand in goodbrands:
obj["spec"]["comment"] = "this is a great instrument"
elif brand in badbrands:
obj["spec"]["comment"] = "this is shit"
else:
obj["spec"]["comment"] = "nobody knows this brand"
print("Updating: %s" % name)
crds.replace_namespaced_custom_object(DOMAIN, "v1", namespace, "guitars", name, obj)
if __name__ == "__main__":
if 'KUBERNETES_PORT' in os.environ:
config.load_incluster_config()
definition = '/tmp/guitar.yml'
else:
config.load_kube_config()
definition = 'guitar.yml'
configuration = client.Configuration()
configuration.assert_hostname = False
api_client = client.api_client.ApiClient(configuration=configuration)
v1 = client.ApiextensionsV1beta1Api(api_client)
current_crds = [x['spec']['names']['kind'].lower() for x in v1.list_custom_resource_definition().to_dict()['items']]
if 'guitar' not in current_crds:
print("Creating guitar definition")
with open(definition) as data:
body = yaml.load(data)
v1.create_custom_resource_definition(body)
crds = client.CustomObjectsApi(api_client)
print("Waiting for Guitars to come up...")
resource_version = ''
while True:
stream = watch.Watch().stream(crds.list_cluster_custom_object, DOMAIN, "v1", "guitars", resource_version=resource_version)
for event in stream:
obj = event["object"]
operation = event['type']
spec = obj.get("spec")
if not spec:
continue
metadata = obj.get("metadata")
resource_version = metadata['resourceVersion']
name = metadata['name']
print("Handling %s on %s" % (operation, name))
done = spec.get("review", False)
if done:
continue
review_guitar(crds, obj)