Skip to content

Commit

Permalink
Support creating machines in "stopped" state (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
pborzenkov authored Jun 28, 2024
1 parent 5125fb6 commit c4cd2cf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
13 changes: 12 additions & 1 deletion cmd/fly-autoscaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log/slog"
"os"
"os/signal"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -75,7 +76,7 @@ func run(ctx context.Context, args []string) error {
printUsage()
return flag.ErrHelp
}
return fmt.Errorf("litefs %s: unknown command", cmd)
return fmt.Errorf("fly-autoscaler %s: unknown command", cmd)
}
}

Expand Down Expand Up @@ -120,6 +121,7 @@ type Config struct {
CreatedMachineN string `yaml:"created-machine-count"`
MinCreatedMachineN string `yaml:"min-created-machine-count"`
MaxCreatedMachineN string `yaml:"max-created-machine-count"`
InitialMachineState string `yaml:"initial-machine-state"`
StartedMachineN string `yaml:"started-machine-count"`
MinStartedMachineN string `yaml:"min-started-machine-count"`
MaxStartedMachineN string `yaml:"max-started-machine-count"`
Expand Down Expand Up @@ -149,6 +151,7 @@ func NewConfigFromEnv() (_ *Config, err error) {
c.CreatedMachineN = os.Getenv("FAS_CREATED_MACHINE_COUNT")
c.MinCreatedMachineN = os.Getenv("FAS_MIN_CREATED_MACHINE_COUNT")
c.MaxCreatedMachineN = os.Getenv("FAS_MAX_CREATED_MACHINE_COUNT")
c.InitialMachineState = os.Getenv("FAS_INITIAL_MACHINE_STATE")
c.StartedMachineN = os.Getenv("FAS_STARTED_MACHINE_COUNT")
c.MinStartedMachineN = os.Getenv("FAS_MIN_STARTED_MACHINE_COUNT")
c.MaxStartedMachineN = os.Getenv("FAS_MAX_STARTED_MACHINE_COUNT")
Expand All @@ -158,6 +161,10 @@ func NewConfigFromEnv() (_ *Config, err error) {
c.Regions = strings.Split(s, ",")
}

if c.InitialMachineState == "" {
c.InitialMachineState = fly.MachineStateStarted
}

if s := os.Getenv("FAS_CONCURRENCY"); s != "" {
if c.Concurrency, err = strconv.Atoi(s); err != nil {
return nil, fmt.Errorf("cannot parse FAS_CONCURRENCY as integer: %q", s)
Expand Down Expand Up @@ -266,6 +273,10 @@ func (c *Config) Validate() error {
return err
}

if !slices.Contains([]string{fly.MachineStateStarted, fly.MachineStateStopped}, c.InitialMachineState) {
return fmt.Errorf("initial machine state must be either 'started' or 'stopped'")
}

for i, collectorConfig := range c.MetricCollectors {
if err := collectorConfig.Validate(); err != nil {
return fmt.Errorf("metric-collectors[%d]: %w", i, err)
Expand Down
1 change: 1 addition & 0 deletions cmd/fly-autoscaler/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (c *ServeCommand) Run(ctx context.Context, args []string) (err error) {
r.MaxCreatedMachineN = maxCreatedMachineN
r.MinStartedMachineN = minStartedMachineN
r.MaxStartedMachineN = maxStartedMachineN
r.InitialMachineState = c.Config.InitialMachineState
r.Regions = c.Config.Regions
r.Collectors = collectors
return r
Expand Down
8 changes: 6 additions & 2 deletions reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type Reconciler struct {
MinStartedMachineN string
MaxStartedMachineN string

// Initial machine state (started or stopped)
InitialMachineState string

// List of collectors to fetch metric values from.
Collectors []MetricCollector

Expand Down Expand Up @@ -335,8 +338,9 @@ func (r *Reconciler) listMachines(ctx context.Context) ([]*fly.Machine, error) {

func (r *Reconciler) createMachine(ctx context.Context, config *fly.MachineConfig, region string) (*fly.Machine, error) {
machine, err := r.Client.Launch(ctx, fly.LaunchMachineInput{
Config: config,
Region: region,
Config: config,
Region: region,
SkipLaunch: r.InitialMachineState == fly.MachineStateStopped,
})
if err != nil {
r.Stats.MachineCreateFailed.Add(1)
Expand Down

0 comments on commit c4cd2cf

Please sign in to comment.