Skip to content

Commit 95ad020

Browse files
committed
Duplicate a single helper from pkg/apis
Duplicating GetPodPriority code from k8s.io/kubernetes/pkg/api/v1/pod as PodPriority
1 parent 75cee78 commit 95ad020

File tree

6 files changed

+160
-1
lines changed

6 files changed

+160
-1
lines changed

staging/src/k8s.io/component-helpers/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ filegroup(
77

88
filegroup(
99
name = "all-srcs",
10-
srcs = [":package-srcs"],
10+
srcs = [
11+
":package-srcs",
12+
"//staging/src/k8s.io/component-helpers/scheduling/corev1:all-srcs",
13+
],
1114
tags = ["automanaged"],
1215
visibility = ["//visibility:public"],
1316
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# See the OWNERS docs at https://go.k8s.io/owners
2+
3+
approvers:
4+
- sig-scheduling-maintainers
5+
reviewers:
6+
- sig-scheduling
7+
labels:
8+
- sig/scheduling
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
2+
3+
go_library(
4+
name = "go_default_library",
5+
srcs = [
6+
"doc.go",
7+
"helpers.go",
8+
],
9+
importmap = "k8s.io/kubernetes/vendor/k8s.io/component-helpers/scheduling/corev1",
10+
importpath = "k8s.io/component-helpers/scheduling/corev1",
11+
visibility = ["//visibility:public"],
12+
deps = ["//staging/src/k8s.io/api/core/v1:go_default_library"],
13+
)
14+
15+
go_test(
16+
name = "go_default_test",
17+
srcs = ["helpers_test.go"],
18+
embed = [":go_default_library"],
19+
deps = ["//staging/src/k8s.io/api/core/v1:go_default_library"],
20+
)
21+
22+
filegroup(
23+
name = "package-srcs",
24+
srcs = glob(["**"]),
25+
tags = ["automanaged"],
26+
visibility = ["//visibility:private"],
27+
)
28+
29+
filegroup(
30+
name = "all-srcs",
31+
srcs = [":package-srcs"],
32+
tags = ["automanaged"],
33+
visibility = ["//visibility:public"],
34+
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright 2020 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 corev1 defines functions which should satisfy one of the following:
18+
//
19+
// - Be used by more than one core component (kube-scheduler, kubelet, kube-apiserver, etc.)
20+
// - Be used by a core component and another kubernetes project (cluster-autoscaler, descheduler)
21+
//
22+
// And be a scheduling feature.
23+
package corev1 // import "k8s.io/component-helpers/scheduling/corev1"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright 2020 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 corev1
18+
19+
import (
20+
v1 "k8s.io/api/core/v1"
21+
)
22+
23+
// PodPriority returns priority of the given pod.
24+
func PodPriority(pod *v1.Pod) int32 {
25+
if pod.Spec.Priority != nil {
26+
return *pod.Spec.Priority
27+
}
28+
// When priority of a running pod is nil, it means it was created at a time
29+
// that there was no global default priority class and the priority class
30+
// name of the pod was empty. So, we resolve to the static default priority.
31+
return 0
32+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2020 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 corev1
18+
19+
import (
20+
"testing"
21+
22+
v1 "k8s.io/api/core/v1"
23+
)
24+
25+
// TestPodPriority tests PodPriority function.
26+
func TestPodPriority(t *testing.T) {
27+
p := int32(20)
28+
tests := []struct {
29+
name string
30+
pod *v1.Pod
31+
expectedPriority int32
32+
}{
33+
{
34+
name: "no priority pod resolves to static default priority",
35+
pod: &v1.Pod{
36+
Spec: v1.PodSpec{Containers: []v1.Container{
37+
{Name: "container", Image: "image"}},
38+
},
39+
},
40+
expectedPriority: 0,
41+
},
42+
{
43+
name: "pod with priority resolves correctly",
44+
pod: &v1.Pod{
45+
Spec: v1.PodSpec{Containers: []v1.Container{
46+
{Name: "container", Image: "image"}},
47+
Priority: &p,
48+
},
49+
},
50+
expectedPriority: p,
51+
},
52+
}
53+
for _, test := range tests {
54+
if PodPriority(test.pod) != test.expectedPriority {
55+
t.Errorf("expected pod priority: %v, got %v", test.expectedPriority, PodPriority(test.pod))
56+
}
57+
58+
}
59+
}

0 commit comments

Comments
 (0)