-
Notifications
You must be signed in to change notification settings - Fork 1.4k
cli: reorganise diagnostics subcommand #5205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
1956029
f14d984
0a9bce3
5f1d3f9
b5f1add
6d57fbf
c34a156
4fd37fb
c7bd5eb
0ff5c36
97ec35b
c6e2e7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/linkerd/linkerd2/pkg/k8s" | ||
| "github.com/spf13/cobra" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| // ControllerMetricsOptions holds values for command line flags that apply to the controller-metrics | ||
| // command. | ||
| type ControllerMetricsOptions struct { | ||
| wait time.Duration | ||
| } | ||
|
|
||
| // newControllerMetricsOptions initializes controller-metrics options setting | ||
| // the max wait time duration as 30 seconds to fetch controller-metrics | ||
| // | ||
| // This option may be overridden on the CLI at run-time | ||
| func newControllerMetricsOptions() *ControllerMetricsOptions { | ||
| return &ControllerMetricsOptions{ | ||
| wait: 30 * time.Second, | ||
| } | ||
| } | ||
|
|
||
| // newCmdControllerMetrics creates a new cobra command `controller-metrics` which contains commands to fetch control plane container's metrics | ||
| func newCmdControllerMetrics() *cobra.Command { | ||
| options := newControllerMetricsOptions() | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "controller-metrics", | ||
| Short: "Fetch metrics directly from the Linkerd control plane containers", | ||
| Long: `Fetch metrics directly from Linkerd control plane containers. | ||
|
|
||
| This command initiates port-forward to each control plane process, and | ||
| queries the /metrics endpoint on them.`, | ||
| Args: cobra.NoArgs, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| k8sAPI, err := k8s.NewAPI(kubeconfigPath, kubeContext, impersonate, impersonateGroup, 0) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| pods, err := k8sAPI.CoreV1().Pods(controlPlaneNamespace).List(cmd.Context(), metav1.ListOptions{}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| results := getMetrics(k8sAPI, pods.Items, adminHTTPPortName, options.wait, verbose) | ||
|
|
||
| var buf bytes.Buffer | ||
| for i, result := range results { | ||
| content := fmt.Sprintf("#\n# POD %s (%d of %d)\n", result.pod, i+1, len(results)) | ||
| if result.err != nil { | ||
| content += fmt.Sprintf("# ERROR %s\n", result.err) | ||
| } else { | ||
| content += fmt.Sprintf("# CONTAINER %s \n#\n", result.container) | ||
| content += string(result.metrics) | ||
| } | ||
| buf.WriteString(content) | ||
| } | ||
| fmt.Printf("%s", buf.String()) | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().DurationVarP(&options.wait, "wait", "w", options.wait, "Time allowed to fetch diagnostics") | ||
|
|
||
| return cmd | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,78 +1,42 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/linkerd/linkerd2/pkg/k8s" | ||
| "github.com/spf13/cobra" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| const ( | ||
| adminHTTPPortName string = "admin-http" | ||
| ) | ||
|
|
||
| // diagnosticsOptions holds values for command line flags that apply to the diagnostics | ||
| // command. | ||
| type diagnosticsOptions struct { | ||
| wait time.Duration | ||
| } | ||
|
|
||
| // newDiagnosticsOptions initializes diagnostics options setting | ||
| // the max wait time duration as 30 seconds to fetch diagnostics | ||
| // | ||
| // This option may be overridden on the CLI at run-time | ||
| func newDiagnosticsOptions() *diagnosticsOptions { | ||
| return &diagnosticsOptions{ | ||
| wait: 30 * time.Second, | ||
| } | ||
| } | ||
|
|
||
| // newCmdDashboard creates a new cobra command `diagnostics` which contains commands to fetch control plane container's metrics | ||
| // newCmdDiagnostics creates a new cobra command `diagnostics` which contains commands to fetch Linkerd diagnostics | ||
| func newCmdDiagnostics() *cobra.Command { | ||
| options := newDiagnosticsOptions() | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "diagnostics", | ||
| Short: "Fetch metrics directly from the Linkerd control plane containers", | ||
| Long: `Fetch metrics directly from Linkerd control plane containers. | ||
|
|
||
| This command initiates port-forward to each control plane process, and | ||
| queries the /metrics endpoint on them.`, | ||
| Args: cobra.NoArgs, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| k8sAPI, err := k8s.NewAPI(kubeconfigPath, kubeContext, impersonate, impersonateGroup, 0) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| pods, err := k8sAPI.CoreV1().Pods(controlPlaneNamespace).List(cmd.Context(), metav1.ListOptions{}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| results := getMetrics(k8sAPI, pods.Items, adminHTTPPortName, options.wait, verbose) | ||
|
|
||
| var buf bytes.Buffer | ||
| for i, result := range results { | ||
| content := fmt.Sprintf("#\n# POD %s (%d of %d)\n", result.pod, i+1, len(results)) | ||
| if result.err != nil { | ||
| content += fmt.Sprintf("# ERROR %s\n", result.err) | ||
| } else { | ||
| content += fmt.Sprintf("# CONTAINER %s \n#\n", result.container) | ||
| content += string(result.metrics) | ||
| } | ||
| buf.WriteString(content) | ||
| } | ||
| fmt.Printf("%s", buf.String()) | ||
|
|
||
| return nil | ||
| }, | ||
| diagnosticsCmd := &cobra.Command{ | ||
| Use: "diagnostics [flags]", | ||
| Aliases: []string{"dg"}, | ||
| Args: cobra.NoArgs, | ||
| Short: "Commands used to diagnose Linkerd components", | ||
| Long: `Commands used to diagnose Linkerd components. | ||
|
|
||
| This command provides subcommands to diagnose the functionality of Linkerd.`, | ||
| Example: ` # Get control-plane component metrics | ||
| linkerd diagnostics cp-metrics | ||
|
Pothulapati marked this conversation as resolved.
Outdated
|
||
|
|
||
| # Get metrics from the web deployment in the emojivoto namespace. | ||
| linkerd diagnostics proxy-metrics -n emojivoto deploy/web | ||
|
|
||
| # Get the endpoints for authorities in Linkerd's control-plane itself | ||
| linkerd diagnostics endpoints linkerd-controller-api.linkerd.svc.cluster.local:8085 | ||
|
|
||
| # Install service profiles for the control-plane components. | ||
| linkerd diagnostics install-sp | ||
| `, | ||
| } | ||
|
|
||
| cmd.Flags().DurationVarP(&options.wait, "wait", "w", options.wait, "Time allowed to fetch diagnostics") | ||
| diagnosticsCmd.AddCommand(newCmdControllerMetrics()) | ||
| diagnosticsCmd.AddCommand(newCmdEndpoints()) | ||
| diagnosticsCmd.AddCommand(newCmdMetrics()) | ||
| diagnosticsCmd.AddCommand(newCmdInstallSP()) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we call the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't have a better suggestion for the name of this. |
||
|
|
||
| return cmd | ||
| return diagnosticsCmd | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cp-metrics doesn't seem like it's a valid abbreviation for this command.