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

question: use kubectl with KubernetesServer mock #1601

Open
palmerabollo opened this issue Jun 23, 2019 · 15 comments
Open

question: use kubectl with KubernetesServer mock #1601

palmerabollo opened this issue Jun 23, 2019 · 15 comments

Comments

@palmerabollo
Copy link

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
Copy link

stale bot commented Sep 21, 2019

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 stale bot added the status/stale label Sep 21, 2019
@palmerabollo
Copy link
Author

Not stale.

@stale stale bot removed the status/stale label Sep 23, 2019
@stale
Copy link

stale bot commented Dec 22, 2019

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 stale bot added the status/stale label Dec 22, 2019
@rohanKanojia
Copy link
Member

@palmerabollo : have you figured this out yet? I think you're right you can use it like done here:

public KubernetesServer server = new KubernetesServer(true, true);

@palmerabollo
Copy link
Author

@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
Copy link
Member

manusa commented May 5, 2020

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.

@devang-gaur devang-gaur self-assigned this May 5, 2020
@palmerabollo
Copy link
Author

@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
Copy link
Contributor

@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
Copy link
Member

manusa commented Mar 5, 2024

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

@palmerabollo
Copy link
Author

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

Copy link

stale bot commented Jun 13, 2024

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 stale bot added the status/stale label Jun 13, 2024
@manusa
Copy link
Member

manusa commented Jun 14, 2024

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

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

@stale stale bot removed the status/stale label Jun 14, 2024
@rohanKanojia
Copy link
Member

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

Copy link

stale bot commented Sep 17, 2024

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 stale bot added the status/stale label Sep 17, 2024
@manusa
Copy link
Member

manusa commented Sep 17, 2024

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.

@stale stale bot removed the status/stale label Sep 17, 2024
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

4 participants