-
-
Save ctcampbell/fc6cad8a84fa0730838b28316ec73b75 to your computer and use it in GitHub Desktop.
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 | |
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