Skip to content

Commit 5238e1c

Browse files
authored
Merge pull request kubernetes#76362 from yue9944882/chore/follow-up-prune-internal-client-scheduling-v1-api
Follow-up of kubernetes#71136: Switch system priority class usage to versioned (v1) api
2 parents 31b7e3d + 09cf42d commit 5238e1c

File tree

14 files changed

+60
-54
lines changed

14 files changed

+60
-54
lines changed

pkg/apis/scheduling/BUILD

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
package(default_visibility = ["//visibility:public"])
22

3-
load(
4-
"@io_bazel_rules_go//go:def.bzl",
5-
"go_library",
6-
"go_test",
7-
)
3+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
84

95
go_library(
106
name = "go_default_library",
117
srcs = [
128
"doc.go",
13-
"helpers.go",
149
"register.go",
1510
"types.go",
1611
"zz_generated.deepcopy.go",
@@ -45,10 +40,3 @@ filegroup(
4540
],
4641
tags = ["automanaged"],
4742
)
48-
49-
go_test(
50-
name = "go_default_test",
51-
srcs = ["helpers_test.go"],
52-
embed = [":go_default_library"],
53-
deps = ["//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library"],
54-
)

pkg/apis/scheduling/v1/BUILD

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go_library(
55
srcs = [
66
"defaults.go",
77
"doc.go",
8+
"helpers.go",
89
"register.go",
910
"zz_generated.conversion.go",
1011
"zz_generated.defaults.go",
@@ -17,6 +18,7 @@ go_library(
1718
"//pkg/features:go_default_library",
1819
"//staging/src/k8s.io/api/core/v1:go_default_library",
1920
"//staging/src/k8s.io/api/scheduling/v1:go_default_library",
21+
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
2022
"//staging/src/k8s.io/apimachinery/pkg/conversion:go_default_library",
2123
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
2224
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
@@ -40,14 +42,19 @@ filegroup(
4042

4143
go_test(
4244
name = "go_default_test",
43-
srcs = ["defaults_test.go"],
45+
srcs = [
46+
"defaults_test.go",
47+
"helpers_test.go",
48+
],
4449
embed = [":go_default_library"],
4550
deps = [
4651
"//pkg/api/legacyscheme:go_default_library",
4752
"//pkg/api/testapi:go_default_library",
53+
"//pkg/apis/scheduling:go_default_library",
4854
"//pkg/features:go_default_library",
4955
"//staging/src/k8s.io/api/core/v1:go_default_library",
5056
"//staging/src/k8s.io/api/scheduling/v1:go_default_library",
57+
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
5158
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
5259
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
5360
"//staging/src/k8s.io/component-base/featuregate/testing:go_default_library",
Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2018 The Kubernetes Authors.
2+
Copyright 2019 The Kubernetes Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -14,52 +14,54 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package scheduling
17+
package v1
1818

1919
import (
2020
"fmt"
21+
"k8s.io/api/scheduling/v1"
2122
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"k8s.io/kubernetes/pkg/apis/scheduling"
2224
)
2325

2426
// SystemPriorityClasses define system priority classes that are auto-created at cluster bootstrapping.
2527
// Our API validation logic ensures that any priority class that has a system prefix or its value
2628
// is higher than HighestUserDefinablePriority is equal to one of these SystemPriorityClasses.
27-
var systemPriorityClasses = []*PriorityClass{
29+
var systemPriorityClasses = []*v1.PriorityClass{
2830
{
2931
ObjectMeta: metav1.ObjectMeta{
30-
Name: SystemNodeCritical,
32+
Name: scheduling.SystemNodeCritical,
3133
},
32-
Value: SystemCriticalPriority + 1000,
34+
Value: scheduling.SystemCriticalPriority + 1000,
3335
Description: "Used for system critical pods that must not be moved from their current node.",
3436
},
3537
{
3638
ObjectMeta: metav1.ObjectMeta{
37-
Name: SystemClusterCritical,
39+
Name: scheduling.SystemClusterCritical,
3840
},
39-
Value: SystemCriticalPriority,
41+
Value: scheduling.SystemCriticalPriority,
4042
Description: "Used for system critical pods that must run in the cluster, but can be moved to another node if necessary.",
4143
},
4244
}
4345

4446
// SystemPriorityClasses returns the list of system priority classes.
4547
// NOTE: be careful not to modify any of elements of the returned array directly.
46-
func SystemPriorityClasses() []*PriorityClass {
48+
func SystemPriorityClasses() []*v1.PriorityClass {
4749
return systemPriorityClasses
4850
}
4951

50-
// IsKnownSystemPriorityClass checks that "pc" is equal to one of the system PriorityClasses.
51-
// It ignores "description", labels, annotations, etc. of the PriorityClass.
52-
func IsKnownSystemPriorityClass(pc *PriorityClass) (bool, error) {
53-
for _, spc := range systemPriorityClasses {
54-
if spc.Name == pc.Name {
55-
if spc.Value != pc.Value {
52+
// IsKnownSystemPriorityClass returns true if there's any of the system priority classes exactly
53+
// matches "name", "value", "globalDefault". otherwise it will return an error.
54+
func IsKnownSystemPriorityClass(name string, value int32, globalDefault bool) (bool, error) {
55+
for _, spc := range SystemPriorityClasses() {
56+
if spc.Name == name {
57+
if spc.Value != value {
5658
return false, fmt.Errorf("value of %v PriorityClass must be %v", spc.Name, spc.Value)
5759
}
58-
if spc.GlobalDefault != pc.GlobalDefault {
60+
if spc.GlobalDefault != globalDefault {
5961
return false, fmt.Errorf("globalDefault of %v PriorityClass must be %v", spc.Name, spc.GlobalDefault)
6062
}
6163
return true, nil
6264
}
6365
}
64-
return false, fmt.Errorf("%v is not a known system priority class", pc.Name)
66+
return false, fmt.Errorf("%v is not a known system priority class", name)
6567
}

pkg/apis/scheduling/helpers_test.go renamed to pkg/apis/scheduling/v1/helpers_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2018 The Kubernetes Authors.
2+
Copyright 2019 The Kubernetes Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -14,18 +14,20 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package scheduling
17+
package v1
1818

1919
import (
2020
"testing"
2121

22+
v1 "k8s.io/api/scheduling/v1"
2223
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"k8s.io/kubernetes/pkg/apis/scheduling"
2325
)
2426

2527
func TestIsKnownSystemPriorityClass(t *testing.T) {
2628
tests := []struct {
2729
name string
28-
pc *PriorityClass
30+
pc *v1.PriorityClass
2931
expected bool
3032
}{
3133
{
@@ -35,19 +37,19 @@ func TestIsKnownSystemPriorityClass(t *testing.T) {
3537
},
3638
{
3739
name: "non-system priority class",
38-
pc: &PriorityClass{
40+
pc: &v1.PriorityClass{
3941
ObjectMeta: metav1.ObjectMeta{
40-
Name: SystemNodeCritical,
42+
Name: scheduling.SystemNodeCritical,
4143
},
42-
Value: SystemCriticalPriority, // This is the value of system cluster critical
44+
Value: scheduling.SystemCriticalPriority, // This is the value of system cluster critical
4345
Description: "Used for system critical pods that must not be moved from their current node.",
4446
},
4547
expected: false,
4648
},
4749
}
4850

4951
for _, test := range tests {
50-
if is, err := IsKnownSystemPriorityClass(test.pc); test.expected != is {
52+
if is, err := IsKnownSystemPriorityClass(test.pc.Name, test.pc.Value, test.pc.GlobalDefault); test.expected != is {
5153
t.Errorf("Test [%v]: Expected %v, but got %v. Error: %v", test.name, test.expected, is, err)
5254
}
5355
}

pkg/apis/scheduling/validation/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ go_test(
1212
embed = [":go_default_library"],
1313
deps = [
1414
"//pkg/apis/scheduling:go_default_library",
15+
"//pkg/apis/scheduling/v1:go_default_library",
1516
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
1617
"//staging/src/k8s.io/apimachinery/pkg/util/validation/field:go_default_library",
1718
],
@@ -24,6 +25,7 @@ go_library(
2425
deps = [
2526
"//pkg/apis/core/validation:go_default_library",
2627
"//pkg/apis/scheduling:go_default_library",
28+
"//pkg/apis/scheduling/v1:go_default_library",
2729
"//staging/src/k8s.io/apimachinery/pkg/api/validation:go_default_library",
2830
"//staging/src/k8s.io/apimachinery/pkg/util/validation/field:go_default_library",
2931
],

pkg/apis/scheduling/validation/validation.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"k8s.io/apimachinery/pkg/util/validation/field"
2525
apivalidation "k8s.io/kubernetes/pkg/apis/core/validation"
2626
"k8s.io/kubernetes/pkg/apis/scheduling"
27+
schedulingapiv1 "k8s.io/kubernetes/pkg/apis/scheduling/v1"
2728
)
2829

2930
// ValidatePriorityClass tests whether required fields in the PriorityClass are
@@ -34,7 +35,7 @@ func ValidatePriorityClass(pc *scheduling.PriorityClass) field.ErrorList {
3435
// If the priorityClass starts with a system prefix, it must be one of the
3536
// predefined system priority classes.
3637
if strings.HasPrefix(pc.Name, scheduling.SystemPriorityClassPrefix) {
37-
if is, err := scheduling.IsKnownSystemPriorityClass(pc); !is {
38+
if is, err := schedulingapiv1.IsKnownSystemPriorityClass(pc.Name, pc.Value, pc.GlobalDefault); !is {
3839
allErrs = append(allErrs, field.Forbidden(field.NewPath("metadata", "name"), "priority class names with '"+scheduling.SystemPriorityClassPrefix+"' prefix are reserved for system use only. error: "+err.Error()))
3940
}
4041
} else if pc.Value > scheduling.HighestUserDefinablePriority {

pkg/apis/scheduling/validation/validation_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ import (
2222
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2323
"k8s.io/apimachinery/pkg/util/validation/field"
2424
"k8s.io/kubernetes/pkg/apis/scheduling"
25+
schedulingapiv1 "k8s.io/kubernetes/pkg/apis/scheduling/v1"
2526
)
2627

2728
func TestValidatePriorityClass(t *testing.T) {
28-
spcs := scheduling.SystemPriorityClasses()
29+
spcs := schedulingapiv1.SystemPriorityClasses()
2930
successCases := map[string]scheduling.PriorityClass{
3031
"no description": {
3132
ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: ""},

pkg/registry/scheduling/priorityclass/storage/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ go_test(
1212
embed = [":go_default_library"],
1313
deps = [
1414
"//pkg/apis/scheduling:go_default_library",
15+
"//pkg/apis/scheduling/v1:go_default_library",
1516
"//pkg/registry/registrytest:go_default_library",
1617
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
1718
"//staging/src/k8s.io/apimachinery/pkg/fields:go_default_library",
@@ -31,6 +32,7 @@ go_library(
3132
importpath = "k8s.io/kubernetes/pkg/registry/scheduling/priorityclass/storage",
3233
deps = [
3334
"//pkg/apis/scheduling:go_default_library",
35+
"//pkg/apis/scheduling/v1:go_default_library",
3436
"//pkg/printers:go_default_library",
3537
"//pkg/printers/internalversion:go_default_library",
3638
"//pkg/printers/storage:go_default_library",

pkg/registry/scheduling/priorityclass/storage/storage.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
2727
"k8s.io/apiserver/pkg/registry/rest"
2828
"k8s.io/kubernetes/pkg/apis/scheduling"
29+
schedulingapiv1 "k8s.io/kubernetes/pkg/apis/scheduling/v1"
2930
"k8s.io/kubernetes/pkg/printers"
3031
printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
3132
printerstorage "k8s.io/kubernetes/pkg/printers/storage"
@@ -68,7 +69,7 @@ func (r *REST) ShortNames() []string {
6869

6970
// Delete ensures that system priority classes are not deleted.
7071
func (r *REST) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
71-
for _, spc := range scheduling.SystemPriorityClasses() {
72+
for _, spc := range schedulingapiv1.SystemPriorityClasses() {
7273
if name == spc.Name {
7374
return nil, false, apierrors.NewForbidden(scheduling.Resource("priorityclasses"), spc.Name, errors.New("this is a system priority class and cannot be deleted"))
7475
}

pkg/registry/scheduling/priorityclass/storage/storage_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/apiserver/pkg/registry/rest"
3030
etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing"
3131
"k8s.io/kubernetes/pkg/apis/scheduling"
32+
schedulingapiv1 "k8s.io/kubernetes/pkg/apis/scheduling/v1"
3233
"k8s.io/kubernetes/pkg/registry/registrytest"
3334
)
3435

@@ -118,8 +119,12 @@ func TestDeleteSystemPriorityClass(t *testing.T) {
118119
defer storage.Store.DestroyFunc()
119120
key := "test/system-node-critical"
120121
ctx := genericapirequest.NewContext()
121-
pc := scheduling.SystemPriorityClasses()[0]
122-
if err := storage.Store.Storage.Create(ctx, key, pc, nil, 0, false); err != nil {
122+
pc := schedulingapiv1.SystemPriorityClasses()[0]
123+
internalPc := &scheduling.PriorityClass{}
124+
if err := schedulingapiv1.Convert_v1_PriorityClass_To_scheduling_PriorityClass(pc, internalPc, nil); err != nil {
125+
t.Fatal(err)
126+
}
127+
if err := storage.Store.Storage.Create(ctx, key, internalPc, nil, 0, false); err != nil {
123128
t.Fatalf("unexpected error: %v", err)
124129
}
125130
if _, _, err := storage.Delete(ctx, pc.Name, rest.ValidateAllObjectFunc, nil); err == nil {

0 commit comments

Comments
 (0)