Skip to content

Support filters in YAML path #181

Description

@christiansiegel

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?

Activity

  1. christiansiegel commented on Apr 24, 2022

    @christiansiegel
    MemberAuthor

    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

  2. joachimprinzbach commented on Apr 25, 2022

    @joachimprinzbach
    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.

  3. added 2 commits that reference this issue on May 8, 2022
    787afef
    c0327eb
  4. added a commit that references this issue on May 9, 2022
    cb2c6c2
  5. baopso commented on May 9, 2022

    @baopso

    🎉 This issue has been resolved in version 5.3.0 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions