Skip to content

Commit

Permalink
Support all VMSizeFlags in fly postgres create (#4216)
Browse files Browse the repository at this point in the history
Before this change, `fly postgres create` only supported a set of
bespoke VM sizes via the --vm-size flag, making it impossible to create
a postgres cluster with, say, 1 shared CPU and 1 GB of memory.

As of this change, we create the underlying machine using the entire
`MachineGuest` set of options, which allows for additional
customization.

The legacy prompt and error behavior given no --vm-size or an invalid
--vm-size is preserved.
  • Loading branch information
vboginskey authored Feb 12, 2025
1 parent cd05a97 commit ef6c170
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions internal/command/postgres/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"reflect"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -34,6 +35,7 @@ func newCreate() *cobra.Command {
flag.Region(),
flag.Org(),
flag.Detach(),
flag.VMSizeFlags,
flag.Bool{
Name: "enable-backups",
Description: "Create a new tigris bucket and enable WAL-based backups",
Expand All @@ -48,10 +50,6 @@ func newCreate() *cobra.Command {
Shorthand: "p",
Description: "The superuser password. The password will be generated for you if you leave this blank",
},
flag.String{
Name: "vm-size",
Description: "the size of the VM",
},
flag.Int{
Name: "initial-cluster-size",
Description: "Initial cluster size",
Expand Down Expand Up @@ -288,7 +286,24 @@ func CreateCluster(ctx context.Context, org *fly.Organization, region *fly.Regio
BarmanRemoteRestoreConfig: flag.GetString(ctx, "restore-target-app"),
}

customConfig := params.DiskGb != 0 || params.VMSize != "" || params.InitialClusterSize != 0 || params.ScaleToZero != nil
isCustomMachine := false
for _, sizeFlag := range flag.VMSizeFlags {
nameField := reflect.ValueOf(sizeFlag).FieldByName("Name")

if nameField.IsValid() {
name := nameField.String()
if name == "vm-size" {
continue
}

if flag.IsSpecified(ctx, name) {
isCustomMachine = true
break
}
}
}

customConfig := isCustomMachine || params.DiskGb != 0 || params.VMSize != "" || params.InitialClusterSize != 0 || params.ScaleToZero != nil

var config *PostgresConfiguration

Expand Down Expand Up @@ -344,13 +359,22 @@ func CreateCluster(ctx context.Context, org *fly.Organization, region *fly.Regio
}
input.InitialClusterSize = params.PostgresConfiguration.InitialClusterSize

// Resolve VM size
vmSize, err := resolveVMSize(ctx, params.VMSize)
if err != nil {
return err
}
if isCustomMachine {
guest, err := flag.GetMachineGuest(ctx, nil)
if err != nil {
return err
}

input.VMSize = vmSize
input.Guest = guest
} else {
// Resolve VM size
vmSize, err := resolveVMSize(ctx, params.VMSize)
if err != nil {
return err
}

input.VMSize = vmSize
}

if params.ScaleToZero != nil {
input.ScaleToZero = *params.ScaleToZero
Expand Down

0 comments on commit ef6c170

Please sign in to comment.