Converting Kubernetes unstructured to typed objects
To interact with the Kubernetes API using the
client-go library there are two
primary APIs: the typed
kubernetes.Interface API and the unstructured
Although using the core kubernetes API is (for Kubernetes) well-documented, the dynamic API has fewer examples. There are a couple of examples, these did not show how more advanced examples how to work with the unstructured responses.
Because of the lack of documentation or examples, it took me some time to find out the specific package/function to convert unstructured to a typed object—which is why this post aims to document it for others (or at least my future self).
The short anwer to converting
unstructured.Unstructure to a typed resource
is to use the
runtime.UnstructuredConverter interface. Generally, the
runtime.DefaultUnstructuredConverter implementation suffices for almost all
A full example which interacts with the Cluster CRD from Cluster API: