How can I turn Kustomize resources into typed Kubernetes objects in Go?

0
0
Asked By VelvetOrbit27 On

I'm using Kustomize from Go to render manifests, including CRDs, with `krusty.MakeKustomizer` and `k.Run(...)`. I currently have a `resmap.ResMap`, but I need to process each rendered resource as a Kubernetes object. I was looking at code that uses `info.Object` and `info.Mapping.GroupVersionKind`, then converts the object through a scheme, but I can't find how to obtain that `info` value from Kustomize. Is there a way to extract `resource.Info`, or should I iterate through the Kustomize resources and decode them into `unstructured.Unstructured` or typed Go objects?

3 Answers

Answered By CedarPixel41 On

For CRDs and other resources whose Go types may not be registered, `unstructured.Unstructured` is usually the simplest option. A typical conversion is to loop over `m.Resources()`, serialize each `*resource.Resource` with `AsYAML()`, and decode it with `yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)`. The resulting `*unstructured.Unstructured` implements `client.Object`, so it can be assigned directly to a controller-runtime client object.

QuietHarbor6 -

That approach worked for me: each Kustomize resource is converted with `AsYAML()`, decoded into an `unstructured.Unstructured`, and appended as a `client.Object`. It avoids needing generated Go types for the CRDs.

Answered By MapleRook8 On

`resource.Info` belongs to Kubernetes `cli-runtime`, not Kustomize. `krusty.Run()` returns a `ResMap` containing rendered resources, so it does not create kubectl-style `Info` objects. If you specifically need `resource.Info`, use the `cli-runtime` `resource.Builder` pipeline instead. Otherwise, iterate over `m.Resources()`, call `AsYAML()` on each resource, and decode the result into `unstructured.Unstructured` or into a registered typed scheme.

VelvetOrbit27 -

So the important part is calling `m.Resources()` rather than trying to iterate the internal `ResId` map, then decoding each returned resource?

Answered By AmberCircuit19 On

You can still inspect metadata such as the group, version, and kind from each Kustomize resource, for example with its GVK-related accessors. However, that metadata is not the same thing as `resource.Info`; `Info` also represents kubectl's source and mapping context. Use the Kustomize resource directly when you only need the rendered object, or use `cli-runtime` if the full kubectl abstraction is required.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.