ytt icon indicating copy to clipboard operation
ytt copied to clipboard

Support "replacert" in overlays

Open pivotaljohn opened this issue 4 years ago • 4 comments

Describe the problem/challenge you have

I would like to be able to, in one overlay operation:

  • replace a {map item, array item, document} if it exists with my content; or
  • append my content as a new item in the collection.

Today, I need to do that in two operations:

#@overlay/match by=overlay.subset({"kind":"Deployment", "metadata":{"name":"frontend"}})
---
spec:
  template:
    spec:
      containers:
        #@overlay/match by=overlay.subset({"name": "webserver"})
        -
          #@overlay/match missing_ok=True
          #@overlay/remove
          args:

#@overlay/match by=overlay.subset({"kind":"Deployment", "metadata":{"name":"frontend"}})
---
spec:
  template:
    spec:
      containers:
        #@overlay/match by=overlay.subset({"name": "webserver"})
        -
          #@overlay/match missing_ok=True
          args: #@ data.values.deployment.args

Describe the solution you'd like

I'd rather @overlay/replace have an "replacert" behavior.

If that were the case, this would accomplish the same thing as above with just one overlay, greatly simplifying it:

#@overlay/match by=overlay.subset({"kind":"Deployment", "metadata":{"name":"frontend"}})
---
spec:
  template:
    spec:
      containers:
        #@overlay/match by=overlay.subset({"name": "webserver"})
        -
          #@overlay/match missing_ok=True
          #@overlay/replace
          args:

Anything else you would like to add: The verbosity of doing this in multiple operations grows noticeably as the number of items being overlay'ed within the map increases

pivotaljohn avatar Mar 25 '21 03:03 pivotaljohn

is this not possible with when= kwarg?

cppforlife avatar Mar 25 '21 20:03 cppforlife

when applies to the overlay/match annotation, is it possible to provide two actions to the same node with different matchers?

Or did you mean repeat the node, each with a matcher/action annotation.

#! base.yml
---
id: 1
meta:
  format: .mobi
---
id: 2
meta:
  format: .epub
  preview: true
#! 1.yml

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

#@overlay/match by=overlay.all
---
meta:
  #@overlay/match when=0
  #@overlay/insert
  #@overlay/match when="1+"
  #@overlay/replace 
  preview: false
#! 2.yml

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

#@overlay/match by=overlay.all
---
meta:
  #@overlay/match when="1+"
  #@overlay/replace 
  preview: false
  #@overlay/match when=0
  #@overlay/insert 
  preview: false

These are both errors :/ I assume you had something else in mind :)

StevenLocke avatar Mar 26 '21 00:03 StevenLocke

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

#@overlay/match by=overlay.all
---
meta:
  #@overlay/match when="1+"
  #@overlay/replace 
  preview: false
  #@overlay/match when=0
  #@overlay/insert 
  preview: false

this will fail since in both cases involve same map key. this isnt the case with array items and from what i see that's what the initial question was about.

cppforlife avatar Mar 26 '21 13:03 cppforlife

I added the discussion label to this issue as it seems like we're still hashing out the details. Please feel free to remove the label and add carvel-accepted when we've reached an agreement.

aaronshurley avatar Mar 31 '21 17:03 aaronshurley