Skip to content

Commit faab36a

Browse files
authored
\\Better handling for Postgres image versions and features on Stolon clusters (#3803)
* Check whether a cluster is Flex before allowing backup-related commands. * Emit better errors when image versions are malformed and likely indicate an update. * Add missing nil pointer check.
1 parent 25aa2b7 commit faab36a

File tree

2 files changed

+74
-50
lines changed

2 files changed

+74
-50
lines changed

internal/command/postgres/backup.go

Lines changed: 73 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,6 @@ func runBackupRestore(ctx context.Context) error {
8686
destAppName = flag.FirstArg(ctx)
8787
)
8888

89-
enabled, err := isBackupEnabled(ctx, appName)
90-
if err != nil {
91-
return err
92-
}
93-
94-
if !enabled {
95-
return fmt.Errorf("backups are not enabled. Run `fly pg backup enable -a %s` to enable them", appName)
96-
}
97-
9889
flapsClient, err := flapsutil.NewClientWithOptions(ctx, flaps.NewClientOpts{
9990
AppName: appName,
10091
})
@@ -111,17 +102,30 @@ func runBackupRestore(ctx context.Context) error {
111102
return fmt.Errorf("No active machines")
112103
}
113104

114-
// Ensure the the app has the required flex version.
115-
if err := hasRequiredFlexVersionOnMachines(appName, machines, backupVersion); err != nil {
105+
// Resolve the leader
106+
leader, err := pickLeader(ctx, machines)
107+
if err != nil {
116108
return err
117109
}
118110

119-
// Resolve the leader
120-
leader, err := pickLeader(ctx, machines)
111+
if !IsFlex(leader) {
112+
return fmt.Errorf("backups are only supported on Flexclusters")
113+
}
114+
115+
enabled, err := isBackupEnabled(ctx, appName)
121116
if err != nil {
122117
return err
123118
}
124119

120+
if !enabled {
121+
return fmt.Errorf("backups are not enabled. Run `fly pg backup enable -a %s` to enable them", appName)
122+
}
123+
124+
// Ensure the the app has the required flex version.
125+
if err := hasRequiredFlexVersionOnMachines(appName, machines, backupVersion); err != nil {
126+
return err
127+
}
128+
125129
// TODO - Use this to create new Tigris access keys. However, if we can't yet revoke
126130
// access keys after the restore process completes, we should understand the implications of
127131
// creating potentially many access keys.
@@ -212,15 +216,6 @@ func runBackupCreate(ctx context.Context) error {
212216
appName = appconfig.NameFromContext(ctx)
213217
)
214218

215-
enabled, err := isBackupEnabled(ctx, appName)
216-
if err != nil {
217-
return err
218-
}
219-
220-
if !enabled {
221-
return fmt.Errorf("backups are not enabled. Run `fly pg backup enable -a %s` to enable them", appName)
222-
}
223-
224219
flapsClient, err := flapsutil.NewClientWithOptions(ctx, flaps.NewClientOpts{
225220
AppName: appName,
226221
})
@@ -237,16 +232,29 @@ func runBackupCreate(ctx context.Context) error {
237232
return fmt.Errorf("No active machines")
238233
}
239234

240-
if err := hasRequiredFlexVersionOnMachines(appName, machines, backupVersion); err != nil {
235+
// Ensure the backup is issued against the primary.
236+
leader, err := pickLeader(ctx, machines)
237+
if err != nil {
241238
return err
242239
}
243240

244-
// Ensure the backup is issued against the primary.
245-
leader, err := pickLeader(ctx, machines)
241+
if !IsFlex(leader) {
242+
return fmt.Errorf("backups are only supported on Flexclusters")
243+
}
244+
245+
enabled, err := isBackupEnabled(ctx, appName)
246246
if err != nil {
247247
return err
248248
}
249249

250+
if !enabled {
251+
return fmt.Errorf("backups are not enabled. Run `fly pg backup enable -a %s` to enable them", appName)
252+
}
253+
254+
if err := hasRequiredFlexVersionOnMachines(appName, machines, backupVersion); err != nil {
255+
return err
256+
}
257+
250258
if !hasRequiredMemoryForBackup(*leader) {
251259
return fmt.Errorf("backup creation requires at least 512MB of memory. Use `fly m update %s --vm-memory 512` to scale up.", leader.ID)
252260
}
@@ -330,12 +338,16 @@ func runBackupEnable(ctx context.Context) error {
330338
return fmt.Errorf("No active machines")
331339
}
332340

333-
if err := hasRequiredFlexVersionOnMachines(appName, machines, backupVersion); err != nil {
341+
leader, err := pickLeader(ctx, machines)
342+
if err != nil {
334343
return err
335344
}
336345

337-
leader, err := pickLeader(ctx, machines)
338-
if err != nil {
346+
if !IsFlex(leader) {
347+
return fmt.Errorf("backups are only supported on Flexclusters")
348+
}
349+
350+
if err := hasRequiredFlexVersionOnMachines(appName, machines, backupVersion); err != nil {
339351
return err
340352
}
341353

@@ -397,23 +409,14 @@ func runBackupList(ctx context.Context) error {
397409
appName = appconfig.NameFromContext(ctx)
398410
)
399411

400-
enabled, err := isBackupEnabled(ctx, appName)
401-
if err != nil {
402-
return err
403-
}
404-
405-
if !enabled {
406-
return fmt.Errorf("backups are not enabled. Run `fly pg backup enable -a %s` to enable them", appName)
407-
}
408-
409412
flapsClient, err := flapsutil.NewClientWithOptions(ctx, flaps.NewClientOpts{
410413
AppName: appName,
411414
})
412415
if err != nil {
413416
return fmt.Errorf("list of machines could not be retrieved: %w", err)
414417
}
415418

416-
machines, err := flapsClient.List(ctx, "started")
419+
machines, err := flapsClient.ListActive(ctx)
417420
if err != nil {
418421
return err
419422
}
@@ -422,11 +425,24 @@ func runBackupList(ctx context.Context) error {
422425
return fmt.Errorf("No active machines")
423426
}
424427

425-
if err = hasRequiredFlexVersionOnMachines(appName, machines, backupVersion); err != nil {
428+
machine := machines[0]
429+
430+
if !IsFlex(machine) {
431+
return fmt.Errorf("backups are only supported on Flexclusters")
432+
}
433+
434+
enabled, err := isBackupEnabled(ctx, appName)
435+
if err != nil {
426436
return err
427437
}
428438

429-
machine := machines[0]
439+
if !enabled {
440+
return fmt.Errorf("backups are not enabled. Run `fly pg backup enable -a %s` to enable them", appName)
441+
}
442+
443+
if err = hasRequiredFlexVersionOnMachines(appName, machines, backupVersion); err != nil {
444+
return err
445+
}
430446

431447
return ExecOnMachine(ctx, flapsClient, machine.ID, "flexctl backup list")
432448
}
@@ -540,15 +556,6 @@ func runBackupConfigShow(ctx context.Context) error {
540556
appName = appconfig.NameFromContext(ctx)
541557
)
542558

543-
enabled, err := isBackupEnabled(ctx, appName)
544-
if err != nil {
545-
return err
546-
}
547-
548-
if !enabled {
549-
return fmt.Errorf("backups are not enabled. Run `fly pg backup enable -a %s` to enable them", appName)
550-
}
551-
552559
flapsClient, err := flapsutil.NewClientWithOptions(ctx, flaps.NewClientOpts{
553560
AppName: appName,
554561
})
@@ -565,6 +572,19 @@ func runBackupConfigShow(ctx context.Context) error {
565572
return fmt.Errorf("No active machines")
566573
}
567574

575+
if !IsFlex(machines[0]) {
576+
return fmt.Errorf("backups are only supported on Flexclusters")
577+
}
578+
579+
enabled, err := isBackupEnabled(ctx, appName)
580+
if err != nil {
581+
return err
582+
}
583+
584+
if !enabled {
585+
return fmt.Errorf("backups are not enabled. Run `fly pg backup enable -a %s` to enable them", appName)
586+
}
587+
568588
// Ensure the the app has the required flex version.
569589
if err := hasRequiredFlexVersionOnMachines(appName, machines, backupConfigVersion); err != nil {
570590
return err
@@ -594,6 +614,10 @@ func runBackupConfigUpdate(ctx context.Context) error {
594614
return fmt.Errorf("No active machines")
595615
}
596616

617+
if !IsFlex(machines[0]) {
618+
return fmt.Errorf("backups are only supported on Flexclusters")
619+
}
620+
597621
// Ensure the the app has the required flex version.
598622
if err := hasRequiredFlexVersionOnMachines(appName, machines, backupConfigVersion); err != nil {
599623
return err

internal/command/postgres/postgres.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func hasRequiredFlexVersionOnMachines(appName string, machines []*fly.Machine, f
126126
}
127127

128128
err := hasRequiredVersionOnMachines(appName, machines, "", flexVersion, "")
129-
if strings.Contains(err.Error(), "Malformed version") {
129+
if err != nil && strings.Contains(err.Error(), "Malformed version") {
130130
return fmt.Errorf("This image is not compatible with this feature.")
131131
}
132132
return err

0 commit comments

Comments
 (0)