Skip to content

Commit

Permalink
fix: prevent a panic if the machine is missing
Browse files Browse the repository at this point in the history
Not really sure the condition to trigger the panic, but returning
an error would make investigating the issue slightly easier.
  • Loading branch information
kzys committed Nov 12, 2024
1 parent 2c0bfe7 commit d1b365f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
8 changes: 7 additions & 1 deletion internal/command/deploy/machines_deploymachinesapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ func (md *machineDeployment) inferCanaryGuest(processGroup string) *fly.MachineG
return canaryGuest
}

// deployCanaryMachines creates canary machines for each process group.
// The canary machines are destroyed before returning to the caller.
func (md *machineDeployment) deployCanaryMachines(ctx context.Context) (err error) {
ctx, span := tracing.GetTracer().Start(ctx, "deploy_canary")
defer span.End()
Expand Down Expand Up @@ -591,6 +593,7 @@ func (md *machineDeployment) updateExistingMachines(ctx context.Context, updateE
return err
}

// updateExistingMachinesWRecovery updates existing machines.
// The code duplication is on purpose here. The plan is to completely move over to updateExistingMachinesWRecovery
func (md *machineDeployment) updateExistingMachinesWRecovery(ctx context.Context, updateEntries []*machineUpdateEntry) (err error) {
ctx, span := tracing.GetTracer().Start(ctx, "update_existing_machines_w_recovery", trace.WithAttributes(
Expand Down Expand Up @@ -652,9 +655,12 @@ func (md *machineDeployment) updateExistingMachinesWRecovery(ctx context.Context
canaryAppState.Machines = []*fly.Machine{oldAppState.Machines[0]}

newCanaryAppState := newAppState
canaryMach, _ := lo.Find(newAppState.Machines, func(m *fly.Machine) bool {
canaryMach, exists := lo.Find(newAppState.Machines, func(m *fly.Machine) bool {
return m.ID == oldAppState.Machines[0].ID
})
if !exists {
return fmt.Errorf("failed to find machine %s under app %s", oldAppState.Machines[0].ID, md.app.Name)
}
newCanaryAppState.Machines = []*fly.Machine{canaryMach}

if err := md.updateMachinesWRecovery(ctx, &canaryAppState, &newCanaryAppState, nil, updateMachineSettings{
Expand Down
36 changes: 36 additions & 0 deletions internal/command/deploy/machines_deploymachinesapp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package deploy

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/superfly/fly-go"
"github.com/superfly/flyctl/internal/machine"
"github.com/superfly/flyctl/iostreams"
)

func TestUpdateExistingMachinesWRecovery(t *testing.T) {
ios, _, _, _ := iostreams.Test()
client := &mockFlapsClient{}
client.machines = []*fly.Machine{{ID: "test-machine-id"}}
md := &machineDeployment{
app: &fly.AppCompact{},
io: ios,
colorize: ios.ColorScheme(),
flapsClient: client,
strategy: "canary",
}

ctx := context.Background()
err := md.updateExistingMachinesWRecovery(ctx, nil)
assert.NoError(t, err)

err = md.updateExistingMachinesWRecovery(ctx, []*machineUpdateEntry{
{
leasableMachine: machine.NewLeasableMachine(client, ios, &fly.Machine{}, false),
launchInput: &fly.LaunchMachineInput{},
},
})
assert.Error(t, err, "failed to find machine test-machine-id")
}
8 changes: 7 additions & 1 deletion internal/command/deploy/mock_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type mockFlapsClient struct {
breakWait bool
breakUncordon bool
breakSetMetadata bool
breakList bool

machines []*fly.Machine
}

func (m *mockFlapsClient) AcquireLease(ctx context.Context, machineID string, ttl *int) (*fly.MachineLease, error) {
Expand Down Expand Up @@ -123,7 +126,10 @@ func (m *mockFlapsClient) Launch(ctx context.Context, builder fly.LaunchMachineI
}

func (m *mockFlapsClient) List(ctx context.Context, state string) ([]*fly.Machine, error) {
return nil, fmt.Errorf("failed to list machines")
if m.breakList {
return nil, fmt.Errorf("failed to list machines")
}
return m.machines, nil
}

func (m *mockFlapsClient) ListActive(ctx context.Context) ([]*fly.Machine, error) {
Expand Down
1 change: 1 addition & 0 deletions internal/command/deploy/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type machinePairing struct {
newMachine *fly.Machine
}

// appState returns the app's state from Flaps.
func (md *machineDeployment) appState(ctx context.Context, existingAppState *AppState) (*AppState, error) {
ctx, span := tracing.GetTracer().Start(ctx, "app_state")
defer span.End()
Expand Down

0 comments on commit d1b365f

Please sign in to comment.