Skip to content

Commit cadf74a

Browse files
authored
Merge pull request #1287 from planetscale/postgres-branch-infra
Support Postgres branches in branch infra
2 parents 658c7f4 + ded5a90 commit cadf74a

3 files changed

Lines changed: 84 additions & 9 deletions

File tree

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/charmbracelet/huh v1.0.0
1515
github.com/charmbracelet/lipgloss v1.1.0
1616
github.com/charmbracelet/x/ansi v0.11.2
17+
github.com/dustin/go-humanize v1.0.1
1718
github.com/fatih/color v1.19.0
1819
github.com/frankban/quicktest v1.14.6
1920
github.com/go-sql-driver/mysql v1.9.3
@@ -31,7 +32,7 @@ require (
3132
github.com/mitchellh/go-homedir v1.1.0
3233
github.com/muesli/termenv v0.16.0
3334
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
34-
github.com/planetscale/planetscale-go v0.176.0
35+
github.com/planetscale/planetscale-go v0.177.0
3536
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4
3637
github.com/planetscale/psdbproxy v0.0.0-20250728082226-3f4ea3a74ec7
3738
github.com/spf13/cobra v1.10.2
@@ -68,7 +69,6 @@ require (
6869
github.com/clipperhouse/uax29/v2 v2.3.0 // indirect
6970
github.com/danieljoos/wincred v1.2.3 // indirect
7071
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
71-
github.com/dustin/go-humanize v1.0.1 // indirect
7272
github.com/dvsekhvalnov/jose2go v1.8.0 // indirect
7373
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
7474
github.com/fsnotify/fsnotify v1.9.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjL
183183
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
184184
github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e h1:MZ8D+Z3m2vvqGZLvoQfpaGg/j1fNDr4j03s3PRz4rVY=
185185
github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e/go.mod h1:hwAsSPQdvPa3WcfKfzTXxtEq/HlqwLjQasfO6QbGo4Q=
186-
github.com/planetscale/planetscale-go v0.176.0 h1:EkwiSM0+GbRVZ7/sCMP+AXD7VK1cO/gPBdMziY2JEMA=
187-
github.com/planetscale/planetscale-go v0.176.0/go.mod h1:paQCI5SgquuoewvMQM7R+r1XJO868bdP6/ihGidYRM0=
186+
github.com/planetscale/planetscale-go v0.177.0 h1:jVE8uInjGayJ7RLaqwBK7VPgu96U/99dwV2MbbuRywA=
187+
github.com/planetscale/planetscale-go v0.177.0/go.mod h1:paQCI5SgquuoewvMQM7R+r1XJO868bdP6/ihGidYRM0=
188188
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4 h1:Xv5pj20Rhfty1Tv0OVcidg4ez4PvGrpKvb6rvUwQgDs=
189189
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4/go.mod h1:M52h5IWxAcbdQ1hSZrLAGQC4ZXslxEsK/Wh9nu3wdWs=
190190
github.com/planetscale/psdbproxy v0.0.0-20250728082226-3f4ea3a74ec7 h1:aRd6vdE1fyuSI4RVj7oCr8lFmgqXvpnPUmN85VbZCp8=

internal/cmd/branch/infra.go

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"time"
77

8+
"github.com/dustin/go-humanize"
89
"github.com/planetscale/cli/internal/cmdutil"
910
"github.com/planetscale/cli/internal/printer"
1011
ps "github.com/planetscale/planetscale-go/planetscale"
@@ -74,6 +75,67 @@ func toInfraPods(pods []*ps.BranchInfraPod) []*InfraPod {
7475
return result
7576
}
7677

78+
type PostgresInfraRow struct {
79+
Component string `header:"component" json:"component"`
80+
Role string `header:"role" json:"role"`
81+
Size string `header:"size" json:"size"`
82+
AZ string `header:"az" json:"az"`
83+
Storage string `header:"storage" json:"storage"`
84+
Name string `header:"name" json:"name"`
85+
86+
orig any
87+
}
88+
89+
func (r *PostgresInfraRow) MarshalJSON() ([]byte, error) {
90+
return json.MarshalIndent(r.orig, "", " ")
91+
}
92+
93+
func (r *PostgresInfraRow) MarshalCSVValue() interface{} {
94+
return []*PostgresInfraRow{r}
95+
}
96+
97+
func toPostgresInfraRows(pg *ps.PostgresBranchInfrastructure) []*PostgresInfraRow {
98+
rows := make([]*PostgresInfraRow, 0, len(pg.Nodes)+len(pg.Bouncers))
99+
100+
for _, node := range pg.Nodes {
101+
storage := "-"
102+
if node.VolumeUsageBytes != nil && node.VolumeCapacityBytes != nil {
103+
storage = fmt.Sprintf("%s / %s",
104+
humanize.IBytes(uint64(*node.VolumeUsageBytes)),
105+
humanize.IBytes(uint64(*node.VolumeCapacityBytes)))
106+
}
107+
108+
rows = append(rows, &PostgresInfraRow{
109+
Component: "postgres",
110+
Role: node.Role,
111+
Size: node.ClusterDisplayName,
112+
AZ: node.AvailabilityZone,
113+
Storage: storage,
114+
Name: node.Name,
115+
orig: node,
116+
})
117+
}
118+
119+
for _, bouncer := range pg.Bouncers {
120+
size := "-"
121+
if bouncer.SKU != nil {
122+
size = bouncer.SKU.DisplayName
123+
}
124+
125+
rows = append(rows, &PostgresInfraRow{
126+
Component: "pgbouncer",
127+
Role: bouncer.Target,
128+
Size: size,
129+
AZ: "-",
130+
Storage: "-",
131+
Name: bouncer.Name,
132+
orig: bouncer,
133+
})
134+
}
135+
136+
return rows
137+
}
138+
77139
func humanAge(d time.Duration) string {
78140
if d < time.Minute {
79141
return fmt.Sprintf("%ds", int(d.Seconds()))
@@ -137,12 +199,25 @@ func InfraCmd(ch *cmdutil.Helper) *cobra.Command {
137199

138200
end()
139201

140-
if len(infra.Pods) == 0 && ch.Printer.Format() == printer.Human {
141-
ch.Printer.Printf("No pods found for branch %s.\n", printer.BoldBlue(branch))
142-
return nil
143-
}
202+
switch {
203+
case infra.Postgres != nil:
204+
rows := toPostgresInfraRows(infra.Postgres)
205+
if len(rows) == 0 && ch.Printer.Format() == printer.Human {
206+
ch.Printer.Printf("No nodes found for branch %s.\n", printer.BoldBlue(branch))
207+
return nil
208+
}
144209

145-
return ch.Printer.PrintResource(toInfraPods(infra.Pods))
210+
return ch.Printer.PrintResource(rows)
211+
case infra.Vitess != nil:
212+
if len(infra.Vitess.Pods) == 0 && ch.Printer.Format() == printer.Human {
213+
ch.Printer.Printf("No pods found for branch %s.\n", printer.BoldBlue(branch))
214+
return nil
215+
}
216+
217+
return ch.Printer.PrintResource(toInfraPods(infra.Vitess.Pods))
218+
default:
219+
return fmt.Errorf("unexpected infrastructure response for branch %s", branch)
220+
}
146221
},
147222
}
148223

0 commit comments

Comments
 (0)