Skip to content

Commit 1b61d69

Browse files
committed
TUN-3237: By default, don't show connections that are pending reconnect
1 parent a7562df commit 1b61d69

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

cmd/cloudflared/tunnel/subcommands.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ var (
5353
Aliases: []string{"i"},
5454
Usage: "List tunnel by ID",
5555
}
56+
showRecentlyDisconnected = &cli.BoolFlag{
57+
Name: "show-recently-disconnected",
58+
Aliases: []string{"rd"},
59+
Usage: "Include connections that have recently disconnected in the list",
60+
}
5661
outputFormatFlag = &cli.StringFlag{
5762
Name: "output",
5863
Aliases: []string{"o"},
@@ -234,7 +239,7 @@ func buildListCommand() *cli.Command {
234239
Usage: "List existing tunnels",
235240
ArgsUsage: " ",
236241
Hidden: hideSubcommands,
237-
Flags: []cli.Flag{outputFormatFlag, showDeletedFlag, listNameFlag, listExistedAtFlag, listIDFlag},
242+
Flags: []cli.Flag{outputFormatFlag, showDeletedFlag, listNameFlag, listExistedAtFlag, listIDFlag, showRecentlyDisconnected},
238243
}
239244
}
240245

@@ -281,15 +286,15 @@ func listTunnels(c *cli.Context) error {
281286
}
282287

283288
if len(tunnels) > 0 {
284-
fmtAndPrintTunnelList(tunnels)
289+
fmtAndPrintTunnelList(tunnels, c.Bool("show-recently-disconnected"))
285290
} else {
286291
fmt.Println("You have no tunnels, use 'cloudflared tunnel create' to define a new tunnel")
287292
}
288293

289294
return nil
290295
}
291296

292-
func fmtAndPrintTunnelList(tunnels []tunnelstore.Tunnel) {
297+
func fmtAndPrintTunnelList(tunnels []tunnelstore.Tunnel, showRecentlyDisconnected bool) {
293298
const (
294299
minWidth = 0
295300
tabWidth = 8
@@ -305,20 +310,28 @@ func fmtAndPrintTunnelList(tunnels []tunnelstore.Tunnel) {
305310

306311
// Loop through tunnels, create formatted string for each, and print using tabwriter
307312
for _, t := range tunnels {
308-
formattedStr := fmt.Sprintf("%s\t%s\t%s\t%s\t", t.ID, t.Name, t.CreatedAt.Format(time.RFC3339), fmtConnections(t.Connections))
313+
formattedStr := fmt.Sprintf(
314+
"%s\t%s\t%s\t%s\t",
315+
t.ID,
316+
t.Name,
317+
t.CreatedAt.Format(time.RFC3339),
318+
fmtConnections(t.Connections, showRecentlyDisconnected),
319+
)
309320
fmt.Fprintln(writer, formattedStr)
310321
}
311322

312323
// Write data buffered in tabwriter to output
313324
writer.Flush()
314325
}
315326

316-
func fmtConnections(connections []tunnelstore.Connection) string {
327+
func fmtConnections(connections []tunnelstore.Connection, showRecentlyDisconnected bool) string {
317328

318329
// Count connections per colo
319330
numConnsPerColo := make(map[string]uint, len(connections))
320331
for _, connection := range connections {
321-
numConnsPerColo[connection.ColoName]++
332+
if !connection.IsPendingReconnect || showRecentlyDisconnected {
333+
numConnsPerColo[connection.ColoName]++
334+
}
322335
}
323336

324337
// Get sorted list of colos

cmd/cloudflared/tunnel/subcommands_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ func Test_fmtConnections(t *testing.T) {
4040
},
4141
want: "1xDFW",
4242
},
43+
{
44+
name: "with a pending reconnect",
45+
args: args{
46+
connections: []tunnelstore.Connection{
47+
{
48+
ColoName: "DFW",
49+
ID: uuid.MustParse("ea550130-57fd-4463-aab1-752822231ddd"),
50+
IsPendingReconnect: true,
51+
},
52+
},
53+
},
54+
want: "",
55+
},
4356
{
4457
name: "many colos",
4558
args: args{
@@ -67,7 +80,7 @@ func Test_fmtConnections(t *testing.T) {
6780
}
6881
for _, tt := range tests {
6982
t.Run(tt.name, func(t *testing.T) {
70-
if got := fmtConnections(tt.args.connections); got != tt.want {
83+
if got := fmtConnections(tt.args.connections, false); got != tt.want {
7184
t.Errorf("fmtConnections() = %v, want %v", got, tt.want)
7285
}
7386
})

tunnelstore/client.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ type Tunnel struct {
3838
}
3939

4040
type Connection struct {
41-
ColoName string `json:"colo_name"`
42-
ID uuid.UUID `json:"uuid"`
41+
ColoName string `json:"colo_name"`
42+
ID uuid.UUID `json:"uuid"`
43+
IsPendingReconnect bool `json:"is_pending_reconnect"`
4344
}
4445

4546
// Route represents a record type that can route to a tunnel

0 commit comments

Comments
 (0)