Skip to content

Commit c593426

Browse files
clean up tekton pipelineruns and taskruns
1 parent ea3ad01 commit c593426

File tree

6 files changed

+189
-0
lines changed

6 files changed

+189
-0
lines changed

tests/pipelines/cleanup/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Cleanup old TaskRuns and PipelineRuns
2+
3+
Here is how users can clean up old TaskRuns and PipelineRuns.
4+
5+
The general method is to use a CronJob to trigger a Task that deletes all but the `n` most recent PipelineRuns and `2*n` most recent TaskRuns.
6+
7+
## Prerequisites
8+
9+
* A Kubernetes cluster with Tekton Pipelines installed
10+
* Several old TaskRuns and/or PipelineRuns you wish to delete
11+
12+
## Scheduling the cleanup job
13+
14+
You'll need to install all the files in this directory to run the cleanup task.
15+
16+
* [serviceaccount.yaml](serviceaccount.yaml): this creates the service account needed to run the job, along with the associated ClusterRole and Rolebinding.
17+
18+
* [cleanup-template.yaml](cleanup-template.yaml): this creates the TriggerTemplate that spawns the TaskRun that does the deleting. It uses the `tkn` CLI to do the deleting.
19+
20+
* [binding.yaml](binding.yaml): this creates the TriggerBinding that is used to pass parameters to the TaskRun.
21+
22+
* [eventlistener.yaml](eventlistener.yaml): this creates the sink that receives the incoming event that triggers the creation of the cleanup job.
23+
24+
* [cronjob.yaml](cronjob.yaml): this is used to run the cleanup job on a schedule. There are two environmental variables that need to be set in the job: `NAMESPACE` for the namespace you wish to clean up, and `CLEANUP_KEEP` for the number of PipelineRuns to keep. The schedule for the job running can be set in the `.spec.schedule` field using [crontab format](https://crontab.guru/)

tests/pipelines/cleanup/binding.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: triggers.tekton.dev/v1alpha1
2+
kind: TriggerBinding
3+
metadata:
4+
name: cleanup-details
5+
namespace: tekton-pipelines
6+
spec:
7+
params:
8+
- name: keep
9+
value: $(body.params.cleanup.keep)
10+
- name: namespace
11+
value: $(body.params.target.namespace)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
apiVersion: triggers.tekton.dev/v1alpha1
2+
kind: TriggerTemplate
3+
metadata:
4+
name: cleanup-runs
5+
namespace: tekton-pipelines
6+
spec:
7+
params:
8+
- name: namespace
9+
description: Namespace to cleanup to in the target cluster
10+
- name: clusterResource
11+
description: Name of the cluster resource that points to the target cluster
12+
- name: keep
13+
description: Amount of old resources to keep
14+
default: "200"
15+
resourcetemplates:
16+
- apiVersion: tekton.dev/v1beta1
17+
kind: TaskRun
18+
metadata:
19+
name: cleanupruns-$(uid)
20+
spec:
21+
serviceAccountName: tekton-cleaner
22+
taskSpec:
23+
params:
24+
- name: keep
25+
- name: namespace
26+
steps:
27+
- name: cleanup-pr-tr
28+
image: gcr.io/tekton-releases/dogfooding/tkn
29+
script: |
30+
#!/bin/sh
31+
set -ex
32+
# A safety check, to avoid deleting too much!
33+
if [[ $(params.keep) -eq 0 || $(params.keep) == "" ]]; then
34+
echo "This task cannot be used to delete *all* resources from a cluster" >&2
35+
echo "Please specifcy a value for keep > 0"
36+
exit 1
37+
fi
38+
# Cleanup pipelineruns first, as this will delete tasksruns too
39+
tkn pr delete -n $(params.namespace) --keep $(params.keep)
40+
params:
41+
- name: keep
42+
value: $(tt.params.keep)
43+
- name: namespace
44+
value: $(tt.params.namespace)

tests/pipelines/cleanup/cronjob.yaml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
apiVersion: batch/v1beta1
2+
kind: CronJob
3+
metadata:
4+
name: cleanup-trigger
5+
namespace: tekton-pipelines
6+
spec:
7+
schedule: "0 * * * *"
8+
jobTemplate:
9+
spec:
10+
template:
11+
spec:
12+
volumes:
13+
- name: workspace
14+
emptyDir: {}
15+
containers:
16+
- name: trigger
17+
image: curlimages/curl
18+
command:
19+
- /bin/sh
20+
args:
21+
- -ce
22+
- |
23+
cat <<EOF > /workspace/post-body.json
24+
{
25+
"trigger-template": "cleanup",
26+
"params": {
27+
"target": {
28+
"namespace": "$NAMESPACE"
29+
},
30+
"cleanup": {
31+
"keep": "$CLEANUP_KEEP"
32+
}
33+
}
34+
}
35+
EOF
36+
curl -d @/workspace/post-body.json $SINK_URL
37+
volumeMounts:
38+
- mountPath: /workspace
39+
name: workspace
40+
env:
41+
- name: SINK_URL
42+
value: "http://el-tekton-cd.tekton-pipelines.svc.cluster.local:8080"
43+
- name: NAMESPACE
44+
value: "tekton-pipelines"
45+
- name: CLEANUP_KEEP
46+
value: "50"
47+
restartPolicy: Never
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: triggers.tekton.dev/v1beta1
2+
kind: EventListener
3+
metadata:
4+
name: tekton-cd
5+
namespace: tekton-pipelines
6+
spec:
7+
serviceAccountName: tekton-cleaner
8+
triggers:
9+
- name: cleanup
10+
interceptors:
11+
- ref:
12+
name: "cel"
13+
params:
14+
- name: "filter"
15+
value: |
16+
'trigger-template' in body && body['trigger-template'] == 'cleanup'
17+
bindings:
18+
- ref: cleanup-details
19+
template:
20+
ref: cleanup-runs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
apiVersion: v1
2+
kind: ServiceAccount
3+
metadata:
4+
name: tekton-cleaner
5+
namespace: tekton-pipelines
6+
---
7+
apiVersion: rbac.authorization.k8s.io/v1
8+
kind: ClusterRole
9+
metadata:
10+
name: tekton-cleaner-roles
11+
rules:
12+
- apiGroups: [""]
13+
resources: ["namespaces", "configmaps"]
14+
verbs: ["get", "list", "watch"]
15+
- apiGroups: ["tekton.dev"]
16+
resources: ["pipelineruns", "taskruns", "pipelineresources"]
17+
verbs: ["get", "list", "delete", "create"]
18+
- apiGroups: ["triggers.tekton.dev"]
19+
resources: ["eventlisteners", "triggerbindings", "triggertemplates", "interceptors"]
20+
verbs: ["get", "list", "watch"]
21+
---
22+
apiVersion: rbac.authorization.k8s.io/v1
23+
kind: ClusterRole
24+
metadata:
25+
name: tekton-cleaner-clusterroles
26+
rules:
27+
- apiGroups: ["triggers.tekton.dev"]
28+
resources: ["clustertriggerbindings", "clusterinterceptors"]
29+
verbs: ["get", "list", "watch"]
30+
---
31+
apiVersion: rbac.authorization.k8s.io/v1
32+
kind: RoleBinding
33+
metadata:
34+
name: tektoncd-cleaner-delete-pr-tr-rolebinding
35+
namespace: tekton-pipelines
36+
subjects:
37+
- kind: ServiceAccount
38+
name: tekton-cleaner
39+
namespace: tekton-pipelines
40+
roleRef:
41+
apiGroup: rbac.authorization.k8s.io
42+
kind: ClusterRole
43+
name: tekton-cleaner-roles

0 commit comments

Comments
 (0)