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

fix: prefer IP addresses as "less" when comparing endpoints #2716

Merged
merged 3 commits into from
Jun 10, 2022

Conversation

wbh1
Copy link
Contributor

@wbh1 wbh1 commented Apr 20, 2022

Description

This PR attempts to handle comparison between the targets of two endpoints more robustly by checking if they are IP addresses before falling back to the original string comparison behavior.

IP addresses are already typically preferred over a hostname, but there is an edge case (like the one linked in the issue below) in which a hostname could be preferred over an IP address. This occurs when the hostname sorts lexicographically before the IP address during string comparison. For example, 1password.com would be preferred over 8.8.8.8 but 8.8.8.8 would be preferred over onepassword.com.

Because I chose to use to the new net/netip package, I bumped to Go 1.18. I hope that's alright. If not, I can rewrite to use the old net.IP type.

Fixes #2499

Checklist

  • Unit tests updated
  • End user documentation updated

Previously there was no distinction between an IP address and any other string
when doing a comparison to determine which is "less" when determining which endpoint to actually create.

This explicitly handles IP addresses and will always prefer
them over non-IP addresses when determining which of two targets is less.
Required for net/netip package
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Apr 20, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot
Copy link
Contributor

Welcome @wbh1!

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 the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Apr 20, 2022
@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. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Apr 20, 2022
@wbh1
Copy link
Contributor Author

wbh1 commented May 3, 2022

/assign @Raffo

@Raffo
Copy link
Contributor

Raffo commented Jun 1, 2022

ACK that I’ve seen this and I will take a look at it this week.

@Raffo Raffo closed this Jun 1, 2022
@Raffo Raffo reopened this Jun 1, 2022
Copy link
Contributor

@Raffo Raffo left a comment

Choose a reason for hiding this comment

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

Change LGTM except for one minor comments on error handling. I want to call out explicitly that I am okay with the upgrade to Go 1.18 which is something we should have done anyway, but that will of course require some bake time in terms of testing the image although I don’t expect breaking changes.

return e < o[i]
// Explicitly prefers IP addresses (e.g. A records) over FQDNs (e.g. CNAMEs).
// This prevents behavior like `1-2-3-4.example.com` being "less" than `1.2.3.4` when doing lexicographical string comparison.
ipA, _ := netip.ParseAddr(e)
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if ParseAddr returns an error? Can we guarantee that it is impossible and thus ignore the error? If this is the case, can we add an error to clarify it to future readers? Same for the line right below this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Raffo Any parsing errors are caught when the IsValid() function is run below, since an invalid IP address is returned in those cases.

I will add debug-level logging to print out the error and a note to clarify, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the aforementioned logging and additional comments. Worth mentioning that the debug logging will probably be noisy as I believe it'll result in at least 1 log line per DNS record being managed when debug logging is enabled.

Log whenever a target is not able to be parsed as an IP address. This is expected
to occur fairly often (for example, with CNAME targets), but allows more visibility
into how targets are being compared.

NOTE: depending on the number and type of targets, this could be quite noisy.
@Raffo
Copy link
Contributor

Raffo commented Jun 10, 2022

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 10, 2022
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

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

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 Jun 10, 2022
@k8s-ci-robot k8s-ci-robot merged commit 2613d47 into kubernetes-sigs:master Jun 10, 2022
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/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.

On Linode, external-dns is creating CNAME alias and no TXT/A Records when target is Load Balancer
3 participants