mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-11-06 13:18:21 +00:00
Make kubelet always canonicalize the PodIPs
This commit is contained in:
@@ -22,6 +22,7 @@ import (
|
|||||||
goerrors "errors"
|
goerrors "errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
@@ -1988,23 +1989,27 @@ func (kl *Kubelet) generateAPIPodStatus(pod *v1.Pod, podStatus *kubecontainer.Po
|
|||||||
// and use them for the Pod.Status.PodIPs and the Downward API environment variables
|
// and use them for the Pod.Status.PodIPs and the Downward API environment variables
|
||||||
func (kl *Kubelet) sortPodIPs(podIPs []string) []string {
|
func (kl *Kubelet) sortPodIPs(podIPs []string) []string {
|
||||||
ips := make([]string, 0, 2)
|
ips := make([]string, 0, 2)
|
||||||
var validPrimaryIP, validSecondaryIP func(ip string) bool
|
var validPrimaryIP, validSecondaryIP func(ip net.IP) bool
|
||||||
if len(kl.nodeIPs) == 0 || utilnet.IsIPv4(kl.nodeIPs[0]) {
|
if len(kl.nodeIPs) == 0 || utilnet.IsIPv4(kl.nodeIPs[0]) {
|
||||||
validPrimaryIP = utilnet.IsIPv4String
|
validPrimaryIP = utilnet.IsIPv4
|
||||||
validSecondaryIP = utilnet.IsIPv6String
|
validSecondaryIP = utilnet.IsIPv6
|
||||||
} else {
|
} else {
|
||||||
validPrimaryIP = utilnet.IsIPv6String
|
validPrimaryIP = utilnet.IsIPv6
|
||||||
validSecondaryIP = utilnet.IsIPv4String
|
validSecondaryIP = utilnet.IsIPv4
|
||||||
}
|
}
|
||||||
for _, ip := range podIPs {
|
|
||||||
|
// We parse and re-stringify the IPs in case the values from CRI use an irregular format.
|
||||||
|
for _, ipStr := range podIPs {
|
||||||
|
ip := utilnet.ParseIPSloppy(ipStr)
|
||||||
if validPrimaryIP(ip) {
|
if validPrimaryIP(ip) {
|
||||||
ips = append(ips, ip)
|
ips = append(ips, ip.String())
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, ip := range podIPs {
|
for _, ipStr := range podIPs {
|
||||||
|
ip := utilnet.ParseIPSloppy(ipStr)
|
||||||
if validSecondaryIP(ip) {
|
if validSecondaryIP(ip) {
|
||||||
ips = append(ips, ip)
|
ips = append(ips, ip.String())
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4580,6 +4580,12 @@ func TestSortPodIPs(t *testing.T) {
|
|||||||
podIPs: []string{"10.0.0.1", "10.0.0.2", "fd01::1234", "10.0.0.3", "fd01::5678"},
|
podIPs: []string{"10.0.0.1", "10.0.0.2", "fd01::1234", "10.0.0.3", "fd01::5678"},
|
||||||
expectedIPs: []string{"10.0.0.1", "fd01::1234"},
|
expectedIPs: []string{"10.0.0.1", "fd01::1234"},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Badly-formatted IPs from CRI",
|
||||||
|
nodeIP: "",
|
||||||
|
podIPs: []string{"010.000.000.001", "fd01:0:0:0:0:0:0:1234"},
|
||||||
|
expectedIPs: []string{"10.0.0.1", "fd01::1234"},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testcases {
|
for _, tc := range testcases {
|
||||||
|
|||||||
Reference in New Issue
Block a user