carvel-dev / ytt

YAML templating tool that works on YAML structure instead of text
https://carvel.dev/ytt
Apache License 2.0
1.66k stars 135 forks source link

How to use overlay annotation with plain yaml objects #839

Closed Hugal31 closed 1 year ago

Hugal31 commented 1 year ago

My goal is to have overlayed data/values that result in a dictionary of objects. Then, I apply an operation to replace missing fields in each of those sub-objects by a default object, also defined in the overlayed data/values. It works well, however I can't specify the usual overlay annotations, as #@overlay/replace for the arrays.

I have those two files:

test.values.yaml

#@ load("@ytt:overlay", "overlay")
#@data/values
#@overlay/match-child-defaults missing_ok=True
---
root:
  default:
    x: 1
    'y': 2
    array: [300, 400]

  high:
    x: 5
    array: [600]

  low:
    'y': -1

and test.yaml:

#@ load("@ytt:data", "data")
#@ load("@ytt:overlay", "overlay")
#@ default = data.values.root.default
root: #@ {k: overlay.apply(default, v) for k, v in dict(data.values.root).items() if k != "default"}

The result :

root:
  high:
    x: 5
    "y": 2
    array:
    # Here the items are appended
    - 300
    - 400
    - 600
  low:
    x: 1
    "y": -1
    array:
    - 300
    - 400

Expected result:

root:
  high:
    x: 5
    "y": 2
    array:
    - 600
  low:
    x: 1
    "y": -1
    array:
    - 300
    - 400

How can I achieve this?

vmunishwar commented 1 year ago

@Hugal31 Hi, sorry for the late reply. Just curious, do you still need help to resolve this issue or you found a workaround? Kindly let us know. Thanks

Hugal31 commented 1 year ago

Yes, but I had to create my own overlay function. Since I was only merging on one level, this was easy:

def merge(left, right):
    result = dict(left)
    for k, v in dict(right).items():
        result[k] = v
    end
    return result
end

Edit: I think using overlay for this kind of task is not doable. The overlay annotation are consumed by the first overlay application (when merging my *.values.yaml). I'll close the issue.