Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mocking kubernetes: namespace not set when creating resources #3194

Closed
lburgazzoli opened this issue May 31, 2021 · 4 comments
Closed

mocking kubernetes: namespace not set when creating resources #3194

lburgazzoli opened this issue May 31, 2021 · 4 comments
Assignees

Comments

@lburgazzoli
Copy link
Contributor

I've created the following basic application based on one of the examples from the k8s client README:

///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.slf4j:slf4j-simple:1.7.30
//DEPS io.fabric8:kubernetes-server-mock:5.4.0
//DEPS io.fabric8:kubernetes-server-mock:5.4.0

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;

class kc {
    public static void main(String... args) throws Exception {
        KubernetesServer server = new KubernetesServer(false, true);
        server.before();

        try (KubernetesClient kc = server.getClient()) {
            Pod aPod = new PodBuilder().withNewMetadata().withName("demo-pod1").endMetadata()
                .withNewSpec()
                .addNewContainer()
                .withName("nginx")
                .withImage("nginx:1.7.9")
                .addNewPort().withContainerPort(80).endPort()
                .endContainer()
                .endSpec()
                .build();
                
            Pod createdPod = kc.pods().inNamespace("my-ns").create(aPod);
            System.out.println("n  : " + createdPod.getMetadata().getName());
            System.out.println("ns : " + createdPod.getMetadata().getNamespace());
        } finally {
            server.after();
        }
    }
}

When this test runs, it prints:

May 31, 2021 3:24:55 PM okhttp3.mockwebserver.MockWebServer$2 execute
INFO: MockWebServer[51475] starting to accept connections
May 31, 2021 3:24:55 PM okhttp3.mockwebserver.MockWebServer$3 processOneRequest
INFO: MockWebServer[51475] received request: POST /api/v1/namespaces/my-ns/pods HTTP/1.1 and responded: HTTP/1.1 200 OK
n  : demo-pod1
ns : null

As you note, the ns is null instead of the expected my-ns.
When executing the same code against a real k8s cluster, the code works as expected.

@shawkins
Copy link
Contributor

This is addressed as part of #3156

There was an earlier attempt at addressing this, but it relied upon fabric8io/mockwebserver#59 - which may not be available anytime soon.

@lburgazzoli
Copy link
Contributor Author

@shawkins for my understanding, why fabric8io/mockwebserver#59 may not be available anytime soon ?

I'm rely upon the CRUD functionalities for testing operators based on the java operator sdk and CRD, makes things a lot easier that writing expectations.

@shawkins
Copy link
Contributor

may not be available anytime soon ?

It requires a new release of the mockwebserver, which doesn't appear like it will happen anytime soon.

I'm rely upon the CRUD functionalities for testing operators based on the java operator sdk and CRD, makes things a lot easier that writing expectations.

#3156 should be picked up as part of 5.5

shawkins added a commit to shawkins/kubernetes-client that referenced this issue Jun 2, 2021
…d aware

also working around making the namespace and kind available from the
path
shawkins added a commit to shawkins/kubernetes-client that referenced this issue Jun 7, 2021
…d aware

also working around making the namespace and kind available from the
path
manusa pushed a commit that referenced this issue Jun 8, 2021
also working around making the namespace and kind available from the
path
@shawkins
Copy link
Contributor

shawkins commented Jun 8, 2021

Fixed in #3156

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants