Istio Java SDK API-リソースアクセス-VirtualServices/Gateway/DestinationRule/ServiceEntry

11866 ワード

環境
前の文章を参考にする
 
JavaがIstioに接続する方法
前の文章を参考にする
 
Isitoリソースへのアクセス
  • VirtualService
  • Gateway
  • DestinationRule
  • ServiceEntry

  •  
    プロジェクトソース
    package com.you.micro.istiodemo.test;
    
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import me.snowdrop.istio.api.networking.v1alpha3.*;
    import me.snowdrop.istio.client.IstioClient;
    
    public class QueryIstio {
    
        public static final String ISTIO_NAMESPACE = "istio-system";
    
        public static void main(String[] args) {
            ConnectIstio connectIstio = new ConnectIstio();
            IstioClient istioClient = connectIstio.getIstioClient();
    
            QueryIstio queryIstio = new QueryIstio();
            queryIstio.queryServiceEntryList(istioClient);
            queryIstio.queryDestinationRuleList(istioClient);
            queryIstio.queryVirtualServiceList(istioClient);
            queryIstio.queryGateway(istioClient);
    
            connectIstio.closeIstioClient(istioClient);
    
        }
    
        public void queryVirtualServiceList(IstioClient istioClient) {
    
            ObjectMapper mapper = new ObjectMapper();
            if (istioClient == null) {
                return;
            }
            VirtualServiceList virtualServiceList = istioClient.virtualService().inNamespace(ISTIO_NAMESPACE).list();
    
            if (virtualServiceList == null || virtualServiceList.getItems() == null || virtualServiceList.getItems().size() == 0) {
                return;
            }
            for (int i = 0; i < virtualServiceList.getItems().size(); i++) {
                VirtualService virtualService = virtualServiceList.getItems().get(i);
                try {
                    System.out.println(mapper.writeValueAsString(virtualService));
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }
    
        }
    
        public void queryGateway(IstioClient istioClient) {
    
            ObjectMapper mapper = new ObjectMapper();
            if (istioClient == null) {
                return;
            }
            Gateway gateway = istioClient.gateway().inNamespace(ISTIO_NAMESPACE).withName("ingressgateway").get();
    
            if (gateway != null) {
                try {
                    System.out.println(mapper.writeValueAsString(gateway));
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }
    
            Gateway nullGateway = istioClient.gateway().inNamespace(ISTIO_NAMESPACE).withName("none").get();
            if (nullGateway == null) {
                System.out.println("Gateway is null");
            }
    
        }
    
        public void queryDestinationRuleList(IstioClient istioClient) {
            if (istioClient == null) {
                return;
            }
            DestinationRuleList ruleList = istioClient.destinationRule().inNamespace(ISTIO_NAMESPACE).list();
            if (ruleList == null || ruleList.getItems() == null || ruleList.getItems().size() == 0) {
                return;
            }
            ObjectMapper mapper = new ObjectMapper();
            for (int i = 0; i < ruleList.getItems().size(); i++) {
                DestinationRule destinationRule = ruleList.getItems().get(i);
                try {
                    System.out.println(mapper.writeValueAsString(destinationRule));
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public void queryServiceEntryList(IstioClient istioClient) {
            if (istioClient == null) {
                return;
            }
    //        ServiceEntryList entryList = istioClient.serviceEntry().inAnyNamespace().list();
            ServiceEntryList entryList = istioClient.serviceEntry().inNamespace("default").list();
            if (entryList == null || entryList.getItems() == null || entryList.getItems().size() == 0) {
                return;
            }
            ObjectMapper mapper = new ObjectMapper();
            try {
                System.out.println(mapper.writeValueAsString(entryList.getItems().get(0)));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    
    }
    
    ServiceEntry-Json
    {
    	"apiVersion": "networking.istio.io/v1alpha3",
    	"kind": "ServiceEntry",
    	"metadata": {
    		"annotations": {
    			"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"networking.istio.io/v1alpha3\",\"kind\":\"ServiceEntry\",\"metadata\":{\"annotations\":{},\"name\":\"valid-service-entry\",\"namespace\":\"default\"},\"spec\":{\"endpoints\":[{\"address\":\"test.istio-system.svc.cluster.local\",\"ports\":{\"http\":8080}}],\"hosts\":[\"zh.bookinfo.com\"],\"ports\":[{\"name\":\"http\",\"number\":80,\"protocol\":\"HTTP\"}],\"resolution\":\"DNS\"}}
    " }, "creationTimestamp": "2020-05-28T08:19:53Z", "generation": 1, "name": "valid-service-entry", "namespace": "default", "resourceVersion": "9848171", "selfLink": "/apis/networking.istio.io/v1alpha3/namespaces/default/serviceentries/valid-service-entry", "uid": "a27a4454-696f-4514-b44f-e71b34fe9ca5" }, "spec": { "endpoints": [{ "address": "test.istio-system.svc.cluster.local", "ports": { "http": 8080 } }], "hosts": ["zh.bookinfo.com"], "ports": [{ "name": "http", "number": 80, "protocol": "HTTP" }], "resolution": "DNS" } }
    DestinationRule-Json
    {
    	"apiVersion": "networking.istio.io/v1alpha3",
    	"kind": "DestinationRule",
    	"metadata": {
    		"annotations": {
    			"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"networking.istio.io/v1alpha3\",\"kind\":\"DestinationRule\",\"metadata\":{\"annotations\":{},\"labels\":{\"app\":\"istio-telemetry\",\"release\":\"istio\"},\"name\":\"istio-telemetry\",\"namespace\":\"istio-system\"},\"spec\":{\"host\":\"istio-telemetry.istio-system.svc.cluster.local\",\"trafficPolicy\":{\"connectionPool\":{\"http\":{\"http2MaxRequests\":10000,\"maxRequestsPerConnection\":10000}},\"portLevelSettings\":[{\"port\":{\"number\":15004},\"tls\":{\"mode\":\"ISTIO_MUTUAL\"}},{\"port\":{\"number\":9091},\"tls\":{\"mode\":\"DISABLE\"}}]}}}
    " }, "creationTimestamp": "2020-04-19T22:38:38Z", "generation": 1, "labels": { "app": "istio-telemetry", "release": "istio" }, "name": "istio-telemetry", "namespace": "istio-system", "resourceVersion": "3088834", "selfLink": "/apis/networking.istio.io/v1alpha3/namespaces/istio-system/destinationrules/istio-telemetry", "uid": "6a3016c5-34db-4019-b166-d658f0f732c4" }, "spec": { "host": "istio-telemetry.istio-system.svc.cluster.local", "trafficPolicy": { "connectionPool": { "http": { "http2MaxRequests": 10000, "maxRequestsPerConnection": 10000 } }, "portLevelSettings": [{ "port": { "number": 15004 }, "tls": { "mode": "ISTIO_MUTUAL" } }, { "port": { "number": 9091 }, "tls": { "mode": "DISABLE" } }] } } }
    VirtualService-Json
    {
    	"apiVersion": "networking.istio.io/v1alpha3",
    	"kind": "VirtualService",
    	"metadata": {
    		"annotations": {
    			"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"networking.istio.io/v1alpha3\",\"kind\":\"VirtualService\",\"metadata\":{\"annotations\":{},\"labels\":{\"app\":\"istio-egressgateway\",\"release\":\"istio\"},\"name\":\"istio-multicluster-egressgateway\",\"namespace\":\"istio-system\"},\"spec\":{\"gateways\":[\"istio-multicluster-egressgateway\"],\"hosts\":[\"*.global\"],\"tls\":[{\"match\":[{\"port\":15443,\"sniHosts\":[\"*.global\"]}],\"route\":[{\"destination\":{\"host\":\"non.existent.cluster\",\"port\":{\"number\":15443}},\"weight\":100}]}]}}
    " }, "creationTimestamp": "2020-04-19T22:37:53Z", "generation": 1, "labels": { "app": "istio-egressgateway", "release": "istio" }, "name": "istio-multicluster-egressgateway", "namespace": "istio-system", "resourceVersion": "3088352", "selfLink": "/apis/networking.istio.io/v1alpha3/namespaces/istio-system/virtualservices/istio-multicluster-egressgateway", "uid": "5d4c01ff-2bdd-4ca7-ad2c-baa4b6c029e2" }, "spec": { "gateways": ["istio-multicluster-egressgateway"], "hosts": ["*.global"], "tls": [{ "match": [{ "port": 15443, "sniHosts": ["*.global"] }], "route": [{ "destination": { "host": "non.existent.cluster", "port": { "number": 15443 } }, "weight": 100 }] }] } }

    Gateway-Json
    {
    	"apiVersion": "networking.istio.io/v1alpha3",
    	"kind": "Gateway",
    	"metadata": {
    		"annotations": {
    			"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"networking.istio.io/v1alpha3\",\"kind\":\"Gateway\",\"metadata\":{\"annotations\":{},\"labels\":{\"release\":\"istio\"},\"name\":\"ingressgateway\",\"namespace\":\"istio-system\"},\"spec\":{\"selector\":{\"istio\":\"ingressgateway\"},\"servers\":[{\"hosts\":[\"*\"],\"port\":{\"name\":\"http\",\"number\":80,\"protocol\":\"HTTP\"}}]}}
    " }, "creationTimestamp": "2020-04-19T22:38:06Z", "generation": 1, "labels": { "release": "istio" }, "name": "ingressgateway", "namespace": "istio-system", "resourceVersion": "3088470", "selfLink": "/apis/networking.istio.io/v1alpha3/namespaces/istio-system/gateways/ingressgateway", "uid": "8baedf93-fb2e-4df9-a8f8-3326c9102593" }, "spec": { "selector": { "istio": "ingressgateway" }, "servers": [{ "hosts": ["*"], "port": { "name": "http", "number": 80, "protocol": "HTTP" } }] } }