Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add merge shortcut for arrays using arbitrary element key(s) #77

Open
krouma opened this issue Dec 14, 2021 · 2 comments
Open

Add merge shortcut for arrays using arbitrary element key(s) #77

krouma opened this issue Dec 14, 2021 · 2 comments

Comments

@krouma
Copy link

krouma commented Dec 14, 2021

Hi, would it be possible to implement a merge operation? For example if the mutated resource had

env:
- name: ENV1
  value: true
- name: ENV2
  value: false

patch section had path ending with env and value contained:

- name: ENV2
  value: true
- name: ENV3
  value: true

the result would be:

env:
- name: ENV1
  value: true
- name: ENV2
  value: true
- name: ENV3
  value: true

Currently with operation add the ENV1 would be missing. I know I can specify multiple patches and each setting env/-1 but this could simplify some scenarios.

@vassilvk
Copy link
Member

The challenge here is that this is a merge of two arrays with no indication of what key (or possibly even multiple keys) to use for the merge.

For example, we do see that the key here is name, but there is no way to specify this through the JSON Patch or JSON Merge specifications.

This could possibly be implemented as an extension of JSON Merge, although I don't think this would be trivial.

@vassilvk
Copy link
Member

vassilvk commented Dec 14, 2021

Note that with KubeMod today, you can use the select clause of an add operation to target items in an array based on an arbitrary key criteria.

For example, to find an environment variable called ENV2 in any container in a Deployment and replace it with a new environment variable object, the following add operation would do the trick:

...
  patch:
    - op: add
      select: '$.spec.template.spec.containers[*].env[? @.name == "ENV2"]'
      path: '/spec/template/containers/#0/env/#1'
      value: |-
        name: ENV2
        value: true

The above technique only works for replacing existing items in an array.
If you want to add ENV2 to an array in case it is missing, you will need another rule with a negate match where you add the variable with env/-1 in case such a variable is missing.

That said, I understand that this sounds like a lot of work for something that seems quite simple and I agree that a merge operation with the ability to specify the key(s) of the items in the array will make things much better.

@vassilvk vassilvk changed the title Add merge operation Add merge shortcut for arrays using arbitrary element key(s) Dec 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants