Skip to content

Commit e31a398

Browse files
committed
Make kubelet always canonicalize the PodIPs
1 parent 6512de7 commit e31a398

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

pkg/kubelet/kubelet_pods.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
goerrors "errors"
2323
"fmt"
2424
"io"
25+
"net"
2526
"net/http"
2627
"net/url"
2728
"os"
@@ -1988,23 +1989,27 @@ func (kl *Kubelet) generateAPIPodStatus(pod *v1.Pod, podStatus *kubecontainer.Po
19881989
// and use them for the Pod.Status.PodIPs and the Downward API environment variables
19891990
func (kl *Kubelet) sortPodIPs(podIPs []string) []string {
19901991
ips := make([]string, 0, 2)
1991-
var validPrimaryIP, validSecondaryIP func(ip string) bool
1992+
var validPrimaryIP, validSecondaryIP func(ip net.IP) bool
19921993
if len(kl.nodeIPs) == 0 || utilnet.IsIPv4(kl.nodeIPs[0]) {
1993-
validPrimaryIP = utilnet.IsIPv4String
1994-
validSecondaryIP = utilnet.IsIPv6String
1994+
validPrimaryIP = utilnet.IsIPv4
1995+
validSecondaryIP = utilnet.IsIPv6
19951996
} else {
1996-
validPrimaryIP = utilnet.IsIPv6String
1997-
validSecondaryIP = utilnet.IsIPv4String
1997+
validPrimaryIP = utilnet.IsIPv6
1998+
validSecondaryIP = utilnet.IsIPv4
19981999
}
1999-
for _, ip := range podIPs {
2000+
2001+
// We parse and re-stringify the IPs in case the values from CRI use an irregular format.
2002+
for _, ipStr := range podIPs {
2003+
ip := utilnet.ParseIPSloppy(ipStr)
20002004
if validPrimaryIP(ip) {
2001-
ips = append(ips, ip)
2005+
ips = append(ips, ip.String())
20022006
break
20032007
}
20042008
}
2005-
for _, ip := range podIPs {
2009+
for _, ipStr := range podIPs {
2010+
ip := utilnet.ParseIPSloppy(ipStr)
20062011
if validSecondaryIP(ip) {
2007-
ips = append(ips, ip)
2012+
ips = append(ips, ip.String())
20082013
break
20092014
}
20102015
}

pkg/kubelet/kubelet_pods_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4580,6 +4580,12 @@ func TestSortPodIPs(t *testing.T) {
45804580
podIPs: []string{"10.0.0.1", "10.0.0.2", "fd01::1234", "10.0.0.3", "fd01::5678"},
45814581
expectedIPs: []string{"10.0.0.1", "fd01::1234"},
45824582
},
4583+
{
4584+
name: "Badly-formatted IPs from CRI",
4585+
nodeIP: "",
4586+
podIPs: []string{"010.000.000.001", "fd01:0:0:0:0:0:0:1234"},
4587+
expectedIPs: []string{"10.0.0.1", "fd01::1234"},
4588+
},
45834589
}
45844590

45854591
for _, tc := range testcases {

0 commit comments

Comments
 (0)