mirror of
https://github.com/kubernetes-sigs/descheduler.git
synced 2026-01-26 05:14:13 +01:00
Strategy: Set threshold priority from strategy's parameters
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
clientset "k8s.io/client-go/kubernetes"
|
||||
"sigs.k8s.io/descheduler/pkg/api"
|
||||
)
|
||||
|
||||
const SystemCriticalPriority = 2 * int32(1000000000)
|
||||
@@ -33,3 +39,36 @@ func PodMatchesTermsNamespaceAndSelector(pod *v1.Pod, namespaces sets.String, se
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// GetPriorityFromPriorityClass gets priority from the given priority class.
|
||||
// If no priority class is provided, it will return SystemCriticalPriority by default.
|
||||
func GetPriorityFromPriorityClass(ctx context.Context, client clientset.Interface, name string) (int32, error) {
|
||||
if name != "" {
|
||||
priorityClass, err := client.SchedulingV1().PriorityClasses().Get(ctx, name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return priorityClass.Value, nil
|
||||
}
|
||||
return SystemCriticalPriority, nil
|
||||
}
|
||||
|
||||
// GetPriorityFromStrategyParams gets priority from the given StrategyParameters.
|
||||
// It will return SystemCriticalPriority by default.
|
||||
func GetPriorityFromStrategyParams(ctx context.Context, client clientset.Interface, params *api.StrategyParameters) (priority int32, err error) {
|
||||
if params == nil {
|
||||
return SystemCriticalPriority, nil
|
||||
}
|
||||
if params.ThresholdPriority != nil {
|
||||
priority = *params.ThresholdPriority
|
||||
} else {
|
||||
priority, err = GetPriorityFromPriorityClass(ctx, client, params.ThresholdPriorityClassName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
if priority > SystemCriticalPriority {
|
||||
return 0, fmt.Errorf("Priority threshold can't be greater than %d", SystemCriticalPriority)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user