-
Notifications
You must be signed in to change notification settings - Fork 40.9k
fix pod template spec validation missing in sts #131790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This issue is currently awaiting triage. If a SIG or subproject determines this is a relevant issue, they will accept it by applying the The Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
Hi @chengjoey. Thanks for your PR. I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
/ok-to-test |
The failed tests seems to need fixing |
5989a2b
to
5287322
Compare
5287322
to
6a79033
Compare
/test pull-kubernetes-unit |
done |
// volume mounts in the containers. | ||
// allErrs = append(allErrs, apivalidation.ValidatePodTemplateSpec(template, fldPath)...) | ||
// TODO: Add validation for PodSpec containers volumeMounts if volumeClaimTemplates are defined | ||
allErrs = append(allErrs, apivalidation.ValidatePodTemplateSpec(template, fldPath, opts)...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tightening validation here can only be done conditionally... we have to tolerate updates to existing statefulset objects that wouldn't pass this validation
I think that looks like:
- adding an
SkipValidatePodTemplateSpec bool
option toStatefulSetValidationOptions
- plumbing that from ValidateStatefulSetSpec down into this function
- calling this line if and only if setOpts.SkipValidatePodTemplateSpec is false
- in ValidateStatefulSet (for create), set SkipValidatePodTemplateSpec to false. in ValidateStatefulSetUpdate (for update), set SkipValidatePodTemplateSpec to true if and only if oldStatefulSet fails ValidatePodTemplateSpec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
I got it, we should only validate the creation of sts, and tolerate the existing.
@@ -2928,6 +2928,9 @@ func ValidateVolumeMounts(mounts []core.VolumeMount, voldevices map[string]strin | |||
if len(mnt.Name) == 0 { | |||
allErrs = append(allErrs, field.Required(idxPath.Child("name"), "")) | |||
} | |||
if opts.AllowVolumeNotFoundInVolumeMounts && !IsMatchedVolume(mnt.Name, volumes) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a way to avoid skipping this validation by having statefulset validation modify a temporary copy of the pod template spec being validated in the same way the controller will to inject the volumes that will be constructed?
sort of like
kubernetes/pkg/controller/statefulset/stateful_set_utils.go
Lines 411 to 435 in ced19ff
// updateStorage updates pod's Volumes to conform with the PersistentVolumeClaim of set's templates. If pod has | |
// conflicting local Volumes these are replaced with Volumes that conform to the set's templates. | |
func updateStorage(set *apps.StatefulSet, pod *v1.Pod) { | |
currentVolumes := pod.Spec.Volumes | |
claims := getPersistentVolumeClaims(set, pod) | |
newVolumes := make([]v1.Volume, 0, len(claims)) | |
for name, claim := range claims { | |
newVolumes = append(newVolumes, v1.Volume{ | |
Name: name, | |
VolumeSource: v1.VolumeSource{ | |
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ | |
ClaimName: claim.Name, | |
// TODO: Use source definition to set this value when we have one. | |
ReadOnly: false, | |
}, | |
}, | |
}) | |
} | |
for i := range currentVolumes { | |
if _, ok := claims[currentVolumes[i].Name]; !ok { | |
newVolumes = append(newVolumes, currentVolumes[i]) | |
} | |
} | |
pod.Spec.Volumes = newVolumes | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
6a79033
to
d230082
Compare
/test pull-kubernetes-unit |
1 similar comment
/test pull-kubernetes-unit |
// Skip validating pod template spec, specifically for StatefulSet. | ||
SkipValidatePodTemplateSpec bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this into the StatefulSetValidationOptions type, and plumb that options instance into ValidatePodTemplateSpecForStatefulSet where it is needed
spec.Template.Spec.Volumes = newVolumes | ||
} | ||
|
||
func getPersistentVolumeClaims(spec *apps.StatefulSetSpec) map[string]api.PersistentVolumeClaim { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we just need to return []string
with the claim names to add the volumes to the pod spec like we need to
@@ -193,6 +238,9 @@ func ValidateStatefulSetUpdate(statefulSet, oldStatefulSet *apps.StatefulSet, op | |||
setOpts := StatefulSetValidationOptions{ | |||
AllowInvalidServiceName: true, // serviceName is immutable, tolerate existing invalid names on update | |||
} | |||
// In order to tolerate the existing sts, we choose to skip the validation error of podTemplateSpec. | |||
// TODO: pod template spec validation for statefulset update | |||
opts.SkipValidatePodTemplateSpec = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should look more like if len(ValidateStatefulSetSpec(&oldStatefulSet.Spec, nil, opts, setOpts)) > 0 { opts.SkipValidatePodTemplateSpec = true }
so we only relax validation on update for things which already fail spec validation
@@ -158,6 +201,8 @@ func ValidateStatefulSetSpec(spec *apps.StatefulSetSpec, fldPath *field.Path, op | |||
if err != nil { | |||
allErrs = append(allErrs, field.Invalid(fldPath.Child("selector"), spec.Selector, "")) | |||
} else { | |||
// update the template's volumes to include the volumes from the volumeClaimTemplates | |||
updateStorageForStatefulSet(spec) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't mutate this in place ... if we need to add in volumes for validation purposes to match what will get auto-added by the controller, do that by assigning to a new variable, deep-copying, and appending the fewest new volumes we need, something like this:
templateToValidate := &spec.Template
if volumesToAddForTemplates(spec); len(volumesToAddForTemplates) > 0 {
templateToValidate = templateToValidate.DeepCopy()
templateToValidate.Spec.Volumes = append(templateToValidate.Spec.Volumes, ...)
}
allErrs = append(allErrs, ValidatePodTemplateSpecForStatefulSet(templateToValidate, selector, fldPath.Child("template"), opts)...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, and add some test cases for update
d230082
to
bc33879
Compare
podSpecErrs := apivalidation.ValidatePodTemplateSpec(template, fldPath, opts) | ||
if len(podSpecErrs) > 0 && !setOpts.SkipValidatePodTemplateSpec { | ||
allErrs = append(allErrs, podSpecErrs...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check the boolean first to avoid doing work we'll throw away
podSpecErrs := apivalidation.ValidatePodTemplateSpec(template, fldPath, opts) | |
if len(podSpecErrs) > 0 && !setOpts.SkipValidatePodTemplateSpec { | |
allErrs = append(allErrs, podSpecErrs...) | |
if !setOpts.SkipValidatePodTemplateSpec { | |
allErrs = append(allErrs, apivalidation.ValidatePodTemplateSpec(template, fldPath, opts)...) |
bc33879
to
eeecf20
Compare
Signed-off-by: joey <[email protected]>
eeecf20
to
dfd34a5
Compare
/retest |
/lgtm Thanks for the improvement here and the tests! |
LGTM label has been added. Git tree hash: ce713cfd8cb38a474496c4e9c6dcd3052cd5b447
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: chengjoey, liggitt The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
What type of PR is this?
/kind bug
What this PR does / why we need it:
add pod spec validation for create sts
Which issue(s) this PR fixes:
Fixes #131761 #112609
Special notes for your reviewer:
Does this PR introduce a user-facing change?