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
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.
Do you fully implement the interface of ClientSet
? It seems that something about resource kind you have not handled.