diff --git a/staging/src/k8s.io/component-helpers/BUILD b/staging/src/k8s.io/component-helpers/BUILD index 6df04e38cd7..9f9d881f84e 100644 --- a/staging/src/k8s.io/component-helpers/BUILD +++ b/staging/src/k8s.io/component-helpers/BUILD @@ -7,7 +7,10 @@ filegroup( filegroup( name = "all-srcs", - srcs = [":package-srcs"], + srcs = [ + ":package-srcs", + "//staging/src/k8s.io/component-helpers/scheduling/corev1:all-srcs", + ], tags = ["automanaged"], visibility = ["//visibility:public"], ) diff --git a/staging/src/k8s.io/component-helpers/scheduling/OWNERS b/staging/src/k8s.io/component-helpers/scheduling/OWNERS new file mode 100644 index 00000000000..485eb202d98 --- /dev/null +++ b/staging/src/k8s.io/component-helpers/scheduling/OWNERS @@ -0,0 +1,8 @@ +# See the OWNERS docs at https://go.k8s.io/owners + +approvers: +- sig-scheduling-maintainers +reviewers: +- sig-scheduling +labels: +- sig/scheduling diff --git a/staging/src/k8s.io/component-helpers/scheduling/corev1/BUILD b/staging/src/k8s.io/component-helpers/scheduling/corev1/BUILD new file mode 100644 index 00000000000..acc8f9c23c0 --- /dev/null +++ b/staging/src/k8s.io/component-helpers/scheduling/corev1/BUILD @@ -0,0 +1,34 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = [ + "doc.go", + "helpers.go", + ], + importmap = "k8s.io/kubernetes/vendor/k8s.io/component-helpers/scheduling/corev1", + importpath = "k8s.io/component-helpers/scheduling/corev1", + visibility = ["//visibility:public"], + deps = ["//staging/src/k8s.io/api/core/v1:go_default_library"], +) + +go_test( + name = "go_default_test", + srcs = ["helpers_test.go"], + embed = [":go_default_library"], + deps = ["//staging/src/k8s.io/api/core/v1:go_default_library"], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/staging/src/k8s.io/component-helpers/scheduling/corev1/doc.go b/staging/src/k8s.io/component-helpers/scheduling/corev1/doc.go new file mode 100644 index 00000000000..c6cf8132afb --- /dev/null +++ b/staging/src/k8s.io/component-helpers/scheduling/corev1/doc.go @@ -0,0 +1,23 @@ +/* +Copyright 2020 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 corev1 defines functions which should satisfy one of the following: +// +// - Be used by more than one core component (kube-scheduler, kubelet, kube-apiserver, etc.) +// - Be used by a core component and another kubernetes project (cluster-autoscaler, descheduler) +// +// And be a scheduling feature. +package corev1 // import "k8s.io/component-helpers/scheduling/corev1" diff --git a/staging/src/k8s.io/component-helpers/scheduling/corev1/helpers.go b/staging/src/k8s.io/component-helpers/scheduling/corev1/helpers.go new file mode 100644 index 00000000000..688a9218cc5 --- /dev/null +++ b/staging/src/k8s.io/component-helpers/scheduling/corev1/helpers.go @@ -0,0 +1,32 @@ +/* +Copyright 2020 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 corev1 + +import ( + v1 "k8s.io/api/core/v1" +) + +// PodPriority returns priority of the given pod. +func PodPriority(pod *v1.Pod) int32 { + if pod.Spec.Priority != nil { + return *pod.Spec.Priority + } + // When priority of a running pod 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 0 +} diff --git a/staging/src/k8s.io/component-helpers/scheduling/corev1/helpers_test.go b/staging/src/k8s.io/component-helpers/scheduling/corev1/helpers_test.go new file mode 100644 index 00000000000..3a3249c03d3 --- /dev/null +++ b/staging/src/k8s.io/component-helpers/scheduling/corev1/helpers_test.go @@ -0,0 +1,59 @@ +/* +Copyright 2020 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 corev1 + +import ( + "testing" + + v1 "k8s.io/api/core/v1" +) + +// TestPodPriority tests PodPriority function. +func TestPodPriority(t *testing.T) { + p := int32(20) + tests := []struct { + name string + pod *v1.Pod + expectedPriority int32 + }{ + { + name: "no priority pod resolves to static default priority", + pod: &v1.Pod{ + Spec: v1.PodSpec{Containers: []v1.Container{ + {Name: "container", Image: "image"}}, + }, + }, + expectedPriority: 0, + }, + { + name: "pod with priority resolves correctly", + pod: &v1.Pod{ + Spec: v1.PodSpec{Containers: []v1.Container{ + {Name: "container", Image: "image"}}, + Priority: &p, + }, + }, + expectedPriority: p, + }, + } + for _, test := range tests { + if PodPriority(test.pod) != test.expectedPriority { + t.Errorf("expected pod priority: %v, got %v", test.expectedPriority, PodPriority(test.pod)) + } + + } +}