-
-
Save tosin2013/0824f040dd42b362b57d1cce1ff3b527 to your computer and use it in GitHub Desktop.
Cleaning up Tekton pipeline runs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
apiVersion: v1 | |
kind: ServiceAccount | |
metadata: | |
name: cleaner | |
--- | |
kind: Role | |
apiVersion: rbac.authorization.k8s.io/v1 | |
metadata: | |
name: cleaner | |
rules: | |
- apiGroups: ["tekton.dev"] | |
resources: ["pipelineruns"] | |
verbs: ["delete", "get", "watch", "list"] | |
--- | |
kind: RoleBinding | |
apiVersion: rbac.authorization.k8s.io/v1 | |
metadata: | |
name: cleaner-to-cleaner | |
roleRef: | |
kind: Role | |
name: cleaner | |
apiGroup: rbac.authorization.k8s.io | |
subjects: | |
- kind: ServiceAccount | |
name: cleaner | |
--- | |
apiVersion: batch/v1beta1 | |
kind: CronJob | |
metadata: | |
name: cleanup-pipelineruns | |
spec: | |
schedule: "*/15 * * * *" | |
concurrencyPolicy: Forbid | |
jobTemplate: | |
spec: | |
template: | |
spec: | |
restartPolicy: OnFailure | |
serviceAccount: cleaner | |
containers: | |
- name: kubectl | |
image: ghcr.io/ctron/kubectl:latest | |
env: | |
- name: NUM_TO_KEEP | |
value: "3" | |
command: | |
- /bin/bash | |
- -c | |
- | | |
TO_DELETE="$(kubectl get pipelinerun -o jsonpath='{range .items[?(@.status.completionTime)]}{.status.completionTime}{" "}{.metadata.name}{"\n"}{end}' | sort | head -n -${NUM_TO_KEEP} | awk '{ print $2}')" | |
test -n "$TO_DELETE" && kubectl delete pipelinerun ${TO_DELETE} || true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment