k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/common/chaos_monkey_measurement.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package common
    18  
    19  import (
    20  	"fmt"
    21  	"sync"
    22  
    23  	"k8s.io/apimachinery/pkg/util/sets"
    24  	"k8s.io/klog/v2"
    25  	"k8s.io/perf-tests/clusterloader2/api"
    26  	"k8s.io/perf-tests/clusterloader2/pkg/chaos"
    27  	"k8s.io/perf-tests/clusterloader2/pkg/measurement"
    28  	"k8s.io/perf-tests/clusterloader2/pkg/util"
    29  )
    30  
    31  const (
    32  	chaosMonkeyMeasurementName = "ChaosMonkey"
    33  )
    34  
    35  func init() {
    36  	create := func() measurement.Measurement {
    37  		return &chaosMonkeyMeasurement{
    38  			killedNodes: sets.NewString(),
    39  		}
    40  	}
    41  	if err := measurement.Register(chaosMonkeyMeasurementName, create); err != nil {
    42  		klog.Fatalf("Cannot register %s: %v", chaosMonkeyMeasurementName, err)
    43  	}
    44  }
    45  
    46  type chaosMonkeyMeasurement struct {
    47  	api.NodeFailureConfig
    48  	stopChannel          chan struct{}
    49  	chaosMonkey          *chaos.Monkey
    50  	chaosMonkeyWaitGroup *sync.WaitGroup
    51  	killedNodes          sets.String
    52  }
    53  
    54  func (c *chaosMonkeyMeasurement) Execute(config *measurement.Config) ([]measurement.Summary, error) {
    55  	action, err := util.GetString(config.Params, "action")
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	switch action {
    61  	case "start":
    62  		if err := util.ToStruct(config.Params, &c.NodeFailureConfig); err != nil {
    63  			return nil, err
    64  		}
    65  		c.stopChannel = make(chan struct{})
    66  		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)
    68  		if err != nil {
    69  			close(c.stopChannel)
    70  			return nil, fmt.Errorf("error while creating chaos monkey: %v", err)
    71  		}
    72  		return nil, nil
    73  	case "stop":
    74  		close(c.stopChannel)
    75  		if c.chaosMonkeyWaitGroup != nil {
    76  			// Wait for the Chaos Monkey subroutine to end
    77  			klog.V(2).Info("Waiting for the chaos monkey subroutine to end...")
    78  			c.chaosMonkeyWaitGroup.Wait()
    79  			klog.V(2).Info("Chaos monkey ended.")
    80  		}
    81  		c.killedNodes = c.chaosMonkey.KilledNodes()
    82  		klog.V(2).Infof(c.chaosMonkey.Summary())
    83  		// ChaosMonkey doesn't collect metrics and returns empty measurement.Summary.
    84  		return []measurement.Summary{}, nil
    85  	default:
    86  		return nil, fmt.Errorf("unknown action: %v", action)
    87  	}
    88  }
    89  
    90  func (c *chaosMonkeyMeasurement) Dispose() {}
    91  
    92  func (c *chaosMonkeyMeasurement) String() string {
    93  	return chaosMonkeyMeasurementName
    94  }