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
/
image_cleaner.py
46 lines (42 loc) · 1.63 KB
/
image_cleaner.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
from kubernetes import config
from openshift import client, watch
import os
def process_image(obj):
metadata = obj.metadata
name = metadata.name
namespace = metadata.namespace
for tag in obj.spec.tags:
if tag.name == 'latest':
continue
else:
print("Deleting %s:%s" % (name, tag.name))
oapi.delete_namespaced_image_stream_tag("%s:%s" % (name, tag.name), namespace)
return
if __name__ == "__main__":
global oapi
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 deployment configs with annotation %s set to true" % (inject, annotation)
else:
print "Injecting %s to all new deployment configs" % inject
if 'KUBERNETES_PORT' in os.environ:
config.load_incluster_config()
else:
config.load_kube_config()
oapi = client.OapiApi()
resource_version = ''
while True:
stream = watch.Watch().stream(oapi.list_image_stream_for_all_namespaces, 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
if operation == 'ADDED':
print("Handling %s on %s" % (operation, name))
process_image(obj)