generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alex Wang <wangqingcan1990@gmail.com>
- Loading branch information
Showing
10 changed files
with
283 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package priority | ||
|
||
import ( | ||
"context" | ||
|
||
schedulingv1 "k8s.io/api/scheduling/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"sigs.k8s.io/kueue/pkg/constants" | ||
"sigs.k8s.io/kueue/pkg/workload" | ||
) | ||
|
||
// Priority returns priority of the given workload. | ||
func Priority(w workload.Info) int32 { | ||
if w.Obj.Spec.Priority != nil { | ||
return *w.Obj.Spec.Priority | ||
} | ||
// When priority of a running workload is nil, it means it was created at a time | ||
// that there was no global default priority class and the priority class | ||
// name of the pod was empty. So, we resolve to the static default priority. | ||
return constants.DefaultPriority | ||
} | ||
|
||
// GetPriorityFromPriorityClass returns the priority populated from | ||
// priority class. If not specified, priority will be default or | ||
// zero if there is no default. | ||
func GetPriorityFromPriorityClass(ctx context.Context, client client.Client, | ||
priorityClass string) (int32, error) { | ||
if len(priorityClass) == 0 { | ||
return getDefaultPriority(ctx, client) | ||
} | ||
|
||
pc := &schedulingv1.PriorityClass{} | ||
if err := client.Get(ctx, types.NamespacedName{Name: priorityClass}, pc); err != nil { | ||
return 0, err | ||
} | ||
|
||
return pc.Value, nil | ||
} | ||
|
||
func getDefaultPriority(ctx context.Context, client client.Client) (int32, error) { | ||
dpc, err := getDefaultPriorityClass(ctx, client) | ||
if err != nil { | ||
return 0, err | ||
} | ||
if dpc != nil { | ||
return dpc.Value, nil | ||
} | ||
return int32(constants.DefaultPriority), nil | ||
} | ||
|
||
func getDefaultPriorityClass(ctx context.Context, client client.Client) (*schedulingv1.PriorityClass, error) { | ||
pcs := schedulingv1.PriorityClassList{} | ||
err := client.List(ctx, &pcs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// In case more than one global default priority class is added as a result of a race condition, | ||
// we pick the one with the lowest priority value. | ||
var defaultPC *schedulingv1.PriorityClass | ||
for _, pci := range pcs.Items { | ||
if pci.GlobalDefault { | ||
if defaultPC == nil || defaultPC.Value > pci.Value { | ||
defaultPC = &pci | ||
} | ||
} | ||
} | ||
return defaultPC, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.