Skip to content

Commit

Permalink
fix: ensure create plan is executed once (#221)
Browse files Browse the repository at this point in the history
The plan list must be cleared from memory between runs so that the plan
completes and the state is saved. This keeps us from falling into an
infinite loop.

Done it as a function for extra effect!
  • Loading branch information
Callisto13 authored Nov 5, 2021
1 parent 47ce98e commit c0ad5ef
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
9 changes: 9 additions & 0 deletions core/plans/microvm_create_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (p *microvmCreateOrUpdatePlan) Create(ctx context.Context) ([]planner.Proce
return []planner.Procedure{}, nil
}

p.clearPlanList()
p.ensureStatus()

var err error
Expand All @@ -72,6 +73,14 @@ func (p *microvmCreateOrUpdatePlan) Result() interface{} {
return nil
}

// This is the most important function in the codebase DO NOT REMOVE
// Without this, the Create will always return the full origin list of steps
// and the State will never be saved, meaning the steps will always return true
// on ShouldDo. The loop will be infinite.
func (p *microvmCreateOrUpdatePlan) clearPlanList() {
p.steps = []planner.Procedure{}
}

func (p *microvmCreateOrUpdatePlan) create(ctx context.Context, ports *ports.Collection) error {
if err := p.addStep(ctx, runtime.NewCreateDirectory(p.stateDir, defaults.DataDirPerm, ports.FileSystem)); err != nil {
return fmt.Errorf("adding root dir step: %w", err)
Expand Down
10 changes: 9 additions & 1 deletion core/plans/microvm_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (p *microvmDeletePlan) Create(ctx context.Context) ([]planner.Procedure, er
return []planner.Procedure{}, nil
}

p.steps = []planner.Procedure{}
p.clearPlanList()

// MicroVM provider delete
if err := p.addStep(ctx, microvm.NewDeleteStep(p.vm, ports.Provider)); err != nil {
Expand Down Expand Up @@ -98,6 +98,14 @@ func (p *microvmDeletePlan) Result() interface{} {
return nil
}

// This is the most important function in the codebase DO NOT REMOVE
// Without this, the Delete will always return the full origin list of steps
// and the State will never be saved, meaning the steps will always return true
// on ShouldDo. The loop will be infinite.
func (p *microvmDeletePlan) clearPlanList() {
p.steps = []planner.Procedure{}
}

func (p *microvmDeletePlan) addStep(ctx context.Context, step planner.Procedure) error {
shouldDo, err := step.ShouldDo(ctx)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions core/steps/microvm/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func (s *createStep) Name() string {
}

func (s *createStep) ShouldDo(ctx context.Context) (bool, error) {
logger := log.GetLogger(ctx).WithFields(logrus.Fields{
"step": s.Name(),
"vmid": s.vm.ID,
})
logger.Debug("checking if procedure should be run")

state, err := s.vmSvc.State(ctx, s.vm.ID.String())
if err != nil {
return false, fmt.Errorf("checking if microvm is running: %w", err)
Expand Down
6 changes: 6 additions & 0 deletions core/steps/microvm/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func (s *startStep) Name() string {
}

func (s *startStep) ShouldDo(ctx context.Context) (bool, error) {
logger := log.GetLogger(ctx).WithFields(logrus.Fields{
"step": s.Name(),
"vmid": s.vm.ID,
})
logger.Debug("checking if procedure should be run")

state, err := s.vmSvc.State(ctx, s.vm.ID.String())
if err != nil {
return false, fmt.Errorf("checking if microvm is running: %w", err)
Expand Down

0 comments on commit c0ad5ef

Please sign in to comment.