Skip to content

Managed fields are not updated when patching or updating the /scale subresource for custom and built-in resources #131892

Open
@MohammadAlavi1986

Description

@MohammadAlavi1986

What happened?

When updating or patching the /scale subresource of a custom or built-in resource, the managed field for spec.replicas is not updated as expected if the object was originally created without specifying a value for the spec.replicas field.

What did you expect to happen?

When updating or patching the /scale subresource, the managed field for specReplicasPath should be updated to reflect the subresource operation and the field manager used.

How can we reproduce it (as minimally and precisely as possible)?

  1. Create a CRD with a scale subresource:
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: mypods.example.com
spec:
  group: example.com
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                name:
                  type: string
                replicas:
                  type: integer
            status:
              type: object
              properties:
                replicas:
                  type: integer
      subresources:
        status: {}
        scale:
          specReplicasPath: .spec.replicas
          statusReplicasPath: .status.replicas
  scope: Namespaced
  names:
    plural: mypods
    singular: mypod
    kind: MyPod
    shortNames:
      - mpo
  1. Create a custom resource:
kubectl apply --server-side --field-manager m1 -f - <<'EOF'
apiVersion: example.com/v1
kind: MyPod
metadata:
  name: test
spec:
  name: mypod
EOF
  1. Update or patch replicas using the /scale subresource:

    a. Update the /scale subresource:

    kubectl scale mypods.example.com test --replicas 2
    # Or via direct API call:
    curl -XPUT -H 'content-type: application/json' \
      'http://localhost:8001/apis/example.com/v1/namespaces/default/mypods/test/scale?fieldManager=m2' \
      -d '{"apiVersion": "autoscaling/v1", "kind": "Scale", "metadata": {"name": "test"}, "spec": {"replicas": 2}}'

    b. Patch the /scale subresource:

    kubectl patch mypods.example.com test --subresource scale --type merge --field-manager m2 -p '{"spec": {"replicas": 2}}'
    # Or via direct API call:
    curl -XPATCH -H 'content-type: application/merge-patch+json' \
      'http://localhost:8001/apis/example.com/v1/namespaces/default/mypods/test/scale?fieldManager=m2' \
      -d '{"spec": {"replicas": 2}}'
  2. Observe that spec.replicas is not in the managed fields:

    kubectl get mypods.example.com test --show-managed-fields -o yaml

    Output:

    apiVersion: example.com/v1
    kind: MyPod
    metadata:
      creationTimestamp: "2025-05-22T00:46:21Z"
      generation: 2
      managedFields:
      - apiVersion: example.com/v1
        fieldsType: FieldsV1
        fieldsV1:
          f:spec:
            f:name: {}
        manager: m1
        operation: Apply
        time: "2025-05-22T00:46:21Z"
      name: test
      namespace: default
      resourceVersion: "49104"
      uid: 87e87c4d-8557-4aaf-a9e4-15794ea8ad5d
    spec:
      name: mypod
      replicas: 2

For built-in types please see this comment.

Anything else we need to know?

No response

Kubernetes version

$ kubectl version
Client Version: v1.32.2
Kustomize Version: v5.5.0
Server Version: v1.32.4

Cloud provider

OS version

# On Linux:
$ cat /etc/os-release
# paste output here
$ uname -a
# paste output here

# On Windows:
C:\> wmic os get Caption, Version, BuildNumber, OSArchitecture
# paste output here

Install tools

Container runtime (CRI) and version (if applicable)

Related plugins (CNI, CSI, ...) and versions (if applicable)

Metadata

Metadata

Assignees

Labels

kind/bugCategorizes issue or PR as related to a bug.needs-kindIndicates a PR lacks a `kind/foo` label and requires one.sig/api-machineryCategorizes an issue or PR as relevant to SIG API Machinery.triage/acceptedIndicates an issue or PR is ready to be actively worked on.

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions