New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clone the content of a node to another #665
Comments
Hey @mojito317, good question. Yes, you can. If you mean wholesale duplicate a single key, you can use the #@overlay/match by=lambda _,left,__: "A" in left
#@overlay/replace via=lambda orig, _: {"A": orig["A"], "B": orig["A"]}
--- Note:
However, if there are other parts of the document, e.g. A:
e: f
c: d
G:
h: i that approach will remove them (here, ... If you need a more general approach, this works: #@ load("@ytt:yaml", "yaml")
#@ def dup(orig, src, dest):
#@ result = yaml.decode(yaml.encode(orig))
#@ result[dest] = result[src]
#@ return result
#@ end
#@overlay/match by=lambda _,left,__: "A" in left
#@overlay/replace via=lambda orig, _: dup(orig, "A", "B")
--- Note:
... All of this makes me quite curious: what in your world is wanting to create a duplicate like this? 🤔 |
@pivotaljohn thank you for your help, I would not figure this out by myself. I kept trying with the
It's a tricky one. We are using ytt for overlaying an OpenAPI descriptor, where we have one schema with a tiny difference from the original, but we want to keep both of them, so we have to duplicate it before we modify our custom one. I provided helpful modification for the future if someone wants to duplicate one nested part of a tree. In this example I would like to duplicate the right of input: A:
B:
C:
d: e
F:
G:
h: i overlay: #@ load("@ytt:yaml", "yaml")
#@ def dup(orig, src, dest):
#@ result = yaml.decode(yaml.encode(orig))
#@ result["A"]["B"][dest] = result["A"]["B"][src]
#@ return result
#@ end
#@ load("@ytt:overlay", "overlay")
#@overlay/match by=lambda indexOrKey, left, right: "C" in left["A"]["B"]
#@overlay/replace via=lambda left, right: dup(left, "C", "X")
--- Output: A:
B:
C:
d: e
X:
d: e
F:
G:
h: i |
I'm delighted you got there. Happy to help. Dropping your questions in a GitHub issue like you just did is good and spot on. Note that you can also get support on our Kubernetes Slack channel: #carvel. If you're not already in the Kubernetes Slack workspace, you can get an invite here: http://slack.k8s.io/ |
Closing this issue to note that we've got a solution. If you'd like any additional support feel free to ask! |
Hi, sorry to resurrect an old thread, but I have a related question: is there any way to do something like this to clone an entire document? Basically, changing this ---
A:
B: foo into ---
A:
B: foo
---
A:
B: bar I tried to play with the example above but if I return a docset from the Thanks |
@lorenzobenvenuti: I was looking for similar functionality (cloning a document), and I think it's possible using So, for example, if you just need to clone a document (without making any changes): Input: #@ load("@ytt:overlay", "overlay")
---
A:
B: foo
#@overlay/match by=overlay.all
#@overlay/insert after=True, via=lambda left, right: left
--- Output: A:
B: foo
---
A:
B: foo But for a fuller example, you can also combine this with Input: #@ load("@ytt:overlay", "overlay")
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
namespace: example-namespace
#@overlay/match by=overlay.subset({"metadata":{"name":"example-ingress"}})
#@overlay/insert after=True, via=lambda left, right: overlay.apply(left, right)
---
metadata:
name: cloned-ingress
#@overlay/match missing_ok=True
spec:
ingressClassName: alb Output: apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
namespace: example-namespace
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: cloned-ingress
namespace: example-namespace
spec:
ingressClassName: alb |
TYVM! That's exactly what I was looking for! |
I would like to clone (without duplication) the right side of an
A
node to aB
node keepingA
node as is. Can I somehow reach this with overlays in ytt?Example input:
Expected output:
The text was updated successfully, but these errors were encountered: