github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/.tekton/cleanup-cron-job.yaml (about) 1 # (c) Copyright IBM Corp. 2024 2 # Ref : https://github.com/tektoncd/experimental/issues/479 3 4 --- 5 apiVersion: v1 6 kind: ServiceAccount 7 metadata: 8 name: cleaner 9 --- 10 kind: Role 11 apiVersion: rbac.authorization.k8s.io/v1 12 metadata: 13 name: cleaner 14 rules: 15 - apiGroups: ["tekton.dev"] 16 resources: ["pipelineruns"] 17 verbs: ["delete", "get", "watch", "list"] 18 --- 19 kind: RoleBinding 20 apiVersion: rbac.authorization.k8s.io/v1 21 metadata: 22 name: cleaner-to-cleaner 23 roleRef: 24 kind: Role 25 name: cleaner 26 apiGroup: rbac.authorization.k8s.io 27 subjects: 28 - kind: ServiceAccount 29 name: cleaner 30 --- 31 apiVersion: batch/v1 32 kind: CronJob 33 metadata: 34 name: tekton-pipelinerun-cleaner 35 spec: 36 schedule: "0 2 * * *" 37 concurrencyPolicy: Forbid 38 jobTemplate: 39 spec: 40 template: 41 spec: 42 restartPolicy: OnFailure 43 serviceAccount: cleaner 44 containers: 45 - name: kubectl 46 image: ghcr.io/ctron/kubectl:latest 47 env: 48 - name: NUM_TO_KEEP 49 value: "50" 50 command: 51 - /bin/bash 52 - -c 53 - | 54 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}')" 55 test -n "$TO_DELETE" && kubectl delete pipelinerun ${TO_DELETE} || true