all repos — janny @ 029611c5fc8222d5cf5da4f2d96c9cd13f9f3759

clean up Kubernetes resources after a set TTL

Better error handling
Anirudh Oppiliappan x@icyphox.sh
Tue, 09 Mar 2021 18:13:59 +0530
commit

029611c5fc8222d5cf5da4f2d96c9cd13f9f3759

parent

f504aa9245876aa806532e909f2c410ad9c240e7

3 files changed, 28 insertions(+), 19 deletions(-)

jump to
M deploy/clusterrole.yamldeploy/clusterrole.yaml

@@ -8,6 +8,7 @@ - ""

resources: - pods - services + - configmaps verbs: - get - list
M deploy/deployment.yamldeploy/deployment.yaml

@@ -21,3 +21,8 @@ containers:

- name: janny image: janny:latest imagePullPolicy: IfNotPresent + args: + - "--included-resources" + - "deployments,configmaps,pods" + - "--namespaces" + - "default"
M janny/main.pyjanny/main.py

@@ -49,25 +49,28 @@ Spawns a clean up job -- runs on a new thread.

""" url, resource = resource_tuple resource_list = get(f"{url}/namespaces/{namespace}/{resource.name}") - for r in resource_list.items: - try: - if ( - "janny.ttl" in vars(r.metadata.annotations) - and r.metadata.name not in RUNNING - ): - logger.info( - f"New resource to clean up: {resource.name}/{r.metadata.name}: {vars(r.metadata.annotations)}" - ) - kill_time = vars(r.metadata.annotations)["janny.ttl"] - t = threading.Thread( - target=clean_up, - args=[url, resource.name, r.metadata.name, kill_time, namespace], - ) - logger.info(f"Starting cleaner thread for {r.metadata.name}") - RUNNING.append(r.metadata.name) - t.start() - except AttributeError: - pass + try: + for r in resource_list.items: + try: + if ( + "janny.ttl" in vars(r.metadata.annotations) + and r.metadata.name not in RUNNING + ): + logger.info( + f"New resource to clean up: {resource.name}/{r.metadata.name}: {vars(r.metadata.annotations)}" + ) + kill_time = vars(r.metadata.annotations)["janny.ttl"] + t = threading.Thread( + target=clean_up, + args=[url, resource.name, r.metadata.name, kill_time, namespace], + ) + logger.info(f"Starting cleaner thread for {r.metadata.name}") + RUNNING.append(r.metadata.name) + t.start() + except AttributeError: + pass + except AttributeError: + logger.error(f"Received: {resource_list.message}") def main(include_list, namespace_list):