Skip to content

Commit

Permalink
Merge pull request #4351 from kubeservice-stack/support-ipv6-expanded
Browse files Browse the repository at this point in the history
Fix(ipv6):  support ipv6 shortener and expander equal
  • Loading branch information
k8s-ci-robot committed Apr 9, 2024
2 parents 46dd6d2 + f3c1e32 commit c7f1559
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
33 changes: 32 additions & 1 deletion endpoint/endpoint.go
Expand Up @@ -75,7 +75,17 @@ func (t Targets) Len() int {
}

func (t Targets) Less(i, j int) bool {
return t[i] < t[j]
ipi, err := netip.ParseAddr(t[i])
if err != nil {
return t[i] < t[j]
}

ipj, err := netip.ParseAddr(t[j])
if err != nil {
return t[i] < t[j]
}

return ipi.String() < ipj.String()
}

func (t Targets) Swap(i, j int) {
Expand All @@ -92,6 +102,27 @@ func (t Targets) Same(o Targets) bool {

for i, e := range t {
if !strings.EqualFold(e, o[i]) {
// IPv6 can be shortened, so it should be parsed for equality checking
ipA, err := netip.ParseAddr(e)
if err != nil {
log.WithFields(log.Fields{
"targets": t,
"comparisonTargets": o,
}).Debugf("Couldn't parse %s as an IP address: %v", e, err)
}

ipB, err := netip.ParseAddr(o[i])
if err != nil {
log.WithFields(log.Fields{
"targets": t,
"comparisonTargets": o,
}).Debugf("Couldn't parse %s as an IP address: %v", e, err)
}

// IPv6 Address Shortener == IPv6 Address Expander
if ipA.IsValid() && ipB.IsValid() {
return ipA.String() == ipB.String()
}
return false
}
}
Expand Down
36 changes: 36 additions & 0 deletions endpoint/endpoint_test.go
Expand Up @@ -41,6 +41,7 @@ func TestTargetsSame(t *testing.T) {
{""},
{"1.2.3.4"},
{"8.8.8.8", "8.8.4.4"},
{"dd:dd::01", "::1", "::0001"},
{"example.org", "EXAMPLE.ORG"},
}

Expand All @@ -51,6 +52,37 @@ func TestTargetsSame(t *testing.T) {
}
}

func TestSameSuccess(t *testing.T) {
tests := []struct {
a Targets
b Targets
}{
{
[]string{"::1"},
[]string{"::0001"},
},
{
[]string{"::1", "dd:dd::01"},
[]string{"dd:00dd::0001", "::0001"},
},

{
[]string{"::1", "dd:dd::01"},
[]string{"00dd:dd::0001", "::0001"},
},
{
[]string{"::1", "1.1.1.1", "2600.com", "3.3.3.3"},
[]string{"2600.com", "::0001", "3.3.3.3", "1.1.1.1"},
},
}

for _, d := range tests {
if d.a.Same(d.b) == false {
t.Errorf("%#v should equal %#v", d.a, d.b)
}
}
}

func TestSameFailures(t *testing.T) {
tests := []struct {
a Targets
Expand All @@ -69,6 +101,10 @@ func TestSameFailures(t *testing.T) {
[]string{"1.2.3.4", "4.3.2.1"},
[]string{"8.8.8.8", "8.8.4.4"},
},
{
[]string{"::1", "2600.com", "3.3.3.3"},
[]string{"2600.com", "3.3.3.3", "1.1.1.1"},
},
}

for _, d := range tests {
Expand Down

0 comments on commit c7f1559

Please sign in to comment.