Open palmerabollo opened 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!
Not stale.
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 : 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 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.
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.
@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 I was able to access the mockserver using kubectl...you have to configure the kubectl for that..
^ 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
With the rise of Test Containers with support for multiple programming languages, and many other alternatives, is this issue still relevant?
@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.
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 is experimenting with this for a new feature on JKube.
Please, @rohanKanojia provide feedback in this issue too with your findings.
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:
/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 endpointskubeconfig
. 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.kubectl
is using HTTP2 which logs a warning while running testsThis 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!
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.
Is it possible to start a
KubernetesServer
mock this way:and then use a standard
kubectl
client to make requests? Thank you for you help.