This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
forked from istio/istio
/
client.go
301 lines (267 loc) · 9.51 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2018 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.
package kubernetes
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
"istio.io/istio/pkg/kube"
"istio.io/istio/pkg/version"
)
var (
proxyContainer = "istio-proxy"
discoveryContainer = "discovery"
pilotDiscoveryPath = "/usr/local/bin/pilot-discovery"
pilotAgentPath = "/usr/local/bin/pilot-agent"
)
// Client is a helper wrapper around the Kube RESTClient for istioctl -> Pilot/Envoy/Mesh related things
type Client struct {
Config *rest.Config
*rest.RESTClient
}
// ExecClient is an interface for remote execution
type ExecClient interface {
EnvoyDo(podName, podNamespace, method, path string, body []byte) ([]byte, error)
AllPilotsDiscoveryDo(pilotNamespace, method, path string, body []byte) (map[string][]byte, error)
GetIstioVersions(namespace string) (*version.MeshInfo, error)
}
// NewClient is the constructor for the client wrapper
func NewClient(kubeconfig, configContext string) (*Client, error) {
config, err := defaultRestConfig(kubeconfig, configContext)
if err != nil {
return nil, err
}
restClient, err := rest.RESTClientFor(config)
if err != nil {
return nil, err
}
return &Client{config, restClient}, nil
}
func defaultRestConfig(kubeconfig, configContext string) (*rest.Config, error) {
config, err := kube.BuildClientConfig(kubeconfig, configContext)
if err != nil {
return nil, err
}
config.APIPath = "/api"
config.GroupVersion = &v1.SchemeGroupVersion
config.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}
return config, nil
}
// PodExec takes a command and the pod data to run the command in the specified pod
func (client *Client) PodExec(podName, podNamespace, container string, command []string) (*bytes.Buffer, *bytes.Buffer, error) {
req := client.Post().
Resource("pods").
Name(podName).
Namespace(podNamespace).
SubResource("exec").
Param("container", container).
VersionedParams(&v1.PodExecOptions{
Container: container,
Command: command,
Stdin: false,
Stdout: true,
Stderr: true,
TTY: false,
}, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(client.Config, "POST", req.URL())
if err != nil {
return nil, nil, err
}
var stdout, stderr bytes.Buffer
err = exec.Stream(remotecommand.StreamOptions{
Stdin: nil,
Stdout: &stdout,
Stderr: &stderr,
Tty: false,
})
return &stdout, &stderr, err
}
// AllPilotsDiscoveryDo makes an http request to each Pilot discovery instance
func (client *Client) AllPilotsDiscoveryDo(pilotNamespace, method, path string, body []byte) (map[string][]byte, error) {
pilots, err := client.GetIstioPods(pilotNamespace, map[string]string{
"labelSelector": "istio=pilot",
"fieldSelector": "status.phase=Running",
})
if err != nil {
return nil, err
}
if len(pilots) == 0 {
return nil, errors.New("unable to find any Pilot instances")
}
cmd := []string{"sh", "-c", fmt.Sprintf("GODEBUG= %s request %s %s %s", pilotDiscoveryPath, method, path, string(body))}
result := map[string][]byte{}
for _, pilot := range pilots {
res, err := client.ExtractExecResult(pilot.Name, pilot.Namespace, discoveryContainer, cmd)
if err != nil {
return nil, err
}
if len(res) > 0 {
result[pilot.Name] = res
}
}
return result, err
}
// PilotDiscoveryDo makes an http request to a single Pilot discovery instance
func (client *Client) PilotDiscoveryDo(pilotNamespace, method, path string, body []byte) ([]byte, error) {
pilots, err := client.GetIstioPods(pilotNamespace, map[string]string{
"labelSelector": "istio=pilot",
"fieldSelector": "status.phase=Running",
})
if err != nil {
return nil, err
}
if len(pilots) == 0 {
return nil, errors.New("unable to find any Pilot instances")
}
cmd := []string{"sh", "-c", fmt.Sprintf("GODEBUG= %s request %s %s %s", pilotDiscoveryPath, method, path, string(body))}
return client.ExtractExecResult(pilots[0].Name, pilots[0].Namespace, discoveryContainer, cmd)
}
// EnvoyDo makes an http request to the Envoy in the specified pod
func (client *Client) EnvoyDo(podName, podNamespace, method, path string, body []byte) ([]byte, error) {
container, err := client.GetPilotAgentContainer(podName, podNamespace)
if err != nil {
return nil, fmt.Errorf("unable to retrieve proxy container name: %v", err)
}
cmd := []string{pilotAgentPath, "request", method, path, string(body)}
return client.ExtractExecResult(podName, podNamespace, container, cmd)
}
// ExtractExecResult wraps PodExec and return the execution result and error if has any.
func (client *Client) ExtractExecResult(podName, podNamespace, container string, cmd []string) ([]byte, error) {
stdout, stderr, err := client.PodExec(podName, podNamespace, container, cmd)
if err != nil {
return nil, fmt.Errorf("error execing into %v/%v %v container: %v", podName, podNamespace, container, err)
}
if stderr.String() != "" {
fmt.Printf("Warning! error execing into %v/%v %v container: %v\n", podName, podNamespace, container, stderr.String())
}
return stdout.Bytes(), nil
}
// GetIstioPods retrieves the pod objects for Istio deployments
func (client *Client) GetIstioPods(namespace string, params map[string]string) ([]v1.Pod, error) {
req := client.Get().
Resource("pods").
Namespace(namespace)
for k, v := range params {
req.Param(k, v)
}
res := req.Do()
if res.Error() != nil {
return nil, fmt.Errorf("unable to retrieve Pods: %v", res.Error())
}
list := &v1.PodList{}
if err := res.Into(list); err != nil {
return nil, fmt.Errorf("unable to parse PodList: %v", res.Error())
}
return list.Items, nil
}
// GetPilotAgentContainer retrieves the pilot-agent container name for the specified pod
func (client *Client) GetPilotAgentContainer(podName, podNamespace string) (string, error) {
req := client.Get().
Resource("pods").
Namespace(podNamespace).
Name(podName)
res := req.Do()
if res.Error() != nil {
return "", fmt.Errorf("unable to retrieve Pod: %v", res.Error())
}
pod := &v1.Pod{}
if err := res.Into(pod); err != nil {
return "", fmt.Errorf("unable to parse Pod: %v", res.Error())
}
for _, c := range pod.Spec.Containers {
switch c.Name {
case "egressgateway", "ingress", "ingressgateway":
return c.Name, nil
}
}
return proxyContainer, nil
}
type podDetail struct {
binary string
container string
}
// GetIstioVersions gets the version for each Istio component
func (client *Client) GetIstioVersions(namespace string) (*version.MeshInfo, error) {
pods, err := client.GetIstioPods(namespace, map[string]string{
"labelSelector": "istio",
"fieldSelector": "status.phase=Running",
})
if err != nil {
return nil, err
}
if len(pods) == 0 {
return nil, errors.New("unable to find any Istio pod in namespace " + namespace)
}
labelToPodDetail := map[string]podDetail{
"pilot": {"/usr/local/bin/pilot-discovery", "discovery"},
"citadel": {"/usr/local/bin/istio_ca", "citadel"},
"egressgateway": {"/usr/local/bin/pilot-agent", "istio-proxy"},
"galley": {"/usr/local/bin/galley", "galley"},
"ingressgateway": {"/usr/local/bin/pilot-agent", "istio-proxy"},
"telemetry": {"/usr/local/bin/mixs", "mixer"},
"policy": {"/usr/local/bin/mixs", "mixer"},
"sidecar-injector": {"/usr/local/bin/sidecar-injector", "sidecar-injector-webhook"},
}
res := version.MeshInfo{}
for _, pod := range pods {
component := pod.Labels["istio"]
// Special cases
switch component {
case "statsd-prom-bridge":
continue
case "mixer":
component = pod.Labels["istio-mixer-type"]
}
server := version.ServerInfo{Component: component}
if detail, ok := labelToPodDetail[component]; ok {
cmd := []string{detail.binary, "version"}
cmdJSON := append(cmd, "-o", "json")
var info version.BuildInfo
var v version.Version
stdout, stderr, err := client.PodExec(pod.Name, pod.Namespace, detail.container, cmdJSON)
if err != nil {
return nil, fmt.Errorf("error execing into %v %v container: %v", pod.Name, detail.container, err)
}
// At first try parsing stdout
err = json.Unmarshal(stdout.Bytes(), &v)
if err == nil && v.ClientVersion.Version != "" {
info = *v.ClientVersion
} else {
// If stdout fails, try the old behavior
if strings.HasPrefix(stderr.String(), "Error: unknown shorthand flag") {
stdout, err := client.ExtractExecResult(pod.Name, pod.Namespace, detail.container, cmd)
if err != nil {
return nil, fmt.Errorf("error execing into %v %v container: %v", pod.Name, detail.container, err)
}
info, err = version.NewBuildInfoFromOldString(string(stdout))
if err != nil {
return nil, fmt.Errorf("error converting server info from JSON: %v", err)
}
} else {
return nil, fmt.Errorf("error execing into %v %v container: %v", pod.Name, detail.container, stderr.String())
}
}
server.Info = info
}
res = append(res, server)
}
return &res, nil
}