gokubernetesoperator-sdk

Generic client.Get for custom Kubernetes GO operator


In a custom Kubernetes operator implemented with the operator-sdk in golang is it possible to call the custom API directly and retrieve the object as YAML?

For example. I have a custom resource

apiVersion: test.com/v1alpha1
kind: TEST
metadata::
  name: example-test
spec:
  replicas: 3
  randomname: value

I don't know ahead of time what the fields in the spec are going to be apart from replicas. So I am not able to create a go type that includes structs to hold the entries.

So rather than doing:

instance := &testv1alpha1.Test{}
err := r.client.Get(context.TODO(), nameSpaceName, instance)

I want to be able to do something like:

instanceYAML := genericContainer{}
err := r.client.GetGeneric(context.TODO(), nameSpaceName, instance)

and then parse the instanceYAML to check the entries.


Solution

  • This is called the "unstructured" client. The docs are pretty light so I recommend looking over the tests as examples https://github.com/kubernetes-sigs/controller-runtime/blob/ea32729106c995d9df310ac4731c2061490addfb/pkg/client/client_test.go#L1536-L1566