Add cleanup job support for pre-deletion hooks via Kubernetes Jobs#48
Add cleanup job support for pre-deletion hooks via Kubernetes Jobs#48
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Pull Request Overview
This PR adds cleanup job functionality to the object-lease-controller, enabling custom scripts to run via Kubernetes Jobs before expired objects are deleted. This feature allows users to perform tasks like backing up data, notifying external systems, or cleaning up related resources.
- Introduces a new
CleanupJobConfigstruct and utility functions for parsing cleanup job annotations and creating Kubernetes Jobs - Integrates cleanup job execution into the lease expiration workflow with optional synchronous/asynchronous modes
- Adds new Prometheus metrics for tracking cleanup job lifecycle (created, completed, failed, duration)
Reviewed Changes
Copilot reviewed 12 out of 13 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
pkg/util/cleanup_job.go |
Core implementation for parsing cleanup job config, creating Jobs, and waiting for completion |
pkg/util/cleanup_job_test.go |
Comprehensive test coverage for cleanup job utilities |
pkg/controllers/lease_controller.go |
Integrates cleanup job execution into lease expiration handling |
pkg/metrics/metrics.go |
Adds four new Prometheus metrics for cleanup job monitoring |
cmd/main.go |
Registers cleanup job annotations and adds batch/v1 scheme |
object-lease-operator/helm-charts/leasecontroller/templates/role.yaml |
Adds RBAC permissions for Job operations |
examples/cleanup/*.yaml |
Three complete example scenarios with documentation |
README.md |
Documents cleanup job feature, annotations, and environment variables |
go.mod, go.sum |
Dependency updates |
cb07a8d to
7f99612
Compare
Signed-off-by: Magnus Ullberg <magnus@ullberg.us>
e074391 to
98d6de8
Compare
…tion Signed-off-by: Magnus Ullberg <magnus@ullberg.us>
… for consistency Signed-off-by: Magnus Ullberg <magnus@ullberg.us>
Signed-off-by: Magnus Ullberg <magnus@ullberg.us>
Signed-off-by: Magnus Ullberg <magnus@ullberg.us>
…allation Signed-off-by: Magnus Ullberg <magnus@ullberg.us>
…missions Signed-off-by: Magnus Ullberg <magnus@ullberg.us>
Signed-off-by: Magnus Ullberg <magnus@ullberg.us>
Signed-off-by: Magnus Ullberg <magnus@ullberg.us>
Signed-off-by: Magnus Ullberg <magnus@ullberg.us>
|
Pull Request closed and locked due to lack of activity. |
Enables custom cleanup scripts to run before deleting expired lease objects. Users can configure Jobs via annotations to backup data, notify webhooks, or clean up related resources.
Changes
Core Implementation
on-delete-job(ConfigMap/script reference),job-service-account,job-image,job-wait,job-timeout,job-ttl,job-backoff-limitpkg/util/cleanup_job.go: Job creation, config parsing, and completion polling with timeoutpkg/controllers/lease_controller.go: ModifiedhandleExpired()to check for cleanup config and execute jobs before deletionbatch/jobspermissions to ClusterRoleObservability
cleanup_jobs_created_total,cleanup_jobs_failed_total,cleanup_jobs_completed_total,cleanup_job_duration_secondsCleanupJobCreated,CleanupJobCompleted,CleanupJobFailed,CleanupJobTimeoutEnvironment Variables
Jobs receive 11 env vars:
OBJECT_NAME,OBJECT_NAMESPACE,OBJECT_KIND,OBJECT_GROUP,OBJECT_VERSION,OBJECT_UID,OBJECT_RESOURCE_VERSION,LEASE_STARTED_AT,LEASE_EXPIRED_AT,OBJECT_LABELS(JSON),OBJECT_ANNOTATIONS(JSON)Example Usage
ConfigMap script has access to all object metadata via env vars. Jobs auto-cleanup via
ttlSecondsAfterFinished. Seeexamples/cleanup/for S3 backup, webhook, and multi-resource cleanup examples.Testing
Original prompt
This section details on the original issue you should resolve
<issue_title>Feature: Support custom cleanup scripts via Kubernetes Jobs before object deletion</issue_title>
<issue_description>## Summary
Add support for executing custom cleanup scripts before deleting expired lease objects. Scripts would run as Kubernetes Jobs with proper RBAC and secret access via ServiceAccount bindings.
Motivation
Users often need to perform cleanup actions before an object is deleted, such as:
Currently, when a lease expires, the object is immediately deleted without any opportunity for custom cleanup logic.
Proposed Solution
User Experience
1. Create a cleanup script in a ConfigMap
2. Create a ServiceAccount with necessary permissions and secrets
3. Annotate the resource with cleanup configuration
Behavior
When a lease expires:
on-delete-jobannotationjob-ttlseconds (viattlSecondsAfterFinished)New Annotations
object-lease-controller.ullberg.io/on-delete-jobconfigmap-name/script-keyobject-lease-controller.ullberg.io/job-service-accountdefaultobject-lease-controller.ullberg.io/job-imagebitnami/kubectl:latestobject-lease-controller.ullberg.io/job-waitfalseobject-lease-controller.ullberg.io/job-timeout5mobject-lease-controller.ullberg.io/job-ttl300ttlSecondsAfterFinished)object-lease-controller.ullberg.io/job-backoff-limit3* Only if cleanup hook is desired
Environment Variables Available in Script
The cleanup script receives these environment variables:
OBJECT_NAME- Name of the object being deleted💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.