From 88a849626f56a8f6defc13df7a7f6bd3bd71e7d0 Mon Sep 17 00:00:00 2001 From: Sam Edwards Date: Fri, 8 Sep 2023 21:43:23 -0600 Subject: [PATCH 1/2] Don't use `To16() != nil` to detect IPv6 addresses The ip.To16() function returns non-nil if `ip` is any kind of IP address, including IPv4. To look for IPv6 specifically, use ip.To4() == nil. Signed-off-by: Sam Edwards --- pkg/cri/sbserver/sandbox_run.go | 2 +- pkg/cri/server/sandbox_run.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/cri/sbserver/sandbox_run.go b/pkg/cri/sbserver/sandbox_run.go index e86b867c5..056c452cf 100644 --- a/pkg/cri/sbserver/sandbox_run.go +++ b/pkg/cri/sbserver/sandbox_run.go @@ -612,7 +612,7 @@ func selectPodIPs(ctx context.Context, configs []*cni.IPConfig, preference strin } case "ipv6": for i, ip := range configs { - if ip.IP.To16() != nil { + if ip.IP.To4() == nil { return ipString(ip), append(extra, toStrings(configs[i+1:])...) } extra = append(extra, ipString(ip)) diff --git a/pkg/cri/server/sandbox_run.go b/pkg/cri/server/sandbox_run.go index 55e14e5a8..6cf051823 100644 --- a/pkg/cri/server/sandbox_run.go +++ b/pkg/cri/server/sandbox_run.go @@ -692,7 +692,7 @@ func selectPodIPs(ctx context.Context, configs []*cni.IPConfig, preference strin } case "ipv6": for i, ip := range configs { - if ip.IP.To16() != nil { + if ip.IP.To4() == nil { return ipString(ip), append(extra, toStrings(configs[i+1:])...) } extra = append(extra, ipString(ip)) From f77185f9e8e2090486897c2240e5f6b945e3f234 Mon Sep 17 00:00:00 2001 From: Sam Edwards Date: Fri, 8 Sep 2023 21:41:41 -0600 Subject: [PATCH 2/2] Fix "even if IPv4 comes first" test to have IPv4 first Signed-off-by: Sam Edwards --- pkg/cri/sbserver/sandbox_run_test.go | 2 +- pkg/cri/server/sandbox_run_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/cri/sbserver/sandbox_run_test.go b/pkg/cri/sbserver/sandbox_run_test.go index a51810925..238ef031f 100644 --- a/pkg/cri/sbserver/sandbox_run_test.go +++ b/pkg/cri/sbserver/sandbox_run_test.go @@ -145,7 +145,7 @@ func TestSelectPodIP(t *testing.T) { }, { desc: "ipv6 should be picked even if ipv4 comes first", - ips: []string{"2001:db8:85a3::8a2e:370:7334", "192.168.17.43"}, + ips: []string{"192.168.17.43", "2001:db8:85a3::8a2e:370:7334"}, expectedIP: "2001:db8:85a3::8a2e:370:7334", expectedAdditionalIPs: []string{"192.168.17.43"}, pref: "ipv6", diff --git a/pkg/cri/server/sandbox_run_test.go b/pkg/cri/server/sandbox_run_test.go index d8f494625..b1bc1ec13 100644 --- a/pkg/cri/server/sandbox_run_test.go +++ b/pkg/cri/server/sandbox_run_test.go @@ -293,7 +293,7 @@ func TestSelectPodIP(t *testing.T) { }, { desc: "ipv6 should be picked even if ipv4 comes first", - ips: []string{"2001:db8:85a3::8a2e:370:7334", "192.168.17.43"}, + ips: []string{"192.168.17.43", "2001:db8:85a3::8a2e:370:7334"}, expectedIP: "2001:db8:85a3::8a2e:370:7334", expectedAdditionalIPs: []string{"192.168.17.43"}, pref: "ipv6",