From 1b61d699c4f50ab28fab22ee20ed8287704b8702 Mon Sep 17 00:00:00 2001 From: Adam Chalmers Date: Wed, 5 Aug 2020 15:43:56 -0500 Subject: [PATCH] TUN-3237: By default, don't show connections that are pending reconnect --- cmd/cloudflared/tunnel/subcommands.go | 25 ++++++++++++++++------ cmd/cloudflared/tunnel/subcommands_test.go | 15 ++++++++++++- tunnelstore/client.go | 5 +++-- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/cmd/cloudflared/tunnel/subcommands.go b/cmd/cloudflared/tunnel/subcommands.go index ad6a03f7..25858d0c 100644 --- a/cmd/cloudflared/tunnel/subcommands.go +++ b/cmd/cloudflared/tunnel/subcommands.go @@ -53,6 +53,11 @@ var ( Aliases: []string{"i"}, Usage: "List tunnel by ID", } + showRecentlyDisconnected = &cli.BoolFlag{ + Name: "show-recently-disconnected", + Aliases: []string{"rd"}, + Usage: "Include connections that have recently disconnected in the list", + } outputFormatFlag = &cli.StringFlag{ Name: "output", Aliases: []string{"o"}, @@ -234,7 +239,7 @@ func buildListCommand() *cli.Command { Usage: "List existing tunnels", ArgsUsage: " ", Hidden: hideSubcommands, - Flags: []cli.Flag{outputFormatFlag, showDeletedFlag, listNameFlag, listExistedAtFlag, listIDFlag}, + Flags: []cli.Flag{outputFormatFlag, showDeletedFlag, listNameFlag, listExistedAtFlag, listIDFlag, showRecentlyDisconnected}, } } @@ -281,7 +286,7 @@ func listTunnels(c *cli.Context) error { } if len(tunnels) > 0 { - fmtAndPrintTunnelList(tunnels) + fmtAndPrintTunnelList(tunnels, c.Bool("show-recently-disconnected")) } else { fmt.Println("You have no tunnels, use 'cloudflared tunnel create' to define a new tunnel") } @@ -289,7 +294,7 @@ func listTunnels(c *cli.Context) error { return nil } -func fmtAndPrintTunnelList(tunnels []tunnelstore.Tunnel) { +func fmtAndPrintTunnelList(tunnels []tunnelstore.Tunnel, showRecentlyDisconnected bool) { const ( minWidth = 0 tabWidth = 8 @@ -305,7 +310,13 @@ func fmtAndPrintTunnelList(tunnels []tunnelstore.Tunnel) { // Loop through tunnels, create formatted string for each, and print using tabwriter for _, t := range tunnels { - formattedStr := fmt.Sprintf("%s\t%s\t%s\t%s\t", t.ID, t.Name, t.CreatedAt.Format(time.RFC3339), fmtConnections(t.Connections)) + formattedStr := fmt.Sprintf( + "%s\t%s\t%s\t%s\t", + t.ID, + t.Name, + t.CreatedAt.Format(time.RFC3339), + fmtConnections(t.Connections, showRecentlyDisconnected), + ) fmt.Fprintln(writer, formattedStr) } @@ -313,12 +324,14 @@ func fmtAndPrintTunnelList(tunnels []tunnelstore.Tunnel) { writer.Flush() } -func fmtConnections(connections []tunnelstore.Connection) string { +func fmtConnections(connections []tunnelstore.Connection, showRecentlyDisconnected bool) string { // Count connections per colo numConnsPerColo := make(map[string]uint, len(connections)) for _, connection := range connections { - numConnsPerColo[connection.ColoName]++ + if !connection.IsPendingReconnect || showRecentlyDisconnected { + numConnsPerColo[connection.ColoName]++ + } } // Get sorted list of colos diff --git a/cmd/cloudflared/tunnel/subcommands_test.go b/cmd/cloudflared/tunnel/subcommands_test.go index fb5ea914..343419ee 100644 --- a/cmd/cloudflared/tunnel/subcommands_test.go +++ b/cmd/cloudflared/tunnel/subcommands_test.go @@ -40,6 +40,19 @@ func Test_fmtConnections(t *testing.T) { }, want: "1xDFW", }, + { + name: "with a pending reconnect", + args: args{ + connections: []tunnelstore.Connection{ + { + ColoName: "DFW", + ID: uuid.MustParse("ea550130-57fd-4463-aab1-752822231ddd"), + IsPendingReconnect: true, + }, + }, + }, + want: "", + }, { name: "many colos", args: args{ @@ -67,7 +80,7 @@ func Test_fmtConnections(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := fmtConnections(tt.args.connections); got != tt.want { + if got := fmtConnections(tt.args.connections, false); got != tt.want { t.Errorf("fmtConnections() = %v, want %v", got, tt.want) } }) diff --git a/tunnelstore/client.go b/tunnelstore/client.go index e5d11681..991e59ec 100644 --- a/tunnelstore/client.go +++ b/tunnelstore/client.go @@ -38,8 +38,9 @@ type Tunnel struct { } type Connection struct { - ColoName string `json:"colo_name"` - ID uuid.UUID `json:"uuid"` + ColoName string `json:"colo_name"` + ID uuid.UUID `json:"uuid"` + IsPendingReconnect bool `json:"is_pending_reconnect"` } // Route represents a record type that can route to a tunnel