# istio-client-java **Repository Path**: july_young/istio-client-java ## Basic Information - **Project Name**: istio-client-java - **Description**: istio-client是继承了 `fabric8-kubernetes-client` 的SDK包,包含对istio最基本的四种资源对象(Virtualservice,DestinationRule,Gateway,ServiceEntry)的链式拼装,暴露fabric一致的操作方法. - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-03-18 - **Last Updated**: 2023-03-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # istio-client istio-client是继承了 `fabric8-kubernetes-client` 的SDK包,包含对istio最基本的四种资源对象(Virtualservice,DestinationRule,Gateway,ServiceEntry)的链式拼装,暴露fabric一致的操作方法. 实现方式 [Java操作Istio(kubernetes-client)](https://blog.csdn.net/qq_34657543/article/details/106238261) --- ### 直接使用 如果不需要其他包装,可以直接使用IstioClient进行资源操作,操作方法如下 1. 基本操作 ``` final ConfigBuilder builder = new ConfigBuilder(); builder.withMasterUrl("https://localhost:6443") /*TODO 如果client上有server的证书,就不需要配置trustCert了*/ .withTrustCerts(true) .withUsername("admin") .withPassword("admin"); IstioClient istioClient = new IstioClient(builder.build()) VirtualService vs = istioClient.istios().virtualServices().inNamespace("bookinfo").withName("productpage").get(); ``` 2. 对象拼装 ``` VirtualService a = new VirtualServiceBuilder() .withNewMetadata() .withName("bookinfo-vs") .withNamespace("bookinfo") .endMetadata() .withNewSpec() .addNewGateway("gateway") .addNewHost("test-host") .addNewHttps() .addNewRoutes() .withWeight(100) .withNewDestination() .withHost("testhost") .withSubset("v1") .withNewPort() .setNumber(8080) .endPort() .endDestination() .withWeight(100) .endRoute() .addNewRoutes() .withWeight(90) .endRoute() .addNewMatch() .withNewMethods() .withExact("xxxx") .endMethod() .addNewHeader("tom") .withExact("xxxxxxx") .endHeader() .addNewHeader("jak") .withExact("aaaaa") .endHeader() .endMatch() .endHttp() .endSpec() .build(); ``` ### 集成使用 如果需要集成集群操作,需要您自己完成以下操作 1. 继承KubernetesClient对象自定义自己的client端,并在其中引入您自定义的对象操作,并额外引入IstiosAPIGroupDSL对象集成istio的操作器 2. 实现定义的client接口,在构造器中定义以下内容,将操作适配器集成到客户端中 ``` super(build); IstioAPIGroupExtensionAdapter istioAPIGroupExtensionAdapter = new IstioAPIGroupExtensionAdapter(); Adapters.register(istioAPIGroupExtensionAdapter); ``` 3. 对外提供您定义的客户端方法即可. ### k8s的config文件 ```yaml apiVersion: v1 clusters: - cluster: certificate-authority-data: server: name: contexts: - context: cluster: user: name: current-context: kind: Config preferences: {} users: - name: user: client-certificate-data: client-key-data: ``` ``` //对应上面的字段 Config config = new ConfigBuilder().withMasterUrl(server) .withCaCertData(certificateAuthorityData) .withClientCertData(clientCertificateData) .withClientKeyData(clientKeyData) .build(); config.setApiVersion("networking.istio.io/v1alpha3"); IstioClient istioClient = new IstioClient(config); ``` 备注:这个项目是来自于github的一个项目,地址:https://github.com/llarao/istio-client-java。 它原来项目有一些错误,跑不起来,我自己在原来的项目上做了一下改造,一般简单用用足够了。 最后感谢一下这位老哥~