|
| 1 | +import json |
| 2 | +import kopf |
| 3 | +from kubernetes import client |
| 4 | + |
| 5 | + |
| 6 | +DOMAIN = "kool.karmalabs.local" |
| 7 | +goodbrands = ['coleclark', 'fender', 'gibson', 'ibanez', 'martin', 'seagull', 'squier', 'washburn'] |
| 8 | +badbrands = ['epiphone', 'guild', 'gretsch', 'jackson', 'ovation', 'prs', 'rickenbauer', 'taylor', 'yamaha'] |
| 9 | + |
| 10 | + |
| 11 | +def review_guitar(name, namespace): |
| 12 | + configuration = client.Configuration() |
| 13 | + configuration.assert_hostname = False |
| 14 | + api_client = client.api_client.ApiClient(configuration=configuration) |
| 15 | + crds = client.CustomObjectsApi(api_client) |
| 16 | + guitar = crds.get_namespaced_custom_object(DOMAIN, 'v1', namespace, 'guitars', name) |
| 17 | + metadata = guitar.get("metadata") |
| 18 | + if not metadata: |
| 19 | + print("No metadata in object, skipping: %s" % json.dumps(guitar, indent=1)) |
| 20 | + return |
| 21 | + name = metadata.get("name") |
| 22 | + namespace = metadata.get("namespace") |
| 23 | + guitar["spec"]["review"] = True |
| 24 | + brand = guitar["spec"]["brand"] |
| 25 | + if brand in goodbrands: |
| 26 | + guitar["spec"]["comment"] = "this is a great instrument" |
| 27 | + elif brand in badbrands: |
| 28 | + guitar["spec"]["comment"] = "this is shit" |
| 29 | + else: |
| 30 | + guitar["spec"]["comment"] = "nobody knows this brand" |
| 31 | + print("Updating: %s" % name) |
| 32 | + crds.replace_namespaced_custom_object(DOMAIN, "v1", namespace, "guitars", name, guitar) |
| 33 | + |
| 34 | + |
| 35 | +@kopf.on.create('kool.karmalabs.local', 'v1', 'guitars') |
| 36 | +# def create_fn(body, **kwargs): |
| 37 | +def create_fn(meta, spec, namespace, logger, **kwargs): |
| 38 | + name = meta.get('name') |
| 39 | + print("Handling %s" % (name)) |
| 40 | + done = spec.get("review", False) |
| 41 | + if done: |
| 42 | + return |
| 43 | + else: |
| 44 | + review_guitar(name, namespace) |
0 commit comments