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.
Merge pull request #104 from denkensk/add-workload-priority
Add PriorityClass in Workload api
- Loading branch information
Showing
12 changed files
with
372 additions
and
11 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,87 @@ | ||
/* | ||
Copyright 2022 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package priority | ||
|
||
import ( | ||
"context" | ||
|
||
schedulingv1 "k8s.io/api/scheduling/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
kueue "sigs.k8s.io/kueue/api/v1alpha1" | ||
"sigs.k8s.io/kueue/pkg/constants" | ||
) | ||
|
||
// Priority returns priority of the given workload. | ||
func Priority(w *kueue.QueuedWorkload) int32 { | ||
if w.Spec.Priority != nil { | ||
return *w.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) (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.Name, pc.Value, nil | ||
} | ||
|
||
func getDefaultPriority(ctx context.Context, client client.Client) (string, int32, error) { | ||
dpc, err := getDefaultPriorityClass(ctx, client) | ||
if err != nil { | ||
return "", 0, err | ||
} | ||
if dpc != nil { | ||
return dpc.Name, 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.