|
| 1 | +/* |
| 2 | +Copyright 2025 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 | + "time" |
| 22 | + |
| 23 | + "k8s.io/klog/v2" |
| 24 | + "k8s.io/perf-tests/clusterloader2/pkg/measurement" |
| 25 | + measurementutil "k8s.io/perf-tests/clusterloader2/pkg/measurement/util" |
| 26 | + "k8s.io/perf-tests/clusterloader2/pkg/provider" |
| 27 | + "k8s.io/perf-tests/clusterloader2/pkg/util" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + scaleNodesMeasurementName = "ScaleNodes" |
| 32 | + defaultScalingTimeout = 30 * time.Minute |
| 33 | + nodeCountCheckInterval = 30 * time.Second |
| 34 | +) |
| 35 | + |
| 36 | +type scaleNodesMeasurement struct{} |
| 37 | + |
| 38 | +func init() { |
| 39 | + if err := measurement.Register(scaleNodesMeasurementName, createScaleNodesMeasurement); err != nil { |
| 40 | + klog.Fatalf("Cannot register %s: %v", scaleNodesMeasurementName, err) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func createScaleNodesMeasurement() measurement.Measurement { |
| 45 | + return &scaleNodesMeasurement{} |
| 46 | +} |
| 47 | + |
| 48 | +// Execute performs the node scaling operation with the specified parameters |
| 49 | +func (n *scaleNodesMeasurement) Execute(config *measurement.Config) ([]measurement.Summary, error) { |
| 50 | + // Get parameters from config.Params |
| 51 | + providerName, err := util.GetString(config.Params, "provider") |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + region, err := util.GetString(config.Params, "region") |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + clusterName, err := util.GetString(config.Params, "clusterName") |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + scaleRate, err := util.GetInt(config.Params, "scaleRate") |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + intervalMinutes, err := util.GetInt(config.Params, "intervalMinutes") |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + targetNodeCount, err := util.GetInt(config.Params, "targetNodeCount") |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + // Get timeout with default value if not specified |
| 77 | + timeout, err := util.GetDurationOrDefault(config.Params, "timeout", defaultScalingTimeout) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + // Initialize provider specific scaler |
| 83 | + scaler, err := provider.CreateNodeScaler(providerName, region, clusterName) |
| 84 | + if err != nil { |
| 85 | + return nil, fmt.Errorf("failed to create node scaler: %v", err) |
| 86 | + } |
| 87 | + |
| 88 | + // Start scaling operation |
| 89 | + klog.Infof("Starting node scaling: target=%d, rate=%d/interval, interval=%dm, timeout=%v", |
| 90 | + targetNodeCount, scaleRate, intervalMinutes, timeout) |
| 91 | + |
| 92 | + // Start the scaling operation in a goroutine |
| 93 | + errCh := make(chan error) |
| 94 | + go func() { |
| 95 | + errCh <- scaler.ScaleNodes(scaleRate, intervalMinutes, targetNodeCount) |
| 96 | + }() |
| 97 | + |
| 98 | + // Create stop channel for timeout |
| 99 | + stopCh := make(chan struct{}) |
| 100 | + time.AfterFunc(timeout, func() { |
| 101 | + close(stopCh) |
| 102 | + }) |
| 103 | + |
| 104 | + // Set up options for waiting on nodes |
| 105 | + options := &measurementutil.WaitForNodeOptions{ |
| 106 | + Selector: util.NewObjectSelector(), |
| 107 | + MinDesiredNodeCount: targetNodeCount, |
| 108 | + MaxDesiredNodeCount: targetNodeCount, |
| 109 | + CallerName: n.String(), |
| 110 | + WaitForNodesInterval: nodeCountCheckInterval, |
| 111 | + } |
| 112 | + |
| 113 | + // Wait for either the scaling operation to fail or nodes to be ready |
| 114 | + select { |
| 115 | + case err := <-errCh: |
| 116 | + if err != nil { |
| 117 | + return nil, fmt.Errorf("failed to scale nodes: %v", err) |
| 118 | + } |
| 119 | + // Scaling operation completed, now wait for nodes to be ready |
| 120 | + if err := measurementutil.WaitForNodes(config.ClusterFramework.GetClientSets().GetClient(), stopCh, options); err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + return nil, nil |
| 124 | + case <-stopCh: |
| 125 | + return nil, fmt.Errorf("timeout while waiting for scaling operation to complete after %v", timeout) |
| 126 | + } |
| 127 | +} |
| 128 | +// Dispose cleans up after the measurement. |
| 129 | +func (*scaleNodesMeasurement) Dispose() {} |
| 130 | + |
| 131 | +// String returns string representation of this measurement. |
| 132 | +func (*scaleNodesMeasurement) String() string { |
| 133 | + return scaleNodesMeasurementName |
| 134 | +} |
0 commit comments