Last active
August 30, 2024 15:29
-
-
Save ctron/4764c0c4c4ea0b22353f2a23941928ad 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
Thanks! It helped me to delete pipelinesruns sucessfully.....