0

I recently realized that to get a YTT substitution to work properly, I had to add this:

#@overlay/match by="name"

like so:


spec:
  template:
    spec:
      containers:
      #@overlay/match by="name"
      - name: php-redis
        #@overlay/match missing_ok=False
        resources:
          requests:
            cpu: 200m

I was just wondering, why specifically, is this? It seems like ytt logically is overlaying new fields on top of old ones, so, shouldn't it be able to imply that we're matching to the map by its name, since this is the top field in the overlay stanza?

jayunit100
  • 17,388
  • 22
  • 92
  • 167

1 Answers1

0

From asking on slack, it seems that lists need an element to match against, so we specify the name.

As a way to think about what's going on under the hood, we can consider this as an equivalent Yaml snippet:

#@ load("@ytt:overlay", "overlay")
#@overlay/match by=overlay.subset({"metadata":{"name":"frontend-dep"}})
---
#@overlay/match by="spec"
spec:
  #@overlay/match by="template"
  template:
    #@overlay/match by="spec"
    spec:
      #@overlay/match by="containers"
      containers:
      #@overlay/match by="name"
      - name: php-redis
        #@overlay/match missing_ok=False
        resources:
          requests:
            cpu: 200m

That is, the "map" elements of our yaml are naturally mappable, because they have a key at their beginning. Since our "list of maps" in the containers field, however, contains several maps with many keys, we need to give ytt a hint on what specific field in the list needs to be matched.

jayunit100
  • 17,388
  • 22
  • 92
  • 167