Skip to content

VPA: match CronJob pods when jobTemplate pod labels are omitted#9489

Open
isumitsolanki wants to merge 2 commits intokubernetes:masterfrom
isumitsolanki:issue_9483
Open

VPA: match CronJob pods when jobTemplate pod labels are omitted#9489
isumitsolanki wants to merge 2 commits intokubernetes:masterfrom
isumitsolanki:issue_9483

Conversation

@isumitsolanki
Copy link
Copy Markdown

What type of PR is this?

/kind bug

What this PR does / why we need it:

When a CronJob omits spec.jobTemplate.spec.template.labels, the field is nil. Building the selector via metav1.SetAsLabelSelector(nil) leads to metav1.LabelSelectorAsSelector(nil), i.e. labels.Nothing(), so no pod matched the VPA despite correct targetRef and parent-controller association. This PR returns labels.Everything() when the job pod template labels are nil or empty, and adds unit tests in pkg/target.

Which issue(s) this PR fixes:

Fixes #9483

Special notes for your reviewer:

Aligns omitted labels with an explicit empty map labels: {} on the job pod template. If multiple VPAs in one namespace only differed by this path, recommender overlap was already possible with {}; this change makes nil behave the same.

Does this PR introduce a user-facing change?

Fixed Vertical Pod Autoscaler not applying recommendations to pods from CronJobs whose job pod template labels were omitted (nil). VPA now matches those pods the same as when an empty label map is set.

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

Signed-off-by: Sumit Solanki <sumit.solanki@ibm.com>
@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/bug Categorizes issue or PR as related to a bug. labels Apr 15, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

The full list of commands accepted by this bot can be found here.

Details 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
Copy link
Copy Markdown
Contributor

Welcome @isumitsolanki!

It looks like this is your first PR to kubernetes/autoscaler 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/autoscaler has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Apr 15, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

Hi @isumitsolanki. 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.

Details

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 area/vertical-pod-autoscaler size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed do-not-merge/needs-area labels Apr 15, 2026
Signed-off-by: Sumit Solanki <sumit.solanki@ibm.com>
@adrianmoisey
Copy link
Copy Markdown
Member

/ok-to-test

@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 Apr 15, 2026
func getLabelSelectorFromCronJob(apiObj *batchv1.CronJob) (labels.Selector, error) {
templateLabels := apiObj.Spec.JobTemplate.Spec.Template.Labels
if len(templateLabels) == 0 {
return labels.Everything(), nil
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure that labels.Everything() is the right solution here. I'm pretty sure this will match all pods in that namespace, which may cause performance issues.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree. in large clusters this can be very problematic.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I never thought in that direction thanks for highlighting this aspect @adrianmoisey @omerap12
what could be the right approach here ?can you please help here ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In the original issue where we discussed this, we concluded to document the behaviour, rather than fix it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yup. @adrianmoisey , will it be beneficial if we would print something when user apply VPA with targetRef pointing at CronJob/Job?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think we can. Returning a warning doesn't feel right for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/vertical-pod-autoscaler cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. 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. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VPA: CronJob without pod labels can not be optimized

4 participants