Skip to content

fix(rfc2136): surface record build errors and skip partial updates in ApplyChanges - #6868

Merged
kubernetes-prow[bot] merged 4 commits into
kubernetes-sigs:masterfrom
mmontes11:feature-rfc2136-applychanges
Nov 5, 2026
Merged

kubernetes-prow[bot] merged 4 commits into
kubernetes-sigs:masterfrom
mmontes11:feature-rfc2136-applychanges

Conversation

@mmontes11

@mmontes11 mmontes11 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

What does it do ?

Splits the RFC2136 ApplyChanges batch handling into a single applyBatches
loop driven by a per-action closure (create/update/delete), and pulls the
shared pieces into newBatchMsgs (per-zone message map), acceptsEndpoint
(domain filter) and sendBatchMsgs (send non-empty messages, collect
errors). ApplyChanges keeps the log line, the three batch calls and the
final SoftError aggregation; the update path's UpdateOld index mapping
(c * batchChangeSize + i) is preserved.

Beyond the refactor, two behavior changes:

  • Record build errors are no longer dropped: AddRecord, UpdateRecord and
    RemoveRecord errors are collected as RFC2136 <action> <dns name>: <err>
    and surfaced in the SoftError returned by ApplyChanges.
  • A failed endpoint no longer sends a partial update: each endpoint is built
    into a scratch message and merged into the batch only on success, so a
    failed update (e.g. an invalid target) never sends its delete without its
    add.

Cyclomatic complexity of ApplyChanges drops from 23 to 2 (helpers are all
cc <= 4).

Motivation

Each batch loop duplicated the message-map construction, endpoint filtering,
zone lookup and send loop, so a behavior change had to be replicated three
times and the function was the most complex one in the provider. With the
loops collapsed into one, the silently dropped error handling (record build
errors, partial batch updates) could be fixed in a single place.

More

  • Yes, this PR title follows Conventional Commits
  • Yes, I added unit tests
  • Yes, I updated end user documentation accordingly

New tests:

  • TestRfc2136ApplyChangesAllFilteredSendsNothing pins the len(z.Ns) > 0
    guard — a changeset whose endpoints are all filtered out sends no DNS
    messages at all (the stub counts SendMessage calls, with
    UpdateOld/UpdateNew entries covering the update path).
  • TestRfc2136ApplyChangesInvalidTargetSendsNothing pins that an invalid
    target produces a SoftError naming the endpoint, and that no partial
    update reaches the nameserver.

Note: this change was drafted with AI assistance and reviewed locally before submission.

@kubernetes-prow
kubernetes-prow Bot requested a review from mloiseleur October 27, 2026 17:57
@kubernetes-prow kubernetes-prow Bot added the provider Issues or PRs related to a provider label Oct 27, 2026
@kubernetes-prow
kubernetes-prow Bot requested a review from szuecs October 27, 2026 17:57
@kubernetes-prow kubernetes-prow Bot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 27, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Hi @mmontes11. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@kubernetes-prow kubernetes-prow Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 27, 2026
@vflaux

vflaux commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

/ok-to-test

@kubernetes-prow kubernetes-prow Bot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 27, 2026
@coveralls

coveralls commented Oct 27, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 41238936273

Coverage increased (+0.04%) to 83.053%

Details

  • Coverage increased (+0.04%) from the base build.
  • Patch coverage: No coverable lines changed in this PR.
  • 55 coverage regressions across 1 file.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

55 previously-covered lines in 1 file lost coverage.

File Lines Losing Coverage Coverage
rfc2136/rfc2136.go 55 76.7%

Coverage Stats

Coverage Status
Relevant Lines: 19390
Covered Lines: 16104
Line Coverage: 83.05%
Coverage Strength: 1495.23 hits per line

💛 - Coveralls

@mloiseleur mloiseleur left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, the refactor behaves the same as before.

The three apply*Batches helpers are still mostly copy-paste. Only the per-endpoint call differs.

Wdyt of collapsing them into one helper, e.g.:

func (r *rfc2136Provider) applyBatches(action string, eps []*endpoint.Endpoint, apply func(m *dns.Msg, idx int, ep *endpoint.Endpoint) error) []error

The update closure would then use updateOld[idx]. Otherwise any future change has to be made three times.

Comment thread provider/rfc2136/rfc2136_test.go Resolved
Comment thread provider/rfc2136/rfc2136_test.go Resolved
Comment thread provider/rfc2136/rfc2136.go Resolved
@mmontes11

Copy link
Copy Markdown
Contributor Author

The three apply*Batches helpers are still mostly copy-paste. Only the per-endpoint call differs.

func (r *rfc2136Provider) applyBatches(action string, eps []*endpoint.Endpoint, apply func(m *dns.Msg, idx int, ep *endpoint.Endpoint) error) []error

@mloiseleur Fair point, still a lot of boilerplate, agreed. Will work on the changes

@linux-foundation-easycla

linux-foundation-easycla Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

@kubernetes-prow kubernetes-prow Bot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. and removed cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Oct 28, 2026
@mmontes11
mmontes11 force-pushed the feature-rfc2136-applychanges branch from 2045f6b to 62a2126 Compare October 28, 2026 16:02
@kubernetes-prow kubernetes-prow Bot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Oct 28, 2026

@mmontes11 mmontes11 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Review comments addressed:

  • Replaced the three apply*Batches helpers with a single applyBatches(action, eps, apply) used from all three call sites.
  • Extended TestRfc2136ApplyChangesAllFilteredSendsNothing with filtered UpdateOld/UpdateNew pairs and a sendCount on the stub, asserting no packets are sent at all.

One small behavior change to note: the previous loop ignored per-endpoint apply errors; this version collects them into the returned error. Please let me know if this acceptable or you rather keep the previous behaviour.

Verified locally: go test -race ./provider/rfc2136/... passes, golangci-lint clean, ApplyChanges cc=2.

@mmontes11
mmontes11 requested a review from mloiseleur October 28, 2026 16:09
@mmontes11

Copy link
Copy Markdown
Contributor Author

Also, it seems like this CI step is not happy, but seems unrelated to the changes:
https://prow.k8s.io/view/gs/kubernetes-ci-logs/pr-logs/pull/kubernetes-sigs_external-dns/6868/pull-external-dns-unit-test/2115474306380075008

@mloiseleur

Copy link
Copy Markdown
Collaborator

/retest

@mloiseleur mloiseleur left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes, collecting the errors is better than ignoring them.

#6896 will drop most invalid targets before they reach the provider, but TXT still slips through: "unbalanced passes CheckEndpoint and fails dns.NewRR. On an update that sends the delete without the add.

Could you build each endpoint into a scratch msg and merge it into m[zone] only on success, wrapping the error with ep.DNSName? A test with an invalid TXT target on UpdateNew would cover it. Fine as a follow-up.

Nits: drop // Add the root zone, shorten the idx comment, and update the PR body (it still names the three apply*Batches helpers) to mention the error behavior change.

@mmontes11

Copy link
Copy Markdown
Contributor Author

Thanks @mloiseleur for the review — all three comments are addressed (62a21265, plus follow-up cf620f2f):

  1. The stub now counts SendMessage calls, and TestRfc2136ApplyChangesAllFilteredSendsNothing asserts the count is zero, so removing the len(z.Ns) > 0 check would now fail the test.
  2. The all-filtered test now includes UpdateOld/UpdateNew entries, so the filter and the UpdateOld index mapping are covered on the update path too; TestRfc2136ApplyChangesInvalidTargetSendsNothing additionally pins that a failed update never reaches the nameserver.
  3. The doc comments that restated the helper names are dropped.

One extra change since your review: update batches now build into a scratch message and merge only on success, so a failed endpoint does not leave a partial update in the batch (cf620f2f).

go test -race ./provider/rfc2136/... and golangci-lint v2.14.0 are both clean on this branch.

@mmontes11
mmontes11 requested a review from mloiseleur October 30, 2026 09:54
@mloiseleur mloiseleur changed the title refactor(rfc2136): reduce ApplyChanges cyclomatic complexity fix(rfc2136): surface record build errors and skip partial updates in ApplyChanges Oct 30, 2026
@mloiseleur

mloiseleur commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks, cf620f2 looks right: the scratch msg keeps a failed update from sending its delete.
One ask left: please update the PR body, it still reads as a pure refactor. It now also surfaces Add/Update/RemoveRecord errors (they were dropped before) and skips invalid endpoints instead of sending a partial update.
I retitled the PR to fix(rfc2136) so the changelog shows that.

@kubernetes-prow kubernetes-prow Bot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 30, 2026
@mmontes11

Copy link
Copy Markdown
Contributor Author

@mloiseleur Very much appreciated for the in-depth review, I've updated the PR body to reflect all the changes.

@mloiseleur mloiseleur left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

/lgtm

Comment thread provider/rfc2136/rfc2136.go Outdated
for c, chunk := range chunkBy(changes.Create, r.batchChangeSize) {
log.Debugf("Processing batch %d of create changes", c)
if len(errs) > 0 {
return provider.NewSoftErrorf("RFC2136 had errors in one or more of its batches: %v", errs)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wrong wrapping. flattens %w wrappers, so errors.Is can't recover inner causes

Comment thread provider/rfc2136/rfc2136.go Outdated
}

zone := findMsgZone(ep, r.zoneNames)
m[zone].SetUpdate(zone)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

could be called once per batch, no need to call it per endpoint.

Comment thread provider/rfc2136/rfc2136.go Outdated

r.AddRecord(m[zone], ep)
}
func (r *rfc2136Provider) acceptsEndpoint(ep *endpoint.Endpoint) bool {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The helper is called in exactly one place, and is quite simple logic

 for i, ep := range chunk {
      if !r.domainFilter.Match(ep.DNSName) {
          log.Debugf("Skipping record %s because it was filtered out by the specified --domain-filter", ep.DNSName)
          continue
      }
      ...
  }

@kubernetes-prow kubernetes-prow Bot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed lgtm "Looks good to me", indicates that a PR is ready to be merged. labels Oct 31, 2026
Signed-off-by: Martin Montes <martin11lrx@gmail.com>
Address review comments:
- collapse applyCreateBatches/applyUpdateBatches/applyDeleteBatches into
  one applyBatches(action, eps, apply) helper, with the update closure
  indexing changes.UpdateOld[idx]
- test stub now counts SendMessage calls and the all-filtered test
  asserts a zero count and covers the update path (UpdateOld/UpdateNew)
- drop doc comments that restate the helper names

Signed-off-by: Martin Montes <martin11lrx@gmail.com>
Address review comments:
- build each endpoint into a scratch message and merge it into the
  batch only on success, so a failed endpoint (e.g. an invalid TXT
  target) no longer leaves a partial update behind; wrap the error
  with the endpoint's DNS name
- drop the root-zone comment and shorten the idx comment
- new test with an invalid TXT target on UpdateNew asserts the error
  is a SoftError naming the endpoint and that nothing is sent

Signed-off-by: Martin Montes <martin11lrx@gmail.com>
…h setup

Address review comments:
- join the batch errors with errors.Join and wrap them with %w so the
  soft error keeps the inner causes recoverable via errors.Is
- call SetUpdate once per zone per batch instead of once per endpoint
- inline the one-use acceptsEndpoint helper back into the batch loop

Signed-off-by: Martin Montes <martin11lrx@gmail.com>
@mmontes11
mmontes11 force-pushed the feature-rfc2136-applychanges branch from cf620f2 to 2c9d408 Compare October 31, 2026 20:26
@kubernetes-prow kubernetes-prow Bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 31, 2026
@ivankatliarchuk

Copy link
Copy Markdown
Member

#6868 (review)

/lgtm
/approve

@kubernetes-prow kubernetes-prow Bot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 5, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ivankatliarchuk

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubernetes-prow kubernetes-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 5, 2026
@kubernetes-prow
kubernetes-prow Bot merged commit 46ed5e5 into kubernetes-sigs:master Nov 5, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. provider Issues or PRs related to a provider size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants