Skip to content

Commit cb42fed

Browse files
authored
Merge pull request #3078 from devodev/pr/devodev/fix-chaosmonkey-disabled-logging
clusterloader2: Avoid logging and awaiting waitgroup when chaosmonkey is disabled
2 parents 0bd0aa1 + b4291ba commit cb42fed

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

clusterloader2/pkg/chaos/monkey.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package chaos
1818

1919
import (
20-
"fmt"
2120
"strings"
2221
"sync"
2322

@@ -42,25 +41,27 @@ func NewMonkey(client clientset.Interface, provider provider.Provider) *Monkey {
4241
// Init initializes Monkey with given config.
4342
// When stopCh is closed, the Monkey will stop simulating failures.
4443
func (m *Monkey) Init(config api.ChaosMonkeyConfig, stopCh <-chan struct{}) (*sync.WaitGroup, error) {
45-
wg := sync.WaitGroup{}
46-
if config.NodeFailure != nil {
47-
nodeKiller, err := NewNodeKiller(*config.NodeFailure, m.client, config.ExcludedNodes, m.provider)
48-
if err != nil {
49-
return nil, err
50-
}
51-
m.nodeKiller = nodeKiller
52-
wg.Add(1)
53-
go m.nodeKiller.Run(stopCh, &wg)
44+
if config.NodeFailure == nil {
45+
return nil, nil
5446
}
5547

48+
nodeKiller, err := NewNodeKiller(*config.NodeFailure, m.client, config.ExcludedNodes, m.provider)
49+
if err != nil {
50+
return nil, err
51+
}
52+
m.nodeKiller = nodeKiller
53+
54+
var wg sync.WaitGroup
55+
wg.Add(1)
56+
go m.nodeKiller.Run(stopCh, &wg)
5657
return &wg, nil
5758
}
5859

5960
// Summary logs Monkey execution
6061
func (m *Monkey) Summary() string {
6162
var sb strings.Builder
6263
if m.nodeKiller != nil {
63-
sb.WriteString(fmt.Sprintf("Summary of Chaos Monkey execution\n"))
64+
sb.WriteString("Summary of Chaos Monkey execution\n")
6465
sb.WriteString(m.nodeKiller.Summary())
6566
}
6667
return sb.String()

clusterloader2/pkg/measurement/common/chaos_monkey_measurement.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ func (c *chaosMonkeyMeasurement) Execute(config *measurement.Config) ([]measurem
6464
}
6565
c.stopChannel = make(chan struct{})
6666
c.chaosMonkey = chaos.NewMonkey(config.ClusterFramework.GetClientSets().GetClient(), config.CloudProvider)
67-
c.chaosMonkeyWaitGroup, err = c.chaosMonkey.Init(api.ChaosMonkeyConfig{&c.NodeFailureConfig, c.killedNodes}, c.stopChannel)
67+
chaosMonkeyConfig := api.ChaosMonkeyConfig{NodeFailure: &c.NodeFailureConfig, ExcludedNodes: c.killedNodes}
68+
c.chaosMonkeyWaitGroup, err = c.chaosMonkey.Init(chaosMonkeyConfig, c.stopChannel)
6869
if err != nil {
6970
close(c.stopChannel)
7071
return nil, fmt.Errorf("error while creating chaos monkey: %v", err)
@@ -79,7 +80,10 @@ func (c *chaosMonkeyMeasurement) Execute(config *measurement.Config) ([]measurem
7980
klog.V(2).Info("Chaos monkey ended.")
8081
}
8182
c.killedNodes = c.chaosMonkey.KilledNodes()
82-
klog.V(2).Infof(c.chaosMonkey.Summary())
83+
summary := c.chaosMonkey.Summary()
84+
if summary != "" {
85+
klog.V(2).Info(summary)
86+
}
8387
// ChaosMonkey doesn't collect metrics and returns empty measurement.Summary.
8488
return []measurement.Summary{}, nil
8589
default:

clusterloader2/pkg/test/simple_test_executor.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ func (ste *simpleExecutor) ExecuteTest(ctx Context, conf *api.Config) *errors.Er
9999
}
100100
}
101101
}
102-
klog.V(2).Infof(ctx.GetChaosMonkey().Summary())
102+
summary := ctx.GetChaosMonkey().Summary()
103+
if summary != "" {
104+
klog.V(2).Info(summary)
105+
}
103106
return errList
104107
}
105108

0 commit comments

Comments
 (0)