From 8cf0918a4575a4317e73a95bae8a526c13cfb8f7 Mon Sep 17 00:00:00 2001 From: yangfeiyu20102011 Date: Mon, 15 Jul 2024 14:20:35 +0800 Subject: [PATCH] koord-manager: support manually configured default cpu normalization settings (#2128) Signed-off-by: yangfeiyu20102011 --- apis/configuration/slo_controller_config.go | 2 + apis/configuration/zz_generated.deepcopy.go | 5 ++ .../plugins/cpunormalization/plugin.go | 16 +++- .../plugins/cpunormalization/plugin_test.go | 84 +++++++++++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-) diff --git a/apis/configuration/slo_controller_config.go b/apis/configuration/slo_controller_config.go index 24fc80c9d..b589ef7ee 100644 --- a/apis/configuration/slo_controller_config.go +++ b/apis/configuration/slo_controller_config.go @@ -275,6 +275,8 @@ type CPUNormalizationStrategy struct { // Enable defines whether the cpu normalization is enabled. // If set to false, the node cpu normalization ratio will be removed. Enable *bool `json:"enable,omitempty"` + // DefaultRatio defines the default cpu normalization. + DefaultRatio *float64 `json:"defaultRatio,omitempty"` // RatioModel defines the cpu normalization ratio of each CPU model. // It maps the CPUModel of BasicInfo into the ratios. RatioModel map[string]ModelRatioCfg `json:"ratioModel,omitempty"` diff --git a/apis/configuration/zz_generated.deepcopy.go b/apis/configuration/zz_generated.deepcopy.go index 6de05c540..9c57ce584 100644 --- a/apis/configuration/zz_generated.deepcopy.go +++ b/apis/configuration/zz_generated.deepcopy.go @@ -83,6 +83,11 @@ func (in *CPUNormalizationStrategy) DeepCopyInto(out *CPUNormalizationStrategy) *out = new(bool) **out = **in } + if in.DefaultRatio != nil { + in, out := &in.DefaultRatio, &out.DefaultRatio + *out = new(float64) + **out = **in + } if in.RatioModel != nil { in, out := &in.RatioModel, &out.RatioModel *out = make(map[string]ModelRatioCfg, len(*in)) diff --git a/pkg/slo-controller/noderesource/plugins/cpunormalization/plugin.go b/pkg/slo-controller/noderesource/plugins/cpunormalization/plugin.go index 5df5ae0fe..f5378ee0c 100644 --- a/pkg/slo-controller/noderesource/plugins/cpunormalization/plugin.go +++ b/pkg/slo-controller/noderesource/plugins/cpunormalization/plugin.go @@ -174,10 +174,11 @@ func (p *Plugin) Calculate(_ *configuration.ColocationStrategy, node *corev1.Nod return nil, fmt.Errorf("failed to get CPUBasicInfo in cpu normalization calculation, err: info is missing") } - ratio, err := getCPUNormalizationRatioFromModel(basicInfo, strategy) + ratio, err := getCPUNormalizationRatio(basicInfo, strategy) if err != nil { return nil, fmt.Errorf("failed to get ratio in cpu normalization calculation, err: %s", err) } + if err = isCPUNormalizationRatioValid(ratio); err != nil { return nil, fmt.Errorf("failed to validate ratio in cpu normalization calculation, ratio %v, err: %s", ratio, err) } @@ -218,6 +219,19 @@ func isCPUBasicInfoChanged(infoOld, infoNew *extension.CPUBasicInfo) (bool, stri return false, "" } +func getCPUNormalizationRatio(info *extension.CPUBasicInfo, strategy *configuration.CPUNormalizationStrategy) (float64, error) { + ratio, err := getCPUNormalizationRatioFromModel(info, strategy) + if err != nil { + if strategy.DefaultRatio != nil { + klog.V(6).Infof("get no cpu ratio from model, use the default ratio %v", *strategy.DefaultRatio) + ratio = *strategy.DefaultRatio + } else { + return -1, fmt.Errorf("failed to get cpu normalization ratio from model, err: %s", err) + } + } + return ratio, nil +} + func getCPUNormalizationRatioFromModel(info *extension.CPUBasicInfo, strategy *configuration.CPUNormalizationStrategy) (float64, error) { if strategy.RatioModel == nil { return -1, fmt.Errorf("ratio model is nil") diff --git a/pkg/slo-controller/noderesource/plugins/cpunormalization/plugin_test.go b/pkg/slo-controller/noderesource/plugins/cpunormalization/plugin_test.go index b9986e115..70c1d0122 100644 --- a/pkg/slo-controller/noderesource/plugins/cpunormalization/plugin_test.go +++ b/pkg/slo-controller/noderesource/plugins/cpunormalization/plugin_test.go @@ -894,6 +894,90 @@ func Test_getCPUNormalizationRatioFromModel(t *testing.T) { } } +func Test_getCPUNormalizationRatio(t *testing.T) { + type args struct { + info *extension.CPUBasicInfo + strategy *configuration.CPUNormalizationStrategy + } + tests := []struct { + name string + args args + want float64 + wantErr bool + }{ + { + name: "nil model and nil default ratio", + args: args{ + strategy: &configuration.CPUNormalizationStrategy{ + Enable: pointer.Bool(true), + DefaultRatio: nil, + RatioModel: nil, + }, + }, + want: -1, + wantErr: true, + }, + { + name: "nil model and valid default ratio", + args: args{ + strategy: &configuration.CPUNormalizationStrategy{ + Enable: pointer.Bool(true), + DefaultRatio: pointer.Float64(2.5), + RatioModel: nil, + }, + }, + want: 2.5, + wantErr: false, + }, + { + name: "model takes precedence over the default ratio", + args: args{ + info: &extension.CPUBasicInfo{ + CPUModel: "CPU XXX", + }, + strategy: &configuration.CPUNormalizationStrategy{ + Enable: pointer.Bool(true), + DefaultRatio: pointer.Float64(3.5), + RatioModel: map[string]configuration.ModelRatioCfg{ + "CPU XXX": { + BaseRatio: pointer.Float64(1.9), + TurboEnabledRatio: pointer.Float64(1.65), + HyperThreadEnabledRatio: pointer.Float64(1.0), + HyperThreadTurboEnabledRatio: pointer.Float64(1.1), + }, + }, + }, + }, + want: 1.9, + wantErr: false, + }, + { + name: "use the default ratio with invalid model", + args: args{ + info: &extension.CPUBasicInfo{ + CPUModel: "CPU XXX", + }, + strategy: &configuration.CPUNormalizationStrategy{ + Enable: pointer.Bool(true), + DefaultRatio: pointer.Float64(2.2), + RatioModel: map[string]configuration.ModelRatioCfg{ + "CPU XXX": {}, + }, + }, + }, + want: 2.2, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, gotErr := getCPUNormalizationRatio(tt.args.info, tt.args.strategy) + assert.Equal(t, tt.want, got) + assert.Equal(t, tt.wantErr, gotErr != nil) + }) + } +} + func testPluginCleanup() { client = nil cfgHandler = nil