How to create Ingress Controller in kubernetes-client-java

6/1/2020

I am developing an application where I have to spin a pod on the user demand and that specific pod should be exposed to the user using Ingress. To create a pod and service on the fly I am leveraging Kubernetes-client for java.

InputStream is = this.getClass().getClassLoader().getResourceAsStream("config");
InputStreamReader isr = new InputStreamReader(is);

ApiClient client = ClientBuilder.kubeconfig(io.kubernetes.client.util.KubeConfig.loadKubeConfig(isr)).build();
        
Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();

Creating Service:

V1Service createResult = api.createNamespacedService("default", svc, null, null, null);

Creating Pod:

V1Pod podCreateResult =  api.createNamespacedPod("default", pod, null, null, null);

But I am not able to find any method to create Ingress Controller.

-- doc_sportello
java
kubernetes
kubernetes-ingress

1 Answer

8/12/2020

To create ingress in namespace we have to use NetworkingV1beta1Api provided by Kubernetes java client library.

InputStream is = this.getClass().getClassLoader().getResourceAsStream("config");
InputStreamReader isr = new InputStreamReader(is);

ApiClient client = ClientBuilder.kubeconfig(io.kubernetes.client.util.KubeConfig.loadKubeConfig(isr)).build();

Configuration.setDefaultApiClient(client);
NetworkingV1beta1Api networkingV1beta1Api = new NetworkingV1beta1Api ();


NetworkingV1beta1Ingress ingress = new NetworkingV1beta1IngressBuilder()
		.withNewMetadata()
		.withName("flask-ingress")
		.endMetadata()
		.withNewSpec().withNewBackend()
		.withNewServiceName("flask-app-backend-service")
		.withNewServicePort(9001).endBackend().endSpec().build();
    

ApiResponse ingressCreateResult = networkingV1beta1Api.createNamespacedIngressWithHttpInfo("default", ingress, null, null, null);
-- doc_sportello
Source: StackOverflow