Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

Commit a216fea

Browse files
authored
Merge pull request #4 from karmab/kopf
sample with kopf
2 parents fb0b452 + 730d549 commit a216fea

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ oc expose svc sampleui
5151

5252
You can also have a look at [initializer](initializer) for extra code around Initializers
5353

54+
Also, if you prefer to use kopf, you can use the code in `kopf_handler.py`
55+
5456
## Copyright
5557

5658
Copyright 2017 Karim Boumedhel

kopf_handler.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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

Comments
 (0)