Skip to content
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

Golang KPT fn SDK: Improve interoperability between KubeObject and sigs.k8s.io/kustomize/kyaml #4174

Open
kispaljr opened this issue Sep 8, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@kispaljr
Copy link

kispaljr commented Sep 8, 2024

In porch and Nephio projects we utilize the kpt-fn-sdk to manipulate KRM objects stored in YAML files, but we also rely on sigs.k8s.io/kustomize/kyaml, especially sigs.k8s.io/kustomize/kyaml/kio to read/write the YAML data to various abstractions.
That requires us to convert back and forth between fn.KubeObject and yaml.RNode, however this conversion is not natively supported by either libraries. We have to use workarounds, like round-trip Marshall/Unmarshall, e.g.:

// AsRNode converts a KubeObject to a yaml.RNode
func AsRNode(obj *fn.KubeObject) (*yaml.RNode, error) {
	// TODO: remove the need for this unnecessary round-trip marshalling by adding a direct conversion method to KubeObject
	return yaml.Parse(obj.String())
}

This is both inefficient and also sometimes inadvertently changes the YAML formatting . Having native KubeObject methods and SDK functions for these conversions would help our projects to write better code.

@kispaljr kispaljr added the enhancement New feature or request label Sep 8, 2024
@kispaljr
Copy link
Author

kispaljr commented Sep 8, 2024

I already have an implementation proposal to solve this problem that you can cherry-pick from here: nokia/kpt-functions-sdk@08ebcb3 (I cannot send a PR, since my employer doesn't allow me to sign the CLA)

My implementation provides two sets of conversion functions:

  • copy-style conversions (NewKubeObjectFromResourceNode, CopyToResourceNode) do a deep copy of the internal yaml nodes, thus preventing the RNode and the KubeObject to modify each other's internal state
  • move-style conversions (MoveToResourceNode, MoveToKubeObject) transfer the internal YAML nodes from the source object to the destination, and clear the source object by setting it to empty, thus preventing the RNode and the KubeObject to modify each other's internal state

@yuwenma Could you help our "kpt sister projects" by reviewing and merging this fairly small change to https://github.com/GoogleContainerTools/kpt-functions-sdk under your name?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant