forked from kyma-project/kyma
/
istio.go
129 lines (108 loc) · 3.08 KB
/
istio.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package istio
import (
"bufio"
"io"
"os"
"strings"
"testing"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
)
func podExec(cfg *restclient.Config, namespace, name, container string, command []string, output io.Writer) error {
k8sClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
return err
}
req := k8sClient.CoreV1().RESTClient().
Post().
Resource("pods").
Namespace(namespace).
Name(name).
SubResource("exec")
req.VersionedParams(&v1.PodExecOptions{
Container: container,
Command: command,
Stdout: true,
}, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(cfg, "POST", req.URL())
if err != nil {
return err
}
err = exec.Stream(remotecommand.StreamOptions{
Stdout: output,
})
return err
}
func filterLines(input io.Reader, expectedString string) {
scanner := bufio.NewScanner(input)
for scanner.Scan() {
if strings.Contains(scanner.Text(), expectedString) {
fmt.Println(scanner.Text())
}
}
}
func DumpConfig(t *testing.T, host, labelSelector string) {
podName := os.Getenv("POD_NAME")
cfg, err := restclient.InClusterConfig()
require.NoError(t, err)
k8sClient, err := kubernetes.NewForConfig(cfg)
require.NoError(t, err)
podList, err := k8sClient.CoreV1().Pods(v1.NamespaceAll).List(metav1.ListOptions{
LabelSelector: labelSelector,
})
require.NoError(t, err)
assert.NotEmpty(t, podList.Items)
var brokerPodName string
var brokerNamespace string
for _, pod := range podList.Items {
fmt.Printf("The tested broker %s/%s IP=%s\n", pod.Namespace, pod.Name, pod.Status.PodIP)
brokerPodName = pod.Name
brokerNamespace = pod.Namespace
}
dumpIstioClustersConfig(t, cfg, podName, host)
dumpEnvoyRBACConfig(t, cfg, brokerNamespace, brokerPodName)
}
func dumpIstioClustersConfig(t *testing.T, cfg *restclient.Config, podName, host string) {
r, w := io.Pipe()
go func() {
defer w.Close()
err := podExec(cfg, "kyma-system", podName, "istio-proxy", []string{"curl", "http://localhost:15000/clusters", "-s"}, w)
assert.NoError(t, err)
}()
fmt.Printf("Istio-proxy clusters config for the host %s:\n", host)
filterLines(r, host)
fmt.Println()
}
func dumpEnvoyRBACConfig(t *testing.T, cfg *restclient.Config, namespace, name string) {
r, w := io.Pipe()
go func() {
defer w.Close()
err := podExec(cfg, namespace, name, "istio-proxy", []string{"curl", "http://localhost:15000/config_dump", "-s"}, w)
assert.NoError(t, err)
}()
fmt.Printf("Envoy RBAC filters for %s/%s:\n", namespace, name)
scanner := bufio.NewScanner(r)
// if counter is greater than zero - print to the output
counter := -1
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "envoy.filters.http.rbac") {
counter = 120
}
if strings.Contains(line, "\"mixer\"") {
counter = 0
}
counter = counter - 1
if counter > 0 {
fmt.Println(line)
}
}
fmt.Println()
}