apiserver: set borrowing defaults for flowcontrol API

This commit is contained in:
Abu Kashem 2022-10-17 16:27:51 -04:00 committed by Mike Spreitzer
parent a76223f8da
commit ca949d5188
11 changed files with 379 additions and 0 deletions

View File

@ -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))

View File

@ -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,
)

View File

@ -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
}
},
}
}

View File

@ -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

View File

@ -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))
}
})
}
}

View File

@ -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

View File

@ -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))
}
})
}
}

View File

@ -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

View File

@ -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))
}
})
}
}

View File

@ -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

View File

@ -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))
}
})
}
}