fabric8io / kubernetes-client

Java client for Kubernetes & OpenShift
http://fabric8.io
Apache License 2.0
3.41k stars 1.46k forks source link

question: use kubectl with KubernetesServer mock #1601

Open palmerabollo opened 5 years ago

palmerabollo commented 5 years ago

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.

stale[bot] commented 5 years ago

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!

palmerabollo commented 5 years ago

Not stale.

stale[bot] commented 4 years ago

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!

rohanKanojia commented 4 years ago

@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

palmerabollo commented 4 years ago

@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.

manusa commented 4 years ago

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.

palmerabollo commented 4 years ago

@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
    }
}
devang-gaur commented 4 years ago

@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

manusa commented 8 months ago

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

palmerabollo commented 8 months ago

@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.

stale[bot] commented 4 months ago

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!

manusa commented 4 months ago

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

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

rohanKanojia commented 4 months ago

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:

stale[bot] commented 1 month ago

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!

manusa commented 1 month ago

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.