kubernetes / kubectl

Issue tracker and mirror of kubectl code
Apache License 2.0
2.76k stars 898 forks source link

The result after executing the Json Patch command is not as expected #1349

Open dayongi opened 1 year ago

dayongi commented 1 year ago

What happened: When using the add operator of JSON Patch to modify the CRD, it is found that the original value is overwritten instead of appended

What you expected to happen: Append the desired value to the corresponding property.

How to reproduce it (as minimally and precisely as possible): 1、This is the initial declaration for the corresponding CRD:

[root@k8s-master-001 3.1.0-4.0.0]# kubectl get crd baserouteserversettings.configuration.baosight.io -o yaml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  annotations:
    controller-gen.kubebuilder.io/version: v0.4.1
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"apiextensions.k8s.io/v1","kind":"CustomResourceDefinition","metadata":{"annotations":{"controller-gen.kubebuilder.io/version":"v0.4.1"},"creationTimestamp":null,"name":"baserouteserversettings.configuration.baosight.io"},"spec":{"group":"configuration.baosight.io","names":{"kind":"BaseRouteServerSetting","listKind":"BaseRouteServerSettingList","plural":"baserouteserversettings","singular":"baserouteserversetting"},"scope":"Namespaced","versions":[{"name":"v1","schema":{"openAPIV3Schema":{"description":"BaseRouteServerSetting is the Schema for the baserouteserversettings API","properties":{"apiVersion":{"description":"APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources","type":"string"},"kind":{"description":"Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds","type":"string"},"metadata":{"type":"object"},"spec":{"description":"BaseRouteServerSettingSpec defines the desired state of BaseRouteServerSetting","properties":{"auth_by_secret_address":{"description":"Query the interface address of APP_SECRET","type":"string"},"auth_by_secret_effective_time":{"description":"Effective time","type":"integer"},"auth_by_secret_rigorous_mode":{"description":"Secret Authentication Rigorous Mode","type":"boolean"},"auth_by_secret_switch":{"description":"Secret authentication switch","type":"boolean"},"auth_by_token_switch":{"description":"Token authentication switch","type":"boolean"},"base_security_public_key":{"description":"The public key used when parsing the Security Center JWT","type":"string"},"enable_tracing":{"description":"Whether to enable Access log buried point","type":"boolean"},"ignore_domain":{"description":"The request domain name that needs to be ignored when processing share service route","items":{"type":"string"},"type":"array"}},"type":"object"},"status":{"description":"BaseRouteServerSettingStatus defines the observed state of BaseRouteServerSetting","type":"object"}},"type":"object"}},"served":true,"storage":true,"subresources":{"status":{}}}]},"status":{"acceptedNames":{"kind":"","plural":""},"conditions":[],"storedVersions":[]}}
  creationTimestamp: "2023-01-16T12:54:37Z"
  generation: 7
  managedFields:
  - apiVersion: apiextensions.k8s.io/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:status:
        f:acceptedNames:
          f:kind: {}
          f:listKind: {}
          f:plural: {}
          f:singular: {}
        f:conditions: {}
    manager: kube-apiserver
    operation: Update
    time: "2023-01-16T12:54:37Z"
  - apiVersion: apiextensions.k8s.io/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          .: {}
          f:controller-gen.kubebuilder.io/version: {}
          f:kubectl.kubernetes.io/last-applied-configuration: {}
      f:spec:
        f:conversion:
          .: {}
          f:strategy: {}
        f:group: {}
        f:names:
          f:kind: {}
          f:listKind: {}
          f:plural: {}
          f:singular: {}
        f:scope: {}
      f:status:
        f:storedVersions: {}
    manager: kubectl-client-side-apply
    operation: Update
    time: "2023-01-16T12:54:37Z"
  - apiVersion: apiextensions.k8s.io/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:spec:
        f:versions: {}
    manager: kubectl-edit
    operation: Update
    time: "2023-01-16T13:43:53Z"
  name: baserouteserversettings.configuration.baosight.io
  resourceVersion: "193561305"
  selfLink: /apis/apiextensions.k8s.io/v1/customresourcedefinitions/baserouteserversettings.configuration.baosight.io
  uid: 5ce311ab-aa03-45fe-92d5-c75c2f548e0f
spec:
  conversion:
    strategy: None
  group: configuration.baosight.io
  names:
    kind: BaseRouteServerSetting
    listKind: BaseRouteServerSettingList
    plural: baserouteserversettings
    singular: baserouteserversetting
  scope: Namespaced
  versions:
  - name: v1
    schema:
      openAPIV3Schema:
        description: BaseRouteServerSetting is the Schema for the baserouteserversettings
          API
        properties:
          apiVersion:
            description: 'APIVersion defines the versioned schema of this representation
              of an object. Servers should convert recognized schemas to the latest
              internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
            type: string
          kind:
            description: 'Kind is a string value representing the REST resource this
              object represents. Servers may infer this from the endpoint the client
              submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
            type: string
          metadata:
            type: object
          spec:
            description: BaseRouteServerSettingSpec defines the desired state of BaseRouteServerSetting
            properties:
              auth_by_secret_address:
                description: Query the interface address of APP_SECRET
                type: string
              auth_by_secret_effective_time:
                description: Effective time
                type: integer
              auth_by_secret_rigorous_mode:
                description: Secret Authentication Rigorous Mode
                type: boolean
              auth_by_secret_switch:
                description: Secret authentication switch
                type: boolean
              auth_by_token_switch:
                description: Token authentication switch
                type: boolean
              base_security_public_key:
                description: The public key used when parsing the Security Center
                  JWT
                type: string
            type: object
          status:
            description: BaseRouteServerSettingStatus defines the observed state of
              BaseRouteServerSetting
            type: object
        type: object
    served: true
    storage: true
    subresources:
      status: {}
status:
  acceptedNames:
    kind: BaseRouteServerSetting
    listKind: BaseRouteServerSettingList
    plural: baserouteserversettings
    singular: baserouteserversetting
  conditions:
  - lastTransitionTime: "2023-01-16T12:54:37Z"
    message: no conflicts found
    reason: NoConflicts
    status: "True"
    type: NamesAccepted
  - lastTransitionTime: "2023-01-16T12:54:37Z"
    message: the initial names have been accepted
    reason: InitialNamesAccepted
    status: "True"
    type: Established
  storedVersions:
  - v1

2、This is the instruction of the executed JSON Patch:

[root@k8s-master-001 backups]# kubectl patch crd baserouteserversettings.configuration.baosight.io  --type json -p '[{"op":"add","path":"/spec/versions/0/schema/openAPIV3Schema/properties/spec/properties","value":{"enable_tracing":{"description":"Whether to enable Access log buried point","type":"boolean"}}}]'
customresourcedefinition.apiextensions.k8s.io/baserouteserversettings.configuration.baosight.io patched
[root@k8s-master-001 backups]# kubectl get crd baserouteserversettings.configuration.baosight.io -o yaml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  annotations:
    controller-gen.kubebuilder.io/version: v0.4.1
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"apiextensions.k8s.io/v1","kind":"CustomResourceDefinition","metadata":{"annotations":{"controller-gen.kubebuilder.io/version":"v0.4.1"},"creationTimestamp":null,"name":"baserouteserversettings.configuration.baosight.io"},"spec":{"group":"configuration.baosight.io","names":{"kind":"BaseRouteServerSetting","listKind":"BaseRouteServerSettingList","plural":"baserouteserversettings","singular":"baserouteserversetting"},"scope":"Namespaced","versions":[{"name":"v1","schema":{"openAPIV3Schema":{"description":"BaseRouteServerSetting is the Schema for the baserouteserversettings API","properties":{"apiVersion":{"description":"APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources","type":"string"},"kind":{"description":"Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds","type":"string"},"metadata":{"type":"object"},"spec":{"description":"BaseRouteServerSettingSpec defines the desired state of BaseRouteServerSetting","properties":{"auth_by_secret_address":{"description":"Query the interface address of APP_SECRET","type":"string"},"auth_by_secret_effective_time":{"description":"Effective time","type":"integer"},"auth_by_secret_rigorous_mode":{"description":"Secret Authentication Rigorous Mode","type":"boolean"},"auth_by_secret_switch":{"description":"Secret authentication switch","type":"boolean"},"auth_by_token_switch":{"description":"Token authentication switch","type":"boolean"},"base_security_public_key":{"description":"The public key used when parsing the Security Center JWT","type":"string"},"enable_tracing":{"description":"Whether to enable Access log buried point","type":"boolean"},"ignore_domain":{"description":"The request domain name that needs to be ignored when processing share service route","items":{"type":"string"},"type":"array"}},"type":"object"},"status":{"description":"BaseRouteServerSettingStatus defines the observed state of BaseRouteServerSetting","type":"object"}},"type":"object"}},"served":true,"storage":true,"subresources":{"status":{}}}]},"status":{"acceptedNames":{"kind":"","plural":""},"conditions":[],"storedVersions":[]}}
  creationTimestamp: "2023-01-16T12:54:37Z"
  generation: 8
  managedFields:
  - apiVersion: apiextensions.k8s.io/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:status:
        f:acceptedNames:
          f:kind: {}
          f:listKind: {}
          f:plural: {}
          f:singular: {}
        f:conditions: {}
    manager: kube-apiserver
    operation: Update
    time: "2023-01-16T12:54:37Z"
  - apiVersion: apiextensions.k8s.io/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          .: {}
          f:controller-gen.kubebuilder.io/version: {}
          f:kubectl.kubernetes.io/last-applied-configuration: {}
      f:spec:
        f:conversion:
          .: {}
          f:strategy: {}
        f:group: {}
        f:names:
          f:kind: {}
          f:listKind: {}
          f:plural: {}
          f:singular: {}
        f:scope: {}
      f:status:
        f:storedVersions: {}
    manager: kubectl-client-side-apply
    operation: Update
    time: "2023-01-16T12:54:37Z"
  - apiVersion: apiextensions.k8s.io/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:spec:
        f:versions: {}
    manager: kubectl-patch
    operation: Update
    time: "2023-01-16T13:48:03Z"
  name: baserouteserversettings.configuration.baosight.io
  resourceVersion: "193562069"
  selfLink: /apis/apiextensions.k8s.io/v1/customresourcedefinitions/baserouteserversettings.configuration.baosight.io
  uid: 5ce311ab-aa03-45fe-92d5-c75c2f548e0f
spec:
  conversion:
    strategy: None
  group: configuration.baosight.io
  names:
    kind: BaseRouteServerSetting
    listKind: BaseRouteServerSettingList
    plural: baserouteserversettings
    singular: baserouteserversetting
  scope: Namespaced
  versions:
  - name: v1
    schema:
      openAPIV3Schema:
        description: BaseRouteServerSetting is the Schema for the baserouteserversettings
          API
        properties:
          apiVersion:
            description: 'APIVersion defines the versioned schema of this representation
              of an object. Servers should convert recognized schemas to the latest
              internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
            type: string
          kind:
            description: 'Kind is a string value representing the REST resource this
              object represents. Servers may infer this from the endpoint the client
              submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
            type: string
          metadata:
            type: object
          spec:
            description: BaseRouteServerSettingSpec defines the desired state of BaseRouteServerSetting
            properties:
              enable_tracing:
                description: Whether to enable Access log buried point
                type: boolean
            type: object
          status:
            description: BaseRouteServerSettingStatus defines the observed state of
              BaseRouteServerSetting
            type: object
        type: object
    served: true
    storage: true
    subresources:
      status: {}
status:
  acceptedNames:
    kind: BaseRouteServerSetting
    listKind: BaseRouteServerSettingList
    plural: baserouteserversettings
    singular: baserouteserversetting
  conditions:
  - lastTransitionTime: "2023-01-16T12:54:37Z"
    message: no conflicts found
    reason: NoConflicts
    status: "True"
    type: NamesAccepted
  - lastTransitionTime: "2023-01-16T12:54:37Z"
    message: the initial names have been accepted
    reason: InitialNamesAccepted
    status: "True"
    type: Established
  storedVersions:
  - v1
[root@k8s-master-001 backups]#

From the final result, it can be found that after the Patch command is executed, the required value is not added to the corresponding attribute, but the value of the original attribute is overwritten. What is the problem?

Anything else we need to know?:

Environment:

k8s-ci-robot commented 1 year ago

@dayongi: This issue is currently awaiting triage.

SIG CLI takes a lead on issue triage for this repo, but any Kubernetes member can accept issues by applying the triage/accepted label.

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](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
sftim commented 1 year ago

Does this only happen with kubectl, or do the symptoms also show up when making a request using (eg) curl?

dayongi commented 1 year ago

Does this only happen with kubectl, or do the symptoms also show up when making a request using (eg) curl?

I only tried it with kubectl

k8s-triage-robot commented 1 year ago

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot commented 1 year ago

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

mpuckett159 commented 1 month ago

Hi, can you please verify this on a newer version of kubectl? Either 1.30 or 1.29. If it is still an issue on those versions, could you please provide a simpler example of how to observe the issue?

/triage needs-information /remove-lifecycle rotten