Add HostIPs for kubelet

This commit is contained in:
Shiming Zhang 2023-05-28 00:36:54 +08:00
parent dacb689002
commit e6bdd224c1
3 changed files with 31 additions and 1 deletions

View File

@ -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) {

View File

@ -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()

View File

@ -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{
{