Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only update PiHole entries when they have actually changed #3297

Merged
merged 4 commits into from
Feb 5, 2023

Conversation

sfleener
Copy link
Contributor

@sfleener sfleener commented Jan 8, 2023

Description

While testing the PiHole provider, I noticed some behavior that caused issues with internal hostnames being resolved.

Even when the DNS entries have not changed, the provider first deletes all entries it manages before re-adding them one by one with the same endpoint they originally had. With a moderate amount of entries managed by external-dns (~10-15 or so in my testing), this process takes about 10 seconds total. Using the default update interval of 60 seconds, this leaves a 5-10 second window every minute where these hostnames don't exist on PiHole and fail to resolve.

This PR adds a simple dedupe check that makes sure the entries have actually changed before deleting and re-adding them. This removes any time period where unchanged hostnames do not resolve, and only leaves an unresolvable window in cases where the target has actually changed (which is probably what you'd want anyway).

Considered Alternatives

  • Adding a proper upsert API to PiHole itself. While doable, it would be a bit more work and would also only cover new versions of PiHole.
  • Increase the default update interval for PiHole providers. Kicks the can down the road, not a real solution.
  • Make delete and create requests concurrently instead of running them one-by-one. This would shorten the window where hostnames are not available, but the unresolvable window does still exist and we would be bursting a bunch of requests to pihole simultaneously. This could be combated with limiting the number of concurrent requests, but it seemed like too much effort and complexity to be really worth it especially considering it doesn't fully solve the original problem.

Checklist

  • Unit tests updated
  • End user documentation updated: N/A

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jan 8, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot
Copy link
Contributor

Welcome @sfleener!

It looks like this is your first PR to kubernetes-sigs/external-dns 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/external-dns has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. 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. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Jan 8, 2023
@sfleener sfleener changed the title Pihole Only update PiHole entries when they have actually changed Jan 8, 2023
provider/pihole/pihole.go Outdated Show resolved Hide resolved
provider/pihole/pihole.go Outdated Show resolved Hide resolved
provider/pihole/pihole.go Outdated Show resolved Hide resolved
// Handle updated state - there are no endpoints for updating in place.
updateNew := make(map[string]*endpoint.Endpoint)
for _, ep := range changes.UpdateNew {
updateNew[ep.DNSName] = ep
Copy link
Contributor

Choose a reason for hiding this comment

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

What if changes.UpdateNew has multiple endpoints with the same DNSName? Shouldn't you put the RecordType in the map key?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! PiHole doesn't allow two records of the same RecordType with the same DNS name, but it does allow two records with the same DNS name if the record types are different. Fixed

Comment on lines 61 to 62
createRequests int
deleteRequests int
Copy link
Contributor

Choose a reason for hiding this comment

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

Better unit tests would explicitly specify the expected requests, verifying the dnsname, recordtype, and target.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the tests to compare the actual and expected requests instead of just the counts

Copy link
Contributor

Choose a reason for hiding this comment

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

The new version of the tests is asserting that the three new records are created in order, which isn't actually a requirement.

I would have written this to put the expected calls in the tracker. The client would then find the request in the tracker and remove it, asserting that the request was found in the tracker. At the end, the test would assert that there were no remaining expected calls in the tracker.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jan 9, 2023
@johngmyers
Copy link
Contributor

/lgtm
/assign @Raffo

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 9, 2023
@Raffo
Copy link
Contributor

Raffo commented Feb 4, 2023

@sfleener can you fix the linter issues? I'm happy to approve after those are fixed

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 4, 2023
@sfleener
Copy link
Contributor Author

sfleener commented Feb 4, 2023

@Raffo The lints appear to be gofmt errors in files I didn't touch, I sync'd with master which hopefully should resolve them

@Raffo
Copy link
Contributor

Raffo commented Feb 5, 2023

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Raffo, sfleener

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

The pull request process is described here

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 approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 5, 2023
@Raffo
Copy link
Contributor

Raffo commented Feb 5, 2023

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 5, 2023
@k8s-ci-robot k8s-ci-robot merged commit 91d8c22 into kubernetes-sigs:master Feb 5, 2023
@johngmyers johngmyers mentioned this pull request Jun 7, 2023
2 tasks
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. 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.

None yet

4 participants