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

Support filters in YAML path #181

Closed
christiansiegel opened this issue Apr 24, 2022 · 3 comments · Fixed by #182
Closed

Support filters in YAML path #181

christiansiegel opened this issue Apr 24, 2022 · 3 comments · Fixed by #182
Assignees
Labels
enhancement New feature or request released

Comments

@christiansiegel
Copy link
Member

The replace mechanism only supports selecting the YAML path by object key and array index (e.g. foo.bar.[0].baz). This is quite fragile if you have to replace within arrays.

Example:

# kustomizazion.yaml
images:
- name: frontend
  newTag: v2
- name: backend
  newTag: v2

or

# values.yaml
myapp:
   env:
   - name: FOO
     value: foo
   - name: BAR
     value: bar

Replacing the image tags or the env values requires the array to be in a stable order. Also the replacement path is hard to read if you don't know the YAML file: myapp.env.[1].value -> are we replacing FOO or BAR here?

Possible solution:
JsonPath has filter expressions. Selecting the value of FOO would look like this: myapp.env[?(@.name=='FOO')].value

There is a python library jsonpath-ng which can output the matching paths:

>>> jsonpath_expr = parse('foo[*].baz')
# Matches remember where they came from
>>> [str(match.full_path) for match in jsonpath_expr.find({'foo': [{'baz': 1}, {'baz': 2}]})]
['foo.[0].baz', 'foo.[1].baz']

Those returned full_path strings are exactly the simplified YAML path supported by the GitOpsCLI. We could just plug this library in between and translate complicated JsonPath expressions.

The library even has a short form to specify the filters: myapp.env[?name=='FOO'].value

This solution would be backwards compatible. However, we could also choose to just enable this JsonPath translation feature via CLI flag. Any opinions on that?

@christiansiegel christiansiegel added the enhancement New feature or request label Apr 24, 2022
@christiansiegel
Copy link
Member Author

Just noticed that jsonpath-ng also has an update function. This could replace the whole self-built path parse & replace. It also works in combination with ruamel.yaml because it only operates on the loaded python objects.

WIP on branch feat/json-path

@christiansiegel christiansiegel self-assigned this Apr 24, 2022
@joachimprinzbach
Copy link
Member

I would change the implementation in the back for all cli functions instead of using a feature flag. Reasoning: Feature flag could be great for testing, but in the long term will increase support and maintenance / testing efforts.

christiansiegel added a commit that referenced this issue May 8, 2022
Use library `jsonpath-ng` to replace own but limited JSONPath implementation. Especially the filter expressions allow more complex replacements within lists.

Resolves #181.
christiansiegel added a commit that referenced this issue May 8, 2022
Use library `jsonpath-ng` to replace own but limited JSONPath implementation. Especially the filter expressions allow more complex replacements within lists.

Resolves #181.
christiansiegel added a commit that referenced this issue May 9, 2022
Use library `jsonpath-ng` to replace own but limited JSONPath implementation. Especially the filter expressions allow more complex replacements within lists. Example: `backend.env[?name=='TEST'].value`

Resolves #181.
@baopso
Copy link

baopso commented May 9, 2022

🎉 This issue has been resolved in version 5.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@baopso baopso added the released label May 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants