Skip to content

sourcegraph-executor/k8s: opt-in CronJob to clean up orphaned executor job resources - #923

Open
marcleblanc2 wants to merge 1 commit into
mainfrom
executor-job-resource-cleanup
Open

sourcegraph-executor/k8s: opt-in CronJob to clean up orphaned executor job resources#923
marcleblanc2 wants to merge 1 commit into
mainfrom
executor-job-resource-cleanup

Conversation

@marcleblanc2

Copy link
Copy Markdown
Contributor

Problem

The executor deletes each job's Job, Secret, and (optional) PVC in Teardown (kubernetes.go), but Teardown never runs when the executor pod is killed mid-job — OOM kill, node scale-down/spot reclaim, or a deployment rollout. Nothing in Kubernetes garbage-collects the leftovers: the Secrets carry no ownerReferences and the Jobs no ttlSecondsAfterFinished, so they accumulate forever. One real cluster reached 699 orphaned sg-executor-job-* Secrets.

The proper fix (ownerReferences from Secret/PVC → Job plus ttlSecondsAfterFinished) belongs in the executor code in sourcegraph/sourcegraph; until then the chart can sweep up.

Change

  • New opt-in executor.cleanup.* values deploying a CronJob that deletes sg-executor-job-* Jobs, Secrets, and PersistentVolumeClaims older than executor.cleanup.minimumAgeSeconds (default 3600s, every 30 min).
  • Template rendering fails if minimumAgeSeconds does not exceed executor.kubernetesJob.deadline (default 1200s), so resources of running jobs are never touched — any executor job older than the deadline has already been terminated by activeDeadlineSeconds.
  • Reuses the sg-executor ServiceAccount; the executor Role gains get/list on jobs (it already had list on secrets/PVCs and delete on all three).
  • Image: alpine/kubectl (kubectl + shell; bitnami/kubectl is deprecated and registry.k8s.io/kubectl is distroless, no shell). Fully overridable for air-gapped mirrors.

Testing

  • helm lint and helm template clean
  • helm unittest: 10/10 pass, including new tests/cleanup_test.yaml (default off, renders when enabled, fails render on unsafe minimumAgeSeconds)
  • Verified alpine/kubectl:1.33.4 on a live EKS cluster: busybox date -D parses creationTimestamp correctly and kubectl is present
  • helm-docs regenerated (README diff contains only the four new value rows)

… resources

The executor deletes each job's Job, Secret, and PVC in Teardown, but
Teardown never runs when the executor is killed mid-job (OOM kill, node
scale-down, rollout). Nothing garbage-collects those resources: the
Secrets have no ownerReferences and the Jobs no ttlSecondsAfterFinished,
so they accumulate forever (one real cluster reached 699 orphaned
sg-executor-job-* secrets).

Add an opt-in CronJob (executor.cleanup.enabled) that deletes
sg-executor-job-* Jobs, Secrets, and PersistentVolumeClaims older than
executor.cleanup.minimumAgeSeconds. The template fails rendering if that
age does not exceed executor.kubernetesJob.deadline, so resources of
running jobs are never deleted. Reuses the sg-executor ServiceAccount;
the executor Role additionally gets get/list on jobs.

Amp-Thread-ID: https://ampcode.com/threads/T-01a01d55-b956-718b-a863-ef9c8c5648be
Co-authored-by: Amp <amp@ampcode.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant