From 6db199e8859ef93623c810e7c8f97afaa885f33e Mon Sep 17 00:00:00 2001 From: Henning Jacobs Date: Wed, 26 Apr 2017 10:59:47 +0200 Subject: [PATCH] also clean up jobs/pods with reason ContainerCannotRun --- Makefile | 2 +- cleaner.py | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 9f19937..0edadbd 100644 --- a/Makefile +++ b/Makefile @@ -20,5 +20,5 @@ push: docker docker push "$(IMAGE):$(TAG)" scm-source.json: .git - @echo '{"url": "$(GITURL)", "revision": "$(GITHEAD)", "author": "$(USER)", "status": "$(GITSTATUS)"}' > scm-source.json + @echo '{"url": "git:$(GITURL)", "revision": "$(GITHEAD)", "author": "$(USER)", "status": "$(GITSTATUS)"}' > scm-source.json diff --git a/cleaner.py b/cleaner.py index b87f777..943d9e8 100755 --- a/cleaner.py +++ b/cleaner.py @@ -26,7 +26,8 @@ def parse_time(s: str): now = time.time() for job in pykube.Job.objects(api, namespace=pykube.all): completion_time = job.obj['status'].get('completionTime') - if job.obj['status'].get('succeeded') and completion_time: + status = job.obj['status'] + if (status.get('succeeded') or status.get('failed')) and completion_time: completion_time = parse_time(completion_time) seconds_since_completion = now - completion_time if seconds_since_completion > args.seconds: @@ -41,10 +42,17 @@ def parse_time(s: str): seconds_since_completion = 0 for container in pod.obj['status'].get('containerStatuses'): if 'terminated' in container['state']: - if container['state']['terminated']['reason'] == 'Completed': - finish = now - parse_time(container['state']['terminated']['finishedAt']) - if seconds_since_completion == 0 or finish < seconds_since_completion: - seconds_since_completion = finish + state = container['state'] + elif 'terminated' in container.get('lastState', {}): + # current state might be "waiting", but lastState is good enough + state = container['lastState'] + else: + state = None + if state: + finish = now - parse_time(state['terminated']['finishedAt']) + if seconds_since_completion == 0 or finish < seconds_since_completion: + seconds_since_completion = finish + if seconds_since_completion > args.seconds: print('Deleting {} ({:.0f}s old)..'.format(pod.name, seconds_since_completion)) if args.dry_run: