mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-11 04:52:08 +00:00
Downward API support for status.hostIPs
This commit is contained in:
parent
bf030fd68a
commit
335d905ce9
@ -830,6 +830,19 @@ func (kl *Kubelet) podFieldSelectorRuntimeValue(fs *v1.ObjectFieldSelector, pod
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return hostIPs[0].String(), nil
|
return hostIPs[0].String(), nil
|
||||||
|
case "status.hostIPs":
|
||||||
|
if !utilfeature.DefaultFeatureGate.Enabled(features.PodHostIPs) {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
hostIPs, err := kl.getHostIPsAnyWay()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
ips := make([]string, 0, len(hostIPs))
|
||||||
|
for _, ip := range hostIPs {
|
||||||
|
ips = append(ips, ip.String())
|
||||||
|
}
|
||||||
|
return strings.Join(ips, ","), nil
|
||||||
case "status.podIP":
|
case "status.podIP":
|
||||||
return podIP, nil
|
return podIP, nil
|
||||||
case "status.podIPs":
|
case "status.podIPs":
|
||||||
|
@ -408,6 +408,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string // the name of the test case
|
name string // the name of the test case
|
||||||
ns string // the namespace to generate environment for
|
ns string // the namespace to generate environment for
|
||||||
|
enablePodHostIPs bool // enable PodHostIPs feature gate
|
||||||
enableServiceLinks *bool // enabling service links
|
enableServiceLinks *bool // enabling service links
|
||||||
container *v1.Container // the container to use
|
container *v1.Container // the container to use
|
||||||
nilLister bool // whether the lister should be nil
|
nilLister bool // whether the lister should be nil
|
||||||
@ -642,6 +643,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "downward api pod",
|
name: "downward api pod",
|
||||||
|
enablePodHostIPs: true,
|
||||||
ns: "downward-api",
|
ns: "downward-api",
|
||||||
enableServiceLinks: &falseValue,
|
enableServiceLinks: &falseValue,
|
||||||
container: &v1.Container{
|
container: &v1.Container{
|
||||||
@ -709,6 +711,15 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "HOST_IPS",
|
||||||
|
ValueFrom: &v1.EnvVarSource{
|
||||||
|
FieldRef: &v1.ObjectFieldSelector{
|
||||||
|
APIVersion: "v1",
|
||||||
|
FieldPath: "status.hostIPs",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
podIPs: []string{"1.2.3.4", "fd00::6"},
|
podIPs: []string{"1.2.3.4", "fd00::6"},
|
||||||
@ -721,10 +732,12 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||||||
{Name: "POD_IP", Value: "1.2.3.4"},
|
{Name: "POD_IP", Value: "1.2.3.4"},
|
||||||
{Name: "POD_IPS", Value: "1.2.3.4,fd00::6"},
|
{Name: "POD_IPS", Value: "1.2.3.4,fd00::6"},
|
||||||
{Name: "HOST_IP", Value: testKubeletHostIP},
|
{Name: "HOST_IP", Value: testKubeletHostIP},
|
||||||
|
{Name: "HOST_IPS", Value: testKubeletHostIP + "," + testKubeletHostIPv6},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "downward api pod ips reverse order",
|
name: "downward api pod ips reverse order",
|
||||||
|
enablePodHostIPs: true,
|
||||||
ns: "downward-api",
|
ns: "downward-api",
|
||||||
enableServiceLinks: &falseValue,
|
enableServiceLinks: &falseValue,
|
||||||
container: &v1.Container{
|
container: &v1.Container{
|
||||||
@ -756,6 +769,15 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "HOST_IPS",
|
||||||
|
ValueFrom: &v1.EnvVarSource{
|
||||||
|
FieldRef: &v1.ObjectFieldSelector{
|
||||||
|
APIVersion: "v1",
|
||||||
|
FieldPath: "status.hostIPs",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
podIPs: []string{"fd00::6", "1.2.3.4"},
|
podIPs: []string{"fd00::6", "1.2.3.4"},
|
||||||
@ -764,10 +786,12 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||||||
{Name: "POD_IP", Value: "1.2.3.4"},
|
{Name: "POD_IP", Value: "1.2.3.4"},
|
||||||
{Name: "POD_IPS", Value: "1.2.3.4,fd00::6"},
|
{Name: "POD_IPS", Value: "1.2.3.4,fd00::6"},
|
||||||
{Name: "HOST_IP", Value: testKubeletHostIP},
|
{Name: "HOST_IP", Value: testKubeletHostIP},
|
||||||
|
{Name: "HOST_IPS", Value: testKubeletHostIP + "," + testKubeletHostIPv6},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "downward api pod ips multiple ips",
|
name: "downward api pod ips multiple ips",
|
||||||
|
enablePodHostIPs: true,
|
||||||
ns: "downward-api",
|
ns: "downward-api",
|
||||||
enableServiceLinks: &falseValue,
|
enableServiceLinks: &falseValue,
|
||||||
container: &v1.Container{
|
container: &v1.Container{
|
||||||
@ -799,6 +823,15 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "HOST_IPS",
|
||||||
|
ValueFrom: &v1.EnvVarSource{
|
||||||
|
FieldRef: &v1.ObjectFieldSelector{
|
||||||
|
APIVersion: "v1",
|
||||||
|
FieldPath: "status.hostIPs",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
podIPs: []string{"1.2.3.4", "192.168.1.1.", "fd00::6"},
|
podIPs: []string{"1.2.3.4", "192.168.1.1.", "fd00::6"},
|
||||||
@ -807,6 +840,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||||||
{Name: "POD_IP", Value: "1.2.3.4"},
|
{Name: "POD_IP", Value: "1.2.3.4"},
|
||||||
{Name: "POD_IPS", Value: "1.2.3.4,fd00::6"},
|
{Name: "POD_IPS", Value: "1.2.3.4,fd00::6"},
|
||||||
{Name: "HOST_IP", Value: testKubeletHostIP},
|
{Name: "HOST_IP", Value: testKubeletHostIP},
|
||||||
|
{Name: "HOST_IPS", Value: testKubeletHostIP + "," + testKubeletHostIPv6},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -1954,10 +1988,36 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||||||
},
|
},
|
||||||
expectedError: true,
|
expectedError: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "downward api pod without host ips",
|
||||||
|
enablePodHostIPs: false,
|
||||||
|
ns: "downward-api",
|
||||||
|
enableServiceLinks: &falseValue,
|
||||||
|
container: &v1.Container{
|
||||||
|
Env: []v1.EnvVar{
|
||||||
|
{
|
||||||
|
Name: "HOST_IPS",
|
||||||
|
ValueFrom: &v1.EnvVarSource{
|
||||||
|
FieldRef: &v1.ObjectFieldSelector{
|
||||||
|
APIVersion: "v1",
|
||||||
|
FieldPath: "status.hostIPs",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
podIPs: []string{"1.2.3.4", "fd00::6"},
|
||||||
|
nilLister: true,
|
||||||
|
expectedEnvs: []kubecontainer.EnvVar{
|
||||||
|
{Name: "HOST_IPS", Value: ""},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.PodHostIPs, tc.enablePodHostIPs)()
|
||||||
|
|
||||||
fakeRecorder := record.NewFakeRecorder(1)
|
fakeRecorder := record.NewFakeRecorder(1)
|
||||||
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
|
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
|
||||||
testKubelet.kubelet.recorder = fakeRecorder
|
testKubelet.kubelet.recorder = fakeRecorder
|
||||||
|
Loading…
Reference in New Issue
Block a user