From b15a6e02b4e8d0d80027ae980271298c02ccb473 Mon Sep 17 00:00:00 2001 From: malizz Date: Wed, 1 Feb 2023 15:11:05 -0800 Subject: [PATCH] update troubleshoot CLI (#16129) --- command/registry.go | 10 +++-- .../troubleshoot_proxy.go} | 12 ++--- .../troubleshoot_proxy_test.go} | 4 +- command/troubleshoot/troubleshoot.go | 44 +++++++++++++++++++ command/troubleshoot/troubleshoot_test.go | 13 ++++++ .../upstreams/troubleshoot_upstreams.go | 8 ++-- ...shoot_connect.go => troubleshoot_proxy.go} | 0 7 files changed, 75 insertions(+), 16 deletions(-) rename command/troubleshoot/{connect/troubleshoot_connect.go => proxy/troubleshoot_proxy.go} (87%) rename command/troubleshoot/{connect/troubleshoot_connect_test.go => proxy/troubleshoot_proxy_test.go} (68%) create mode 100644 command/troubleshoot/troubleshoot.go create mode 100644 command/troubleshoot/troubleshoot_test.go rename troubleshoot/{connect/troubleshoot_connect.go => troubleshoot_proxy.go} (100%) diff --git a/command/registry.go b/command/registry.go index 37ad991060..beaa3bf43e 100644 --- a/command/registry.go +++ b/command/registry.go @@ -116,8 +116,9 @@ import ( tlscacreate "github.com/hashicorp/consul/command/tls/ca/create" tlscert "github.com/hashicorp/consul/command/tls/cert" tlscertcreate "github.com/hashicorp/consul/command/tls/cert/create" - troubleshoot "github.com/hashicorp/consul/command/troubleshoot/connect" - upstreams "github.com/hashicorp/consul/command/troubleshoot/upstreams" + troubleshoot "github.com/hashicorp/consul/command/troubleshoot" + troubleshootproxy "github.com/hashicorp/consul/command/troubleshoot/proxy" + troubleshootupstreams "github.com/hashicorp/consul/command/troubleshoot/upstreams" "github.com/hashicorp/consul/command/validate" "github.com/hashicorp/consul/command/version" "github.com/hashicorp/consul/command/watch" @@ -242,8 +243,9 @@ func RegisteredCommands(ui cli.Ui) map[string]mcli.CommandFactory { entry{"tls ca create", func(ui cli.Ui) (cli.Command, error) { return tlscacreate.New(ui), nil }}, entry{"tls cert", func(ui cli.Ui) (cli.Command, error) { return tlscert.New(), nil }}, entry{"tls cert create", func(ui cli.Ui) (cli.Command, error) { return tlscertcreate.New(ui), nil }}, - entry{"troubleshoot connect", func(ui cli.Ui) (cli.Command, error) { return troubleshoot.New(ui), nil }}, - entry{"troubleshoot upstreams", func(ui cli.Ui) (cli.Command, error) { return upstreams.New(ui), nil }}, + entry{"troubleshoot", func(ui cli.Ui) (cli.Command, error) { return troubleshoot.New(), nil }}, + entry{"troubleshoot proxy", func(ui cli.Ui) (cli.Command, error) { return troubleshootproxy.New(ui), nil }}, + entry{"troubleshoot upstreams", func(ui cli.Ui) (cli.Command, error) { return troubleshootupstreams.New(ui), nil }}, entry{"validate", func(ui cli.Ui) (cli.Command, error) { return validate.New(ui), nil }}, entry{"version", func(ui cli.Ui) (cli.Command, error) { return version.New(ui), nil }}, entry{"watch", func(ui cli.Ui) (cli.Command, error) { return watch.New(ui, MakeShutdownCh()), nil }}, diff --git a/command/troubleshoot/connect/troubleshoot_connect.go b/command/troubleshoot/proxy/troubleshoot_proxy.go similarity index 87% rename from command/troubleshoot/connect/troubleshoot_connect.go rename to command/troubleshoot/proxy/troubleshoot_proxy.go index a7490a794c..8dde79300b 100644 --- a/command/troubleshoot/connect/troubleshoot_connect.go +++ b/command/troubleshoot/proxy/troubleshoot_proxy.go @@ -1,4 +1,4 @@ -package troubleshoot +package proxy import ( "flag" @@ -7,7 +7,7 @@ import ( "os" "github.com/hashicorp/consul/command/flags" - troubleshoot "github.com/hashicorp/consul/troubleshoot/connect" + troubleshoot "github.com/hashicorp/consul/troubleshoot" "github.com/mitchellh/cli" ) @@ -31,7 +31,7 @@ type cmd struct { func (c *cmd) init() { c.flags = flag.NewFlagSet("", flag.ContinueOnError) - c.flags.StringVar(&c.upstream, "upstream", os.Getenv("TROUBLESHOOT_CONNECT_UPSTREAM"), "The upstream service that receives the communication. ") + c.flags.StringVar(&c.upstream, "upstream", os.Getenv("TROUBLESHOOT_UPSTREAM"), "The upstream service that receives the communication. ") defaultAdminBind := "localhost:19000" if adminBind := os.Getenv("ADMIN_BIND"); adminBind != "" { @@ -53,7 +53,7 @@ func (c *cmd) Run(args []string) int { } if c.upstream == "" { - c.UI.Error("-upstream service SNI is required") + c.UI.Error("-upstream envoy identifier is required") return 1 } @@ -94,11 +94,11 @@ func (c *cmd) Help() string { } const ( - synopsis = "Troubleshoots service mesh issues" + synopsis = "Troubleshoots service mesh issues from the current envoy instance" help = ` Usage: consul troubleshoot proxy [options] Connects to local envoy proxy and troubleshoots service mesh communication issues. - Requires an upstream service SNI. + Requires an upstream service envoy identifier. Examples: $ consul troubleshoot proxy -upstream foo diff --git a/command/troubleshoot/connect/troubleshoot_connect_test.go b/command/troubleshoot/proxy/troubleshoot_proxy_test.go similarity index 68% rename from command/troubleshoot/connect/troubleshoot_connect_test.go rename to command/troubleshoot/proxy/troubleshoot_proxy_test.go index 9b2fca9f9d..7b88a62aaa 100644 --- a/command/troubleshoot/connect/troubleshoot_connect_test.go +++ b/command/troubleshoot/proxy/troubleshoot_proxy_test.go @@ -1,4 +1,4 @@ -package troubleshoot +package proxy import ( "strings" @@ -7,7 +7,7 @@ import ( "github.com/mitchellh/cli" ) -func TestTroubleshootConnectCommand_noTabs(t *testing.T) { +func TestTroubleshootProxyCommand_noTabs(t *testing.T) { t.Parallel() if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') { diff --git a/command/troubleshoot/troubleshoot.go b/command/troubleshoot/troubleshoot.go new file mode 100644 index 0000000000..2935d52cf8 --- /dev/null +++ b/command/troubleshoot/troubleshoot.go @@ -0,0 +1,44 @@ +package troubleshoot + +import ( + "github.com/hashicorp/consul/command/flags" + "github.com/mitchellh/cli" +) + +func New() *cmd { + return &cmd{} +} + +type cmd struct{} + +func (c *cmd) Run(args []string) int { + return cli.RunResultHelp +} + +func (c *cmd) Synopsis() string { + return synopsis +} + +func (c *cmd) Help() string { + return flags.Usage(help, nil) +} + +const synopsis = `CLI tools for troubleshooting Consul service mesh` +const help = ` +Usage: consul troubleshoot [options] + + This command has subcommands for troubleshooting the service mesh. + + Here are some simple examples, and more detailed examples are available + in the subcommands or the documentation. + + Troubleshoot Get Upstreams + + $ consul troubleshoot upstreams + + Troubleshoot Proxy + + $ consul troubleshoot proxy -upstream [options] + + For more examples, ask for subcommand help or view the documentation. +` diff --git a/command/troubleshoot/troubleshoot_test.go b/command/troubleshoot/troubleshoot_test.go new file mode 100644 index 0000000000..65e0042753 --- /dev/null +++ b/command/troubleshoot/troubleshoot_test.go @@ -0,0 +1,13 @@ +package troubleshoot + +import ( + "strings" + "testing" +) + +func TestTroubleshootCommand_noTabs(t *testing.T) { + t.Parallel() + if strings.ContainsRune(New().Help(), '\t') { + t.Fatal("help has tabs") + } +} diff --git a/command/troubleshoot/upstreams/troubleshoot_upstreams.go b/command/troubleshoot/upstreams/troubleshoot_upstreams.go index 0e955ae4c1..46b26135c7 100644 --- a/command/troubleshoot/upstreams/troubleshoot_upstreams.go +++ b/command/troubleshoot/upstreams/troubleshoot_upstreams.go @@ -7,7 +7,7 @@ import ( "os" "github.com/hashicorp/consul/command/flags" - troubleshoot "github.com/hashicorp/consul/troubleshoot/connect" + troubleshoot "github.com/hashicorp/consul/troubleshoot" "github.com/mitchellh/cli" ) @@ -89,13 +89,13 @@ func (c *cmd) Help() string { } const ( - synopsis = "Troubleshoots service mesh issues" + synopsis = "Get upstream envoy identifiers for the current envoy instance" help = ` Usage: consul troubleshoot upstreams [options] - Connects to local Envoy and lists upstream service envoy IDs. + Connects to local Envoy and lists upstream service envoy identifiers. This command is used in combination with - 'consul troubleshoot connect' to diagnose issues in Consul service mesh. + 'consul troubleshoot proxy' to diagnose issues in Consul service mesh. Examples: $ consul troubleshoot upstreams ` diff --git a/troubleshoot/connect/troubleshoot_connect.go b/troubleshoot/troubleshoot_proxy.go similarity index 100% rename from troubleshoot/connect/troubleshoot_connect.go rename to troubleshoot/troubleshoot_proxy.go