Skip to content

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

Merged
merged 1 commit into from
Jul 1, 2025

Conversation

chengjoey
Copy link
Contributor

@chengjoey chengjoey commented May 15, 2025

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?

add podSpec validation for create StatefulSet

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. kind/bug Categorizes issue or PR as related to a bug. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels May 15, 2025
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

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.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels May 15, 2025
@k8s-ci-robot
Copy link
Contributor

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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

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.

@k8s-ci-robot k8s-ci-robot added sig/apps Categorizes an issue or PR as relevant to SIG Apps. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels May 15, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in SIG Apps May 15, 2025
@k8s-ci-robot k8s-ci-robot requested review from deads2k and tallclair May 15, 2025 11:08
@pacoxu
Copy link
Member

pacoxu commented May 16, 2025

/ok-to-test
/cc @adrianmoisey

@k8s-ci-robot k8s-ci-robot requested a review from adrianmoisey May 16, 2025 09:24
@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels May 16, 2025
@adrianmoisey
Copy link
Member

The failed tests seems to need fixing

@chengjoey chengjoey force-pushed the fix/sts-validation branch from 5989a2b to 5287322 Compare May 16, 2025 15:47
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. area/test sig/etcd Categorizes an issue or PR as relevant to SIG Etcd. sig/testing Categorizes an issue or PR as relevant to SIG Testing. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels May 16, 2025
@chengjoey chengjoey force-pushed the fix/sts-validation branch from 5287322 to 6a79033 Compare May 17, 2025 00:01
@chengjoey
Copy link
Contributor Author

/test pull-kubernetes-unit

@chengjoey
Copy link
Contributor Author

The failed tests seems to need fixing

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)...)
Copy link
Member

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:

  1. adding an SkipValidatePodTemplateSpec bool option to StatefulSetValidationOptions
  2. plumbing that from ValidateStatefulSetSpec down into this function
  3. calling this line if and only if setOpts.SkipValidatePodTemplateSpec is false
  4. in ValidateStatefulSet (for create), set SkipValidatePodTemplateSpec to false. in ValidateStatefulSetUpdate (for update), set SkipValidatePodTemplateSpec to true if and only if oldStatefulSet fails ValidatePodTemplateSpec

Copy link
Contributor Author

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) {
Copy link
Member

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

// 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
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@chengjoey chengjoey force-pushed the fix/sts-validation branch from 6a79033 to d230082 Compare June 19, 2025 13:58
@chengjoey
Copy link
Contributor Author

/test pull-kubernetes-unit

1 similar comment
@chengjoey
Copy link
Contributor Author

/test pull-kubernetes-unit

Comment on lines 4124 to 4125
// Skip validating pod template spec, specifically for StatefulSet.
SkipValidatePodTemplateSpec bool
Copy link
Member

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 {
Copy link
Member

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
Copy link
Member

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)
Copy link
Member

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)...)

Copy link
Contributor Author

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

@chengjoey chengjoey force-pushed the fix/sts-validation branch from d230082 to bc33879 Compare June 20, 2025 07:22
Comment on lines 69 to 71
podSpecErrs := apivalidation.ValidatePodTemplateSpec(template, fldPath, opts)
if len(podSpecErrs) > 0 && !setOpts.SkipValidatePodTemplateSpec {
allErrs = append(allErrs, podSpecErrs...)
Copy link
Member

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

Suggested change
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)...)

@chengjoey chengjoey force-pushed the fix/sts-validation branch from bc33879 to eeecf20 Compare June 25, 2025 02:42
@chengjoey chengjoey force-pushed the fix/sts-validation branch from eeecf20 to dfd34a5 Compare June 27, 2025 08:31
@liggitt
Copy link
Member

liggitt commented Jul 1, 2025

/retest

@liggitt
Copy link
Member

liggitt commented Jul 1, 2025

/lgtm
/approve

Thanks for the improvement here and the tests!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 1, 2025
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: ce713cfd8cb38a474496c4e9c6dcd3052cd5b447

@k8s-ci-robot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 1, 2025
@k8s-ci-robot k8s-ci-robot merged commit a9841ca into kubernetes:master Jul 1, 2025
13 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.34 milestone Jul 1, 2025
@github-project-automation github-project-automation bot moved this to Done in @liggitt Jul 1, 2025
@github-project-automation github-project-automation bot moved this from Needs Triage to Done in SIG Apps Jul 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/test cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/apps Categorizes an issue or PR as relevant to SIG Apps. sig/etcd Categorizes an issue or PR as relevant to SIG Etcd. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
Status: Done
Status: Done
Development

Successfully merging this pull request may close these issues.

topologySpreadConstraints are not correctly applied on StatefulSets
5 participants