cmd/example/client.go (61 lines of code) (raw):

// Copyright Istio Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // This example is adapted from: // https://github.com/aspenmesh/istio-client-go/blob/4de6e89009c427dbc602b0c6bbdc8840ef1905e6/cmd/example-client/client.go package main import ( "context" "log" "os" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/tools/clientcmd" versionedclient "istio.io/client-go/pkg/clientset/versioned" _ "istio.io/api/alibabacloud/v1beta1" ) func main() { kubeconfig := os.Getenv("KUBECONFIG") namespace := os.Getenv("NAMESPACE") if len(kubeconfig) == 0 || len(namespace) == 0 { log.Fatalf("Environment variables KUBECONFIG and NAMESPACE need to be set") } restConfig, err := clientcmd.BuildConfigFromFlags("", kubeconfig) if err != nil { log.Fatalf("Failed to create k8s rest client: %s", err) } ic, err := versionedclient.NewForConfig(restConfig) if err != nil { log.Fatalf("Failed to create istio client: %s", err) } // Test VirtualServices vsList, err := ic.NetworkingV1alpha3().VirtualServices(namespace).List(context.TODO(), metav1.ListOptions{}) if err != nil { log.Fatalf("Failed to get VirtualService in %s namespace: %s", namespace, err) } for i := range vsList.Items { vs := vsList.Items[i] log.Printf("Index: %d VirtualService Hosts: %+v\n", i, vs.Spec.GetHosts()) } // Test DestinationRules drList, err := ic.NetworkingV1alpha3().DestinationRules(namespace).List(context.TODO(), metav1.ListOptions{}) if err != nil { log.Fatalf("Failed to get DestinationRule in %s namespace: %s", namespace, err) } for i := range drList.Items { dr := drList.Items[i] log.Printf("Index: %d DestinationRule Host: %+v\n", i, dr.Spec.GetHost()) } // Test Gateway gwList, err := ic.NetworkingV1alpha3().Gateways(namespace).List(context.TODO(), metav1.ListOptions{}) if err != nil { log.Fatalf("Failed to get Gateway in %s namespace: %s", namespace, err) } for i := range gwList.Items { gw := gwList.Items[i] for _, s := range gw.Spec.GetServers() { log.Printf("Index: %d Gateway servers: %+v\n", i, s) } } // Test ServiceEntry seList, err := ic.NetworkingV1alpha3().ServiceEntries(namespace).List(context.TODO(), metav1.ListOptions{}) if err != nil { log.Fatalf("Failed to get ServiceEntry in %s namespace: %s", namespace, err) } for i := range seList.Items { se := seList.Items[i] for _, h := range se.Spec.GetHosts() { log.Printf("Index: %d ServiceEntry hosts: %+v\n", i, h) } } }