carvel-dev / ytt

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

Optionally, `overlay/insert` via function #742

Closed mamachanko closed 2 years ago

mamachanko commented 2 years ago

The insert overlay action has an optional 'via' kwarg, which receives the left and right node and produces the new right node, similar to the replace overlay action.

For example, this allows to add a ConfigMap into every matched Kubernetes Namespace without knowing their names in advance:

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

#@ for i in range(3):
---
apiVersion: v1
kind: Namespace
metadata:
  name: #@ i
#@ end

#@ def cm(ns):
apiVersion: v1
kind: ConfigMap
metadata:
  name: insert
  namespace: #@ ns["metadata"]["name"]
#@ end

#@overlay/match by=overlay.subset({"kind": "Namespace"}), expects="0+"
#@overlay/insert after=True, via=lamda ns,_: cm(ns)
---

produces:

apiVersion: v1
kind: Namespace
metadata:
  name: 0
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: insert
  namespace: 0
---
apiVersion: v1
kind: Namespace
metadata:
  name: 1
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: insert
  namespace: 1
---
apiVersion: v1
kind: Namespace
metadata:
  name: 2
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: insert
  namespace: 2

Addresses #738

Qs for the maintainers:

Companion PR for docs: https://github.com/vmware-tanzu/carvel/pull/553

pivotaljohn commented 2 years ago

Companion PR for docs: https://github.com/vmware-tanzu/carvel/pull/553

Wow! This is wonderful! 🙏🏻

mamachanko commented 2 years ago

@pivotaljohn As per your very reasonable suggestion, the via kwarg now receives both the left and the right node. 🙇🏻‍♂️

mamachanko commented 2 years ago

@pivotaljohn wonderful! 🥹

You closed #718, but is it possible you meant to close #738 instead?

pivotaljohn commented 2 years ago

Oh wow... I think it was this comment that triggered the workflow: https://github.com/vmware-tanzu/carvel-ytt/issues/738#issuecomment-1239662734

I'll be choosing my words more wisely, going forward.