A Kubernetes controller that automatically deletes custom resources after their TTL (Time To Live) expires.
This controller:
- Watches TTLReaper custom resources using generated clients
- For each TTLReaper, discovers and monitors the specified custom resource type
- Checks if resources have the
spec.ttlSecondsAfterFinishedfield set - Validates that resources are completed/finished using common completion patterns
- Calculates expiration time based on completion time + TTL duration
- Automatically deletes expired resources
The TTL reaper can work with any custom resource that:
- Has a
spec.ttlSecondsAfterFinishedfield (integer, seconds) - Indicates completion through one of these patterns:
status.phase= "Succeeded", "Failed", or "Completed"status.conditionswith type="Succeeded" and status="True"status.completionTimefield exists
apiVersion: clusterops.io/v1alpha1
kind: TTLReaper
metadata:
name: tekton-pipelinerun-reaper
spec:
targetKind: PipelineRun
targetAPIVersion: tekton.dev/v1beta1
# Optional: specific namespace
targetNamespace: ci-cd
# Optional: label selector
labelSelector:
matchLabels:
app: ci-pipelineapiVersion: clusterops.io/v1alpha1
kind: TTLReaper
metadata:
name: job-reaper
spec:
targetKind: Job
targetAPIVersion: batch/v1
targetNamespace: batch-processingapiVersion: clusterops.io/v1alpha1
kind: TTLReaper
metadata:
name: workflow-reaper
spec:
targetKind: Workflow
targetAPIVersion: workflows.example.com/v1
# Empty targetNamespace means cluster-wide monitoringThe controller can be containerized and deployed using ko:
# Build and push image
ko build github.com/infernus01/knative-demo/cmd/controller
# Deploy to cluster
kubectl apply -f config/deploy/deployment.yamlAny custom resource can be configured for automatic cleanup by adding the TTL field:
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: example-pipeline
namespace: default
spec:
# This PipelineRun will be deleted 300 seconds (5 minutes) after completion
ttlSecondsAfterFinished: 300
pipelineRef:
name: my-pipelineThe TTL countdown starts when the resource reaches a completed state (succeeded, failed, etc.).