mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 20:53:33 +00:00
support hostIP in downward API
This commit is contained in:
parent
b4c71b1f26
commit
91c027d6cc
@ -190,6 +190,7 @@ func addConversionFuncs(scheme *runtime.Scheme) error {
|
|||||||
"spec.restartPolicy",
|
"spec.restartPolicy",
|
||||||
"spec.serviceAccountName",
|
"spec.serviceAccountName",
|
||||||
"status.phase",
|
"status.phase",
|
||||||
|
"status.hostIP",
|
||||||
"status.podIP":
|
"status.podIP":
|
||||||
return label, value, nil
|
return label, value, nil
|
||||||
// This is for backwards compatibility with old v1 clients which send spec.host
|
// This is for backwards compatibility with old v1 clients which send spec.host
|
||||||
|
@ -1416,7 +1416,7 @@ func ValidateEnv(vars []api.EnvVar, fldPath *field.Path) field.ErrorList {
|
|||||||
return allErrs
|
return allErrs
|
||||||
}
|
}
|
||||||
|
|
||||||
var validFieldPathExpressionsEnv = sets.NewString("metadata.name", "metadata.namespace", "spec.nodeName", "spec.serviceAccountName", "status.podIP")
|
var validFieldPathExpressionsEnv = sets.NewString("metadata.name", "metadata.namespace", "spec.nodeName", "spec.serviceAccountName", "status.hostIP", "status.podIP")
|
||||||
var validContainerResourceFieldPathExpressions = sets.NewString("limits.cpu", "limits.memory", "requests.cpu", "requests.memory")
|
var validContainerResourceFieldPathExpressions = sets.NewString("limits.cpu", "limits.memory", "requests.cpu", "requests.memory")
|
||||||
|
|
||||||
func validateEnvVarValueFrom(ev api.EnvVar, fldPath *field.Path) field.ErrorList {
|
func validateEnvVarValueFrom(ev api.EnvVar, fldPath *field.Path) field.ErrorList {
|
||||||
|
@ -2153,6 +2153,24 @@ func TestValidateEnv(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "abc",
|
||||||
|
ValueFrom: &api.EnvVarSource{
|
||||||
|
FieldRef: &api.ObjectFieldSelector{
|
||||||
|
APIVersion: api.Registry.GroupOrDie(api.GroupName).GroupVersion.String(),
|
||||||
|
FieldPath: "status.hostIP",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "abc",
|
||||||
|
ValueFrom: &api.EnvVarSource{
|
||||||
|
FieldRef: &api.ObjectFieldSelector{
|
||||||
|
APIVersion: api.Registry.GroupOrDie(api.GroupName).GroupVersion.String(),
|
||||||
|
FieldPath: "status.podIP",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Name: "secret_value",
|
Name: "secret_value",
|
||||||
ValueFrom: &api.EnvVarSource{
|
ValueFrom: &api.EnvVarSource{
|
||||||
@ -2328,7 +2346,7 @@ func TestValidateEnv(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}},
|
}},
|
||||||
expectedError: `[0].valueFrom.fieldRef.fieldPath: Unsupported value: "metadata.labels": supported values: metadata.name, metadata.namespace, spec.nodeName, spec.serviceAccountName, status.podIP`,
|
expectedError: `[0].valueFrom.fieldRef.fieldPath: Unsupported value: "metadata.labels": supported values: metadata.name, metadata.namespace, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid fieldPath annotations",
|
name: "invalid fieldPath annotations",
|
||||||
@ -2341,7 +2359,7 @@ func TestValidateEnv(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}},
|
}},
|
||||||
expectedError: `[0].valueFrom.fieldRef.fieldPath: Unsupported value: "metadata.annotations": supported values: metadata.name, metadata.namespace, spec.nodeName, spec.serviceAccountName, status.podIP`,
|
expectedError: `[0].valueFrom.fieldRef.fieldPath: Unsupported value: "metadata.annotations": supported values: metadata.name, metadata.namespace, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "unsupported fieldPath",
|
name: "unsupported fieldPath",
|
||||||
@ -2354,7 +2372,7 @@ func TestValidateEnv(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}},
|
}},
|
||||||
expectedError: `valueFrom.fieldRef.fieldPath: Unsupported value: "status.phase": supported values: metadata.name, metadata.namespace, spec.nodeName, spec.serviceAccountName, status.podIP`,
|
expectedError: `valueFrom.fieldRef.fieldPath: Unsupported value: "status.phase": supported values: metadata.name, metadata.namespace, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tc := range errorCases {
|
for _, tc := range errorCases {
|
||||||
|
@ -41,11 +41,6 @@ func FormatMap(m map[string]string) (fmtStr string) {
|
|||||||
// ExtractFieldPathAsString extracts the field from the given object
|
// ExtractFieldPathAsString extracts the field from the given object
|
||||||
// and returns it as a string. The object must be a pointer to an
|
// and returns it as a string. The object must be a pointer to an
|
||||||
// API type.
|
// API type.
|
||||||
//
|
|
||||||
// Currently, this API is limited to supporting the fieldpaths:
|
|
||||||
//
|
|
||||||
// 1. metadata.name - The name of an API object
|
|
||||||
// 2. metadata.namespace - The namespace of an API object
|
|
||||||
func ExtractFieldPathAsString(obj interface{}, fieldPath string) (string, error) {
|
func ExtractFieldPathAsString(obj interface{}, fieldPath string) (string, error) {
|
||||||
accessor, err := meta.Accessor(obj)
|
accessor, err := meta.Accessor(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -617,6 +617,8 @@ func (kl *Kubelet) podFieldSelectorRuntimeValue(fs *v1.ObjectFieldSelector, pod
|
|||||||
return pod.Spec.NodeName, nil
|
return pod.Spec.NodeName, nil
|
||||||
case "spec.serviceAccountName":
|
case "spec.serviceAccountName":
|
||||||
return pod.Spec.ServiceAccountName, nil
|
return pod.Spec.ServiceAccountName, nil
|
||||||
|
case "status.hostIP":
|
||||||
|
return pod.Status.HostIP, nil
|
||||||
case "status.podIP":
|
case "status.podIP":
|
||||||
return podIP, nil
|
return podIP, nil
|
||||||
}
|
}
|
||||||
|
@ -73,11 +73,19 @@ var _ = framework.KubeDescribe("Downward API", func() {
|
|||||||
FieldPath: "status.podIP",
|
FieldPath: "status.podIP",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Name: "HOST_IP",
|
||||||
|
ValueFrom: &v1.EnvVarSource{
|
||||||
|
FieldRef: &v1.ObjectFieldSelector{
|
||||||
|
APIVersion: "v1",
|
||||||
|
FieldPath: "status.hostIP",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
expectations := []string{
|
expectations := []string{
|
||||||
"POD_IP=(?:\\d+)\\.(?:\\d+)\\.(?:\\d+)\\.(?:\\d+)",
|
"POD_IP=(?:\\d+)\\.(?:\\d+)\\.(?:\\d+)\\.(?:\\d+)",
|
||||||
|
"HOST_IP=(?:\\d+)\\.(?:\\d+)\\.(?:\\d+)\\.(?:\\d+)",
|
||||||
}
|
}
|
||||||
|
|
||||||
testDownwardAPI(f, podName, env, expectations)
|
testDownwardAPI(f, podName, env, expectations)
|
||||||
|
@ -20,4 +20,8 @@ spec:
|
|||||||
valueFrom:
|
valueFrom:
|
||||||
fieldRef:
|
fieldRef:
|
||||||
fieldPath: status.podIP
|
fieldPath: status.podIP
|
||||||
|
- name: MY_HOST_IP
|
||||||
|
valueFrom:
|
||||||
|
fieldRef:
|
||||||
|
fieldPath: status.hostIP
|
||||||
restartPolicy: Never
|
restartPolicy: Never
|
||||||
|
Loading…
Reference in New Issue
Block a user