-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-analyzer-part2.py
195 lines (176 loc) · 6.21 KB
/
text-analyzer-part2.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import kopf
import kubernetes.config as k8s_config
import kubernetes.client as k8s_client
import logging
import zlib
import hashlib
# -------------
# Compute hash function
# -------------
def compute_hash(_text, _hash):
if _hash == "CRC32":
return hex(zlib.crc32(bytes(_text, encoding='utf-8')) & 0xffffffff)
if _hash == "MD5":
return hashlib.md5(bytes(_text, encoding='utf-8')).hexdigest()
if _hash == "SHA512":
return hashlib.sha512(bytes(_text, encoding='utf-8')).hexdigest()
text_analyzer_crd = k8s_client.V1CustomResourceDefinition(
api_version="apiextensions.k8s.io/v1",
kind="CustomResourceDefinition",
metadata=k8s_client.V1ObjectMeta(name="textanalyzers.operators.mytest.it", namespace="mytest"),
spec=k8s_client.V1CustomResourceDefinitionSpec(
group="operators.mytest.it",
versions=[k8s_client.V1CustomResourceDefinitionVersion(
name="v1",
served=True,
storage=True,
schema=k8s_client.V1CustomResourceValidation(
open_apiv3_schema=k8s_client.V1JSONSchemaProps(
type="object",
properties={
"spec": k8s_client.V1JSONSchemaProps(
type="object",
properties={
"text": k8s_client.V1JSONSchemaProps(type="string"),
"hash": k8s_client.V1JSONSchemaProps(type="string", enum=["CRC32", "MD5", "SHA512"])
}
),
"status": k8s_client.V1JSONSchemaProps(
type="object",
x_kubernetes_preserve_unknown_fields=True
)
}
)
),
additional_printer_columns=[k8s_client.V1CustomResourceColumnDefinition(
name="version",
type="string",
json_path=".apiVersion",
priority=0
), k8s_client.V1CustomResourceColumnDefinition(
name="hash",
type="string",
json_path=".spec.hash",
priority=1
), k8s_client.V1CustomResourceColumnDefinition(
name="text",
type="string",
json_path=".spec.text",
priority=1
)],
)],
scope="Namespaced",
names=k8s_client.V1CustomResourceDefinitionNames(
plural="textanalyzers",
singular="textanalyzer",
kind="TextAnalyzer",
short_names=["tan"]
)
)
)
# ------------------------
# LOADING
# ------------------------
try:
logging.info("Trying load kube config")
k8s_config.load_kube_config()
logging.info("Done!")
except k8s_config.ConfigException:
k8s_config.load_incluster_config()
# ------------------------
# CRD Creation
# ------------------------
api_instance = k8s_client.ApiextensionsV1Api()
try:
logging.info("Trying CRD install")
api_instance.create_custom_resource_definition(text_analyzer_crd)
logging.info("CRD Installed")
except k8s_client.rest.ApiException as e:
if e.status == 409:
logging.info("CRD already exists")
else:
raise e
# ------------------------
# CREATE
# ------------------------
@kopf.on.create('operators.mytest.it', 'v1', 'textanalyzers')
def on_create(spec, name, namespace, logger, **kwargs):
_text = spec['text']
_hash = spec['hash']
_h = compute_hash(_text, _hash)
logging.info("CREATE - " + _text)
logging.info("{}: {}".format(_hash, _h))
body = {
'apiVersion': 'v1',
'kind': 'ConfigMap',
'metadata': {
'name': name + '-cm'
},
'data': {
'hash': _hash,
'val': _h
}
}
# Adopting references ConfigMap as child of current Custom Resource
# "metadata": {
# "name": "test-analyzer-1-cm",
# "ownerReferences": [
# {
# "controller": true,
# "blockOwnerDeletion": true,
# "apiVersion": "operators.mytest.it/v1",
# "kind": "TextAnalyzer",
# "name": "test-analyzer-1",
# "uid": "..."
# }
logging.info("Adopting ConfigMap")
kopf.adopt(body)
logging.info("Done!")
try:
logging.info("Creating ConfigMap %s" % (name + '-cm'))
resp = k8s_client.CoreV1Api().create_namespaced_config_map(
body=body,
namespace='mytest'
)
logging.info(resp)
except k8s_client.ApiException as e:
logging.error("Exception when calling CoreV1Api->create_namespaced_config_map: %s\n" % e)
return {"hash": _h}
# ------------------------
# UPDATE
# ------------------------
@kopf.on.update('operators.mytest.it', 'v1', 'textanalyzers')
def on_update(spec, name, namespace, logger, **kwargs):
_text = spec['text']
_hash = spec['hash']
_h = compute_hash(_text, _hash)
logging.info("UPDATE - " + _text)
logging.info("{}: {}".format(_hash, _h))
try:
logging.info("Updating ConfigMap %s" % (name + '-cm'))
resp = k8s_client.CoreV1Api().patch_namespaced_config_map(
body={
'data': {
'hash': _hash,
'val': _h
}
},
name=name + '-cm',
namespace='mytest'
)
logging.info(resp)
except k8s_client.ApiException as e:
logging.error("Exception when calling CoreV1Api->patch_namespaced_config_map: %s\n" % e)
return {"hash": _h}
# ------------------------
# DELETE
# Freeze caused by handlers
# - https://kopf.readthedocs.io/en/latest/troubleshooting/#finalizers-blocking-deletion
# kubectl patch textanalyzers.operators.mytest.it test-analyzer-1 -p '{"metadata": {"finalizers": []}}' --type merge
# - optional=true
# ------------------------
@kopf.on.delete('operators.mytest.it', 'v1', 'textanalyzers')
def on_delete(spec, name, namespace, logger, **kwargs):
text = spec['text']
logging.info("DELETE - " + text)
# No need for delete_namespaced_config_map because it was adopted by the custom resource