Skip to content

Commit 4cf887c

Browse files
authored
Skip error (#177)
* fixing linter errors * linter: using references for Container struct in many places * fix build instructions #170 * can skip error in chaos command and continue on next interval tick #175
1 parent a330985 commit 4cf887c

File tree

14 files changed

+45
-14
lines changed

14 files changed

+45
-14
lines changed

cmd/main.go

+4
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ func main() {
150150
Usage: "dry run does not create chaos, only logs planned chaos commands",
151151
EnvVar: "DRY-RUN",
152152
},
153+
cli.BoolFlag{
154+
Name: "skip-error",
155+
Usage: "skip chaos command error and retry to execute the command on next interval tick",
156+
},
153157
}
154158

155159
if err := app.Run(os.Args); err != nil {

pkg/chaos/command.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func GetNamesOrPattern(c *cli.Context) ([]string, string) {
5252
}
5353

5454
// RunChaosCommand run chaos command in go routine
55-
func RunChaosCommand(topContext context.Context, command Command, intervalStr string, random bool) error {
55+
func RunChaosCommand(topContext context.Context, command Command, intervalStr string, random, skipError bool) error {
5656
// parse interval
5757
interval, err := util.GetIntervalValue(intervalStr)
5858
if err != nil {
@@ -75,7 +75,10 @@ func RunChaosCommand(topContext context.Context, command Command, intervalStr st
7575
for {
7676
// run chaos function
7777
if err := command.Run(ctx, random); err != nil {
78-
return err
78+
if !skipError {
79+
return err
80+
}
81+
log.WithError(err).Warn("skipping error")
7982
}
8083
// wait for next timer tick or cancel
8184
select {

pkg/chaos/docker/cmd/kill.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func (cmd *killContext) kill(c *cli.Context) error {
4646
labels := c.GlobalStringSlice("label")
4747
// get dry-run mode
4848
dryRun := c.GlobalBool("dry-run")
49+
// get skip error flag
50+
skipError := c.GlobalBool("skip-error")
4951
// get interval
5052
interval := c.GlobalString("interval")
5153
// get names or pattern
@@ -60,5 +62,5 @@ func (cmd *killContext) kill(c *cli.Context) error {
6062
return err
6163
}
6264
// run kill command
63-
return chaos.RunChaosCommand(cmd.context, killCommand, interval, random)
65+
return chaos.RunChaosCommand(cmd.context, killCommand, interval, random, skipError)
6466
}

pkg/chaos/docker/cmd/pause.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ func (cmd *pauseContext) pause(c *cli.Context) error {
4343
random := c.GlobalBool("random")
4444
// get dry-run mode
4545
dryRun := c.GlobalBool("dry-run")
46+
// get skip error flag
47+
skipError := c.GlobalBool("skip-error")
4648
// get labels
4749
labels := c.GlobalStringSlice("label")
4850
// get global chaos interval
@@ -59,5 +61,5 @@ func (cmd *pauseContext) pause(c *cli.Context) error {
5961
return err
6062
}
6163
// run pause command
62-
return chaos.RunChaosCommand(cmd.context, pauseCommand, interval, random)
64+
return chaos.RunChaosCommand(cmd.context, pauseCommand, interval, random, skipError)
6365
}

pkg/chaos/docker/cmd/remove.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ func (cmd *removeContext) remove(c *cli.Context) error {
5353
labels := c.GlobalStringSlice("label")
5454
// get dry-run mode
5555
dryRun := c.GlobalBool("dry-run")
56+
// get skip error flag
57+
skipError := c.GlobalBool("skip-error")
5658
// get interval
5759
interval := c.GlobalString("interval")
5860
// get names or pattern
@@ -71,5 +73,5 @@ func (cmd *removeContext) remove(c *cli.Context) error {
7173
return err
7274
}
7375
// run remove command
74-
return chaos.RunChaosCommand(cmd.context, removeCommand, interval, random)
76+
return chaos.RunChaosCommand(cmd.context, removeCommand, interval, random, skipError)
7577
}

pkg/chaos/docker/cmd/stop.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ func (cmd *stopContext) stop(c *cli.Context) error {
5555
labels := c.GlobalStringSlice("label")
5656
// get dry-run mode
5757
dryRun := c.GlobalBool("dry-run")
58+
// get skip error flag
59+
skipError := c.GlobalBool("skip-error")
5860
// get global chaos interval
5961
interval := c.GlobalString("interval")
6062
// get wait time
@@ -73,5 +75,5 @@ func (cmd *stopContext) stop(c *cli.Context) error {
7375
return err
7476
}
7577
// run stop command
76-
return chaos.RunChaosCommand(cmd.context, stopCommand, interval, random)
78+
return chaos.RunChaosCommand(cmd.context, stopCommand, interval, random, skipError)
7779
}

pkg/chaos/netem/cmd/corrupt.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func (cmd *corruptContext) corrupt(c *cli.Context) error {
4646
labels := c.GlobalStringSlice("label")
4747
// get dry-run mode
4848
dryRun := c.GlobalBool("dry-run")
49+
// get skip error flag
50+
skipError := c.GlobalBool("skip-error")
4951
// get names or pattern
5052
names, pattern := chaos.GetNamesOrPattern(c)
5153
// get global chaos interval
@@ -79,5 +81,5 @@ func (cmd *corruptContext) corrupt(c *cli.Context) error {
7981
return err
8082
}
8183
// run netem command
82-
return chaos.RunChaosCommand(cmd.context, corruptCommand, interval, random)
84+
return chaos.RunChaosCommand(cmd.context, corruptCommand, interval, random, skipError)
8385
}

pkg/chaos/netem/cmd/delay.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ func (cmd *delayContext) delay(c *cli.Context) error {
5656
labels := c.GlobalStringSlice("label")
5757
// get dry-run mode
5858
dryRun := c.GlobalBool("dry-run")
59+
// get skip error flag
60+
skipError := c.GlobalBool("skip-error")
5961
// get names or pattern
6062
names, pattern := chaos.GetNamesOrPattern(c)
6163
// get global chaos interval
@@ -93,5 +95,5 @@ func (cmd *delayContext) delay(c *cli.Context) error {
9395
return err
9496
}
9597
// run netem delay command
96-
return chaos.RunChaosCommand(cmd.context, delayCommand, interval, random)
98+
return chaos.RunChaosCommand(cmd.context, delayCommand, interval, random, skipError)
9799
}

pkg/chaos/netem/cmd/duplicate.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func (cmd *duplicateContext) duplicate(c *cli.Context) error {
4646
labels := c.GlobalStringSlice("label")
4747
// get dry-run mode
4848
dryRun := c.GlobalBool("dry-run")
49+
// get skip error flag
50+
skipError := c.GlobalBool("skip-error")
4951
// get names or pattern
5052
names, pattern := chaos.GetNamesOrPattern(c)
5153
// get global chaos interval
@@ -79,5 +81,5 @@ func (cmd *duplicateContext) duplicate(c *cli.Context) error {
7981
return err
8082
}
8183
// run netem command
82-
return chaos.RunChaosCommand(cmd.context, duplicateCommand, interval, random)
84+
return chaos.RunChaosCommand(cmd.context, duplicateCommand, interval, random, skipError)
8385
}

pkg/chaos/netem/cmd/loss.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func (cmd *lossContext) loss(c *cli.Context) error {
4646
labels := c.GlobalStringSlice("label")
4747
// get dry-run mode
4848
dryRun := c.GlobalBool("dry-run")
49+
// get skip error flag
50+
skipError := c.GlobalBool("skip-error")
4951
// get names or pattern
5052
names, pattern := chaos.GetNamesOrPattern(c)
5153
// get global chaos interval
@@ -79,5 +81,5 @@ func (cmd *lossContext) loss(c *cli.Context) error {
7981
return err
8082
}
8183
// run netem command
82-
return chaos.RunChaosCommand(cmd.context, lossCommand, interval, random)
84+
return chaos.RunChaosCommand(cmd.context, lossCommand, interval, random, skipError)
8385
}

pkg/chaos/netem/cmd/loss_ge.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ func (cmd *lossGEContext) lossGE(c *cli.Context) error {
5757
labels := c.GlobalStringSlice("label")
5858
// get dry-run mode
5959
dryRun := c.GlobalBool("dry-run")
60+
// get skip error flag
61+
skipError := c.GlobalBool("skip-error")
6062
// get names or pattern
6163
names, pattern := chaos.GetNamesOrPattern(c)
6264
// get global chaos interval
@@ -94,5 +96,5 @@ func (cmd *lossGEContext) lossGE(c *cli.Context) error {
9496
return err
9597
}
9698
// run netem command
97-
return chaos.RunChaosCommand(cmd.context, lossGECommand, interval, random)
99+
return chaos.RunChaosCommand(cmd.context, lossGECommand, interval, random, skipError)
98100
}

pkg/chaos/netem/cmd/loss_state.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ func (cmd *lossStateContext) lossState(c *cli.Context) error {
6868
labels := c.GlobalStringSlice("label")
6969
// get dry-run mode
7070
dryRun := c.GlobalBool("dry-run")
71+
// get skip error flag
72+
skipError := c.GlobalBool("skip-error")
7173
// get names or pattern
7274
names, pattern := chaos.GetNamesOrPattern(c)
7375
// get global chaos interval
@@ -107,5 +109,5 @@ func (cmd *lossStateContext) lossState(c *cli.Context) error {
107109
return err
108110
}
109111
// run netem command
110-
return chaos.RunChaosCommand(cmd.context, lossStateCommand, interval, random)
112+
return chaos.RunChaosCommand(cmd.context, lossStateCommand, interval, random, skipError)
111113
}

pkg/chaos/netem/cmd/rate.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ func (cmd *rateContext) rate(c *cli.Context) error {
5656
labels := c.GlobalStringSlice("label")
5757
// get dry-run mode
5858
dryRun := c.GlobalBool("dry-run")
59+
// get skip error flag
60+
skipError := c.GlobalBool("skip-error")
5961
// get names or pattern
6062
names, pattern := chaos.GetNamesOrPattern(c)
6163
// get global chaos interval
@@ -93,5 +95,5 @@ func (cmd *rateContext) rate(c *cli.Context) error {
9395
return err
9496
}
9597
// run netem command
96-
return chaos.RunChaosCommand(cmd.context, lossCommand, interval, random)
98+
return chaos.RunChaosCommand(cmd.context, lossCommand, interval, random, skipError)
9799
}

pkg/chaos/stress/cmd/stress.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func (cmd *stressContext) stress(c *cli.Context) error {
5454
labels := c.GlobalStringSlice("label")
5555
// get dry-run mode
5656
dryRun := c.GlobalBool("dry-run")
57+
// get skip error flag
58+
skipError := c.GlobalBool("skip-error")
5759
// get interval
5860
interval := c.GlobalString("interval")
5961
// get names or pattern
@@ -74,5 +76,5 @@ func (cmd *stressContext) stress(c *cli.Context) error {
7476
return err
7577
}
7678
// run stress command
77-
return chaos.RunChaosCommand(cmd.context, stressCommand, interval, random)
79+
return chaos.RunChaosCommand(cmd.context, stressCommand, interval, random, skipError)
7880
}

0 commit comments

Comments
 (0)