kptdev / kpt

Automate Kubernetes Configuration Editing
https://kpt.dev
Apache License 2.0
1.66k stars 226 forks source link

[Question] kpt function sdk golang no deep copy #4029

Closed jdkuki closed 10 months ago

jdkuki commented 10 months ago

I am looking at using the kpt sdk to generate some unique yamls from a base template using a krm function. My template is tracked as a KRM resource as I would like to combine a different set of config from configPath on the function call.

I have code like:

for _, o := range rl.Items {
  newObj := fn.NewEmptyKubeObject()
 // somehow copy o into newObj, then modify newObj
}

How can I make a deep copy of a KubeObject? I don't find anything in the godoc that seems to accomplish this. Apologies if this is a rather obvious task...

natasha41575 commented 10 months ago

@yuwenma Can you take a look at this one?

johnbelamaric commented 10 months ago

This should work, but maybe @yuwenma has a better way:

for _, o := range rl.Items {
    newObj, err := fn.ParseKubeObject(o.String())
    if err != nil {
        ...
    }
}
yuwenma commented 10 months ago

Thanks @johnbelamaric . Yes, I think ParseKubeObject is the best approach right now.

natasha41575 commented 10 months ago

Closing this as it appears to be resolved. Let us know if you have any further questions, and thanks so much for trying out the sdk.

jdkuki commented 10 months ago

Yes this a good solution for now, thank you!