Repository navigation
Expand file tree
/
Copy pathlist.go
More file actions
96 lines (81 loc) · 2.56 KB
/
Copy pathlist.go
File metadata and controls
96 lines (81 loc) · 2.56 KB
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
package cmd
import (
"fmt"
"os"
pkgcmd "github.com/linkerd/linkerd2/pkg/cmd"
pkgK8s "github.com/linkerd/linkerd2/pkg/k8s"
vizLabels "github.com/linkerd/linkerd2/viz/pkg/labels"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type listOptions struct {
namespace string
allNamespaces bool
}
func newCmdList() *cobra.Command {
var options listOptions
cmd := &cobra.Command{
Use: "list [flags]",
Short: "Lists which pods can be tapped",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
k8sAPI, err := pkgK8s.NewAPI(kubeconfigPath, kubeContext, impersonate, impersonateGroup, 0)
if err != nil {
return err
}
if options.namespace == "" {
options.namespace = pkgcmd.GetDefaultNamespace(kubeconfigPath, kubeContext)
}
if options.allNamespaces {
options.namespace = v1.NamespaceAll
}
pods, err := k8sAPI.CoreV1().Pods(options.namespace).List(cmd.Context(), metav1.ListOptions{})
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
var tapEnabled, tapDisabled, tapNotEnabled []v1.Pod
for _, pod := range pods.Items {
pod := pod
if pkgK8s.IsMeshed(&pod, controlPlaneNamespace) {
if vizLabels.IsTapDisabled(pod) {
tapDisabled = append(tapDisabled, pod)
} else if !vizLabels.IsTapEnabled(&pod) {
tapNotEnabled = append(tapNotEnabled, pod)
} else {
tapEnabled = append(tapEnabled, pod)
}
}
}
if len(tapEnabled) > 0 {
fmt.Println("Pods with tap enabled:")
for _, pod := range tapEnabled {
fmt.Printf("\t* %s/%s\n", pod.Namespace, pod.Name)
}
}
if len(tapDisabled) > 0 {
fmt.Println("Pods with tap disabled:")
for _, pod := range tapDisabled {
fmt.Printf("\t* %s/%s\n", pod.Namespace, pod.Name)
}
}
if len(tapNotEnabled) > 0 {
fmt.Println("Pods missing tap configuration (restart these pods to enable tap):")
for _, pod := range tapNotEnabled {
fmt.Printf("\t* %s/%s\n", pod.Namespace, pod.Name)
}
}
if len(tapEnabled)+len(tapDisabled)+len(tapNotEnabled) == 0 {
fmt.Println("No meshed pods found")
}
return nil
},
}
cmd.Flags().StringVarP(&options.namespace, "namespace", "n", options.namespace, "The namespace to list pods in")
cmd.Flags().BoolVarP(&options.allNamespaces, "all-namespaces", "A", options.allNamespaces, "If present, list pods across all namespaces")
pkgcmd.ConfigureNamespaceFlagCompletion(
cmd, []string{"namespace"},
kubeconfigPath, impersonate, impersonateGroup, kubeContext)
return cmd
}