/
client.go
96 lines (79 loc) · 2.89 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// 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"
)
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)
}
}
}