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

How to process gitlab-like reference tags with yq? #2131

Closed
sigurdblueface opened this issue Aug 13, 2024 · 1 comment
Closed

How to process gitlab-like reference tags with yq? #2131

sigurdblueface opened this issue Aug 13, 2024 · 1 comment

Comments

@sigurdblueface
Copy link

sigurdblueface commented Aug 13, 2024

I have a following yaml file, utilizing format somewhat similar to reference tags in gitlab.

ports:
  - name: http
    port: 8080
    hosts:
      - hostname: "!reference[.placeholders, ingressHostname]"
...
placeholders:
  ingressHostname: xyz.com

My goal is to substitute these reference tags with values from placeholders like that.

ports:
  - name: http
    port: 8080
    hosts:
      - hostname: xyz.com

Couldn't find something on this in documentation.

@mikefarah
Copy link
Owner

You can use the eval operator to dynamically evaluate expressions - it gets a little complex for what you're trying to do, but if I change the format to this:

ports:
  - name: http
    port: 8080
    hosts:
      - hostname: !!reference .placeholders.ingressHostname
placeholders:
  ingressHostname: xyz.com

Where !!reference is the custom tag; and the value is a valid yq expression, then you can:

yq '. as $i | (.. | select(tag == "!!reference")) |= (. as $x | $i | eval($x)) | del(.placeholders)' data.yaml

and get

ports:
  - name: http
    port: 8080
    hosts:
      - hostname: xyz.com

Explanation:

  • . as $i saves a pointer to the root doc, we'll need this later
  • (.. | select(tag == "!!reference")) matches all the nodes with tag !!reference
  • This is where it gets a little tricky. We want to call eval against the root document, passing in the current value of the node as the expression. . as $x saves the current value as $x, then we pipe $i into eval, with $x as the param
  • Finally, we can delete 'placeholders'

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