This repository was 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
/
Copy pathinitializer.py
62 lines (57 loc) · 2.39 KB
/
initializer.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
import json
# import yaml
from kubernetes import client, config, watch
import os
INITIALIZER = 'test.initializer.karmalabs.local'
def process_deployment(obj):
metadata = obj.metadata
if not metadata:
print("No metadata in object, skipping: %s" % json.dumps(obj, indent=1))
return
name = metadata.name
initializers = metadata.initializers
namespace = metadata.namespace
# labels = metadata.labels
annotations = metadata.annotations
if initializers is None:
return
for entry in initializers.pending:
if entry.name == INITIALIZER:
print("Updating deployment %s" % name)
initializers.pending.remove(entry)
if not initializers.pending:
initializers = None
obj.metadata.initializers = initializers
if annotation is None or (annotations and annotation in annotations and annotations[annotation] == 'true'):
print("Adding container %s to deployment %s" % (inject, name))
newcontainer = {'image': inject, 'name': 'injected'}
obj.spec.template.spec.containers.append(newcontainer)
v1.replace_namespaced_deployment(name, namespace, obj)
break
if __name__ == "__main__":
global v1
inject = os.environ['INJECT_POD'] if 'INJECT_POD' in os.environ else 'karmab/kdummy'
annotation = os.environ['ANNOTATION'] if 'ANNOTATION' in os.environ else None
if annotation is not None:
print "Injecting %s to new deployments with annotation %s set to true" % (inject, annotation)
else:
print "Injecting %s to all new deployments" % inject
if 'KUBERNETES_PORT' in os.environ:
config.load_incluster_config()
else:
config.load_kube_config()
v1 = client.AppsV1beta1Api()
resource_version = ''
while True:
stream = watch.Watch().stream(v1.list_deployment_for_all_namespaces, include_uninitialized=True, resource_version=resource_version)
for event in stream:
obj = event["object"]
operation = event['type']
spec = obj.spec
if not spec:
continue
metadata = obj.metadata
resource_version = metadata._resource_version
name = metadata.name
print("Handling %s on %s" % (operation, name))
process_deployment(obj)