Last active
December 15, 2022 15:30
-
-
Save raelga/e75e6de4fd04be60f267128e985bde6d to your computer and use it in GitHub Desktop.
CronJob to clean up PipelineRuns, keeping up to `NUM_TO_KEEP` of runs of each pipeline
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: tekton-pipelinerun-cleaner | |
labels: | |
app: tekton-pipelinerun-cleaner | |
app.kubernetes.io/name: tekton-pipelinerun-cleaner | |
app.kubernetes.io/component: pipelinerun-cleaner | |
app.kubernetes.io/part-of: tekton | |
--- | |
kind: Role | |
apiVersion: rbac.authorization.k8s.io/v1 | |
metadata: | |
name: tekton-pipelinerun-cleaner | |
labels: | |
app: tekton-pipelinerun-cleaner | |
app.kubernetes.io/name: tekton-pipelinerun-cleaner | |
app.kubernetes.io/component: pipelinerun-cleaner | |
app.kubernetes.io/part-of: tekton | |
rules: | |
- apiGroups: | |
- tekton.dev | |
resources: | |
- pipelineruns | |
verbs: | |
- delete | |
- get | |
- watch | |
- list | |
--- | |
kind: RoleBinding | |
apiVersion: rbac.authorization.k8s.io/v1 | |
metadata: | |
name: tekton-pipelinerun-cleaner | |
labels: | |
app: tekton-pipelinerun-cleaner | |
app.kubernetes.io/name: tekton-pipelinerun-cleaner | |
app.kubernetes.io/component: pipelinerun-cleaner | |
app.kubernetes.io/part-of: tekton | |
roleRef: | |
kind: Role | |
name: tekton-pipelinerun-cleaner | |
apiGroup: rbac.authorization.k8s.io | |
subjects: | |
- kind: ServiceAccount | |
name: tekton-pipelinerun-cleaner | |
--- | |
apiVersion: batch/v1beta1 | |
kind: CronJob | |
metadata: | |
name: tekton-pipelinerun-cleaner | |
labels: | |
app: tekton-pipelinerun-cleaner | |
app.kubernetes.io/name: tekton-pipelinerun-cleaner | |
app.kubernetes.io/component: pipelinerun-cleaner | |
app.kubernetes.io/part-of: tekton | |
spec: | |
schedule: "*/15 * * * *" | |
concurrencyPolicy: Forbid | |
jobTemplate: | |
spec: | |
template: | |
spec: | |
restartPolicy: OnFailure | |
serviceAccount: tekton-pipelinerun-cleaner | |
containers: | |
- 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); | |
resources: | |
requests: | |
cpu: 50m | |
memory: 32Mi | |
limits: | |
cpu: 100m | |
memory: 64Mi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
could this be migrated to a git repo?
if not I can live with this here