Skip to content

Commit

Permalink
[Dual-stack] Fix generateAPIPodStatus() of kubelet handling Secondary IP
Browse files Browse the repository at this point in the history
hostIPs order may not be be consistent. If secondary IP is before
primary one, current logic adds primary IP twice into PodIPs, which
leads to error: "may specify no more than one IP for each IP family".
In this case, the second IP shouldn't be added.

Co-authored-by: Antonio Ojea <antonio.ojea.garcia@gmail.com>
  • Loading branch information
lzhecheng and aojea committed Jul 3, 2023
1 parent d2be69a commit 985cf71
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/kubelet/kubelet_pods.go
Expand Up @@ -1624,6 +1624,13 @@ func (kl *Kubelet) generateAPIPodStatus(pod *v1.Pod, podStatus *kubecontainer.Po
if err != nil {
klog.V(4).InfoS("Cannot get host IPs", "err", err)
} else {
if s.HostIP != "" {
if utilnet.IPFamilyOfString(s.HostIP) != utilnet.IPFamilyOf(hostIPs[0]) {
kl.recorder.Eventf(pod, v1.EventTypeWarning, "HostIPsIPFamilyMismatch",
"Kubelet detected an IPv%s node IP (%s), but the cloud provider selected an IPv%s node IP (%s); pass an explicit `--node-ip` to kubelet to fix this.",
utilnet.IPFamilyOfString(s.HostIP), s.HostIP, utilnet.IPFamilyOf(hostIPs[0]), hostIPs[0].String())
}
}
s.HostIP = hostIPs[0].String()
// HostNetwork Pods inherit the node IPs as PodIPs. They are immutable once set,
// other than that if the node becomes dual-stack, we add the secondary IP.
Expand All @@ -1635,7 +1642,9 @@ func (kl *Kubelet) generateAPIPodStatus(pod *v1.Pod, podStatus *kubecontainer.Po
}
// Secondary IP is not set #105320
if len(hostIPs) == 2 && len(s.PodIPs) == 1 {
s.PodIPs = append(s.PodIPs, v1.PodIP{IP: hostIPs[1].String()})
if utilnet.IPFamilyOfString(s.PodIPs[0].IP) != utilnet.IPFamilyOf(hostIPs[1]) {
s.PodIPs = append(s.PodIPs, v1.PodIP{IP: hostIPs[1].String()})
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/kubelet/kubelet_pods_test.go
Expand Up @@ -3880,6 +3880,17 @@ func TestNodeAddressUpdatesGenerateAPIPodStatusHostNetworkPodIPs(t *testing.T) {
{IP: "2001:db8::2"},
},
},
{
name: "Update secondary after new secondary address dual-stack - reverse order",
nodeIPs: []string{"2001:db8::2"},
nodeAddresses: []v1.NodeAddress{
{Type: v1.NodeInternalIP, Address: "10.0.0.1"},
{Type: v1.NodeInternalIP, Address: "2001:db8::2"},
},
expectedPodIPs: []v1.PodIP{
{IP: "2001:db8::2"},
},
},
}

for _, tc := range testcases {
Expand Down

0 comments on commit 985cf71

Please sign in to comment.