diff --git a/pkg/api/testing/defaulting_test.go b/pkg/api/testing/defaulting_test.go index a61d7942839..cd341e60a72 100644 --- a/pkg/api/testing/defaulting_test.go +++ b/pkg/api/testing/defaulting_test.go @@ -176,6 +176,14 @@ func TestDefaulting(t *testing.T) { {Group: "scheduling.k8s.io", Version: "v1alpha1", Kind: "PriorityClassList"}: {}, {Group: "scheduling.k8s.io", Version: "v1beta1", Kind: "PriorityClassList"}: {}, {Group: "scheduling.k8s.io", Version: "v1", Kind: "PriorityClassList"}: {}, + {Group: "flowcontrol.apiserver.k8s.io", Version: "v1alpha1", Kind: "PriorityLevelConfiguration"}: {}, + {Group: "flowcontrol.apiserver.k8s.io", Version: "v1alpha1", Kind: "PriorityLevelConfigurationList"}: {}, + {Group: "flowcontrol.apiserver.k8s.io", Version: "v1beta1", Kind: "PriorityLevelConfiguration"}: {}, + {Group: "flowcontrol.apiserver.k8s.io", Version: "v1beta1", Kind: "PriorityLevelConfigurationList"}: {}, + {Group: "flowcontrol.apiserver.k8s.io", Version: "v1beta2", Kind: "PriorityLevelConfiguration"}: {}, + {Group: "flowcontrol.apiserver.k8s.io", Version: "v1beta2", Kind: "PriorityLevelConfigurationList"}: {}, + {Group: "flowcontrol.apiserver.k8s.io", Version: "v1beta3", Kind: "PriorityLevelConfiguration"}: {}, + {Group: "flowcontrol.apiserver.k8s.io", Version: "v1beta3", Kind: "PriorityLevelConfigurationList"}: {}, } f := fuzz.New().NilChance(.5).NumElements(1, 1).RandSource(rand.NewSource(1)) diff --git a/pkg/api/testing/fuzzer.go b/pkg/api/testing/fuzzer.go index 462a7c1bc92..e43f0b8ebb6 100644 --- a/pkg/api/testing/fuzzer.go +++ b/pkg/api/testing/fuzzer.go @@ -38,6 +38,7 @@ import ( corefuzzer "k8s.io/kubernetes/pkg/apis/core/fuzzer" discoveryfuzzer "k8s.io/kubernetes/pkg/apis/discovery/fuzzer" extensionsfuzzer "k8s.io/kubernetes/pkg/apis/extensions/fuzzer" + flowcontrolfuzzer "k8s.io/kubernetes/pkg/apis/flowcontrol/fuzzer" networkingfuzzer "k8s.io/kubernetes/pkg/apis/networking/fuzzer" policyfuzzer "k8s.io/kubernetes/pkg/apis/policy/fuzzer" rbacfuzzer "k8s.io/kubernetes/pkg/apis/rbac/fuzzer" @@ -107,4 +108,5 @@ var FuzzerFuncs = fuzzer.MergeFuzzerFuncs( metafuzzer.Funcs, schedulingfuzzer.Funcs, discoveryfuzzer.Funcs, + flowcontrolfuzzer.Funcs, ) diff --git a/pkg/apis/flowcontrol/fuzzer/fuzzer.go b/pkg/apis/flowcontrol/fuzzer/fuzzer.go new file mode 100644 index 00000000000..37ce6c3eaf4 --- /dev/null +++ b/pkg/apis/flowcontrol/fuzzer/fuzzer.go @@ -0,0 +1,37 @@ +/* +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 fuzzer + +import ( + fuzz "github.com/google/gofuzz" + + runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer" + "k8s.io/kubernetes/pkg/apis/flowcontrol" +) + +// Funcs returns the fuzzer functions for the flowcontrol api group. +var Funcs = func(codecs runtimeserializer.CodecFactory) []interface{} { + return []interface{}{ + func(obj *flowcontrol.LimitedPriorityLevelConfiguration, c fuzz.Continue) { + c.FuzzNoCustom(obj) // fuzz self without calling this function again + if obj.LendablePercent == nil { + i := int32(0) + obj.LendablePercent = &i + } + }, + } +} diff --git a/pkg/apis/flowcontrol/v1alpha1/defaults.go b/pkg/apis/flowcontrol/v1alpha1/defaults.go index c3bfdb208ee..1c85f6c6f7b 100644 --- a/pkg/apis/flowcontrol/v1alpha1/defaults.go +++ b/pkg/apis/flowcontrol/v1alpha1/defaults.go @@ -44,6 +44,10 @@ func SetDefaults_LimitedPriorityLevelConfiguration(lplc *v1alpha1.LimitedPriorit if lplc.AssuredConcurrencyShares == 0 { lplc.AssuredConcurrencyShares = PriorityLevelConfigurationDefaultAssuredConcurrencyShares } + if lplc.LendablePercent == nil { + lplc.LendablePercent = new(int32) + *lplc.LendablePercent = 0 + } } // SetDefaults_FlowSchema sets default values for flow schema diff --git a/pkg/apis/flowcontrol/v1alpha1/defaults_test.go b/pkg/apis/flowcontrol/v1alpha1/defaults_test.go new file mode 100644 index 00000000000..f5a3f54fc2b --- /dev/null +++ b/pkg/apis/flowcontrol/v1alpha1/defaults_test.go @@ -0,0 +1,79 @@ +/* +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 v1alpha1 + +import ( + "reflect" + "testing" + + "github.com/google/go-cmp/cmp" + flowcontrolv1alpha1 "k8s.io/api/flowcontrol/v1alpha1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/utils/pointer" +) + +func TestDefaultWithPriorityLevelConfiguration(t *testing.T) { + tests := []struct { + name string + original runtime.Object + expected runtime.Object + }{ + { + name: "LendablePercent is not specified, should default to zero", + original: &flowcontrolv1alpha1.PriorityLevelConfiguration{ + Spec: flowcontrolv1alpha1.PriorityLevelConfigurationSpec{ + Type: flowcontrolv1alpha1.PriorityLevelEnablementLimited, + Limited: &flowcontrolv1alpha1.LimitedPriorityLevelConfiguration{ + AssuredConcurrencyShares: 5, + LimitResponse: flowcontrolv1alpha1.LimitResponse{ + Type: flowcontrolv1alpha1.LimitResponseTypeReject, + }, + }, + }, + }, + expected: &flowcontrolv1alpha1.PriorityLevelConfiguration{ + Spec: flowcontrolv1alpha1.PriorityLevelConfigurationSpec{ + Type: flowcontrolv1alpha1.PriorityLevelEnablementLimited, + Limited: &flowcontrolv1alpha1.LimitedPriorityLevelConfiguration{ + AssuredConcurrencyShares: 5, + LendablePercent: pointer.Int32(0), + LimitResponse: flowcontrolv1alpha1.LimitResponse{ + Type: flowcontrolv1alpha1.LimitResponseTypeReject, + }, + }, + }, + }, + }, + } + + scheme := runtime.NewScheme() + if err := AddToScheme(scheme); err != nil { + t.Fatalf("Failed to add to scheme: %v", err) + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + original := test.original + expected := test.expected + + scheme.Default(original) + if !reflect.DeepEqual(expected, original) { + t.Errorf("Expected defaulting to work - diff: %s", cmp.Diff(expected, original)) + } + }) + } +} diff --git a/pkg/apis/flowcontrol/v1beta1/defaults.go b/pkg/apis/flowcontrol/v1beta1/defaults.go index 9beb3665111..a8ee8a784bb 100644 --- a/pkg/apis/flowcontrol/v1beta1/defaults.go +++ b/pkg/apis/flowcontrol/v1beta1/defaults.go @@ -44,6 +44,10 @@ func SetDefaults_LimitedPriorityLevelConfiguration(lplc *v1beta1.LimitedPriority if lplc.AssuredConcurrencyShares == 0 { lplc.AssuredConcurrencyShares = PriorityLevelConfigurationDefaultAssuredConcurrencyShares } + if lplc.LendablePercent == nil { + lplc.LendablePercent = new(int32) + *lplc.LendablePercent = 0 + } } // SetDefaults_FlowSchema sets default values for flow schema diff --git a/pkg/apis/flowcontrol/v1beta1/defaults_test.go b/pkg/apis/flowcontrol/v1beta1/defaults_test.go new file mode 100644 index 00000000000..e6a5a356651 --- /dev/null +++ b/pkg/apis/flowcontrol/v1beta1/defaults_test.go @@ -0,0 +1,79 @@ +/* +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 v1beta1 + +import ( + "reflect" + "testing" + + "github.com/google/go-cmp/cmp" + flowcontrolv1beta1 "k8s.io/api/flowcontrol/v1beta1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/utils/pointer" +) + +func TestDefaultWithPriorityLevelConfiguration(t *testing.T) { + tests := []struct { + name string + original runtime.Object + expected runtime.Object + }{ + { + name: "LendablePercent is not specified, should default to zero", + original: &flowcontrolv1beta1.PriorityLevelConfiguration{ + Spec: flowcontrolv1beta1.PriorityLevelConfigurationSpec{ + Type: flowcontrolv1beta1.PriorityLevelEnablementLimited, + Limited: &flowcontrolv1beta1.LimitedPriorityLevelConfiguration{ + AssuredConcurrencyShares: 5, + LimitResponse: flowcontrolv1beta1.LimitResponse{ + Type: flowcontrolv1beta1.LimitResponseTypeReject, + }, + }, + }, + }, + expected: &flowcontrolv1beta1.PriorityLevelConfiguration{ + Spec: flowcontrolv1beta1.PriorityLevelConfigurationSpec{ + Type: flowcontrolv1beta1.PriorityLevelEnablementLimited, + Limited: &flowcontrolv1beta1.LimitedPriorityLevelConfiguration{ + AssuredConcurrencyShares: 5, + LendablePercent: pointer.Int32(0), + LimitResponse: flowcontrolv1beta1.LimitResponse{ + Type: flowcontrolv1beta1.LimitResponseTypeReject, + }, + }, + }, + }, + }, + } + + scheme := runtime.NewScheme() + if err := AddToScheme(scheme); err != nil { + t.Fatalf("Failed to add to scheme: %v", err) + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + original := test.original + expected := test.expected + + scheme.Default(original) + if !reflect.DeepEqual(expected, original) { + t.Errorf("Expected defaulting to work - diff: %s", cmp.Diff(expected, original)) + } + }) + } +} diff --git a/pkg/apis/flowcontrol/v1beta2/defaults.go b/pkg/apis/flowcontrol/v1beta2/defaults.go index 99eb6646004..389995bd090 100644 --- a/pkg/apis/flowcontrol/v1beta2/defaults.go +++ b/pkg/apis/flowcontrol/v1beta2/defaults.go @@ -44,6 +44,10 @@ func SetDefaults_LimitedPriorityLevelConfiguration(lplc *v1beta2.LimitedPriority if lplc.AssuredConcurrencyShares == 0 { lplc.AssuredConcurrencyShares = PriorityLevelConfigurationDefaultAssuredConcurrencyShares } + if lplc.LendablePercent == nil { + lplc.LendablePercent = new(int32) + *lplc.LendablePercent = 0 + } } // SetDefaults_FlowSchema sets default values for flow schema diff --git a/pkg/apis/flowcontrol/v1beta2/defaults_test.go b/pkg/apis/flowcontrol/v1beta2/defaults_test.go new file mode 100644 index 00000000000..4d9f07b7ee7 --- /dev/null +++ b/pkg/apis/flowcontrol/v1beta2/defaults_test.go @@ -0,0 +1,79 @@ +/* +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 v1beta2 + +import ( + "reflect" + "testing" + + "github.com/google/go-cmp/cmp" + flowcontrolv1beta2 "k8s.io/api/flowcontrol/v1beta2" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/utils/pointer" +) + +func TestDefaultWithPriorityLevelConfiguration(t *testing.T) { + tests := []struct { + name string + original runtime.Object + expected runtime.Object + }{ + { + name: "LendablePercent is not specified, should default to zero", + original: &flowcontrolv1beta2.PriorityLevelConfiguration{ + Spec: flowcontrolv1beta2.PriorityLevelConfigurationSpec{ + Type: flowcontrolv1beta2.PriorityLevelEnablementLimited, + Limited: &flowcontrolv1beta2.LimitedPriorityLevelConfiguration{ + AssuredConcurrencyShares: 5, + LimitResponse: flowcontrolv1beta2.LimitResponse{ + Type: flowcontrolv1beta2.LimitResponseTypeReject, + }, + }, + }, + }, + expected: &flowcontrolv1beta2.PriorityLevelConfiguration{ + Spec: flowcontrolv1beta2.PriorityLevelConfigurationSpec{ + Type: flowcontrolv1beta2.PriorityLevelEnablementLimited, + Limited: &flowcontrolv1beta2.LimitedPriorityLevelConfiguration{ + AssuredConcurrencyShares: 5, + LendablePercent: pointer.Int32(0), + LimitResponse: flowcontrolv1beta2.LimitResponse{ + Type: flowcontrolv1beta2.LimitResponseTypeReject, + }, + }, + }, + }, + }, + } + + scheme := runtime.NewScheme() + if err := AddToScheme(scheme); err != nil { + t.Fatalf("Failed to add to scheme: %v", err) + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + original := test.original + expected := test.expected + + scheme.Default(original) + if !reflect.DeepEqual(expected, original) { + t.Errorf("Expected defaulting to work - diff: %s", cmp.Diff(expected, original)) + } + }) + } +} diff --git a/pkg/apis/flowcontrol/v1beta3/defaults.go b/pkg/apis/flowcontrol/v1beta3/defaults.go index 77580145bc3..b590bfb5297 100644 --- a/pkg/apis/flowcontrol/v1beta3/defaults.go +++ b/pkg/apis/flowcontrol/v1beta3/defaults.go @@ -44,6 +44,10 @@ func SetDefaults_LimitedPriorityLevelConfiguration(lplc *v1beta3.LimitedPriority if lplc.NominalConcurrencyShares == 0 { lplc.NominalConcurrencyShares = PriorityLevelConfigurationDefaultNominalConcurrencyShares } + if lplc.LendablePercent == nil { + lplc.LendablePercent = new(int32) + *lplc.LendablePercent = 0 + } } // SetDefaults_FlowSchema sets default values for flow schema diff --git a/pkg/apis/flowcontrol/v1beta3/defaults_test.go b/pkg/apis/flowcontrol/v1beta3/defaults_test.go new file mode 100644 index 00000000000..b36334e96b5 --- /dev/null +++ b/pkg/apis/flowcontrol/v1beta3/defaults_test.go @@ -0,0 +1,79 @@ +/* +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 v1beta3 + +import ( + "reflect" + "testing" + + "github.com/google/go-cmp/cmp" + flowcontrolv1beta3 "k8s.io/api/flowcontrol/v1beta3" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/utils/pointer" +) + +func TestDefaultWithPriorityLevelConfiguration(t *testing.T) { + tests := []struct { + name string + original runtime.Object + expected runtime.Object + }{ + { + name: "LendablePercent is not specified, should default to zero", + original: &flowcontrolv1beta3.PriorityLevelConfiguration{ + Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{ + Type: flowcontrolv1beta3.PriorityLevelEnablementLimited, + Limited: &flowcontrolv1beta3.LimitedPriorityLevelConfiguration{ + NominalConcurrencyShares: 5, + LimitResponse: flowcontrolv1beta3.LimitResponse{ + Type: flowcontrolv1beta3.LimitResponseTypeReject, + }, + }, + }, + }, + expected: &flowcontrolv1beta3.PriorityLevelConfiguration{ + Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{ + Type: flowcontrolv1beta3.PriorityLevelEnablementLimited, + Limited: &flowcontrolv1beta3.LimitedPriorityLevelConfiguration{ + NominalConcurrencyShares: 5, + LendablePercent: pointer.Int32(0), + LimitResponse: flowcontrolv1beta3.LimitResponse{ + Type: flowcontrolv1beta3.LimitResponseTypeReject, + }, + }, + }, + }, + }, + } + + scheme := runtime.NewScheme() + if err := AddToScheme(scheme); err != nil { + t.Fatalf("Failed to add to scheme: %v", err) + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + original := test.original + expected := test.expected + + scheme.Default(original) + if !reflect.DeepEqual(expected, original) { + t.Errorf("Expected defaulting to work - diff: %s", cmp.Diff(expected, original)) + } + }) + } +}