sigs.k8s.io/kueue@v0.6.2/pkg/util/priority/priority.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 priority 18 19 import ( 20 "context" 21 22 schedulingv1 "k8s.io/api/scheduling/v1" 23 "k8s.io/apimachinery/pkg/types" 24 "k8s.io/utils/ptr" 25 "sigs.k8s.io/controller-runtime/pkg/client" 26 27 kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1" 28 "sigs.k8s.io/kueue/pkg/constants" 29 ) 30 31 // Priority returns priority of the given workload. 32 func Priority(w *kueue.Workload) int32 { 33 // When priority of a running workload is nil, it means it was created at a time 34 // that there was no global default priority class and the priority class 35 // name of the pod was empty. So, we resolve to the static default priority. 36 return ptr.Deref(w.Spec.Priority, constants.DefaultPriority) 37 } 38 39 // GetPriorityFromPriorityClass returns the priority populated from 40 // priority class. If not specified, priority will be default or 41 // zero if there is no default. 42 func GetPriorityFromPriorityClass(ctx context.Context, client client.Client, 43 priorityClass string) (string, string, int32, error) { 44 if len(priorityClass) == 0 { 45 return getDefaultPriority(ctx, client) 46 } 47 48 pc := &schedulingv1.PriorityClass{} 49 if err := client.Get(ctx, types.NamespacedName{Name: priorityClass}, pc); err != nil { 50 return "", "", 0, err 51 } 52 53 return pc.Name, constants.PodPriorityClassSource, pc.Value, nil 54 } 55 56 // GetPriorityFromWorkloadPriorityClass returns the priority populated from 57 // workload priority class. If not specified, returns 0. 58 // DefaultPriority is not called within this function 59 // because k8s priority class should be checked next. 60 func GetPriorityFromWorkloadPriorityClass(ctx context.Context, client client.Client, 61 workloadPriorityClass string) (string, string, int32, error) { 62 wpc := &kueue.WorkloadPriorityClass{} 63 if err := client.Get(ctx, types.NamespacedName{Name: workloadPriorityClass}, wpc); err != nil { 64 return "", "", 0, err 65 } 66 return wpc.Name, constants.WorkloadPriorityClassSource, wpc.Value, nil 67 } 68 69 func getDefaultPriority(ctx context.Context, client client.Client) (string, string, int32, error) { 70 dpc, err := getDefaultPriorityClass(ctx, client) 71 if err != nil { 72 return "", "", 0, err 73 } 74 if dpc != nil { 75 return dpc.Name, constants.PodPriorityClassSource, dpc.Value, nil 76 } 77 return "", "", int32(constants.DefaultPriority), nil 78 } 79 80 func getDefaultPriorityClass(ctx context.Context, client client.Client) (*schedulingv1.PriorityClass, error) { 81 pcs := schedulingv1.PriorityClassList{} 82 err := client.List(ctx, &pcs) 83 if err != nil { 84 return nil, err 85 } 86 87 // In case more than one global default priority class is added as a result of a race condition, 88 // we pick the one with the lowest priority value. 89 var defaultPC *schedulingv1.PriorityClass 90 for _, pci := range pcs.Items { 91 item := pci 92 93 if item.GlobalDefault { 94 if defaultPC == nil || defaultPC.Value > item.Value { 95 defaultPC = &item 96 } 97 } 98 } 99 100 return defaultPC, nil 101 }