Reproducing error from Kubernetes while list / get objects

9/16/2021

In a Kubernetes operator code, I have a snippet that gets/lists obj for the given object key from the Kubernetes Cluster

if err := sdk.List(context.TODO(), listObj, listOpts...); err != nil {
	e := fmt.Errorf("failed to list [%s] due to [%s]", listObj.GetObjectKind().GroupVersionKind().Kind, err.Error())
	logger.Error(e, e.Error(), "name", drd.Name, "namespace", drd.Namespace)

The get and list are part of the reader interface

package client
...
type Reader interface {
	// Get retrieves an obj for the given object key from the Kubernetes Cluster.
	// obj must be a struct pointer so that obj can be updated with the response
	// returned by the Server.
	Get(ctx context.Context, key ObjectKey, obj Object) error

	// List retrieves list of objects for a given namespace and list options. On a
	// successful call, Items field in the list will be populated with the
	// result returned from the server.
	List(ctx context.Context, list ObjectList, opts ...ListOption) error
}

I am wondering how do I test the use case where this method throws an error. I am using minikube to run this, but I am not able to test this scenario. Any pointers would be helpful.

-- CSUNNY
kubernetes
operators

0 Answers