kubernetes-client icon indicating copy to clipboard operation
kubernetes-client copied to clipboard

question: use kubectl with KubernetesServer mock

Open palmerabollo opened this issue 6 years ago • 15 comments

Is it possible to start a KubernetesServer mock this way:

  import io.fabric8.kubernetes.client.server.mock.KubernetesServer;

  KubernetesServer server = new KubernetesServer(true, true);

and then use a standard kubectl client to make requests? Thank you for you help.

palmerabollo avatar Jun 23 '19 17:06 palmerabollo

This issue has been automatically marked as stale because it has not had any activity since 90 days. It will be closed if no further activity occurs within 7 days. Thank you for your contributions!

stale[bot] avatar Sep 21 '19 17:09 stale[bot]

Not stale.

palmerabollo avatar Sep 23 '19 08:09 palmerabollo

This issue has been automatically marked as stale because it has not had any activity since 90 days. It will be closed if no further activity occurs within 7 days. Thank you for your contributions!

stale[bot] avatar Dec 22 '19 08:12 stale[bot]

@palmerabollo : have you figured this out yet? I think you're right you can use it like done here: https://github.com/fabric8io/kubernetes-client/blob/38dc6e3237e9c94f2b143ff781962f3e57cdfe0e/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PodCrudTest.java#L37

rohanKanojia avatar Dec 22 '19 16:12 rohanKanojia

@rohanKanojia I wasn't able to make it work back in June, but I'll double-check it as soon as I can (after Xmas) with the example you provide. Thanks for your help.

palmerabollo avatar Dec 22 '19 17:12 palmerabollo

I see there is a lot of potential in this feature and that it can be very useful if MockServer mocks could be configured by using JSON/YAML files and distributed as a binary. This way, the MockServer could be used for different technologies (JavaScript, Go, Python...)

For the scope of this issue it would be nice to actually test if running the server standalone is possible and creating a documented quick start/example on how to achieve this to test a couple of kubectl commands. If the community is interested we could work on the exposed additional features in the future.

manusa avatar May 05 '20 05:05 manusa

@rohanKanojia I've tried it. The code below works. However, I'm still not able to access the server using kubectl or any other k8s client because afaik it's not possible to get a valid kubeconfig.

As @manusa pointed out, I wanted to access the k8s mock server from client that uses a different technology.

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

import java.io.IOException;

public class KubernetesMock {
    public static void main(String[] args) throws IOException {
        KubernetesServer server = new KubernetesServer(true, true);
        server.before(); // mock init
        KubernetesClient client = server.getClient();

        Pod pod = new PodBuilder().withNewMetadata().withName("pod").endMetadata().build();
        client.pods().inNamespace("ns1").create(pod);

        PodList podList = client.pods().inAnyNamespace().list();
        System.out.println(podList.getItems().size()); // 1
    }
}

palmerabollo avatar May 05 '20 22:05 palmerabollo

@palmerabollo I was able to access the mockserver using kubectl...you have to configure the kubectl for that..

https://github.com/fabric8io/kubernetes-client/compare/master...dev-gaur:kubectl_with_mockserver?expand=1

^ I made some minor changes for mockserver to listen on https://localhost:6443 and shutdown on a Ctrl + C by the user on the console.. you can further parameterize the host and port if you want.

on kubectl side

kubectl config set-cluster mockserver --server=https://localhost:6443 --insecure-skip-tls-verify=true

// you can use any namespace and existing user on your kubeconfig 
kubectl config set-context mock --namespace=test --user=minikube --cluster=mockserver

kubectl config use-context mock

However there's a mismatch in expected responses by kubectl and response returned by mockserver, since kubectl does a lot more than being a client

devang-gaur avatar May 06 '20 08:05 devang-gaur

With the rise of Test Containers with support for multiple programming languages, and many other alternatives, is this issue still relevant?

manusa avatar Mar 05 '24 14:03 manusa

@manusa The issue is that some apps interact with kubernetes apis (api server, etc). So a k8s mock would come in handy to develop unit/component tests without deploying a real k8s cluster. For example, we use k8s locks in a fabric8 app and we find it quite hard to unit test it.

palmerabollo avatar Mar 11 '24 21:03 palmerabollo

This issue has been automatically marked as stale because it has not had any activity since 90 days. It will be closed if no further activity occurs within 7 days. Thank you for your contributions!

stale[bot] avatar Jun 13 '24 02:06 stale[bot]

@rohanKanojia is experimenting with this for a new feature on JKube.

Please, @rohanKanojia provide feedback in this issue too with your findings.

manusa avatar Jun 14 '24 17:06 manusa

I managed to get kubectl working with Fabric8 Kubernetes Mock Server . However, I had to add expectations for Kubernetes Aggregated Discovery endpoints manually. I've created a very simple example for kubectl get pods equivalent here .

For now, I see these problems:

  • Kubernetes Mock Server doesn't have support for Kubernetes Aggregated Discovery endpoints (/api and /apis). kubectl requests these endpoints in order to get Kubernetes resource metadata for doing any request. We should add some opinionated response for these endpoints
  • We need to tune the opinionated KubernetesClient config better so that it exports a valid kubeconfig. Currently, we if I try to export kubeconfig from kubernetesClient.getConfiguration() it creates an invalid kubeconfig with empty user. I had to manually set a dummy token to make it work with kubectl.
  • Kubernetes Mock Server is using HTTP/1.1 and kubectl is using HTTP2 which logs a warning while running tests

rohanKanojia avatar Jun 18 '24 12:06 rohanKanojia

This issue has been automatically marked as stale because it has not had any activity since 90 days. It will be closed if no further activity occurs within 7 days. Thank you for your contributions!

stale[bot] avatar Sep 17 '24 02:09 stale[bot]

For now, I see these problems:

I think only the "Kubernetes Aggregated Discovery endpoints" point remains as a problem now, which will be covered by #6220

Once we finish with that maybe we can create some docs explaining how this could be used in a standalone fashion.

manusa avatar Sep 17 '24 04:09 manusa