Skip to content

Commit

Permalink
Merge pull request #3078 from devodev/pr/devodev/fix-chaosmonkey-disa…
Browse files Browse the repository at this point in the history
…bled-logging

clusterloader2: Avoid logging and awaiting waitgroup when chaosmonkey is disabled
  • Loading branch information
k8s-ci-robot authored Jan 7, 2025
2 parents 0bd0aa1 + b4291ba commit cb42fed
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
23 changes: 12 additions & 11 deletions clusterloader2/pkg/chaos/monkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package chaos

import (
"fmt"
"strings"
"sync"

Expand All @@ -42,25 +41,27 @@ func NewMonkey(client clientset.Interface, provider provider.Provider) *Monkey {
// Init initializes Monkey with given config.
// When stopCh is closed, the Monkey will stop simulating failures.
func (m *Monkey) Init(config api.ChaosMonkeyConfig, stopCh <-chan struct{}) (*sync.WaitGroup, error) {
wg := sync.WaitGroup{}
if config.NodeFailure != nil {
nodeKiller, err := NewNodeKiller(*config.NodeFailure, m.client, config.ExcludedNodes, m.provider)
if err != nil {
return nil, err
}
m.nodeKiller = nodeKiller
wg.Add(1)
go m.nodeKiller.Run(stopCh, &wg)
if config.NodeFailure == nil {
return nil, nil
}

nodeKiller, err := NewNodeKiller(*config.NodeFailure, m.client, config.ExcludedNodes, m.provider)
if err != nil {
return nil, err
}
m.nodeKiller = nodeKiller

var wg sync.WaitGroup
wg.Add(1)
go m.nodeKiller.Run(stopCh, &wg)
return &wg, nil
}

// Summary logs Monkey execution
func (m *Monkey) Summary() string {
var sb strings.Builder
if m.nodeKiller != nil {
sb.WriteString(fmt.Sprintf("Summary of Chaos Monkey execution\n"))
sb.WriteString("Summary of Chaos Monkey execution\n")
sb.WriteString(m.nodeKiller.Summary())
}
return sb.String()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func (c *chaosMonkeyMeasurement) Execute(config *measurement.Config) ([]measurem
}
c.stopChannel = make(chan struct{})
c.chaosMonkey = chaos.NewMonkey(config.ClusterFramework.GetClientSets().GetClient(), config.CloudProvider)
c.chaosMonkeyWaitGroup, err = c.chaosMonkey.Init(api.ChaosMonkeyConfig{&c.NodeFailureConfig, c.killedNodes}, c.stopChannel)
chaosMonkeyConfig := api.ChaosMonkeyConfig{NodeFailure: &c.NodeFailureConfig, ExcludedNodes: c.killedNodes}
c.chaosMonkeyWaitGroup, err = c.chaosMonkey.Init(chaosMonkeyConfig, c.stopChannel)
if err != nil {
close(c.stopChannel)
return nil, fmt.Errorf("error while creating chaos monkey: %v", err)
Expand All @@ -79,7 +80,10 @@ func (c *chaosMonkeyMeasurement) Execute(config *measurement.Config) ([]measurem
klog.V(2).Info("Chaos monkey ended.")
}
c.killedNodes = c.chaosMonkey.KilledNodes()
klog.V(2).Infof(c.chaosMonkey.Summary())
summary := c.chaosMonkey.Summary()
if summary != "" {
klog.V(2).Info(summary)
}
// ChaosMonkey doesn't collect metrics and returns empty measurement.Summary.
return []measurement.Summary{}, nil
default:
Expand Down
5 changes: 4 additions & 1 deletion clusterloader2/pkg/test/simple_test_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ func (ste *simpleExecutor) ExecuteTest(ctx Context, conf *api.Config) *errors.Er
}
}
}
klog.V(2).Infof(ctx.GetChaosMonkey().Summary())
summary := ctx.GetChaosMonkey().Summary()
if summary != "" {
klog.V(2).Info(summary)
}
return errList
}

Expand Down

0 comments on commit cb42fed

Please sign in to comment.