Skip to content

refactor(pihole): reduce cyclomatic complexity of ApplyChanges#6390

Open
AndrewCharlesHay wants to merge 2 commits intokubernetes-sigs:masterfrom
AndrewCharlesHay:refactor/pihole-apply-changes-complexity
Open

refactor(pihole): reduce cyclomatic complexity of ApplyChanges#6390
AndrewCharlesHay wants to merge 2 commits intokubernetes-sigs:masterfrom
AndrewCharlesHay:refactor/pihole-apply-changes-complexity

Conversation

@AndrewCharlesHay
Copy link
Copy Markdown
Contributor

Contributes to #5419.

What

PiholeProvider.ApplyChanges was at cyclomatic complexity 16 — a single function juggling delete / build-updates / reconcile-updates / create phases, with nested apiVersion == "6" branches in the middle two phases.

This PR decomposes it into phase-per-method helpers so each function is under the cyclop threshold and the top-level reads as a linear sequence:

ApplyChanges
  - applyDeletes
  - buildUpdateMap     (v6 target merge/dedup lives here)
  - applyUpdateOld     (drops no-op updates, deletes changed ones)
  - updateIsNoOp       (v6 full-target vs v5 first-target compare)
  - applyCreates

After this change ApplyChanges no longer appears in the cyclop offender list (verified locally with max-complexity: 10).

Behavior

Unchanged:

  • v6 still merges and deduplicates targets per (DNSName, RecordType) before calling createRecord.
  • v5 still compares only the first target when deciding whether an update is a no-op (matches the historical single-target semantics of the v5 local-DNS endpoint).
  • Pure creates still run before update-driven creates.
  • Errors short-circuit in the same places.

Test plan

  • go test ./provider/pihole/... — all existing pihole tests pass without modification
  • go build ./...
  • Local cyclop run confirms ApplyChanges is below threshold

No new tests added because every code path is still exercised by the existing TestProvider_ApplyChanges / TestProviderV6_ApplyChanges suites — this is a pure extract-method refactor with no behavioral change.

Split ApplyChanges into phase-per-method helpers so each function is
under the cyclop threshold and the top-level reads as a linear sequence
of phases:

  ApplyChanges
    - applyDeletes
    - buildUpdateMap     (v6 target merge/dedup lives here)
    - applyUpdateOld     (drops no-op updates, deletes changed ones)
    - updateIsNoOp       (v6 full-target vs v5 first-target compare)
    - applyCreates

Behavior is unchanged: v6 still merges and deduplicates targets per
(DNSName, RecordType), v5 still only compares the first target when
deciding whether an update is a no-op, and the create phase still runs
pure creates before update-driven creates.

Contributes to kubernetes-sigs#5419.

Signed-off-by: Andrew Hay <andrew.hay@benchmarkanalytics.com>
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign raffo for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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

@k8s-ci-robot k8s-ci-robot added the provider Issues or PRs related to a provider label Apr 24, 2026
@k8s-ci-robot k8s-ci-robot requested review from szuecs and vflaux April 24, 2026 01:39
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Apr 24, 2026
@coveralls
Copy link
Copy Markdown

coveralls commented Apr 24, 2026

Coverage Report for CI Build 24888140379

Warning

Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes.
Quick fix: rebase this PR. Learn more →

Coverage decreased (-0.02%) to 80.446%

Details

  • Coverage decreased (-0.02%) from the base build.
  • Patch coverage: No coverable lines changed in this PR.
  • 11 coverage regressions across 1 file.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

11 previously-covered lines in 1 file lost coverage.

File Lines Losing Coverage Coverage
pihole/pihole.go 11 70.1%

Coverage Stats

Coverage Status
Relevant Lines: 21382
Covered Lines: 17201
Line Coverage: 80.45%
Coverage Strength: 1466.36 hits per line

💛 - Coveralls

Comment thread provider/pihole/pihole.go Outdated
return err
}
newRecord, ok := updateNew[key]
if !ok {
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.

I’d prefer to keep checking for a nil newRecord.

Per @vflaux's review feedback, use the nil-pointer check on the map
lookup rather than the two-value comma-ok form. Behavior is unchanged
(a missing key and a stored nil *Endpoint are both treated as 'no
paired update to process') but the style matches the pre-refactor code
and reads more naturally.

Signed-off-by: Andrew Hay <andrew.hay@benchmarkanalytics.com>
@AndrewCharlesHay
Copy link
Copy Markdown
Contributor Author

AndrewCharlesHay commented Apr 24, 2026

Thanks @vflaux, just pushed the fix. Switched applyUpdateOld from the two-value newRecord, ok := updateNew[key] form back to newRecord := updateNew[key]; if newRecord == nil { continue } so it matches the pre-refactor style. Behavior is unchanged.

@ivankatliarchuk
Copy link
Copy Markdown
Member

Are you using pihole? Could you share working example similar to #5111 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. provider Issues or PRs related to a provider size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants