kubernetes / utils

Non-Kubernetes-specific utility libraries which are consumed by multiple projects.
Apache License 2.0
331 stars 196 forks source link

Add IPFamilyOf() / IPFamilyOfString() / IPFamilyOfCIDR() / IPFamilyOfCIDRString() #271

Closed danwinship closed 1 year ago

danwinship commented 1 year ago

What type of PR is this? /kind api-change /kind feature

What this PR does / why we need it:

Often you want to test whether two IPs/CIDRs are of the same family,
without actually caring which family they are. While you can do this
by checking:

  if utilsnet.IsIPv6(ip) == utilsnet.IsIPv6CIDR(cidr) {

this is somewhat confusing to read and can look like it is checking
that they are both IPv6 if you read it quickly.

So add new functions so that you can do

  if utilsnet.IPFamilyOf(ip) == utilsnet.IPFamilyOfCIDR(cidr) {

Some examples:

Which issue(s) this PR fixes: none

Special notes for your reviewer: Re-post of #234 now that the API issue around net.IPv6 being an untyped string has been fixed separately (#265).

Release note:

Added IPFamilyOf() / IPFamilyOfString() / IPFamilyOfCIDR() /
IPFamilyOfCIDRString() functions to make it easier to check "are these two
IP strings / net.IPs / CIDR strings / net.IPNets the same family?"

/sig network /cc @aojea @thockin (who both reviewed it before in #234 and approved it; the endpoint here is the same, but I refactored from 2 commits to 4)

k8s-triage-robot commented 1 year ago

This PR may require API review.

If so, when the changes are ready, complete the pre-review checklist and request an API review.

Status of requested reviews is tracked in the API Review project.

danwinship commented 1 year ago

/remove-kind api-change

k8s-ci-robot commented 1 year ago

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: danwinship, thockin

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: - ~~[OWNERS](https://github.com/kubernetes/utils/blob/master/OWNERS)~~ [thockin] Approvers can indicate their approval by writing `/approve` in a comment Approvers can cancel approval by writing `/approve cancel` in a comment