Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add MatchLabelKeys validation for PodTemplateSpec #130534

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
Loading
from

Conversation

mochizuki875
Copy link
Member

What type of PR is this?

/kind feature
/sig scheduling

What this PR does / why we need it:

As #130063, when an attempt is made to create an object(e.g. Deployment) containing a PodTemplateSpec with a specific key that exists in both PodAffinity's matchLabelKeys and labelSelector, the PodTemplateSpec validation will pass.
However, the Pods are not created due to the PodSpec validation.
I think it makes users confused, and the PodTemplateSpec validation should fail in this case.

In this PR, I've added MatchLabelKeys validation for PodTemplateSpec to validate such cases.

Which issue(s) this PR fixes:

Fixes #130063

Special notes for your reviewer:

I've added test cases for this PR to TestValidatePodTemplateSpecSeccomp(), which invokes ValidatePodTemplateSpec() for each test case containing PodTemplateSpec.
While the name of TestValidatePodTemplateSpecSeccomp() contains "seccomp", the invoked method ValidatePodTemplateSpec() used for more than just seccomp validation and should have a more general name.
Therefore I've submitted another PR.(#130155)

Does this PR introduce a user-facing change?

When an attempt is made to create an object containing a `PodTemplateSpec`(e.g. `Deployment`, `StatefulSet` etc), the validation will fail if a specific key exists in both `PodAffinity`'s `matchLabelKeys` and `labelSelector` or duplicate keys exist in `matchLabelKeys`.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@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/XL Denotes a PR that changes 500-999 lines, ignoring generated files. kind/feature Categorizes issue or PR as related to a new feature. sig/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Mar 3, 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 the needs-priority Indicates a PR lacks a `priority/foo` label and requires one. label Mar 3, 2025
@mochizuki875 mochizuki875 changed the title add MatchLabelKeys validation for PodTemplateSpec Add MatchLabelKeys validation for PodTemplateSpec Mar 3, 2025
@k8s-ci-robot k8s-ci-robot added the sig/apps Categorizes an issue or PR as relevant to SIG Apps. label Mar 3, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in SIG Apps Mar 3, 2025
@k8s-ci-robot k8s-ci-robot requested review from thockin and yujuhong March 3, 2025 07:45
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mochizuki875
Once this PR has been reviewed and has the lgtm label, please assign thockin for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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

@mochizuki875 mochizuki875 force-pushed the validate_podtemplate_matchlabelkeys branch from 889738e to ac8226b Compare March 3, 2025 07:49
@mochizuki875
Copy link
Member Author

/retest

1 similar comment
@mochizuki875
Copy link
Member Author

/retest

Copy link
Member

@haosdent haosdent left a comment

Choose a reason for hiding this comment

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

Maybe we could add some valid cases to cover the following scenarios:

  • A test where no conflicts exist.
  • A test with an empty label selector (or even a nil label selector, if allowed) along with non-empty matchLabelKeys to verify that no error is raised when there’s nothing to conflict with.
  • Include a test case where mismatchLabelKeys and matchLabelKeys are both provided but do not overlap.

// Here, we found the duplicate key in labelSelector and the key is specified in labelKeys.
// Meaning that the same key is specified in both labelSelector and matchLabelKeys/mismatchLabelKeys.
allErrs = append(allErrs, field.Invalid(fldPath.Index(i), key, "exists in both matchLabelKeys and labelSelector"))
if strings.Contains(fldPath.String(), "template.spec") { // PodTemplateSpec validation
Copy link
Member

Choose a reason for hiding this comment

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

Do we have a robust way to detect PodTemplateSpec, not sure if this check may match unexpected cases in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've reconsidered and changed to use opts.ResourceIsPod.

expectedErr: field.ErrorList{
field.Invalid(rootFld.Child("spec").Child("affinity").Child("podAffinity").Child("preferredDuringSchedulingIgnoredDuringExecution").Index(0).Child("podAffinityTerm").Child("matchLabelKeys").Index(0), "/key", "prefix part must be non-empty"),
},
spec: &core.PodTemplateSpec{
Copy link
Member

Choose a reason for hiding this comment

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

Do we have test cases to cover core.PodSpec{ scenario

		} else { // PodSpec validation
			// labelKeysMap is keyed by label key and valued by the index of label key in labelKeys.
			labelKeysMap := map[string]int{}
			for i, key := range matchLabelKeys {

Copy link
Member Author

Choose a reason for hiding this comment

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

They have already been added as part of the existing test cases in TestValidatePod().

e.g.

"invalid soft pod affinity, key exists in both matchLabelKeys and labelSelector": {
expectedError: "exists in both matchLabelKeys and labelSelector",
spec: *podtest.MakePod("123",
podtest.SetAffinity(&core.Affinity{
PodAffinity: &core.PodAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []core.WeightedPodAffinityTerm{
{
Weight: 10,
PodAffinityTerm: core.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
// This one should be created from MatchLabelKeys.
{
Key: "key",
Operator: metav1.LabelSelectorOpIn,
Values: []string{"value1"},
},
{
Key: "key",
Operator: metav1.LabelSelectorOpNotIn,
Values: []string{"value2"},
},
},
},
TopologyKey: "k8s.io/zone",
MatchLabelKeys: []string{"key"},
},
},
},
},
}),
),
},

),
},
}, {
description: "invalid hard pod anti-affinity, key exists in both MatchLabelKeys and MismatchLabelKeys",
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to test positive case, where mismatchLabelKeys and matchLabelKeys are both provided but do not overlap.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added positive test cases as successCases.

{
LabelSelector: &metav1.LabelSelector{},
TopologyKey: "k8s.io/zone",
MatchLabelKeys: []string{"key1", "key2", "key1"},
Copy link
Member

Choose a reason for hiding this comment

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

Do we have test cases that only a subset of the provided keys is problematic (for example, one key is duplicated or conflicts while others are valid) between MatchLabelKeys/LabelSelector/MismatchLabelKeys

Copy link
Member Author

Choose a reason for hiding this comment

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

I've fixed the additional test cases.
e.g.

podtest.SetAffinity(&core.Affinity{
PodAffinity: &core.PodAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []core.WeightedPodAffinityTerm{
{
PodAffinityTerm: core.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "key1",
Operator: metav1.LabelSelectorOpIn,
Values: []string{"value1"},
},
{
Key: "key2",
Operator: metav1.LabelSelectorOpIn,
Values: []string{"value2"},
},
},
},
TopologyKey: "k8s.io/zone",
MatchLabelKeys: []string{"key1", "key3"},
},

@mochizuki875 mochizuki875 force-pushed the validate_podtemplate_matchlabelkeys branch from ac8226b to 0ab306a Compare March 4, 2025 04:54
@mochizuki875
Copy link
Member Author

mochizuki875 commented Mar 4, 2025

The origin information "labelKey" has been added to the error of ValidateLabelName() by #130388, and expectedErr of related test cases are required to be added .WithOrigin("labelKey") to pass the unit test.
Also, based on #130388, we might need to fix to replace error comparisons using assert.Equal() with error comparisons using fldtest.MatchErrors() like other test functions(e.g. TestValidateTopologySpreadConstraints()).

@mochizuki875
Copy link
Member Author

@haosdent
Thank you for your comments!
I'll address them later.

@mochizuki875
Copy link
Member Author

@haosdent
OK, I've addressed.

@k8s-ci-robot
Copy link
Contributor

PR needs rebase.

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 the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. 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/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
Status: Needs Triage
Development

Successfully merging this pull request may close these issues.

Validate duplicate keys between matchLabelKeys and labelSelector in PodTemplateSpec
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.