Skip to content

Commit 3480a33

Browse files
authored
Merge pull request #1135 from firecow/tunnel-health
Add cloudflared tunnel ready command
2 parents a26b2a0 + c39f0ae commit 3480a33

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

cmd/cloudflared/tunnel/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ func Commands() []*cli.Command {
139139
buildVirtualNetworkSubcommand(false),
140140
buildRunCommand(),
141141
buildListCommand(),
142+
buildReadyCommand(),
142143
buildInfoCommand(),
143144
buildIngressSubcommand(),
144145
buildDeleteCommand(),

cmd/cloudflared/tunnel/subcommands.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"encoding/base64"
66
"encoding/json"
77
"fmt"
8+
"io"
9+
"net/http"
810
"os"
911
"path/filepath"
1012
"regexp"
@@ -397,6 +399,39 @@ func fmtConnections(connections []cfapi.Connection, showRecentlyDisconnected boo
397399
return strings.Join(output, ", ")
398400
}
399401

402+
func buildReadyCommand() *cli.Command {
403+
return &cli.Command{
404+
Name: "ready",
405+
Action: cliutil.ConfiguredAction(readyCommand),
406+
Usage: "Call /ready endpoint and return proper exit code",
407+
UsageText: "cloudflared tunnel [tunnel command options] ready [subcommand options]",
408+
Description: "cloudflared tunnel ready will return proper exit code based on the /ready endpoint",
409+
Flags: []cli.Flag{},
410+
CustomHelpTemplate: commandHelpTemplate(),
411+
}
412+
}
413+
414+
func readyCommand(c *cli.Context) error {
415+
metricsOpts := c.String("metrics")
416+
if !c.IsSet("metrics") {
417+
return fmt.Errorf("--metrics has to be provided")
418+
}
419+
420+
requestURL := fmt.Sprintf("http://%s/ready", metricsOpts)
421+
res, err := http.Get(requestURL)
422+
if err != nil {
423+
return err
424+
}
425+
if res.StatusCode != 200 {
426+
body, err := io.ReadAll(res.Body)
427+
if err != nil {
428+
return err
429+
}
430+
return fmt.Errorf("http://%s/ready endpoint returned status code %d\n%s", metricsOpts, res.StatusCode, body)
431+
}
432+
return nil
433+
}
434+
400435
func buildInfoCommand() *cli.Command {
401436
return &cli.Command{
402437
Name: "info",

0 commit comments

Comments
 (0)