Skip to content
This repository has been archived by the owner on Dec 24, 2019. It is now read-only.

Commit

Permalink
also clean up jobs/pods with reason ContainerCannotRun
Browse files Browse the repository at this point in the history
  • Loading branch information
hjacobs committed Apr 26, 2017
1 parent a9633cb commit 6db199e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

18 changes: 13 additions & 5 deletions cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 6db199e

Please sign in to comment.