Skip to content

Commit

Permalink
netem: add started log
Browse files Browse the repository at this point in the history
  • Loading branch information
w-miller committed Aug 14, 2023
1 parent e1cb30a commit f4a608c
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions pkg/chaos/netem/netem.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func newNetemCommand(client container.Client, gparams *chaos.GlobalParams, param

// run network emulation command, stop netem on timeout or abort
func runNetem(ctx context.Context, client container.Client, c *container.Container, netInterface string, cmd []string, ips []*net.IPNet, sports, dports []string, duration time.Duration, tcimage string, pull, dryRun bool) error {
log.WithFields(log.Fields{
logger := log.WithFields(log.Fields{
"id": c.ID(),
"name": c.Name(),
"iface": netInterface,
Expand All @@ -79,42 +79,28 @@ func runNetem(ctx context.Context, client container.Client, c *container.Contain
"duration": duration,
"tc-image": tcimage,
"pull": pull,
}).Debug("running netem command")
})
logger.Debug("running netem command")
err := client.NetemContainer(ctx, c, netInterface, cmd, ips, sports, dports, duration, tcimage, pull, dryRun)
if err != nil {
return errors.Wrap(err, "netem failed")
}
logger.Debug("netem command started")

// create new context with timeout for canceling
stopCtx, cancel := context.WithTimeout(context.Background(), duration)
defer cancel()
// wait for specified duration and then stop netem (where it applied) or stop on ctx.Done()
select {
case <-ctx.Done():
log.WithFields(log.Fields{
"id": c.ID(),
"name": c.Name(),
"iface": netInterface,
"ips": ips,
"sports": sports,
"dports": dports,
"tc-image": tcimage,
}).Debug("stopping netem command on abort")
logger.Debug("stopping netem command on abort")
// use different context to stop netem since parent context is canceled
err = client.StopNetemContainer(context.Background(), c, netInterface, ips, sports, dports, tcimage, pull, dryRun)
if err != nil {
return errors.Wrap(err, "failed to stop netem container")
}
case <-stopCtx.Done():
log.WithFields(log.Fields{
"id": c.ID(),
"name": c.Name(),
"iface": netInterface,
"ips": ips,
"sports": sports,
"dports": dports,
"tc-image": tcimage,
}).Debug("stopping netem command on timout")
logger.Debug("stopping netem command on timout")
// use parent context to stop netem in container
err = client.StopNetemContainer(context.Background(), c, netInterface, ips, sports, dports, tcimage, pull, dryRun)
if err != nil {
Expand Down

0 comments on commit f4a608c

Please sign in to comment.