-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1956029
cli: reorganise diagnostics subcommand
Pothulapati f14d984
linting fix
Pothulapati 0a9bce3
update integration tests
Pothulapati 5f1d3f9
Update cli/cmd/diagnostics.go
Pothulapati b5f1add
rename ControlPlaneMetrics to ControllerMetrics
Pothulapati 6d57fbf
Merge branch 'main' into tarun/cli-diagnostics
Pothulapati c34a156
move controller-metrics into a separate file
Pothulapati 4fd37fb
update TestOpaquePorts to include diagnostics prefix
Pothulapati c7bd5eb
Fix #5193 by moving install-sp under `diagnostics`
Pothulapati 0ff5c36
update TestInstallSp
Pothulapati 97ec35b
add cp-metrics as a alias for `controller-metrics`
Pothulapati c6e2e7d
update cmd suggestions to reflect the new changes
Pothulapati File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| 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", | ||
| Aliases: []string{"cp-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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 controller-metrics | ||
|
|
||
| # 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()) | ||
|
|
||
| return cmd | ||
| return diagnosticsCmd | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we call the
install-spsomething else? As its a bit confusing as this installs service profiles for the control-plane components and not the service profiles crds itselfThere 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.
I don't have a better suggestion for the name of this.