fix(scanner): reap finished scanner jobs independent of TTLAfterFinished controller - #492
Merged
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new delete_k8s_job handler and a background reaper service to clean up finished Kubernetes jobs and pods, making cleanup independent of the cluster's TTLAfterFinished controller. The feedback focuses on enhancing the robustness of the background reaper by suggesting a limit on the list query to prevent memory spikes, adding a timeout to context execution to avoid hanging on unresponsive API servers, and suppressing warning logs for expected context cancellations during shutdown.
Contributor
|
📦 Image Tags Updated |
…hed controller Scanner Jobs were only cleaned up by the cluster's TTLAfterFinished controller (via TTLSecondsAfterFinished=600). That controller can be disabled in customer environments, leaving finished Jobs and their Completed pods to accumulate forever. Add a background reaper that periodically deletes the agent's own finished Jobs (cascading to their pods) once past the same 600s grace the TTL field encodes. It is strictly label- and namespace-scoped so it can never touch customer Jobs, and is a no-op where the TTL controller is present. Also add a delete_k8s_job primitive so the orchestrator can delete a Job promptly after fetching its logs.
mayankpande88
force-pushed
the
feat/scanner-job-reaper
branch
from
June 24, 2026 08:36
bb27efc to
105f67b
Compare
Contributor
|
📦 Image Tags Updated |
RamanKharchee
approved these changes
Jun 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Scanner Jobs (trivy image scan, popeye, kube-bench, trivy CIS, helm chart upgrade) were only cleaned up by the cluster's TTLAfterFinished controller — every Job is stamped with
TTLSecondsAfterFinished=600and nothing else deletes them. That controller is GA/on-by-default since k8s 1.21 but can be disabled in customer environments (hardened/managed distros, explicit--controllers=-ttl-after-finished, or pre-1.21). When it's off, finished Jobs and theirCompletedpods accumulate indefinitely.This adds an in-agent background reaper so cleanup never depends on the cluster having that controller:
StartReaperruns a 60s-ticker goroutine (cancelled by the agent shutdown ctx) that deletes the agent's own finished Jobs once they're more than 600s past completion, cascade-deleting their pods (PropagationPolicy=Background).app.kubernetes.io/managed-by=nudgebee-agentandnudgebee.com/orchestrator=api-server, in the runner's namespace. Customer Jobs are never touched.TTLSecondsAfterFinishedsemantics, preserving the log-availability window for retries/manual debugging.Also adds a
delete_k8s_jobprimitive so the api-server orchestrator can delete a Job promptly after fetching its logs (happy-path cleanup); the reaper is the backstop for crash/timeout paths.Type of change
How Has This Been Tested?
go build ./...,go vet,go test ./pkg/scanners/...passReview Notes
Risks & Counterarguments
completion + 600s; the orchestrator fetches logs within seconds of completion, so a finished Job is never deleted before its logs are read.CompletionTime—jobFinishedAtfalls back to the terminal condition'sLastTransitionTime.