A Kubernetes controller that uses a mutating webhook to automatically set the ttlSecondsAfterFinished field for Jobs based on label selectors.
JobTTLController is a Kubernetes admission webhook that intercepts Job creation and updates, and patches the ttlSecondsAfterFinished field to a specified value for Jobs that match the configured label selector.
The easiest way to install JobTTLController is using the Helm chart:
# Add the Helm repository (if not already added)
helm repo add jobttlcontroller https://dheeth.github.io/jobttlcontroller
# Install the chart
helm install jobttl-fix jobttlcontroller \
--namespace jobttlcontroller-system \
--create-namespace \
--set controller.targetTTL=100 \
--set controller.labelSelector="ttl-controller=enabled"
Build the Docker image:
make docker-build IMG=your-registry/jobttlcontroller:latest
make docker-push IMG=your-registry/jobttlcontroller:latest
Deploy to Kubernetes:
# Update the image in deployment.yaml first
kubectl apply -f manifests/
Configure certificates: The webhook requires TLS certificates. You can use cert-manager or manually create certificates.
The controller can be configured using command-line arguments:
--target-ttl: Target TTL in seconds for jobs after they finish (default: 100)--label-selector: Label selector to match jobs for TTL patching (default: "")--cert-dir: Directory containing TLS certificates (default: "/tmp/k8s-webhook-server/serving-certs")--metrics-bind-address: Address for metrics endpoint (default: ":8080")--health-probe-bind-address: Address for health probes (default: ":8081")# Deploy the controller with label selector
kubectl apply -f manifests/
Create Jobs with the label ttl-controller=enabled:
apiVersion: batch/v1
kind: Job
metadata:
name: example-job
labels:
ttl-controller: enabled
spec:
template:
spec:
containers:
- name: example
image: busybox
command: ["echo", "Hello World"]
restartPolicy: Never
Deploy the controller with a custom TTL:
# In manifests/deployment.yaml
args:
- --target-ttl=7200 # 2 hours
- --label-selector=environment=production
Run locally (requires kubeconfig):
make run
Build binary:
make build
Run tests:
make test
# Build Docker image
make docker-build
# Push to registry
make docker-push
The JobTTLController exports the following Prometheus metrics on the metrics endpoint (default: :8080/metrics):
jobttlcontroller_webhook_requests_total{operation,result}: Total number of webhook admission requests by operation (CREATE, UPDATE) and result (allowed, denied)jobttlcontroller_webhook_jobs_patched_total: Total number of Jobs that were patched with TTL valuesjobttlcontroller_jobs_matching_selector_total: Total number of Jobs matching the configured label selectorjobttlcontroller_jobs_ttl_set_total: Total number of Jobs that had their TTL setjobttlcontroller_jobs_ttl_already_set_total: Total number of Jobs that already had the target TTL valueTo view the metrics:
# Port forward to the controller
kubectl port-forward -n job-ttl-system deployment/job-ttl-controller 8080:8080
# Access metrics endpoint
curl http://localhost:8080/metrics
The controller provides detailed logging for all operations. You can check the logs to see:
# Check webhook configuration
kubectl get mutatingwebhookconfigurations job-ttl-webhook-config
# Check controller logs
kubectl logs -n job-ttl-system deployment/job-ttl-controller
# View detailed logs for specific operations
kubectl logs -n job-ttl-system deployment/job-ttl-controller | grep "jobttlcontroller"
# Test webhook
kubectl create -f test-job.yaml
# Check metrics
kubectl port-forward -n job-ttl-system deployment/job-ttl-controller 8080:8080
curl http://localhost:8080/metrics
This project is licensed under the MIT License - see the LICENSE file for details.
Content type
Image
Digest
sha256:74e9114dc…
Size
21.2 MB
Last updated
6 months ago
docker pull dheeth/jobttlcontroller:9ea0847c-7cd2251c-507-7436