How to connect to Istio api from kubernetes java client?

5/21/2019

Kubernetes java client has the sdk functions to create deployment, services and many other core kubernetes function. How can I access custom resources like that of istio's service entry, destination rules, virtual services from kubernetes java client?

-- Malathi
istio
kubernetes
minikube

1 Answer

5/22/2019

To connect to Istio you can use the project istio-java-api. This project use the same approach as Fabric8’s kubernetes-model. The example below shows how to build and create a VirtualService:

import me.snowdrop.istio.api.networking.v1alpha3.ExactMatchType;
import me.snowdrop.istio.api.networking.v1alpha3.VirtualService;
import me.snowdrop.istio.api.networking.v1alpha3.VirtualServiceBuilder;
import me.snowdrop.istio.client.DefaultIstioClient;
import me.snowdrop.istio.client.IstioClient;


Config config = new ConfigBuilder().withMasterUrl(masterURL).build();
IstioClient istioClient = new DefaultIstioClient(config);


VirtualService virtualService = new VirtualServiceBuilder()
        .withApiVersion("networking.istio.io/v1alpha3")
        .withNewMetadata()
        .withName("details")
        .endMetadata()
        .withNewSpec()
        .withHosts("*")
        .withGateways("system-gateway")
        .addNewHttp()
        .addNewRoute()
        .withNewDestination()
      .withHost("service-example")
        .withNewPort()
        .withNewNumberPort(9900)
        .endPort()
 .endDestination()
        .endRoute()
        .endHttp()
        .endSpec()
        .build();
        
 istioClient.virtualService().create(virtualService);
-- Zeina
Source: StackOverflow