Skip to content

Instantly share code, notes, and snippets.

@AnyISalIn
Created March 25, 2021 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AnyISalIn/9d22430d42fac100442b4b3ef0e0451f to your computer and use it in GitHub Desktop.
Save AnyISalIn/9d22430d42fac100442b4b3ef0e0451f 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/v1beta1
kind: CronJob
metadata:
name: cleanup-pipelineruns
namespace: tekton-pipelines
spec:
schedule: "*/15 * * * *"
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: "1000"
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