How to return fake pods using the fake for kubeclient?

3/31/2017

I'm trying to create a unittest for some code which uses the Kubernetes go-client. My unittest uses the Kubernetes fake to mock out Kubernetes. My unittest tries to create some fake pods to be returned by calls to Pods.List.

What's the proper way to create fake pods?

I first tried calling Pods.Create on the fake e.g

_, err := j.ClientSet.CoreV1().Pods(NAMESPACE).Create(
    &v1.Pod{
        TypeMeta: meta_v1.TypeMeta{
            Kind:       "Pod",
            APIVersion: "v1",
        },
        ObjectMeta: v1.ObjectMeta{
            Name: j.masterName(),
            Labels: map[string]string{
                "tag":   "",
            },
        },
    })

l, err := j.ClientSet.CoreV1().Pods(NAMESPACE).List(v1.ListOptions{})

if len(l.Items) == 0 {
    t.Fatalf("No pods were returned by list.")
}

However, the call to list pods didn't return the pod.

I then tried creating the pod when creating the fake; e.g.

j.ClientSet = fake.NewSimpleClientset(&v1.Pod{
    TypeMeta: meta_v1.TypeMeta{
        Kind: "pod",
    },
    ObjectMeta: v1.ObjectMeta{
        Name: j.masterName(),
        Labels: map[string]string{
            "tag":   "",
        },
    },
})

With this code NewSimpleClientSet failed with a panic

panic: no matches for kind /, Kind=Pod [recovered]
        panic: no matches for kind /, Kind=Pod
-- Jeremy Lewi
kubernetes

2 Answers

5/17/2017

It turned out in my case I was using a very old version of the Kubernetes client and fake. When I updated to the latest code it worked.

-- Jeremy Lewi
Source: StackOverflow

5/15/2017

Do you fully implement the interface of ClientSet? It seems that something about resource kind you have not handled.

-- luke
Source: StackOverflow