@@ -31,6 +31,9 @@ import (
31
31
32
32
var (
33
33
controlZport = 0
34
+
35
+ // label selector
36
+ labelSelector = ""
34
37
)
35
38
36
39
// port-forward to Istio System Prometheus; open browser
@@ -261,17 +264,43 @@ func envoyDashCmd() *cobra.Command {
261
264
Long : `Open the Envoy admin dashboard for a sidecar` ,
262
265
Example : `istioctl dashboard envoy productpage-123-456.default` ,
263
266
RunE : func (c * cobra.Command , args []string ) error {
264
- if len (args ) < 1 {
267
+ if labelSelector == "" && len (args ) < 1 {
265
268
c .Println (c .UsageString ())
266
- return fmt .Errorf ("specify a pod" )
269
+ return fmt .Errorf ("specify a pod or --selector" )
270
+ }
271
+
272
+ if labelSelector != "" && len (args ) > 0 {
273
+ c .Println (c .UsageString ())
274
+ return fmt .Errorf ("name cannot be provided when a selector is specified" )
267
275
}
268
276
269
- podName , ns := handlers .InferPodInfo (args [0 ], handlers .HandleNamespace (namespace , defaultNamespace ))
270
277
client , err := clientExecFactory (kubeconfig , configContext )
271
278
if err != nil {
272
279
return fmt .Errorf ("failed to create k8s client: %v" , err )
273
280
}
274
281
282
+ var podName , ns string
283
+ if labelSelector != "" {
284
+ pl , err := client .PodsForSelector (handlers .HandleNamespace (namespace , defaultNamespace ), labelSelector )
285
+ if err != nil {
286
+ return fmt .Errorf ("not able to locate pod with selector %s: %v" , labelSelector , err )
287
+ }
288
+
289
+ if len (pl .Items ) < 1 {
290
+ return errors .New ("no pods found" )
291
+ }
292
+
293
+ if len (pl .Items ) > 1 {
294
+ log .Warnf ("more than 1 pods fits selector: %s; will use pod: %s" , labelSelector , pl .Items [0 ].Name )
295
+ }
296
+
297
+ // only use the first pod in the list
298
+ podName = pl .Items [0 ].Name
299
+ ns = pl .Items [0 ].Namespace
300
+ } else {
301
+ podName , ns = handlers .InferPodInfo (args [0 ], handlers .HandleNamespace (namespace , defaultNamespace ))
302
+ }
303
+
275
304
fw , err := client .BuildPortForwarder (podName , ns , 0 , 15000 )
276
305
if err != nil {
277
306
return fmt .Errorf ("could not build port forwarder for %s: %v" , podName , err )
@@ -300,17 +329,43 @@ func controlZDashCmd() *cobra.Command {
300
329
Long : `Open the ControlZ web UI for a pod in the Istio control plane` ,
301
330
Example : `istioctl dashboard controlz pilot-123-456.istio-system` ,
302
331
RunE : func (c * cobra.Command , args []string ) error {
303
- if len (args ) < 1 {
332
+ if labelSelector == "" && len (args ) < 1 {
304
333
c .Println (c .UsageString ())
305
- return fmt .Errorf ("specify a pod" )
334
+ return fmt .Errorf ("specify a pod or --selector" )
335
+ }
336
+
337
+ if labelSelector != "" && len (args ) > 0 {
338
+ c .Println (c .UsageString ())
339
+ return fmt .Errorf ("name cannot be provided when a selector is specified" )
306
340
}
307
341
308
- podName , ns := handlers .InferPodInfo (args [0 ], handlers .HandleNamespace (namespace , defaultNamespace ))
309
342
client , err := clientExecFactory (kubeconfig , configContext )
310
343
if err != nil {
311
344
return fmt .Errorf ("failed to create k8s client: %v" , err )
312
345
}
313
346
347
+ var podName , ns string
348
+ if labelSelector != "" {
349
+ pl , err := client .PodsForSelector (handlers .HandleNamespace (namespace , defaultNamespace ), labelSelector )
350
+ if err != nil {
351
+ return fmt .Errorf ("not able to locate pod with selector %s: %v" , labelSelector , err )
352
+ }
353
+
354
+ if len (pl .Items ) < 1 {
355
+ return errors .New ("no pods found" )
356
+ }
357
+
358
+ if len (pl .Items ) > 1 {
359
+ log .Warnf ("more than 1 pods fits selector: %s; will use pod: %s" , labelSelector , pl .Items [0 ].Name )
360
+ }
361
+
362
+ // only use the first pod in the list
363
+ podName = pl .Items [0 ].Name
364
+ ns = pl .Items [0 ].Namespace
365
+ } else {
366
+ podName , ns = handlers .InferPodInfo (args [0 ], handlers .HandleNamespace (namespace , defaultNamespace ))
367
+ }
368
+
314
369
fw , err := client .BuildPortForwarder (podName , ns , 0 , controlZport )
315
370
if err != nil {
316
371
return fmt .Errorf ("could not build port forwarder for %s: %v" , podName , err )
@@ -374,9 +429,13 @@ func dashboard() *cobra.Command {
374
429
dashboardCmd .AddCommand (jaegerDashCmd ())
375
430
dashboardCmd .AddCommand (zipkinDashCmd ())
376
431
377
- dashboardCmd .AddCommand (envoyDashCmd ())
432
+ envoy := envoyDashCmd ()
433
+ envoy .PersistentFlags ().StringVarP (& labelSelector , "selector" , "l" , "" , "label selector" )
434
+ dashboardCmd .AddCommand (envoy )
435
+
378
436
controlz := controlZDashCmd ()
379
437
controlz .PersistentFlags ().IntVar (& controlZport , "ctrlz_port" , 9876 , "ControlZ port" )
438
+ controlz .PersistentFlags ().StringVarP (& labelSelector , "selector" , "l" , "" , "label selector" )
380
439
dashboardCmd .AddCommand (controlz )
381
440
382
441
return dashboardCmd
0 commit comments