Skip to content

Instantly share code, notes, and snippets.

@ctcampbell
Forked from AnyISalIn/tekton-cleanup.yaml
Last active August 7, 2023 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctcampbell/fc6cad8a84fa0730838b28316ec73b75 to your computer and use it in GitHub Desktop.
Save ctcampbell/fc6cad8a84fa0730838b28316ec73b75 to your computer and use it in GitHub Desktop.
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: cleaner
namespace: tekton-pipelines
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: cleaner
rules:
- apiGroups: ["tekton.dev"]
resources: ["pipelineruns"]
verbs: ["delete", "get", "watch", "list"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: cleaner-to-cleaner
roleRef:
kind: ClusterRole
name: cleaner
apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
name: cleaner
namespace: tekton-pipelines
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: cleanup-pipelineruns
namespace: tekton-pipelines
spec:
schedule: "*/5 * * * *"
concurrencyPolicy: Forbid
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
serviceAccount: cleaner
containers:
- name: kubectl
image: docker.io/csantanapr/helm-kubectl
env:
- name: NUM_TO_KEEP
value: "5"
command:
- /bin/bash
- -c
- |
kubectl get -A pipelinerun -o jsonpath='{range .items[?(@.status.completionTime)]}{.status.completionTime}{" "}{.metadata.namespace}{" "}{.metadata.name}{"\n"}{end}'|sort|head -n -${NUM_TO_KEEP}|awk '{ print "kubectl delete pipelinerun" " -n " $2 " " $3 }'|xargs -P4 -I{} sh -c "{}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment