Skip to content

🐛 fake client support scale subresource get/update for unstructured objects - #3546

Merged
kubernetes-prow[bot] merged 4 commits into
kubernetes-sigs:mainfrom
PullaguraSantosha:fix/fake-client-unstructured-scale
Jul 29, 2026
Merged

🐛 fake client support scale subresource get/update for unstructured objects#3546
kubernetes-prow[bot] merged 4 commits into
kubernetes-sigs:mainfrom
PullaguraSantosha:fix/fake-client-unstructured-scale

Conversation

@PullaguraSantosha

Copy link
Copy Markdown
Contributor

The fake client currently supports the scale subresource for several built-in workload types such as Deployment and StatefulSet, but it does not provide equivalent behavior for unstructured objects.

As a result, tests and controllers using the fake client cannot correctly exercise scale subresource operations on custom or unstructured resources that expose replica information via spec.replicas and status.replicas.

This change extends the fake client's scale subresource handling to support unstructured objects by reading and updating replica information from spec.replicas and status.replicas, bringing its behavior closer to that of the Kubernetes API server.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 9, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

@kubernetes-prow
kubernetes-prow Bot requested review from JoelSpeed and inteon July 9, 2026 17:01
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @PullaguraSantosha!

It looks like this is your first PR to kubernetes-sigs/controller-runtime 🎉. 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-sigs/controller-runtime 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. 😃

@kubernetes-prow kubernetes-prow Bot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Jul 9, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Hi @PullaguraSantosha. Thanks for your PR.

I'm waiting for a kubernetes-sigs 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.

@kubernetes-prow kubernetes-prow Bot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 9, 2026
@PullaguraSantosha
PullaguraSantosha force-pushed the fix/fake-client-unstructured-scale branch 2 times, most recently from 1d056cc to cfab02f Compare July 9, 2026 17:03
@PullaguraSantosha PullaguraSantosha changed the title fakeclient: support scale subresource Get/Update for unstructured objects 🐛 Patch fix: fake client support scale subresource get/update for unstructured objects Jul 9, 2026
@PullaguraSantosha
PullaguraSantosha force-pushed the fix/fake-client-unstructured-scale branch from e3ce8de to 9b1a068 Compare July 9, 2026 17:25
@kubernetes-prow kubernetes-prow Bot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Jul 9, 2026
@PullaguraSantosha

Copy link
Copy Markdown
Contributor Author

Hi @JoelSpeed @inteon — CLA and title are fixed and I updated the branch. Could a maintainer please approve the workflow so CI can run? If it looks ok, please review and add lgtm. Thanks!

Comment thread pkg/client/fake/client.go Outdated
func extractScale(obj client.Object) (*autoscalingv1.Scale, error) {
switch obj := obj.(type) {
case *unstructured.Unstructured:
replicas, found, err := unstructured.NestedInt64(obj.Object, "spec", "replicas")

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 this is necessarily correct, for crds its configurable where this is stored. If we do this, we should limit it to API types where we know the specReplicasPath and statusReplicasPath

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point — this change is intentionally limited to the conventional unstructured case where spec.replicas and status.replicas are used. I do not mean to claim generic CRD scale support for arbitrary custom specReplicasPath / statusReplicasPath values. If you’d like, I can narrow this further to only known API types or add a TODO noting that fully generic CRD scale support requires CRD metadata.

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.

this change is intentionally limited to the conventional unstructured case where spec.replicas and status.replicas are used

How, because it will error out otherwise? That seems more like an accident than deliberation. Please limit this to the same types where we also support this with the proper api types

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated fake client scale handling for unstructured objects so it only supports known built-in scale resource kinds:

  • apps/v1 Deployment
  • apps/v1 ReplicaSet
  • apps/v1 StatefulSet
  • v1 ReplicationController

Added regression coverage for apps/v1 Deployment and preserved the existing unimplemented scale subresource behavior for unsupported unstructured types.

@kubernetes-prow kubernetes-prow Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 10, 2026
@sbueringer

Copy link
Copy Markdown
Member

/ok-to-test

@kubernetes-prow kubernetes-prow Bot 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 Jul 10, 2026
@sbueringer sbueringer added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Jul 10, 2026
Comment thread pkg/client/fake/client.go Outdated
switch obj := obj.(type) {
case *unstructured.Unstructured:
if !isUnstructuredScaleResource(obj) {
return nil, fmt.Errorf("unimplemented scale subresource for resource %T", obj)

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.

Suggested change
return nil, fmt.Errorf("unimplemented scale subresource for resource %T", obj)
return nil, fmt.Errorf("scale subresource for resource %T is not implemented", obj)

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.

This one does not seem to be addressed

Comment thread pkg/client/fake/client.go Outdated
switch obj := obj.(type) {
case *unstructured.Unstructured:
if !isUnstructuredScaleResource(obj) {
return fmt.Errorf("unimplemented scale subresource for resource %T", obj)

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.

Suggested change
return fmt.Errorf("unimplemented scale subresource for resource %T", obj)
return fmt.Errorf("scale subresource for resource %T is not implemented", obj)

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.

Same here

Comment thread pkg/client/fake/client_test.go Outdated
}
})

It("allows explicitly clearing managedFields on Update", func(ctx SpecContext) {

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.

What does this have to do with the rest of the PR?

@PullaguraSantosha PullaguraSantosha Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review and suggestions, @sbueringer.
I updated the error text to: scale subresource for resource %T is not implemented and adjusted the test coverage to use apps/v1 Deployment to match the fake client's restricted unstructured support. I also reverted the unrelated test changes and force-pushed the cleaned commit. Please let me know if you'd prefer different wording or any additional changes.

@PullaguraSantosha
PullaguraSantosha force-pushed the fix/fake-client-unstructured-scale branch 2 times, most recently from 97f87fe to 80a98e9 Compare July 10, 2026 12:06
@kubernetes-prow kubernetes-prow Bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 10, 2026
@PullaguraSantosha
PullaguraSantosha force-pushed the fix/fake-client-unstructured-scale branch from 80a98e9 to b2a90bc Compare July 10, 2026 12:08
@alvaroaleman alvaroaleman changed the title 🐛 Patch fix: fake client support scale subresource get/update for unstructured objects 🐛 fake client support scale subresource get/update for unstructured objects Jul 12, 2026
Comment thread pkg/client/fake/client.go Outdated
if !isUnstructuredScaleResource(obj) {
return nil, fmt.Errorf("scale subresource for resource %T is not implemented", obj)
}
replicas, found, err := unstructured.NestedInt64(obj.Object, "spec", "replicas")

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.

The type is int32 not 64

Comment thread pkg/client/fake/client.go Outdated
return nil, err
}
if !found {
replicas = 0

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.

This gets defaulted to 1, not 0

Comment thread pkg/client/fake/client.go Outdated
return nil, err
}
if !statusFound {
statusReplicas = replicas

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 this is correct, it should be 0 just like if the field was unset in the typed object

Comment thread pkg/client/fake/client.go Outdated
default:
// TODO: CRDs https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#scale-subresource
return fmt.Errorf("unimplemented scale subresource for resource %T", obj)
return fmt.Errorf("scale subresource for resource %T is not implemented", obj)

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.

Why randomly reword this?

@kubernetes-prow kubernetes-prow Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 12, 2026
@alvaroaleman

Copy link
Copy Markdown
Member

@Dasmat13 please refrain from copy pasting LLM output, we can do that ourselves. What you are doing is considered to be spam and you are risking getting banned from the Github org.

@Dasmat13

Copy link
Copy Markdown

Sorry about that my bad. I was experimenting with an AI assistant to draft reviews.
Won't happen again

Comment thread pkg/client/fake/client_test.go Outdated
})

It("structured and unstructured scale subresources behave consistently", func(ctx SpecContext) {
fuzzer := randfill.New().Funcs(

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.

Lets make failures reproducible:

Suggested change
fuzzer := randfill.New().Funcs(
seed := time.Now().UnixMicro()
GinkgoWriter.Printf("seed: %d", seed)
fuzzer := randfill.NewWithSeed(seed).Funcs(

Expect(int32(replicas)).To(Equal(int32(3)))
})

It("structured and unstructured scale subresources behave consistently", func(ctx SpecContext) {

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.

This test looks good, but you need to run the fuzzing in a loop, a single iteration is not very likely to catch issues. Something simple like for range 100 { existing test body } is fine

Comment thread go.mod Outdated
sigs.k8s.io/yaml v1.6.0
)

require sigs.k8s.io/randfill v1.0.0

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.

Why not keep this in the existing block above this?

@kubernetes-prow kubernetes-prow Bot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. and removed cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Jul 14, 2026
@PullaguraSantosha
PullaguraSantosha force-pushed the fix/fake-client-unstructured-scale branch from ab4f4f0 to cf6880c Compare July 14, 2026 13:40
@kubernetes-prow kubernetes-prow Bot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Jul 14, 2026
@PullaguraSantosha

Copy link
Copy Markdown
Contributor Author

Hi @alvaroaleman — review comments are addressed and CI is green. Could you take another look when you have a chance? Thanks!

@sbueringer

Copy link
Copy Markdown
Member

@PullaguraSantosha Please rebase onto latest main, squash and get rid of the merge commit

@PullaguraSantosha
PullaguraSantosha force-pushed the fix/fake-client-unstructured-scale branch from cf6880c to ee613fa Compare July 21, 2026 12:40
Comment thread pkg/client/fake/client.go Outdated
switch obj := obj.(type) {
case *unstructured.Unstructured:
if !isUnstructuredScaleResource(obj) {
return nil, fmt.Errorf("unimplemented scale subresource for resource %T", obj)

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.

This one does not seem to be addressed

Comment thread pkg/client/fake/client.go
Comment thread pkg/client/fake/client.go Outdated
switch obj := obj.(type) {
case *unstructured.Unstructured:
if !isUnstructuredScaleResource(obj) {
return fmt.Errorf("unimplemented scale subresource for resource %T", obj)

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.

Same here

Comment thread pkg/client/fake/client.go
Comment thread pkg/client/fake/client.go Outdated
@sbueringer

sbueringer commented Jul 29, 2026

Copy link
Copy Markdown
Member

Thx!

/lgtm

@alvaroaleman Do you want to take another look?

@kubernetes-prow kubernetes-prow Bot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 29, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

LGTM label has been added.

DetailsGit tree hash: 219adcef7ac6a7e8782294b9ab67e46f2d156bff

@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alvaroaleman, PullaguraSantosha

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

The pull request process is described 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

@kubernetes-prow kubernetes-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 29, 2026
@kubernetes-prow
kubernetes-prow Bot merged commit 6ec9507 into kubernetes-sigs:main Jul 29, 2026
10 checks passed
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. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants