From f10247db900551234828ee7707c490a9668846cf Mon Sep 17 00:00:00 2001 From: Mads Jon Nielsen Date: Thu, 28 Dec 2023 19:50:49 +0100 Subject: [PATCH 1/9] Add cloudflared tunnel health command --- cmd/cloudflared/tunnel/cmd.go | 1 + cmd/cloudflared/tunnel/subcommands.go | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/cmd/cloudflared/tunnel/cmd.go b/cmd/cloudflared/tunnel/cmd.go index e5b0ebb5529..98a889e38b1 100644 --- a/cmd/cloudflared/tunnel/cmd.go +++ b/cmd/cloudflared/tunnel/cmd.go @@ -128,6 +128,7 @@ func Commands() []*cli.Command { buildVirtualNetworkSubcommand(false), buildRunCommand(), buildListCommand(), + buildHealthCommand(), buildInfoCommand(), buildIngressSubcommand(), buildDeleteCommand(), diff --git a/cmd/cloudflared/tunnel/subcommands.go b/cmd/cloudflared/tunnel/subcommands.go index bef86887ebc..62605c89528 100644 --- a/cmd/cloudflared/tunnel/subcommands.go +++ b/cmd/cloudflared/tunnel/subcommands.go @@ -5,6 +5,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + "net/http" "os" "path/filepath" "regexp" @@ -397,6 +398,31 @@ func fmtConnections(connections []cfapi.Connection, showRecentlyDisconnected boo return strings.Join(output, ", ") } +func buildHealthCommand() *cli.Command { + return &cli.Command{ + Name: "health", + Action: cliutil.ConfiguredAction(healthCommand), + Usage: "Tunnel health exit code", + UsageText: "cloudflared tunnel [tunnel command options] health [subcommand options]", + Description: "cloudflared tunnel health will return proper exit code if tunnel is healthy or unhealthy", + Flags: []cli.Flag{}, + CustomHelpTemplate: commandHelpTemplate(), + } +} + +func healthCommand(c *cli.Context) error { + metrics := strings.Split(c.String("metrics"), ":") + requestURL := fmt.Sprintf("http://%s:%s/ready", metrics[0], metrics[1]) + res, err := http.Get(requestURL) + if err != nil { + return err + } + if res.StatusCode != 200 { + return fmt.Errorf("health /ready endpoint returned status code %d", res.StatusCode) + } + return nil +} + func buildInfoCommand() *cli.Command { return &cli.Command{ Name: "info", From 521f5632d74e9edf6110653f4d5a09145dbd2535 Mon Sep 17 00:00:00 2001 From: Mads Jon Nielsen Date: Wed, 3 Jan 2024 10:37:41 +0100 Subject: [PATCH 2/9] Just use c.String("metrics") Co-authored-by: Julien Laffaye --- cmd/cloudflared/tunnel/subcommands.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/cloudflared/tunnel/subcommands.go b/cmd/cloudflared/tunnel/subcommands.go index 62605c89528..b0084443d4e 100644 --- a/cmd/cloudflared/tunnel/subcommands.go +++ b/cmd/cloudflared/tunnel/subcommands.go @@ -412,7 +412,7 @@ func buildHealthCommand() *cli.Command { func healthCommand(c *cli.Context) error { metrics := strings.Split(c.String("metrics"), ":") - requestURL := fmt.Sprintf("http://%s:%s/ready", metrics[0], metrics[1]) + requestURL := fmt.Sprintf("http://%s/ready", c.String("metrics")) res, err := http.Get(requestURL) if err != nil { return err From 2bf652c6fd4becf4049dcf8b124e96d5d7f43315 Mon Sep 17 00:00:00 2001 From: Mads Jon Nielsen Date: Wed, 3 Jan 2024 10:38:55 +0100 Subject: [PATCH 3/9] Update subcommands.go --- cmd/cloudflared/tunnel/subcommands.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/cloudflared/tunnel/subcommands.go b/cmd/cloudflared/tunnel/subcommands.go index b0084443d4e..9e99aa15f49 100644 --- a/cmd/cloudflared/tunnel/subcommands.go +++ b/cmd/cloudflared/tunnel/subcommands.go @@ -411,7 +411,6 @@ func buildHealthCommand() *cli.Command { } func healthCommand(c *cli.Context) error { - metrics := strings.Split(c.String("metrics"), ":") requestURL := fmt.Sprintf("http://%s/ready", c.String("metrics")) res, err := http.Get(requestURL) if err != nil { From 37210ff661a8f4f13415d6f7c11084cdb7776629 Mon Sep 17 00:00:00 2001 From: Mads Jon Nielsen Date: Tue, 23 Apr 2024 08:20:53 +0200 Subject: [PATCH 4/9] Use /healthcheck over /ready --- cmd/cloudflared/tunnel/subcommands.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/cloudflared/tunnel/subcommands.go b/cmd/cloudflared/tunnel/subcommands.go index 9e99aa15f49..98d97beba65 100644 --- a/cmd/cloudflared/tunnel/subcommands.go +++ b/cmd/cloudflared/tunnel/subcommands.go @@ -411,13 +411,13 @@ func buildHealthCommand() *cli.Command { } func healthCommand(c *cli.Context) error { - requestURL := fmt.Sprintf("http://%s/ready", c.String("metrics")) + requestURL := fmt.Sprintf("http://%s/healthcheck", c.String("metrics")) res, err := http.Get(requestURL) if err != nil { return err } if res.StatusCode != 200 { - return fmt.Errorf("health /ready endpoint returned status code %d", res.StatusCode) + return fmt.Errorf("health /ready endpoint returned status code %d\n%s", res.StatusCode, res.Body) } return nil } From b342c7403cce445046279871bde59190ac3c6779 Mon Sep 17 00:00:00 2001 From: Mads Jon Nielsen Date: Tue, 23 Apr 2024 08:35:25 +0200 Subject: [PATCH 5/9] Rename command to ready --- cmd/cloudflared/tunnel/cmd.go | 2 +- cmd/cloudflared/tunnel/subcommands.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/cloudflared/tunnel/cmd.go b/cmd/cloudflared/tunnel/cmd.go index 067d79ba4a1..823e2d3dfba 100644 --- a/cmd/cloudflared/tunnel/cmd.go +++ b/cmd/cloudflared/tunnel/cmd.go @@ -131,7 +131,7 @@ func Commands() []*cli.Command { buildVirtualNetworkSubcommand(false), buildRunCommand(), buildListCommand(), - buildHealthCommand(), + buildReadyCommand(), buildInfoCommand(), buildIngressSubcommand(), buildDeleteCommand(), diff --git a/cmd/cloudflared/tunnel/subcommands.go b/cmd/cloudflared/tunnel/subcommands.go index 98d97beba65..96d51f32da7 100644 --- a/cmd/cloudflared/tunnel/subcommands.go +++ b/cmd/cloudflared/tunnel/subcommands.go @@ -398,10 +398,10 @@ func fmtConnections(connections []cfapi.Connection, showRecentlyDisconnected boo return strings.Join(output, ", ") } -func buildHealthCommand() *cli.Command { +func buildReadyCommand() *cli.Command { return &cli.Command{ Name: "health", - Action: cliutil.ConfiguredAction(healthCommand), + Action: cliutil.ConfiguredAction(readyCommand), Usage: "Tunnel health exit code", UsageText: "cloudflared tunnel [tunnel command options] health [subcommand options]", Description: "cloudflared tunnel health will return proper exit code if tunnel is healthy or unhealthy", @@ -410,8 +410,8 @@ func buildHealthCommand() *cli.Command { } } -func healthCommand(c *cli.Context) error { - requestURL := fmt.Sprintf("http://%s/healthcheck", c.String("metrics")) +func readyCommand(c *cli.Context) error { + requestURL := fmt.Sprintf("http://%s/ready", c.String("metrics")) res, err := http.Get(requestURL) if err != nil { return err From e03f53144b224c91d8c82261402be30719a5042a Mon Sep 17 00:00:00 2001 From: Mads Jon Nielsen Date: Tue, 23 Apr 2024 08:37:37 +0200 Subject: [PATCH 6/9] Rename command description and usage --- cmd/cloudflared/tunnel/subcommands.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/cloudflared/tunnel/subcommands.go b/cmd/cloudflared/tunnel/subcommands.go index 96d51f32da7..13f90bcf6cd 100644 --- a/cmd/cloudflared/tunnel/subcommands.go +++ b/cmd/cloudflared/tunnel/subcommands.go @@ -400,11 +400,11 @@ func fmtConnections(connections []cfapi.Connection, showRecentlyDisconnected boo func buildReadyCommand() *cli.Command { return &cli.Command{ - Name: "health", + Name: "ready", Action: cliutil.ConfiguredAction(readyCommand), - Usage: "Tunnel health exit code", - UsageText: "cloudflared tunnel [tunnel command options] health [subcommand options]", - Description: "cloudflared tunnel health will return proper exit code if tunnel is healthy or unhealthy", + Usage: "Tunnel /ready exit code", + UsageText: "cloudflared tunnel [tunnel command options] ready [subcommand options]", + Description: "cloudflared tunnel ready will return proper exit code if tunnel is ready or not", Flags: []cli.Flag{}, CustomHelpTemplate: commandHelpTemplate(), } @@ -417,7 +417,7 @@ func readyCommand(c *cli.Context) error { return err } if res.StatusCode != 200 { - return fmt.Errorf("health /ready endpoint returned status code %d\n%s", res.StatusCode, res.Body) + return fmt.Errorf("/ready endpoint returned status code %d\n%s", res.StatusCode, res.Body) } return nil } From d6b03fbabf6a2248e16538f37c328c4b95bd3a99 Mon Sep 17 00:00:00 2001 From: Mads Jon Nielsen Date: Tue, 23 Apr 2024 08:53:32 +0200 Subject: [PATCH 7/9] Prettify Usage and Description --- cmd/cloudflared/tunnel/subcommands.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/cloudflared/tunnel/subcommands.go b/cmd/cloudflared/tunnel/subcommands.go index 13f90bcf6cd..f8d67e7dc44 100644 --- a/cmd/cloudflared/tunnel/subcommands.go +++ b/cmd/cloudflared/tunnel/subcommands.go @@ -402,9 +402,9 @@ func buildReadyCommand() *cli.Command { return &cli.Command{ Name: "ready", Action: cliutil.ConfiguredAction(readyCommand), - Usage: "Tunnel /ready exit code", + Usage: "Call /ready endpoint and returns proper exit code", UsageText: "cloudflared tunnel [tunnel command options] ready [subcommand options]", - Description: "cloudflared tunnel ready will return proper exit code if tunnel is ready or not", + Description: "cloudflared tunnel ready will return proper exit code based on the /ready endpoint", Flags: []cli.Flag{}, CustomHelpTemplate: commandHelpTemplate(), } From d094e52bd130706f856033a64e2acf1e7d954a1b Mon Sep 17 00:00:00 2001 From: Mads Jon Nielsen Date: Tue, 23 Apr 2024 08:53:49 +0200 Subject: [PATCH 8/9] Prettify Usage and Description --- cmd/cloudflared/tunnel/subcommands.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/cloudflared/tunnel/subcommands.go b/cmd/cloudflared/tunnel/subcommands.go index f8d67e7dc44..14f6bd3db39 100644 --- a/cmd/cloudflared/tunnel/subcommands.go +++ b/cmd/cloudflared/tunnel/subcommands.go @@ -402,7 +402,7 @@ func buildReadyCommand() *cli.Command { return &cli.Command{ Name: "ready", Action: cliutil.ConfiguredAction(readyCommand), - Usage: "Call /ready endpoint and returns proper exit code", + Usage: "Call /ready endpoint and return proper exit code", UsageText: "cloudflared tunnel [tunnel command options] ready [subcommand options]", Description: "cloudflared tunnel ready will return proper exit code based on the /ready endpoint", Flags: []cli.Flag{}, From 2941825577186689988a24a7a8bad0bb2f554755 Mon Sep 17 00:00:00 2001 From: Mads Jon Nielsen Date: Tue, 23 Apr 2024 09:00:39 +0200 Subject: [PATCH 9/9] Make sure body is properly printed when status code not equals 200 --- cmd/cloudflared/tunnel/subcommands.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cmd/cloudflared/tunnel/subcommands.go b/cmd/cloudflared/tunnel/subcommands.go index 14f6bd3db39..20604290bb8 100644 --- a/cmd/cloudflared/tunnel/subcommands.go +++ b/cmd/cloudflared/tunnel/subcommands.go @@ -5,6 +5,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + "io" "net/http" "os" "path/filepath" @@ -411,13 +412,18 @@ func buildReadyCommand() *cli.Command { } func readyCommand(c *cli.Context) error { - requestURL := fmt.Sprintf("http://%s/ready", c.String("metrics")) + metricsOpts := c.String("metrics") + requestURL := fmt.Sprintf("http://%s/ready", metricsOpts) res, err := http.Get(requestURL) if err != nil { return err } if res.StatusCode != 200 { - return fmt.Errorf("/ready endpoint returned status code %d\n%s", res.StatusCode, res.Body) + body, err := io.ReadAll(res.Body) + if err != nil { + return err + } + return fmt.Errorf("http://%s/ready endpoint returned status code %d\n%s", metricsOpts, res.StatusCode, body) } return nil }