Skip to content

Commit e2f8f04

Browse files
Merge pull request #96 from form3tech-oss/sergii-make-vmstop-chaos-more-robust
feat: make cloudstackvm vmstop chaos more robust
2 parents f2289bf + b04e6ed commit e2f8f04

File tree

1 file changed

+11
-2
lines changed
  • controllers/chaosimpl/cloudstackvm/vmstop

1 file changed

+11
-2
lines changed

controllers/chaosimpl/cloudstackvm/vmstop/impl.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ type Impl struct {
3232
Log logr.Logger
3333
}
3434

35+
const (
36+
StateRunning = "Running"
37+
StateStopped = "Stopped"
38+
)
39+
3540
func (impl *Impl) Apply(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
3641
cloudstackchaos := obj.(*v1alpha1.CloudStackVMChaos)
3742
spec := cloudstackchaos.Spec
@@ -47,13 +52,15 @@ func (impl *Impl) Apply(ctx context.Context, index int, records []*v1alpha1.Reco
4752
}
4853

4954
params := utils.SelectorToListParams(&selector)
55+
params.SetState(StateRunning)
5056
resp, err := client.VirtualMachine.ListVirtualMachines(params)
5157
if err != nil {
5258
return v1alpha1.NotInjected, fmt.Errorf("listing VMs: %w", err)
5359
}
5460

5561
if len(resp.VirtualMachines) == 0 {
56-
return v1alpha1.NotInjected, fmt.Errorf("no VMs returned matching criteria")
62+
impl.Log.Info("no running VMs returned matching criteria")
63+
return v1alpha1.Injected, nil
5764
}
5865

5966
for _, vm := range resp.VirtualMachines {
@@ -87,13 +94,15 @@ func (impl *Impl) Recover(ctx context.Context, index int, records []*v1alpha1.Re
8794
}
8895

8996
params := utils.SelectorToListParams(&selector)
97+
params.SetState(StateStopped)
9098
resp, err := client.VirtualMachine.ListVirtualMachines(params)
9199
if err != nil {
92100
return v1alpha1.Injected, fmt.Errorf("listing VMs: %w", err)
93101
}
94102

95103
if len(resp.VirtualMachines) == 0 {
96-
return v1alpha1.Injected, fmt.Errorf("no VMs returned matching criteria")
104+
impl.Log.Info("no stopped VMs returned matching criteria")
105+
return v1alpha1.NotInjected, nil
97106
}
98107

99108
for _, vm := range resp.VirtualMachines {

0 commit comments

Comments
 (0)