Search code examples
testingkubernetesdevopskubernetes-operatorkubernetes-networking

No access to k8s pod by internal IP from the envtest


I wrote an integration test for my Kubernetes operator using envtest and local Minikube cluster.

The controller I'm testing is making an http request to one of the pods by IP address. The address i get like this:

endpoints := &corev1.Endpoints{}
client.Get(context.TODO(), types.NamespacedName{Namespace: "ns", Name: "cluster"}, endpoints)
url = fmt.Sprintf("%s:%s", endpoints.Subsets[0].Addresses[0].IP, "8081")

This code works if i deploy operator to Kubernetes cluster.

But when running the test I get the error:

Post \"http://172.17.0.3:8081": dial tcp 172.17.0.3:8081: connect: no route to host

This is due to the fact that earlier the http call was made from the pod with the operator, but now from the envtest?

How to write tests for controllers correctly?


Solution

  • My workaround:

    I did port forwarding to target pod using this example.

    To make the code go to localhost I'll monkeypatch it a bit.