devspace-sh / devspace

DevSpace - The Fastest Developer Tool for Kubernetes ⚡ Automate your deployment workflow with DevSpace and develop software directly inside Kubernetes.
https://devspace.sh
Apache License 2.0
4.28k stars 359 forks source link

Patches do not support inserting into the middle of existing arrays #2741

Open jackwilsdon opened 10 months ago

jackwilsdon commented 10 months ago

What happened?
Patches do not support inserting into the middle (or start) of existing arrays.

What did you expect to happen instead?
Patches can insert values into the middle (or start) of an existing array, per the JSON Patch specification: https://datatracker.ietf.org/doc/html/rfc6902#appendix-A.2

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

  1. Save this devspace.yaml:

    version: v2beta1
    
    hooks:
      - events:
          - event
        command: echo first
      - events:
          - event
        command: echo second
      - events:
          - event
        command: echo third
    
    profiles:
      - name: insert-first
        patches:
          - op: add
            path: hooks[0]
            value:
              events:
                - event
              command: echo new first
      - name: insert-last
        patches:
          - op: add
            path: hooks
            value:
              events:
                - event
              command: echo new last
    1. Run devspace print -p insert-first and get the following error:
      fatal yaml: unmarshal errors:
      line 5: mapping key "command" already defined at line 2
      line 6: mapping key "events" already defined at line 3
    2. Run devspace print -p insert-last and get the following output (correctly added to end of hooks):
      version: v2beta1
      name: test
      hooks:
      - events:
          - event
        command: echo first
      - events:
          - event
        command: echo second
      - events:
          - event
        command: echo third
      - events:
          - event
        command: echo new last

Local Environment:

lizardruss commented 10 months ago

Thanks for reporting this! Yes it looks to work for simple values, but not for objects. We'll look into a fix!