From 71fb4a211767b173fa18905c3930d30292d1e1c1 Mon Sep 17 00:00:00 2001 From: yanggang Date: Wed, 26 Apr 2023 21:05:20 +0800 Subject: [PATCH] Do some code cleans. Signed-off-by: yanggang --- pkg/descheduler/policyconfig.go | 43 ++++++++++++--------------------- pkg/utils/priority.go | 20 --------------- 2 files changed, 16 insertions(+), 47 deletions(-) diff --git a/pkg/descheduler/policyconfig.go b/pkg/descheduler/policyconfig.go index ed76dfa1ba..cd7b7d2e05 100644 --- a/pkg/descheduler/policyconfig.go +++ b/pkg/descheduler/policyconfig.go @@ -123,7 +123,7 @@ func setDefaultEvictor(profile api.DeschedulerProfile, client clientset.Interfac idx = 0 } - thresholdPriority, err := utils.GetPriorityFromStrategyParams(context.TODO(), client, defaultevictorPluginConfig.Args.(*defaultevictor.DefaultEvictorArgs).PriorityThreshold) + thresholdPriority, err := utils.GetPriorityValueFromPriorityThreshold(context.TODO(), client, defaultevictorPluginConfig.Args.(*defaultevictor.DefaultEvictorArgs).PriorityThreshold) if err != nil { klog.Error(err, "Failed to get threshold priority from args") } @@ -137,36 +137,25 @@ func validateDeschedulerConfiguration(in api.DeschedulerPolicy, registry pluginr for _, profile := range in.Profiles { for _, pluginConfig := range profile.PluginConfigs { if _, ok := registry[pluginConfig.Name]; ok { + if _, ok := registry[pluginConfig.Name]; !ok { + errorsInProfiles = fmt.Errorf("%w: %s", errorsInProfiles, fmt.Sprintf("in profile %s: plugin %s in pluginConfig not registered", profile.Name, pluginConfig.Name)) + continue + } + pluginUtilities := registry[pluginConfig.Name] - if pluginUtilities.PluginArgValidator != nil { - err := pluginUtilities.PluginArgValidator(pluginConfig.Args) - errorsInProfiles = setErrorsInProfiles(err, profile.Name, errorsInProfiles) + if pluginUtilities.PluginArgValidator == nil { + continue + } + err := pluginUtilities.PluginArgValidator(pluginConfig.Args) + if err != nil { + if errorsInProfiles == nil { + errorsInProfiles = fmt.Errorf("in profile %s: %s", profile.Name, err.Error()) + } else { + errorsInProfiles = fmt.Errorf("%w: %s", errorsInProfiles, fmt.Sprintf("in profile %s: %s", profile.Name, err.Error())) + } } } } } - if errorsInProfiles != nil { - return errorsInProfiles - } - return nil -} - -func setErrorsInProfiles(err error, profileName string, errorsInProfiles error) error { - if err != nil { - if errorsInProfiles == nil { - errorsInProfiles = fmt.Errorf("in profile %s: %s", profileName, err.Error()) - } else { - errorsInProfiles = fmt.Errorf("%w: %s", errorsInProfiles, fmt.Sprintf("in profile %s: %s", profileName, err.Error())) - } - } return errorsInProfiles } - -func hasPluginConfigsWithSameName(newPluginConfig api.PluginConfig, pluginConfigs []api.PluginConfig) bool { - for _, pluginConfig := range pluginConfigs { - if newPluginConfig.Name == pluginConfig.Name { - return true - } - } - return false -} diff --git a/pkg/utils/priority.go b/pkg/utils/priority.go index 92a140ad3d..9010b34679 100644 --- a/pkg/utils/priority.go +++ b/pkg/utils/priority.go @@ -53,26 +53,6 @@ func GetPriorityFromPriorityClass(ctx context.Context, client clientset.Interfac return SystemCriticalPriority, nil } -// GetPriorityFromStrategyParams gets priority from the given StrategyParameters. -// It will return SystemCriticalPriority by default. -func GetPriorityFromStrategyParams(ctx context.Context, client clientset.Interface, priorityThreshold *api.PriorityThreshold) (priority int32, err error) { - if priorityThreshold == nil { - return SystemCriticalPriority, nil - } - if priorityThreshold.Value != nil { - priority = *priorityThreshold.Value - } else { - priority, err = GetPriorityFromPriorityClass(ctx, client, priorityThreshold.Name) - if err != nil { - return - } - } - if priority > SystemCriticalPriority { - return 0, fmt.Errorf("Priority threshold can't be greater than %d", SystemCriticalPriority) - } - return -} - // GetPriorityValueFromPriorityThreshold gets priority from the given PriorityThreshold. // It will return SystemCriticalPriority by default. func GetPriorityValueFromPriorityThreshold(ctx context.Context, client clientset.Interface, priorityThreshold *api.PriorityThreshold) (priority int32, err error) {