TUN-3237: By default, don't show connections that are pending reconnect

This commit is contained in:
Adam Chalmers 2020-08-05 15:43:56 -05:00
parent a7562dff68
commit 1b61d699c4
3 changed files with 36 additions and 9 deletions

View File

@ -53,6 +53,11 @@ var (
Aliases: []string{"i"}, Aliases: []string{"i"},
Usage: "List tunnel by ID", 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{ outputFormatFlag = &cli.StringFlag{
Name: "output", Name: "output",
Aliases: []string{"o"}, Aliases: []string{"o"},
@ -234,7 +239,7 @@ func buildListCommand() *cli.Command {
Usage: "List existing tunnels", Usage: "List existing tunnels",
ArgsUsage: " ", ArgsUsage: " ",
Hidden: hideSubcommands, 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 { if len(tunnels) > 0 {
fmtAndPrintTunnelList(tunnels) fmtAndPrintTunnelList(tunnels, c.Bool("show-recently-disconnected"))
} else { } else {
fmt.Println("You have no tunnels, use 'cloudflared tunnel create' to define a new tunnel") 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 return nil
} }
func fmtAndPrintTunnelList(tunnels []tunnelstore.Tunnel) { func fmtAndPrintTunnelList(tunnels []tunnelstore.Tunnel, showRecentlyDisconnected bool) {
const ( const (
minWidth = 0 minWidth = 0
tabWidth = 8 tabWidth = 8
@ -305,7 +310,13 @@ func fmtAndPrintTunnelList(tunnels []tunnelstore.Tunnel) {
// Loop through tunnels, create formatted string for each, and print using tabwriter // Loop through tunnels, create formatted string for each, and print using tabwriter
for _, t := range tunnels { 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) fmt.Fprintln(writer, formattedStr)
} }
@ -313,12 +324,14 @@ func fmtAndPrintTunnelList(tunnels []tunnelstore.Tunnel) {
writer.Flush() writer.Flush()
} }
func fmtConnections(connections []tunnelstore.Connection) string { func fmtConnections(connections []tunnelstore.Connection, showRecentlyDisconnected bool) string {
// Count connections per colo // Count connections per colo
numConnsPerColo := make(map[string]uint, len(connections)) numConnsPerColo := make(map[string]uint, len(connections))
for _, connection := range connections { for _, connection := range connections {
numConnsPerColo[connection.ColoName]++ if !connection.IsPendingReconnect || showRecentlyDisconnected {
numConnsPerColo[connection.ColoName]++
}
} }
// Get sorted list of colos // Get sorted list of colos

View File

@ -40,6 +40,19 @@ func Test_fmtConnections(t *testing.T) {
}, },
want: "1xDFW", 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", name: "many colos",
args: args{ args: args{
@ -67,7 +80,7 @@ func Test_fmtConnections(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { 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) t.Errorf("fmtConnections() = %v, want %v", got, tt.want)
} }
}) })

View File

@ -38,8 +38,9 @@ type Tunnel struct {
} }
type Connection struct { type Connection struct {
ColoName string `json:"colo_name"` ColoName string `json:"colo_name"`
ID uuid.UUID `json:"uuid"` ID uuid.UUID `json:"uuid"`
IsPendingReconnect bool `json:"is_pending_reconnect"`
} }
// Route represents a record type that can route to a tunnel // Route represents a record type that can route to a tunnel