-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxyconfig.go
295 lines (260 loc) · 11 KB
/
proxyconfig.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
// 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 main
import (
"fmt"
"io"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
"istio.io/istio/istioctl/pkg/writer/envoy/clusters"
"istio.io/istio/istioctl/pkg/writer/envoy/configdump"
"istio.io/istio/pilot/pkg/model"
)
const (
jsonOutput = "json"
summaryOutput = "short"
)
var (
configCmd = &cobra.Command{
Use: "proxy-config",
Short: "Retrieve information about proxy configuration from Envoy [kube only]",
Long: `A group of commands used to retrieve information about proxy configuration from the Envoy config dump`,
Example: ` # Retrieve information about proxy configuration from an Envoy instance.
istioctl proxy-config <clusters|listeners|routes|endpoints|bootstrap> <pod-name[.namespace]>`,
Aliases: []string{"pc"},
}
fqdn, direction, subset string
port int
clusterConfigCmd = &cobra.Command{
Use: "cluster <pod-name[.namespace]>",
Short: "Retrieves cluster configuration for the Envoy in the specified pod",
Long: `Retrieve information about cluster configuration for the Envoy instance in the specified pod.`,
Example: ` # Retrieve summary about cluster configuration for a given pod from Envoy.
istioctl proxy-config clusters <pod-name[.namespace]>
# Retrieve cluster summary for clusters with port 9080.
istioctl proxy-config clusters <pod-name[.namespace]> --port 9080
# Retrieve full cluster dump for clusters that are inbound with a FQDN of details.default.svc.cluster.local.
istioctl proxy-config clusters <pod-name[.namespace]> --fqdn details.default.svc.cluster.local --direction inbound -o json
`,
Aliases: []string{"clusters", "c"},
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
podName, ns := inferPodInfo(args[0], handleNamespace())
configWriter, err := setupConfigdumpEnvoyConfigWriter(podName, ns, c.OutOrStdout())
if err != nil {
return err
}
filter := configdump.ClusterFilter{
FQDN: model.Hostname(fqdn),
Port: port,
Subset: subset,
Direction: model.TrafficDirection(direction),
}
switch outputFormat {
case summaryOutput:
return configWriter.PrintClusterSummary(filter)
case jsonOutput:
return configWriter.PrintClusterDump(filter)
default:
return fmt.Errorf("output format %q not supported", outputFormat)
}
},
}
address, listenerType string
listenerConfigCmd = &cobra.Command{
Use: "listener <pod-name[.namespace]>",
Short: "Retrieves listener configuration for the Envoy in the specified pod",
Long: `Retrieve information about listener configuration for the Envoy instance in the specified pod.`,
Example: ` # Retrieve summary about listener configuration for a given pod from Envoy.
istioctl proxy-config listeners <pod-name[.namespace]>
# Retrieve listener summary for listeners with port 9080.
istioctl proxy-config listeners <pod-name[.namespace]> --port 9080
# Retrieve full listener dump for HTTP listeners with a wildcard address (0.0.0.0).
istioctl proxy-config listeners <pod-name[.namespace]> --type HTTP --address 0.0.0.0 -o json
`,
Aliases: []string{"listeners", "l"},
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
podName, ns := inferPodInfo(args[0], handleNamespace())
configWriter, err := setupConfigdumpEnvoyConfigWriter(podName, ns, c.OutOrStdout())
if err != nil {
return err
}
filter := configdump.ListenerFilter{
Address: address,
Port: uint32(port),
Type: listenerType,
}
switch outputFormat {
case summaryOutput:
return configWriter.PrintListenerSummary(filter)
case jsonOutput:
return configWriter.PrintListenerDump(filter)
default:
return fmt.Errorf("output format %q not supported", outputFormat)
}
},
}
routeName string
routeConfigCmd = &cobra.Command{
Use: "route <pod-name[.namespace]>",
Short: "Retrieves route configuration for the Envoy in the specified pod",
Long: `Retrieve information about route configuration for the Envoy instance in the specified pod.`,
Example: ` # Retrieve summary about route configuration for a given pod from Envoy.
istioctl proxy-config routes <pod-name[.namespace]>
# Retrieve route summary for route 9080.
istioctl proxy-config route <pod-name[.namespace]> --name 9080
# Retrieve full route dump for route 9080
istioctl proxy-config route <pod-name[.namespace]> --name 9080 -o json
`,
Aliases: []string{"routes", "r"},
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
podName, ns := inferPodInfo(args[0], handleNamespace())
configWriter, err := setupConfigdumpEnvoyConfigWriter(podName, ns, c.OutOrStdout())
if err != nil {
return err
}
filter := configdump.RouteFilter{
Name: routeName,
}
switch outputFormat {
case summaryOutput:
return configWriter.PrintRouteSummary(filter)
case jsonOutput:
return configWriter.PrintRouteDump(filter)
default:
return fmt.Errorf("output format %q not supported", outputFormat)
}
},
}
bootstrapConfigCmd = &cobra.Command{
Use: "bootstrap <pod-name[.namespace]>",
Short: "Retrieves bootstrap configuration for the Envoy in the specified pod",
Long: `Retrieve information about bootstrap configuration for the Envoy instance in the specified pod.`,
Example: ` # Retrieve full bootstrap configuration for a given pod from Envoy.
istioctl proxy-config bootstrap <pod-name[.namespace]>
`,
Aliases: []string{"b"},
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
podName, ns := inferPodInfo(args[0], handleNamespace())
configWriter, err := setupConfigdumpEnvoyConfigWriter(podName, ns, c.OutOrStdout())
if err != nil {
return err
}
return configWriter.PrintBootstrapDump()
},
}
clusterName, status string
endpointConfigCmd = &cobra.Command{
Use: "endpoint <pod-name[.namespace]>",
Short: "Retrieves endpoint configuration for the Envoy in the specified pod",
Long: `Retrieve information about endpoint configuration for the Envoy instance in the specified pod.`,
Example: ` # Retrieve full endpoint configuration for a given pod from Envoy.
istioctl proxy-config endpoint <pod-name[.namespace]>
# Retrieve endpoint summary for endpoint with port 9080.
istioctl proxy-config endpoint <pod-name[.namespace]> --port 9080
# Retrieve full endpoint with a address (172.17.0.2).
istioctl proxy-config endpoint <pod-name[.namespace]> --address 172.17.0.2 -o json
# Retrieve full endpoint with a cluster name (outbound|9411||zipkin.istio-system.svc.cluster.local).
istioctl proxy-config endpoint <pod-name[.namespace]> --cluster "outbound|9411||zipkin.istio-system.svc.cluster.local" -o json
# Retrieve full endpoint with the status (healthy).
istioctl proxy-config endpoint <pod-name[.namespace]> --status healthy -ojson
`,
Aliases: []string{"endpoints", "ep"},
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
podName, ns := inferPodInfo(args[0], handleNamespace())
configWriter, err := setupClustersEnvoyConfigWriter(podName, ns, c.OutOrStdout())
if err != nil {
return err
}
filter := clusters.EndpointFilter{
Address: address,
Port: uint32(port),
Cluster: clusterName,
Status: status,
}
switch outputFormat {
case summaryOutput:
return configWriter.PrintEndpointsSummary(filter)
case jsonOutput:
return configWriter.PrintEndpoints(filter)
default:
return fmt.Errorf("output format %q not supported", outputFormat)
}
},
}
)
func handleNamespace() string {
ns := namespace
if ns == v1.NamespaceAll {
ns = defaultNamespace
}
return ns
}
func setupConfigdumpEnvoyConfigWriter(podName, podNamespace string, out io.Writer) (*configdump.ConfigWriter, error) {
kubeClient, err := clientExecFactory(kubeconfig, configContext)
if err != nil {
return nil, fmt.Errorf("failed to create k8s client: %v", err)
}
path := "config_dump"
debug, err := kubeClient.EnvoyDo(podName, podNamespace, "GET", path, nil)
if err != nil {
return nil, fmt.Errorf("failed to execute command on envoy: %v", err)
}
cw := &configdump.ConfigWriter{Stdout: out}
err = cw.Prime(debug)
if err != nil {
return nil, err
}
return cw, nil
}
// TODO(fisherxu): migrate this to config dump when implemented in Envoy
// Issue to track -> https://github.com/envoyproxy/envoy/issues/3362
func setupClustersEnvoyConfigWriter(podName, podNamespace string, out io.Writer) (*clusters.ConfigWriter, error) {
kubeClient, err := clientExecFactory(kubeconfig, configContext)
if err != nil {
return nil, fmt.Errorf("failed to create k8s client: %v", err)
}
path := "clusters?format=json"
debug, err := kubeClient.EnvoyDo(podName, podNamespace, "GET", path, nil)
if err != nil {
return nil, fmt.Errorf("failed to execute command on envoy: %v", err)
}
cw := &clusters.ConfigWriter{Stdout: out}
err = cw.Prime(debug)
if err != nil {
return nil, err
}
return cw, nil
}
func init() {
rootCmd.AddCommand(configCmd)
configCmd.PersistentFlags().StringVarP(&outputFormat, "output", "o", summaryOutput, "Output format: one of json|short")
clusterConfigCmd.PersistentFlags().StringVar(&fqdn, "fqdn", "", "Filter clusters by substring of Service FQDN field")
clusterConfigCmd.PersistentFlags().StringVar(&direction, "direction", "", "Filter clusters by Direction field")
clusterConfigCmd.PersistentFlags().StringVar(&subset, "subset", "", "Filter clusters by substring of Subset field")
clusterConfigCmd.PersistentFlags().IntVar(&port, "port", 0, "Filter clusters by Port field")
listenerConfigCmd.PersistentFlags().StringVar(&address, "address", "", "Filter listeners by address field")
listenerConfigCmd.PersistentFlags().StringVar(&listenerType, "type", "", "Filter listeners by type field")
listenerConfigCmd.PersistentFlags().IntVar(&port, "port", 0, "Filter listeners by Port field")
routeConfigCmd.PersistentFlags().StringVar(&routeName, "name", "", "Filter listeners by route name field")
endpointConfigCmd.PersistentFlags().StringVar(&address, "address", "", "Filter endpoints by address field")
endpointConfigCmd.PersistentFlags().IntVar(&port, "port", 0, "Filter endpoints by Port field")
endpointConfigCmd.PersistentFlags().StringVar(&clusterName, "cluster", "", "Filter endpoints by cluster name field")
endpointConfigCmd.PersistentFlags().StringVar(&status, "status", "", "Filter endpoints by status field")
configCmd.AddCommand(clusterConfigCmd, listenerConfigCmd, routeConfigCmd, bootstrapConfigCmd, endpointConfigCmd)
}