mirror of https://github.com/k3s-io/k3s
Merge pull request #48125 from xiangpengzhao/downwardapi-poduid
Automatic merge from submit-queue (batch tested with PRs 48264, 48324, 48125, 47944, 47489) Add Pod UID (metadata.uid) to downward API env var **What this PR does / why we need it**: Exposing Pod UID by downward API. **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #28918 **Special notes for your reviewer**: Generated files aren't committed. I'd like CI to tell me what scripts should I run to generate these files. /cc @smarterclayton @vishh @dubstack **Release note**: ```release-note NONE ```pull/6/head
commit
faf4e57f1b
|
@ -1008,7 +1008,7 @@ type DownwardAPIVolumeSource struct {
|
||||||
type DownwardAPIVolumeFile struct {
|
type DownwardAPIVolumeFile struct {
|
||||||
// Required: Path is the relative path name of the file to be created. Must not be absolute or contain the '..' path. Must be utf-8 encoded. The first item of the relative path must not start with '..'
|
// Required: Path is the relative path name of the file to be created. Must not be absolute or contain the '..' path. Must be utf-8 encoded. The first item of the relative path must not start with '..'
|
||||||
Path string
|
Path string
|
||||||
// Required: Selects a field of the pod: only annotations, labels, name and namespace are supported.
|
// Required: Selects a field of the pod: only annotations, labels, name, namespace and uid are supported.
|
||||||
// +optional
|
// +optional
|
||||||
FieldRef *ObjectFieldSelector
|
FieldRef *ObjectFieldSelector
|
||||||
// Selects a resource of the container: only resources limits and requests
|
// Selects a resource of the container: only resources limits and requests
|
||||||
|
@ -1373,7 +1373,7 @@ type EnvVar struct {
|
||||||
// Only one of its fields may be set.
|
// Only one of its fields may be set.
|
||||||
type EnvVarSource struct {
|
type EnvVarSource struct {
|
||||||
// Selects a field of the pod: supports metadata.name, metadata.namespace, metadata.labels, metadata.annotations,
|
// Selects a field of the pod: supports metadata.name, metadata.namespace, metadata.labels, metadata.annotations,
|
||||||
// spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP.
|
// metadata.uid, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP.
|
||||||
// +optional
|
// +optional
|
||||||
FieldRef *ObjectFieldSelector
|
FieldRef *ObjectFieldSelector
|
||||||
// Selects a resource of the container: only resources limits and requests
|
// Selects a resource of the container: only resources limits and requests
|
||||||
|
|
|
@ -188,6 +188,7 @@ func addConversionFuncs(scheme *runtime.Scheme) error {
|
||||||
"metadata.labels",
|
"metadata.labels",
|
||||||
"metadata.name",
|
"metadata.name",
|
||||||
"metadata.namespace",
|
"metadata.namespace",
|
||||||
|
"metadata.uid",
|
||||||
"spec.nodeName",
|
"spec.nodeName",
|
||||||
"spec.restartPolicy",
|
"spec.restartPolicy",
|
||||||
"spec.serviceAccountName",
|
"spec.serviceAccountName",
|
||||||
|
|
|
@ -808,7 +808,8 @@ var validDownwardAPIFieldPathExpressions = sets.NewString(
|
||||||
"metadata.name",
|
"metadata.name",
|
||||||
"metadata.namespace",
|
"metadata.namespace",
|
||||||
"metadata.labels",
|
"metadata.labels",
|
||||||
"metadata.annotations")
|
"metadata.annotations",
|
||||||
|
"metadata.uid")
|
||||||
|
|
||||||
func validateDownwardAPIVolumeFile(file *api.DownwardAPIVolumeFile, fldPath *field.Path) field.ErrorList {
|
func validateDownwardAPIVolumeFile(file *api.DownwardAPIVolumeFile, fldPath *field.Path) field.ErrorList {
|
||||||
allErrs := field.ErrorList{}
|
allErrs := field.ErrorList{}
|
||||||
|
@ -1556,7 +1557,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.hostIP", "status.podIP")
|
var validFieldPathExpressionsEnv = sets.NewString("metadata.name", "metadata.namespace", "metadata.uid", "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 {
|
||||||
|
|
|
@ -2584,6 +2584,24 @@ func TestValidateEnv(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "abc",
|
||||||
|
ValueFrom: &api.EnvVarSource{
|
||||||
|
FieldRef: &api.ObjectFieldSelector{
|
||||||
|
APIVersion: api.Registry.GroupOrDie(api.GroupName).GroupVersion.String(),
|
||||||
|
FieldPath: "metadata.namespace",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "abc",
|
||||||
|
ValueFrom: &api.EnvVarSource{
|
||||||
|
FieldRef: &api.ObjectFieldSelector{
|
||||||
|
APIVersion: api.Registry.GroupOrDie(api.GroupName).GroupVersion.String(),
|
||||||
|
FieldPath: "metadata.uid",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Name: "abc",
|
Name: "abc",
|
||||||
ValueFrom: &api.EnvVarSource{
|
ValueFrom: &api.EnvVarSource{
|
||||||
|
@ -2644,7 +2662,7 @@ func TestValidateEnv(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if errs := ValidateEnv(successCase, field.NewPath("field")); len(errs) != 0 {
|
if errs := ValidateEnv(successCase, field.NewPath("field")); len(errs) != 0 {
|
||||||
t.Errorf("expected success: %v", errs)
|
t.Errorf("expected success, got: %v", errs)
|
||||||
}
|
}
|
||||||
|
|
||||||
errorCases := []struct {
|
errorCases := []struct {
|
||||||
|
@ -2823,7 +2841,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.hostIP, status.podIP`,
|
expectedError: `[0].valueFrom.fieldRef.fieldPath: Unsupported value: "metadata.labels": supported values: metadata.name, metadata.namespace, metadata.uid, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid fieldPath annotations",
|
name: "invalid fieldPath annotations",
|
||||||
|
@ -2836,7 +2854,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.hostIP, status.podIP`,
|
expectedError: `[0].valueFrom.fieldRef.fieldPath: Unsupported value: "metadata.annotations": supported values: metadata.name, metadata.namespace, metadata.uid, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "unsupported fieldPath",
|
name: "unsupported fieldPath",
|
||||||
|
@ -2849,7 +2867,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.hostIP, status.podIP`,
|
expectedError: `valueFrom.fieldRef.fieldPath: Unsupported value: "status.phase": supported values: metadata.name, metadata.namespace, metadata.uid, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tc := range errorCases {
|
for _, tc := range errorCases {
|
||||||
|
|
|
@ -51,6 +51,8 @@ func ExtractFieldPathAsString(obj interface{}, fieldPath string) (string, error)
|
||||||
return accessor.GetName(), nil
|
return accessor.GetName(), nil
|
||||||
case "metadata.namespace":
|
case "metadata.namespace":
|
||||||
return accessor.GetNamespace(), nil
|
return accessor.GetNamespace(), nil
|
||||||
|
case "metadata.uid":
|
||||||
|
return string(accessor.GetUID()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", fmt.Errorf("unsupported fieldPath: %v", fieldPath)
|
return "", fmt.Errorf("unsupported fieldPath: %v", fieldPath)
|
||||||
|
|
Loading…
Reference in New Issue