Skip to content

[CONTP-1335] organize finalizers#2879

Merged
nlchung merged 19 commits into
mainfrom
nlchung/CONTP-1335-organize-finalizers
Apr 17, 2026
Merged

[CONTP-1335] organize finalizers#2879
nlchung merged 19 commits into
mainfrom
nlchung/CONTP-1335-organize-finalizers

Conversation

@nlchung

@nlchung nlchung commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Migrates DatadogAgent, DatadogAgentInternal, DatadogMonitor, DatadogDashboard, and DatadogGenericResource to the shared internal/controller/finalizer/ package, removing ~40–60 lines of duplicated finalizer scaffolding per controller. (DatadogSLO already used it.) Each controller now exposes a deleteResource() callback wrapping its existing cleanup logic.

Motivation

CONTP-1335: consolidate the six copy-pasted finalizer implementations into one.

Notes for reviewers

The six controllers weren't actually doing the same thing — three small differences had to be reconciled before they could share code:

  1. What to return after adding the finalizer. Agent, DDAI, Dashboard, and GenericResource returned Requeue: true (reconcile again right away). Monitor returned RequeueAfter: 60s. SLO didn't return at all — it kept running the body of the reconcile. We standardized on Requeue: true. An earlier iteration tried RequeueAfter: 60s for everyone and it caused 34 integration-test timeouts: the tests wait up to 10s for the controller to do real work, but with a 60s delay the controller just sat there.
  2. What to do if the Datadog API delete fails. Dashboard and GenericResource log and move on (remove the finalizer anyway). Monitor and SLO keep the finalizer and retry. We kept each controller's existing behavior — the ResourceDeleteFunc type has a doc comment explaining that returning an error blocks deletion and retries, while returning nil lets deletion proceed.
  3. The Datadog resource ID. Dashboard and SLO have it as a string. GenericResource calls the field Id instead of ID. Monitor stores it as an int. Agent and DDAI don't have one. The shared callback takes datadogID string; callers convert whatever they have. Empty string means "nothing to delete in Datadog."

NewFinalizer takes two durations: defaultRequeuePeriod (how often to check in while Kubernetes is garbage-collecting the deleted object — 60s for most controllers, 15s for Agent and DDAI) and defaultErrRequeuePeriod (5s retry on client.Update or deleteFunc failures).

Describe your test plan

  • Shared HandleFinalizer unit tests cover add/no-op/delete-success/delete-failure paths.
  • make integration-tests passes in ~15s, including the 34 specs that timed out in an earlier iteration.
  • Deployed to minikube: DatadogAgent create + delete reconciles cleanly, nothing stuck in Terminating.

Minimum Agent Versions

N/A — operator-side refactor only.

Checklist

  • PR has at least one valid label: refactoring
  • PR has a milestone or the qa/skip-qa label
  • All commits are signed

@nlchung
nlchung requested a review from a team April 8, 2026 13:03
@nlchung
nlchung marked this pull request as draft April 8, 2026 13:04
@nlchung nlchung self-assigned this Apr 8, 2026
@nlchung nlchung added this to the v1.26.0 milestone Apr 8, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e2f8940593

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/controller/finalizer/finalizer.go Outdated
@nlchung nlchung added the enhancement New feature or request label Apr 8, 2026
@nlchung nlchung added refactoring qa/skip-qa and removed enhancement New feature or request labels Apr 8, 2026
@codecov-commenter

codecov-commenter commented Apr 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 53.33333% with 28 lines in your changes missing coverage. Please review.
✅ Project coverage is 40.05%. Comparing base (7c966f8) to head (71ce75b).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
internal/controller/datadogmonitor/finalizer.go 25.00% 9 Missing and 3 partials ⚠️
internal/controller/datadogdashboard/finalizer.go 50.00% 4 Missing and 1 partial ⚠️
...ernal/controller/datadogagentinternal/finalizer.go 0.00% 3 Missing ⚠️
internal/controller/datadogagent/finalizer.go 33.33% 1 Missing and 1 partial ⚠️
...er/datadogagentinternal/controller_reconcile_v2.go 0.00% 2 Missing ⚠️
...nal/controller/datadoggenericresource/finalizer.go 84.61% 1 Missing and 1 partial ⚠️
...controller/datadogagent/controller_reconcile_v2.go 50.00% 0 Missing and 1 partial ⚠️
internal/controller/datadogslo/controller.go 66.66% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2879      +/-   ##
==========================================
+ Coverage   39.99%   40.05%   +0.06%     
==========================================
  Files         320      320              
  Lines       28122    28065      -57     
==========================================
- Hits        11247    11242       -5     
+ Misses      16052    16013      -39     
+ Partials      823      810      -13     
Flag Coverage Δ
unittests 40.05% <53.33%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
internal/controller/datadogagent/controller.go 92.85% <ø> (ø)
...rnal/controller/datadogagentinternal/controller.go 0.00% <ø> (ø)
internal/controller/datadogdashboard/controller.go 50.37% <100.00%> (+0.37%) ⬆️
...al/controller/datadoggenericresource/controller.go 62.32% <100.00%> (+8.42%) ⬆️
internal/controller/datadogmonitor/controller.go 54.92% <100.00%> (+0.21%) ⬆️
internal/controller/finalizer/finalizer.go 87.09% <100.00%> (+7.78%) ⬆️
...controller/datadogagent/controller_reconcile_v2.go 61.19% <50.00%> (+0.19%) ⬆️
internal/controller/datadogslo/controller.go 61.04% <66.66%> (+1.16%) ⬆️
internal/controller/datadogagent/finalizer.go 71.73% <33.33%> (+12.03%) ⬆️
...er/datadogagentinternal/controller_reconcile_v2.go 0.00% <0.00%> (ø)
... and 4 more

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7c966f8...71ce75b. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tbavelier tbavelier modified the milestones: v1.26.0, v1.27.0 Apr 8, 2026
@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Apr 12, 2026

Copy link
Copy Markdown

Code Coverage

Fix all issues with BitsAI

🛑 Gate Violations

🎯 1 Code Coverage issue detected

A Patch coverage percentage gate may be blocking this PR.

Patch coverage: 50.00% (threshold: 80.00%)

ℹ️ Info

🎯 Code Coverage (details)
Patch Coverage: 50.00%
Overall Coverage: 40.16% (+0.08%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 71ce75b | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback!

@nlchung
nlchung marked this pull request as ready for review April 13, 2026 12:45
nlchung and others added 3 commits April 13, 2026 08:45
…guards

HandleFinalizer was missing a return on the deletion path, causing
reconciliation to continue into create/update logic. Remove the
compensating GetDeletionTimestamp checks from all 5 callers, fix SLO
deleteResource closure capture, and improve finalizer test coverage.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Comment thread internal/controller/datadogagent/controller_reconcile_v2.go Outdated
nlchung and others added 5 commits April 16, 2026 08:44
…vior

HandleFinalizer was falling through after adding a finalizer instead of
requeuing, causing controllers to run create/update logic on the same
reconcile pass. This broke the datadogmonitor unsupported-type and
datadogslo create-failure test cases. Updated SLO tests to reconcile
twice (once for finalizer, once for actual logic).
…t had Requeue:true

The old datadogagent, datadogagentinternal, datadogdashboard, and
datadoggenericresource controllers all used Requeue:true (immediate)
after adding a finalizer, while datadogmonitor/datadogslo used
RequeueAfter with a period. The shared HandleFinalizer now supports
both: pass 0 for immediate requeue, or a duration for delayed requeue.

This fixes the 34 integration test timeouts where the 60s requeue
delay exceeded the 10s test timeout.
nlchung added 5 commits April 16, 2026 17:25
…alizer

All migrated controllers now pass defaultErrRequeuePeriod (5s) as the
requeue period when adding a finalizer, consistent with the existing
datadogslo pattern. Removes the Requeue:true special case for zero
period from HandleFinalizer.
…nrelated changes

- Remove defaultRequeuePeriod param from NewFinalizer; HandleFinalizer
  now always uses immediate requeue (Requeue: true) after adding a
  finalizer or while waiting for deletion, matching the original behavior
  of most controllers.
- Revert result.IsZero() cleanup and checkRequiredTags signature refactor
  as they are out of scope for this ticket.
The previous "revert unrelated changes" commit rolled main's post-#2478
state back to pre-#2478, undoing the deprecated-Requeue-field cleanup
and the checkRequiredTags (bool, error) signature. Restore main's
current state for those call sites so this PR is limited to the
finalizer reorganization.

@Mathew-Estafanous Mathew-Estafanous left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good!

@nlchung
nlchung merged commit 927e76e into main Apr 17, 2026
37 of 38 checks passed
@nlchung
nlchung deleted the nlchung/CONTP-1335-organize-finalizers branch April 17, 2026 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants