I'm using krusty.Run to read resources from the filesystem and want to work with them in a Kubebuilder project. I have code that converts info.Object to a typed object, but I can't find an info object in the returned ResMap. Where should it come from?
How do I get resource info from a Kustomize ResMap in Go?
Answers
resource.Info belongs to k8s.io/cli-runtime, not Kustomize. krusty.Run returns a ResMap, which contains Kustomize resources rather than resource.Info objects.
If you specifically need resource.Info, look at the cli-runtime resource.Builder pipeline. Otherwise, you can work from the ResMap and decode its resources into Kubernetes objects without creating an Info first.
Reply to this answer
For a Kubebuilder client, an unstructured object may be enough, especially if you don't have Go types registered for every CRD. Iterate over the resources using the ResMap API, serialize each resource to YAML or JSON, then decode it into an unstructured.Unstructured.
A pointer to unstructured.Unstructured implements client.Object, so you can pass it to controller-runtime client methods. Check the decode errors and make sure the object has its API version and kind.
Reply to this answer
If you need a concrete Go type instead, decode each serialized resource using a scheme that has that type registered. The resource's group, version and kind can help identify what you're decoding, but knowing the GVK alone doesn't register or construct the Go type for you.
For CRDs without registered Go types, stick with unstructured objects. The ConvertToVersion call you have expects a runtime object as input, so the missing step is getting one from the Kustomize resource first.
Reply to this answer
Add your answer
Answers are reviewed before publication.
pAuse_for_punctuation:
That helps. I was looking for an internal map to iterate, but the ResMap API is the better place to start.