carvel-dev / ytt

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

Sorry, but I just can't figure it out with overlays #917

Open scastria opened 1 month ago

scastria commented 1 month ago

I have watched the primer youtube video, but I don't think any of those examples apply to my situation. I am trying to add a map item to ONLY the Plan_Containers_development step without using hardcoded indexes since the Plan_Containers_development step could be anywhere. The issue, I think, is that I have arrays of maps of arrays of maps of maps.

My source yaml is:

image:
  name: image:1234

pipelines:
  branches:
    dynamic:
      - parallel:
        - step:
            name: build_generic
        - step:
            name: build_containers
        - step:
            name: build_lambdas
        - step:
            name: publish_docs
      - parallel:
        - step:
            name: 'Plan_Containers_development'
            deployment: 'development_containers_plan'
        - step:
            name: 'Plan_Containers_stage'
            deployment: 'stage_containers_plan'
        - step:
            name: 'Plan_Containers_production'
            deployment: 'production_containers_plan'
      - parallel:
        - step:
            name: 'Apply_Containers_development'
            deployment: 'development_containers_apply'
            trigger: 'automatic'
        - step:
            name: 'Apply_Containers_stage'
            deployment: 'stage_containers_apply'
        - step:
            name: 'Apply_Containers_production'
            deployment: 'production_containers_apply'

My current overlay is:

#@ load("@ytt:overlay", "overlay")
#@overlay/match by=overlay.all
---
pipelines:
  branches:
    #@overlay/match by=overlay.all
    _:
      #@overlay/match by=overlay.all, expects="0+"
      - parallel:
        #@overlay/match by=overlay.all, expects="0+"
        -
          #@overlay/match by=overlay.subset({"name": "Plan_Containers_development"}), missing_ok=True
          step:
            new: "value"
scastria commented 1 month ago

I CAN get it to work if I use hardcoded indicies which I would like to avoid:

#@ load("@ytt:overlay", "overlay")
#@overlay/match by=overlay.all
---
pipelines:
  branches:
    #@overlay/match by=overlay.all
    _:
      #@overlay/match by=overlay.index(1)
      - parallel:
        #@overlay/match by=overlay.index(0)
        - step:
            #@overlay/match missing_ok=True
            new: "value"
scastria commented 1 month ago

This seems to work by using a custom matcher function, but is this efficient? Plus, I would like to match on other step names. Is there a way to reuse my matcher function but pass in a fourth parameter on what step name to look for?

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

#@ def parallel(i, l, r):
#@   for s in l["parallel"]:
#@     if s["step"]["name"] == "Plan_Containers_development":
#@       return True
#@     end
#@   end
#@   return False
#@ end
#@ def step(i, l, r):
#@   return l["step"]["name"] == "Plan_Containers_development"
#@ end

#@overlay/match by=overlay.all
---
pipelines:
  branches:
    #@overlay/match by=overlay.all
    _:
      #@overlay/match by=parallel
      - parallel:
        #@overlay/match by=step
        - step:
            #@overlay/match missing_ok=True
            new: "value"
prembhaskal commented 1 month ago

@scastria You can pass stepname using closure. I will see if this can be done in a better way somehow.

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

#@ def parallelc(stepname):
#@   def parallel(i, l, r):
#@     for s in l["parallel"]:
#@       if s["step"]["name"] == stepname:
#@         return True
#@       end
#@     end
#@     return False
#@   end
#@   return parallel
#@ end
#@ def stepc(stepname):
#@   def step(i, l, r):
#@     return l["step"]["name"] == stepname
#@   end
#@  return step
#@ end

#@overlay/match by=overlay.all
---
pipelines:
  branches:
    #@overlay/match by=overlay.all
    _:
      #@overlay/match by=parallelc("Plan_Containers_development")
      - parallel:
        #@overlay/match by=stepc("Plan_Containers_development")
        - step:
            #@overlay/match missing_ok=True
            new: "value"