...
1 package cmd
2
3 import (
4 "fmt"
5 "os"
6
7 pkgcmd "github.com/linkerd/linkerd2/pkg/cmd"
8 pkgK8s "github.com/linkerd/linkerd2/pkg/k8s"
9 vizLabels "github.com/linkerd/linkerd2/viz/pkg/labels"
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 can be tapped",
26 Args: cobra.NoArgs,
27 RunE: func(cmd *cobra.Command, args []string) error {
28 k8sAPI, err := pkgK8s.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 tapEnabled, tapDisabled, tapNotEnabled []v1.Pod
47
48 for _, pod := range pods.Items {
49 pod := pod
50 if pkgK8s.IsMeshed(&pod, controlPlaneNamespace) {
51 if vizLabels.IsTapDisabled(pod.GetObjectMeta()) {
52 tapDisabled = append(tapDisabled, pod)
53 } else if !vizLabels.IsTapEnabled(&pod) {
54 tapNotEnabled = append(tapNotEnabled, pod)
55 } else {
56 tapEnabled = append(tapEnabled, pod)
57 }
58 }
59 }
60
61 if len(tapEnabled) > 0 {
62 fmt.Println("Pods with tap enabled:")
63 for _, pod := range tapEnabled {
64 fmt.Printf("\t* %s/%s\n", pod.Namespace, pod.Name)
65 }
66 }
67
68 if len(tapDisabled) > 0 {
69 fmt.Println("Pods with tap disabled:")
70 for _, pod := range tapDisabled {
71 fmt.Printf("\t* %s/%s\n", pod.Namespace, pod.Name)
72 }
73 }
74
75 if len(tapNotEnabled) > 0 {
76 fmt.Println("Pods missing tap configuration (restart these pods to enable tap):")
77 for _, pod := range tapNotEnabled {
78 fmt.Printf("\t* %s/%s\n", pod.Namespace, pod.Name)
79 }
80 }
81
82 if len(tapEnabled)+len(tapDisabled)+len(tapNotEnabled) == 0 {
83 fmt.Println("No meshed pods found")
84 }
85
86 return nil
87 },
88 }
89 cmd.Flags().StringVarP(&options.namespace, "namespace", "n", options.namespace, "The namespace to list pods in")
90 cmd.Flags().BoolVarP(&options.allNamespaces, "all-namespaces", "A", options.allNamespaces, "If present, list pods across all namespaces")
91
92 pkgcmd.ConfigureNamespaceFlagCompletion(
93 cmd, []string{"namespace"},
94 kubeconfigPath, impersonate, impersonateGroup, kubeContext)
95 return cmd
96 }
97
View as plain text