kubernetes-sigs / kustomize

Customization of kubernetes YAML configurations
Apache License 2.0
11k stars 2.25k forks source link

Issue when adding multiple elements to an array using patchesJson6902 add op #3752

Closed ajonkisz closed 3 years ago

ajonkisz commented 3 years ago

Describe the bug

Adding multiple array elements to an array using patchesJson6902 and op: add does not behave as expected.

Files that can reproduce the issue Example:

kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1

patchesJson6902:
  - target:
      kind: test
      name: test
    patch: |-
      - op: add
        path: /someArray/-
        value:
          - element2
          - element3

resources:
  - resources.yaml

resources:

kind: test
metadata:
  name: test
someArray:
  - element0
  - element1

Expected output

kind: test
metadata:
  name: test
someArray:
- element0
- element1
- element2
- element3

Actual output

kind: test
metadata:
  name: test
someArray:
- element0
- element1
- - element2
  - element3

Kustomize version

Tested on v3.8.7 as well as v4.0.5

Platform

Both Linux and Windows have been tested

Additional Notes Specifying multiple add operations in the patch as:

- op: add
  path: /someArray/-
  value: element2
- op: add
  path: /someArray/-
  value: element3

Works as expected, but this is rather cumbersome and shouldn't be necessary if the type can be detected the type we're adding to is an array.

ajonkisz commented 3 years ago

I just noticed that this is in accordance with https://tools.ietf.org/html/rfc6902#appendix-A.16 and https://tools.ietf.org/html/rfc6902#section-4.1, but I'm hoping maybe it can be extended by assing something like op: addToArray or svalueArray?

Shell32-Natsu commented 3 years ago

We are using external library for JSON patch (github.com/evanphx/json-patch). Unfortunately we cannot extend its syntax.