From e6bdd224c1702b388f481566c92481780d3179ea Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Sun, 28 May 2023 00:36:54 +0800 Subject: [PATCH] Add HostIPs for kubelet --- pkg/kubelet/kubelet_pods.go | 9 ++++++++- pkg/kubelet/kubelet_pods_test.go | 18 ++++++++++++++++++ pkg/kubelet/kubelet_test.go | 5 +++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/pkg/kubelet/kubelet_pods.go b/pkg/kubelet/kubelet_pods.go index 7e593de8949..a7eb9a312d1 100644 --- a/pkg/kubelet/kubelet_pods.go +++ b/pkg/kubelet/kubelet_pods.go @@ -1732,7 +1732,7 @@ func (kl *Kubelet) generateAPIPodStatus(pod *v1.Pod, podStatus *kubecontainer.Po Type: v1.PodScheduled, Status: v1.ConditionTrue, }) - // set HostIP and initialize PodIP/PodIPs for host network pods + // set HostIP/HostIPs and initialize PodIP/PodIPs for host network pods if kl.kubeClient != nil { hostIPs, err := kl.getHostIPsAnyWay() if err != nil { @@ -1746,6 +1746,13 @@ func (kl *Kubelet) generateAPIPodStatus(pod *v1.Pod, podStatus *kubecontainer.Po } } s.HostIP = hostIPs[0].String() + if utilfeature.DefaultFeatureGate.Enabled(features.PodHostIPs) { + s.HostIPs = []v1.HostIP{{IP: s.HostIP}} + if len(hostIPs) == 2 { + s.HostIPs = append(s.HostIPs, v1.HostIP{IP: hostIPs[1].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. if kubecontainer.IsHostNetworkPod(pod) { diff --git a/pkg/kubelet/kubelet_pods_test.go b/pkg/kubelet/kubelet_pods_test.go index 420bc515de5..b541b7e4630 100644 --- a/pkg/kubelet/kubelet_pods_test.go +++ b/pkg/kubelet/kubelet_pods_test.go @@ -3244,6 +3244,7 @@ func Test_generateAPIPodStatus(t *testing.T) { tests := []struct { name string + enablePodHostIPs bool // enable PodHostIPs feature gate pod *v1.Pod currentStatus *kubecontainer.PodStatus unreadyContainer []string @@ -3329,9 +3330,11 @@ func Test_generateAPIPodStatus(t *testing.T) { runningState("containerB"), }, }, + enablePodHostIPs: true, expected: v1.PodStatus{ Phase: v1.PodRunning, HostIP: "127.0.0.1", + HostIPs: []v1.HostIP{{IP: "127.0.0.1"}, {IP: "::1"}}, QOSClass: v1.PodQOSBestEffort, Conditions: []v1.PodCondition{ {Type: v1.PodInitialized, Status: v1.ConditionTrue}, @@ -3367,9 +3370,11 @@ func Test_generateAPIPodStatus(t *testing.T) { runningState("containerB"), }, }, + enablePodHostIPs: true, expected: v1.PodStatus{ Phase: v1.PodRunning, HostIP: "127.0.0.1", + HostIPs: []v1.HostIP{{IP: "127.0.0.1"}, {IP: "::1"}}, QOSClass: v1.PodQOSBestEffort, Conditions: []v1.PodCondition{ {Type: v1.PodInitialized, Status: v1.ConditionTrue}, @@ -3406,9 +3411,11 @@ func Test_generateAPIPodStatus(t *testing.T) { runningState("containerB"), }, }, + enablePodHostIPs: true, expected: v1.PodStatus{ Phase: v1.PodSucceeded, HostIP: "127.0.0.1", + HostIPs: []v1.HostIP{{IP: "127.0.0.1"}, {IP: "::1"}}, QOSClass: v1.PodQOSBestEffort, Conditions: []v1.PodCondition{ {Type: v1.PodInitialized, Status: v1.ConditionTrue, Reason: "PodCompleted"}, @@ -3449,9 +3456,11 @@ func Test_generateAPIPodStatus(t *testing.T) { Reason: "Test", Message: "test", }, + enablePodHostIPs: true, expected: v1.PodStatus{ Phase: v1.PodSucceeded, HostIP: "127.0.0.1", + HostIPs: []v1.HostIP{{IP: "127.0.0.1"}, {IP: "::1"}}, QOSClass: v1.PodQOSBestEffort, Conditions: []v1.PodCondition{ {Type: v1.PodInitialized, Status: v1.ConditionTrue, Reason: "PodCompleted"}, @@ -3501,9 +3510,11 @@ func Test_generateAPIPodStatus(t *testing.T) { Reason: "Test", Message: "test", }, + enablePodHostIPs: true, expected: v1.PodStatus{ Phase: v1.PodSucceeded, HostIP: "127.0.0.1", + HostIPs: []v1.HostIP{{IP: "127.0.0.1"}, {IP: "::1"}}, QOSClass: v1.PodQOSBestEffort, Conditions: []v1.PodCondition{ {Type: v1.PodInitialized, Status: v1.ConditionTrue, Reason: "PodCompleted"}, @@ -3542,9 +3553,11 @@ func Test_generateAPIPodStatus(t *testing.T) { waitingState("containerB"), }, }, + enablePodHostIPs: true, expected: v1.PodStatus{ Phase: v1.PodPending, HostIP: "127.0.0.1", + HostIPs: []v1.HostIP{{IP: "127.0.0.1"}, {IP: "::1"}}, QOSClass: v1.PodQOSBestEffort, Conditions: []v1.PodCondition{ {Type: v1.PodInitialized, Status: v1.ConditionTrue}, @@ -3594,11 +3607,13 @@ func Test_generateAPIPodStatus(t *testing.T) { runningState("containerB"), }, }, + enablePodHostIPs: true, expected: v1.PodStatus{ Phase: v1.PodPending, Reason: "Test", Message: "test", HostIP: "127.0.0.1", + HostIPs: []v1.HostIP{{IP: "127.0.0.1"}, {IP: "::1"}}, QOSClass: v1.PodQOSBestEffort, Conditions: []v1.PodCondition{ {Type: v1.PodInitialized, Status: v1.ConditionTrue}, @@ -3654,9 +3669,11 @@ func Test_generateAPIPodStatus(t *testing.T) { runningState("containerB"), }, }, + enablePodHostIPs: true, expected: v1.PodStatus{ Phase: v1.PodRunning, HostIP: "127.0.0.1", + HostIPs: []v1.HostIP{{IP: "127.0.0.1"}, {IP: "::1"}}, QOSClass: v1.PodQOSBestEffort, Conditions: []v1.PodCondition{ {Type: v1.PodInitialized, Status: v1.ConditionTrue}, @@ -3679,6 +3696,7 @@ func Test_generateAPIPodStatus(t *testing.T) { for _, enablePodReadyToStartContainersCondition := range []bool{false, true} { t.Run(test.name, func(t *testing.T) { defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.PodDisruptionConditions, test.enablePodDisruptionConditions)() + defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.PodHostIPs, test.enablePodHostIPs)() defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.PodReadyToStartContainersCondition, enablePodReadyToStartContainersCondition)() testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */) defer testKubelet.Cleanup() diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index 73b39f191f7..1b83b320c00 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -109,6 +109,7 @@ func init() { const ( testKubeletHostname = "127.0.0.1" testKubeletHostIP = "127.0.0.1" + testKubeletHostIPv6 = "::1" // TODO(harry) any global place for these two? // Reasonable size range of all container images. 90%ile of images on dockerhub drops into this range. @@ -232,6 +233,10 @@ func newTestKubeletWithImageList( Type: v1.NodeInternalIP, Address: testKubeletHostIP, }, + { + Type: v1.NodeInternalIP, + Address: testKubeletHostIPv6, + }, }, VolumesAttached: []v1.AttachedVolume{ {