Skip to content

Instantly share code, notes, and snippets.

@ctron
Last active May 10, 2024 12:18
Show Gist options
  • Save ctron/4764c0c4c4ea0b22353f2a23941928ad to your computer and use it in GitHub Desktop.
Save ctron/4764c0c4c4ea0b22353f2a23941928ad to your computer and use it in GitHub Desktop.
Cleaning up Tekton pipeline runs
---
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
@NicolasRouquette
Copy link

Thanks! This even works in our old k8s v1.11 environment!

@AnyISalIn
Copy link

AnyISalIn commented Mar 22, 2021

Thanks for your gist to save my time. the following gist is supporting to clean all namespaces PipelineRun.

https://gist.github.com/AnyISalIn/9d22430d42fac100442b4b3ef0e0451f

@andsens
Copy link

andsens commented Jun 10, 2021

here's a version that's also easy to use manually:

kubectl get pipelinerun --sort-by=.metadata.creationTimestamp -o name | head -n-$NUM_TO_KEEP | (while read -r NAME; do kubectl delete "$NAME"; done)

xargs is not available in the container, so we use a poor mans version instead (... | xargs kubectl delete otherwise).

@raelga
Copy link

raelga commented Oct 7, 2021

An evolution of this CronJob keeping up to NUM_TO_KEEP of each Pipeline.

...
- name: kubectl
  image: docker.io/alpine/k8s:1.20.7
  env:
    - name: NUM_TO_KEEP
      value: "3"
  command:
    - /bin/bash
    - -c
    - >
      while read -r PIPELINE; do
        while read -r PIPELINE_TO_REMOVE; do
          test -n "${PIPELINE_TO_REMOVE}" || continue;
          kubectl delete ${PIPELINE_TO_REMOVE} \
              && echo "$(date -Is) PipelineRun ${PIPELINE_TO_REMOVE} deleted." \
              || echo "$(date -Is) Unable to delete PipelineRun ${PIPELINE_TO_REMOVE}.";
        done < <(kubectl get pipelinerun -l tekton.dev/pipeline=${PIPELINE} --sort-by=.metadata.creationTimestamp -o name | head -n -${NUM_TO_KEEP});
      done < <(kubectl get pipelinerun -o go-template='{{range .items}}{{index .metadata.labels "tekton.dev/pipeline"}}{{"\n"}}{{end}}' | uniq);

Full example with rbac at
https://gist.github.com/raelga/e75e6de4fd04be60f267128e985bde6d

@evan-xy-hu
Copy link

evan-xy-hu commented Mar 14, 2022

Really thanks. This cron job is really helpful. I no longer need to delete those pipelineruns by my hand now. Saved a lot of time.

@divyakoun
Copy link

Thanks! It helped me to delete pipelinesruns sucessfully.....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment