FEAT(tekton): add automatic cleanup for old PipelineRuns
- Add CronJob to delete completed PipelineRuns older than 1 hour - Add 30m timeout to TriggerTemplates - Prevent resource accumulation in tekton-pipelines namespace
This commit is contained in:
36
tekton/ci-cd/manifests/cleanup-cronjob.yaml
Normal file
36
tekton/ci-cd/manifests/cleanup-cronjob.yaml
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
apiVersion: batch/v1
|
||||||
|
kind: CronJob
|
||||||
|
metadata:
|
||||||
|
name: tekton-cleanup
|
||||||
|
namespace: tekton-pipelines
|
||||||
|
spec:
|
||||||
|
schedule: "0 * * * *" # Every hour
|
||||||
|
successfulJobsHistoryLimit: 1
|
||||||
|
failedJobsHistoryLimit: 1
|
||||||
|
jobTemplate:
|
||||||
|
spec:
|
||||||
|
ttlSecondsAfterFinished: 300
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
serviceAccountName: tekton-triggers-sa
|
||||||
|
containers:
|
||||||
|
- name: cleanup
|
||||||
|
image: bitnami/kubectl:latest
|
||||||
|
command:
|
||||||
|
- /bin/sh
|
||||||
|
- -c
|
||||||
|
- |
|
||||||
|
echo "Cleaning up completed PipelineRuns older than 1 hour..."
|
||||||
|
kubectl get pipelineruns -n tekton-pipelines \
|
||||||
|
-o jsonpath='{range .items[?(@.status.conditions[0].status=="True")]}{.metadata.name}{" "}{.metadata.creationTimestamp}{"\n"}{end}' | \
|
||||||
|
while read name timestamp; do
|
||||||
|
if [ -n "$name" ]; then
|
||||||
|
age=$(( ($(date +%s) - $(date -d "$timestamp" +%s 2>/dev/null || date -j -f "%Y-%m-%dT%H:%M:%SZ" "$timestamp" +%s)) / 60 ))
|
||||||
|
if [ "$age" -gt 60 ]; then
|
||||||
|
echo "Deleting PipelineRun: $name (age: ${age}m)"
|
||||||
|
kubectl delete pipelinerun "$name" -n tekton-pipelines
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
echo "Cleanup complete"
|
||||||
|
restartPolicy: OnFailure
|
||||||
@@ -14,3 +14,5 @@ resources:
|
|||||||
- pipelines/python-pipeline.yaml
|
- pipelines/python-pipeline.yaml
|
||||||
# Triggers
|
# Triggers
|
||||||
- triggers/
|
- triggers/
|
||||||
|
# Cleanup
|
||||||
|
- cleanup-cronjob.yaml
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ spec:
|
|||||||
app: $(tt.params.repo-name)
|
app: $(tt.params.repo-name)
|
||||||
branch: $(tt.params.git-branch)
|
branch: $(tt.params.git-branch)
|
||||||
spec:
|
spec:
|
||||||
|
timeouts:
|
||||||
|
pipeline: 30m
|
||||||
pipelineRef:
|
pipelineRef:
|
||||||
name: nextjs-build-deploy
|
name: nextjs-build-deploy
|
||||||
params:
|
params:
|
||||||
@@ -79,6 +81,8 @@ spec:
|
|||||||
app: $(tt.params.repo-name)
|
app: $(tt.params.repo-name)
|
||||||
branch: $(tt.params.git-branch)
|
branch: $(tt.params.git-branch)
|
||||||
spec:
|
spec:
|
||||||
|
timeouts:
|
||||||
|
pipeline: 30m
|
||||||
pipelineRef:
|
pipelineRef:
|
||||||
name: fastapi-build-deploy
|
name: fastapi-build-deploy
|
||||||
params:
|
params:
|
||||||
|
|||||||
Reference in New Issue
Block a user