...
1 package cmd
2
3 import (
4 "fmt"
5 "os"
6
7 "github.com/linkerd/linkerd2/jaeger/pkg/labels"
8 pkgcmd "github.com/linkerd/linkerd2/pkg/cmd"
9 "github.com/linkerd/linkerd2/pkg/k8s"
10 "github.com/spf13/cobra"
11 v1 "k8s.io/api/core/v1"
12 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13 )
14
15 type listOptions struct {
16 namespace string
17 allNamespaces bool
18 }
19
20 func newCmdList() *cobra.Command {
21 var options listOptions
22
23 cmd := &cobra.Command{
24 Use: "list [flags]",
25 Short: "Lists which pods have tracing enabled",
26 Args: cobra.NoArgs,
27 RunE: func(cmd *cobra.Command, args []string) error {
28 k8sAPI, err := k8s.NewAPI(kubeconfigPath, kubeContext, impersonate, impersonateGroup, 0)
29 if err != nil {
30 return err
31 }
32
33 if options.namespace == "" {
34 options.namespace = pkgcmd.GetDefaultNamespace(kubeconfigPath, kubeContext)
35 }
36 if options.allNamespaces {
37 options.namespace = v1.NamespaceAll
38 }
39
40 pods, err := k8sAPI.CoreV1().Pods(options.namespace).List(cmd.Context(), metav1.ListOptions{})
41 if err != nil {
42 fmt.Fprintln(os.Stderr, err)
43 os.Exit(1)
44 }
45
46 var tracingEnabled, tracingNotEnabled []v1.Pod
47
48 for _, pod := range pods.Items {
49 pod := pod
50 if k8s.IsMeshed(&pod, controlPlaneNamespace) {
51 if labels.IsTracingEnabled(&pod) {
52 tracingEnabled = append(tracingEnabled, pod)
53 } else {
54 tracingNotEnabled = append(tracingNotEnabled, pod)
55 }
56 }
57 }
58
59 if len(tracingEnabled) > 0 {
60 fmt.Println("Pods with tracing enabled:")
61 for _, pod := range tracingEnabled {
62 fmt.Printf("\t* %s/%s\n", pod.Namespace, pod.Name)
63 }
64 }
65
66 if len(tracingNotEnabled) > 0 {
67 fmt.Println("Pods missing tracing configuration (restart these pods to enable tracing):")
68 for _, pod := range tracingNotEnabled {
69 fmt.Printf("\t* %s/%s\n", pod.Namespace, pod.Name)
70 }
71 }
72
73 if len(tracingEnabled)+len(tracingNotEnabled) == 0 {
74 fmt.Println("No meshed pods found")
75 }
76
77 return nil
78 },
79 }
80 cmd.Flags().StringVarP(&options.namespace, "namespace", "n", options.namespace, "The namespace to list pods in")
81 cmd.Flags().BoolVarP(&options.allNamespaces, "all-namespaces", "A", options.allNamespaces, "If present, list pods across all namespaces")
82
83 pkgcmd.ConfigureNamespaceFlagCompletion(
84 cmd, []string{"namespace"},
85 kubeconfigPath, impersonate, impersonateGroup, kubeContext)
86
87 return cmd
88 }
89
View as plain text