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

Split data into more than one file #820

Closed maudrid closed 1 year ago

maudrid commented 1 year ago

Hi. I'm very new to ytt and I hope someone can point me in the right direction. I'm trying to do something seemingly simple, but I'm not able to get it to work. I have created a config.yml and I have a data1.yml. I can then use #@ load("@ytt:data", "data") and reference the values ex: attribute1: #@ data.values.att1 and this works. Some of the values that I have this the data1.yml, I want to place in a different file for maintenance purposes, now I have 3 files: config.yml

#@ load("@ytt:data", "data")

attribute1: #@ data.values.att1
attribute2: #@ data.values.att2

data1.yml

#@data/values
---
att1: "value1"

data2.yml

#@data/values
---
att2: "value2"

This produces the following error:

Overlaying data values (in following order: data1.yml, data2.yml): Document on line data2.yml:2: Map item (key 'att2') on line data2.yml:3: Expected number of matched nodes to be 1, but was 0

I understand that it is trying to overlay the 2 data files, but that is not what I want. I can add att2 to the data1.yml file to remove this error, but that defeats what I set out to do.

That being said, maybe I'm using this all wrong in the firs place. Here is a real example of why I'm trying to do this: config.yml

 grafana:
    image: gitlab.company.com:4567/myProject/grafana:#@ data.values.branch or "1.9"
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 400m
    #@ if data.values.swarm:
      placement:
        constraints:
          - "node.labels.appserver==true"
      labels: #@ data.values.grafana_labels
    #@ else:
    labels: #@ data.values.grafana_labels
    #@ end

labels.yml

#@data/values
---
grafana_labels:
  - traefik.enable=true
  - traefik.http.routers.grafana.entrypoints=web

settings.yml

#@data/values
---
swarm: true
branch: "master"

So I would not want to mix the values from the labels and the settings files. Since the settings will change all the time and the labels are static. But by using labels as data, I can maintain one copy of labels.

vmunishwar commented 1 year ago

Hi @maudrid, thanks for reaching out. This can be solved by using an overlay/match annotation #@overlay/match missing_ok=True above each new value in the settings.yml file. For your reference, I am attaching screenshots of these examples ran in ytt playground as well. Please let us know if this helps. We have Carvel’s slack channel too if you would like to join #carvel in Kubernetes

Also, Here are some docs using overlays and playground example to help you get started.

Screenshot 2023-04-07 at 11 46 32 PM Screenshot 2023-04-05 at 5 33 21 PM
maudrid commented 1 year ago

Thanks very much for the help.