apiVersion: batch/v1
kind: CronJob
metadata:
  name: cleanup-cronjob
spec:
  schedule: "*/0 0 * * 0"       # It will run at 0:00 in every Sunday
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: Never
          serviceAccount: tekton-pipelinerun-cleaner
          containers:
          - name: kubectl
            image: docker.io/alpine/k8s:1.21.5
            imagePullPolicy: IfNotPresent
            env:
              - name: NUM_TO_KEEP
                value: "2"
            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);
