Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions roles/common/files/k8s/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
#
# Garbage collect completed (or very old) jobs
#
for ctx in $(kubectl config get-contexts --output=name); do
KUBECTL="kubectl --context=$ctx"

echo "# ===== $ctx ===== "
# cleanup successful jobs
$KUBECTL delete jobs --field-selector status.successful==1

# Cleanup failed pods
$KUBECTL delete pods --field-selector status.phase=Failed

# delete jobs older than 1d (have 'd' in the AGE field)
$KUBECTL delete job $($KUBECTL get job | awk 'match($4,/[0-9]+d/) {print $1}')
done
32 changes: 32 additions & 0 deletions roles/common/tasks/k8s.yml
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,35 @@
kubectl --context $ctx create secret generic {{ item.name }} --from-literal=token={{ item.token }} --dry-run=client -o yaml | kubectl --context $ctx apply -f -
done
loop: "{{ kci_tokens.tokens }}"

- name: Create /home/buildslave/bin
tags:
- k8s-scripts
- never
file:
path: "/home/buildslave/bin"
state: directory
owner: buildslave
group: buildslave
mode: 0755

- name: Add k8s helper shell scripts
tags:
- k8s-scripts
- never
copy:
src: k8s/cleanup.sh
dest: "/home/buildslave/bin/k8s-cleanup.sh"
owner: buildslave
group: buildslave
mode: 0755

- name: Daily cron for k8s garbage collection
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be easily turned into a systemd timer instead of cron? Or is Ansible going to create a systemd timer anyway with ansible.builtin.cron?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed it would create a systemd timer, but in fact it creats a cron entry for the buildslave user. I'll need to read ansible docs to see if there's an easy way.

tags:
- k8s-scripts
- never
ansible.builtin.cron:
name: "k8s garbage collection"
special_time: "daily"
job: "/home/buildslave/bin/k8s-cleanup.sh"
user: buildslave