Merge pull request #113485 from MikeSpreitzer/apf-borrowing

Add borrowing between priority levels in APF
This commit is contained in:
Kubernetes Prow Robot
2022-11-09 01:40:12 -08:00
committed by GitHub
72 changed files with 2226 additions and 459 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

@@ -17,31 +17,46 @@ limitations under the License.
package internalbootstrap
import (
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
flowcontrol "k8s.io/api/flowcontrol/v1beta3"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apiserver/pkg/apis/flowcontrol/bootstrap"
)
func TestMandatoryAlreadyDefaulted(t *testing.T) {
func TestBootstrapConfigurationWithDefaulted(t *testing.T) {
scheme := NewAPFScheme()
for _, obj := range bootstrap.MandatoryFlowSchemas {
obj2 := obj.DeepCopyObject().(*flowcontrol.FlowSchema)
scheme.Default(obj2)
if apiequality.Semantic.DeepEqual(obj, obj2) {
t.Logf("Defaulting makes no change to %#+v", *obj)
} else {
t.Errorf("Defaulting changed %#+v to %#+v", *obj, *obj2)
}
bootstrapFlowSchemas := make([]*flowcontrol.FlowSchema, 0)
bootstrapFlowSchemas = append(bootstrapFlowSchemas, bootstrap.MandatoryFlowSchemas...)
bootstrapFlowSchemas = append(bootstrapFlowSchemas, bootstrap.SuggestedFlowSchemas...)
for _, original := range bootstrapFlowSchemas {
t.Run(fmt.Sprintf("FlowSchema/%s", original.Name), func(t *testing.T) {
defaulted := original.DeepCopyObject().(*flowcontrol.FlowSchema)
scheme.Default(defaulted)
if apiequality.Semantic.DeepEqual(original, defaulted) {
t.Logf("Defaulting makes no change to FlowSchema: %q", original.Name)
return
}
t.Errorf("Expected defaulting to not change FlowSchema: %q, diff: %s", original.Name, cmp.Diff(original, defaulted))
})
}
for _, obj := range bootstrap.MandatoryPriorityLevelConfigurations {
obj2 := obj.DeepCopyObject().(*flowcontrol.PriorityLevelConfiguration)
scheme.Default(obj2)
if apiequality.Semantic.DeepEqual(obj, obj2) {
t.Logf("Defaulting makes no change to %#+v", *obj)
} else {
t.Errorf("Defaulting changed %#+v to %#+v", *obj, *obj2)
}
bootstrapPriorityLevels := make([]*flowcontrol.PriorityLevelConfiguration, 0)
bootstrapPriorityLevels = append(bootstrapPriorityLevels, bootstrap.MandatoryPriorityLevelConfigurations...)
bootstrapPriorityLevels = append(bootstrapPriorityLevels, bootstrap.SuggestedPriorityLevelConfigurations...)
for _, original := range bootstrapPriorityLevels {
t.Run(fmt.Sprintf("PriorityLevelConfiguration/%s", original.Name), func(t *testing.T) {
defaulted := original.DeepCopyObject().(*flowcontrol.PriorityLevelConfiguration)
scheme.Default(defaulted)
if apiequality.Semantic.DeepEqual(original, defaulted) {
t.Logf("Defaulting makes no change to PriorityLevelConfiguration: %q", original.Name)
return
}
t.Errorf("Expected defaulting to not change PriorityLevelConfiguration: %q, diff: %s", original.Name, cmp.Diff(original, defaulted))
})
}
}

View File

@@ -416,6 +416,35 @@ type LimitedPriorityLevelConfiguration struct {
// `limitResponse` indicates what to do with requests that can not be executed right now
LimitResponse LimitResponse
// `lendablePercent` prescribes the fraction of the level's NominalCL that
// can be borrowed by other priority levels. The value of this
// field must be between 0 and 100, inclusive, and it defaults to 0.
// The number of seats that other levels can borrow from this level, known
// as this level's LendableConcurrencyLimit (LendableCL), is defined as follows.
//
// LendableCL(i) = round( NominalCL(i) * lendablePercent(i)/100.0 )
//
// +optional
LendablePercent *int32
// `borrowingLimitPercent`, if present, configures a limit on how many
// seats this priority level can borrow from other priority levels.
// The limit is known as this level's BorrowingConcurrencyLimit
// (BorrowingCL) and is a limit on the total number of seats that this
// level may borrow at any one time.
// This field holds the ratio of that limit to the level's nominal
// concurrency limit. When this field is non-nil, it must hold a
// non-negative integer and the limit is calculated as follows.
//
// BorrowingCL(i) = round( NominalCL(i) * borrowingLimitPercent(i)/100.0 )
//
// The value of this field can be more than 100, implying that this
// priority level can borrow a number of seats that is greater than
// its own nominal concurrency limit (NominalCL).
// When this field is left `nil`, the limit is effectively infinite.
// +optional
BorrowingLimitPercent *int32
}
// LimitResponse defines how to handle requests that can not be executed right now.

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,80 @@
/*
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

@@ -459,6 +459,8 @@ func autoConvert_v1alpha1_LimitedPriorityLevelConfiguration_To_flowcontrol_Limit
if err := Convert_v1alpha1_LimitResponse_To_flowcontrol_LimitResponse(&in.LimitResponse, &out.LimitResponse, s); err != nil {
return err
}
out.LendablePercent = (*int32)(unsafe.Pointer(in.LendablePercent))
out.BorrowingLimitPercent = (*int32)(unsafe.Pointer(in.BorrowingLimitPercent))
return nil
}
@@ -467,6 +469,8 @@ func autoConvert_flowcontrol_LimitedPriorityLevelConfiguration_To_v1alpha1_Limit
if err := Convert_flowcontrol_LimitResponse_To_v1alpha1_LimitResponse(&in.LimitResponse, &out.LimitResponse, s); err != nil {
return err
}
out.LendablePercent = (*int32)(unsafe.Pointer(in.LendablePercent))
out.BorrowingLimitPercent = (*int32)(unsafe.Pointer(in.BorrowingLimitPercent))
return nil
}

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,80 @@
/*
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

@@ -459,6 +459,8 @@ func autoConvert_v1beta1_LimitedPriorityLevelConfiguration_To_flowcontrol_Limite
if err := Convert_v1beta1_LimitResponse_To_flowcontrol_LimitResponse(&in.LimitResponse, &out.LimitResponse, s); err != nil {
return err
}
out.LendablePercent = (*int32)(unsafe.Pointer(in.LendablePercent))
out.BorrowingLimitPercent = (*int32)(unsafe.Pointer(in.BorrowingLimitPercent))
return nil
}
@@ -467,6 +469,8 @@ func autoConvert_flowcontrol_LimitedPriorityLevelConfiguration_To_v1beta1_Limite
if err := Convert_flowcontrol_LimitResponse_To_v1beta1_LimitResponse(&in.LimitResponse, &out.LimitResponse, s); err != nil {
return err
}
out.LendablePercent = (*int32)(unsafe.Pointer(in.LendablePercent))
out.BorrowingLimitPercent = (*int32)(unsafe.Pointer(in.BorrowingLimitPercent))
return nil
}

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,80 @@
/*
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

@@ -459,6 +459,8 @@ func autoConvert_v1beta2_LimitedPriorityLevelConfiguration_To_flowcontrol_Limite
if err := Convert_v1beta2_LimitResponse_To_flowcontrol_LimitResponse(&in.LimitResponse, &out.LimitResponse, s); err != nil {
return err
}
out.LendablePercent = (*int32)(unsafe.Pointer(in.LendablePercent))
out.BorrowingLimitPercent = (*int32)(unsafe.Pointer(in.BorrowingLimitPercent))
return nil
}
@@ -467,6 +469,8 @@ func autoConvert_flowcontrol_LimitedPriorityLevelConfiguration_To_v1beta2_Limite
if err := Convert_flowcontrol_LimitResponse_To_v1beta2_LimitResponse(&in.LimitResponse, &out.LimitResponse, s); err != nil {
return err
}
out.LendablePercent = (*int32)(unsafe.Pointer(in.LendablePercent))
out.BorrowingLimitPercent = (*int32)(unsafe.Pointer(in.BorrowingLimitPercent))
return nil
}

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,80 @@
/*
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))
}
})
}
}

View File

@@ -459,6 +459,8 @@ func autoConvert_v1beta3_LimitedPriorityLevelConfiguration_To_flowcontrol_Limite
if err := Convert_v1beta3_LimitResponse_To_flowcontrol_LimitResponse(&in.LimitResponse, &out.LimitResponse, s); err != nil {
return err
}
out.LendablePercent = (*int32)(unsafe.Pointer(in.LendablePercent))
out.BorrowingLimitPercent = (*int32)(unsafe.Pointer(in.BorrowingLimitPercent))
return nil
}
@@ -472,6 +474,8 @@ func autoConvert_flowcontrol_LimitedPriorityLevelConfiguration_To_v1beta3_Limite
if err := Convert_flowcontrol_LimitResponse_To_v1beta3_LimitResponse(&in.LimitResponse, &out.LimitResponse, s); err != nil {
return err
}
out.LendablePercent = (*int32)(unsafe.Pointer(in.LendablePercent))
out.BorrowingLimitPercent = (*int32)(unsafe.Pointer(in.BorrowingLimitPercent))
return nil
}

View File

@@ -388,6 +388,14 @@ func ValidateLimitedPriorityLevelConfiguration(lplc *flowcontrol.LimitedPriority
allErrs = append(allErrs, field.Invalid(fldPath.Child(getVersionedFieldNameForConcurrencyShares(requestGV)), lplc.NominalConcurrencyShares, "must be positive"))
}
allErrs = append(allErrs, ValidateLimitResponse(lplc.LimitResponse, fldPath.Child("limitResponse"))...)
if lplc.LendablePercent != nil && !(*lplc.LendablePercent >= 0 && *lplc.LendablePercent <= 100) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("lendablePercent"), *lplc.LendablePercent, "must be between 0 and 100, inclusive"))
}
if lplc.BorrowingLimitPercent != nil && *lplc.BorrowingLimitPercent < 0 {
allErrs = append(allErrs, field.Invalid(fldPath.Child("borrowingLimitPercent"), *lplc.BorrowingLimitPercent, "if specified, must be a non-negative integer"))
}
return allErrs
}

View File

@@ -17,6 +17,7 @@ limitations under the License.
package validation
import (
"fmt"
"math"
"testing"
@@ -31,6 +32,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apiserver/pkg/authentication/user"
"k8s.io/kubernetes/pkg/apis/flowcontrol"
"k8s.io/utils/pointer"
)
func TestFlowSchemaValidation(t *testing.T) {
@@ -1019,6 +1021,7 @@ func TestPriorityLevelConfigurationValidation(t *testing.T) {
Type: flowcontrol.PriorityLevelEnablementLimited,
Limited: &flowcontrol.LimitedPriorityLevelConfiguration{
NominalConcurrencyShares: 5,
LendablePercent: pointer.Int32(0),
LimitResponse: flowcontrol.LimitResponse{
Type: flowcontrol.LimitResponseTypeReject,
}}},
@@ -1434,3 +1437,92 @@ func TestValidateLimitedPriorityLevelConfiguration(t *testing.T) {
})
}
}
func TestValidateLimitedPriorityLevelConfigurationWithBorrowing(t *testing.T) {
errLendablePercentFn := func(v int32) field.ErrorList {
return field.ErrorList{
field.Invalid(field.NewPath("spec").Child("limited").Child("lendablePercent"), v, "must be between 0 and 100, inclusive"),
}
}
errBorrowingLimitPercentFn := func(v int32) field.ErrorList {
return field.ErrorList{
field.Invalid(field.NewPath("spec").Child("limited").Child("borrowingLimitPercent"), v, "if specified, must be a non-negative integer"),
}
}
makeTestNameFn := func(lendablePercent *int32, borrowingLimitPercent *int32) string {
formatFn := func(v *int32) string {
if v == nil {
return "<nil>"
}
return fmt.Sprintf("%d", *v)
}
return fmt.Sprintf("lendablePercent %s, borrowingLimitPercent %s", formatFn(lendablePercent), formatFn(borrowingLimitPercent))
}
tests := []struct {
lendablePercent *int32
borrowingLimitPercent *int32
errExpected field.ErrorList
}{
{
lendablePercent: nil,
errExpected: nil,
},
{
lendablePercent: pointer.Int32(0),
errExpected: nil,
},
{
lendablePercent: pointer.Int32(100),
errExpected: nil,
},
{
lendablePercent: pointer.Int32(101),
errExpected: errLendablePercentFn(101),
},
{
lendablePercent: pointer.Int32(-1),
errExpected: errLendablePercentFn(-1),
},
{
borrowingLimitPercent: nil,
errExpected: nil,
},
{
borrowingLimitPercent: pointer.Int32(1),
errExpected: nil,
},
{
borrowingLimitPercent: pointer.Int32(100),
errExpected: nil,
},
{
borrowingLimitPercent: pointer.Int32(0),
errExpected: nil,
},
{
borrowingLimitPercent: pointer.Int32(-1),
errExpected: errBorrowingLimitPercentFn(-1),
},
}
for _, test := range tests {
t.Run(makeTestNameFn(test.lendablePercent, test.borrowingLimitPercent), func(t *testing.T) {
configuration := &flowcontrol.LimitedPriorityLevelConfiguration{
NominalConcurrencyShares: 1,
LimitResponse: flowcontrol.LimitResponse{
Type: flowcontrol.LimitResponseTypeReject,
},
LendablePercent: test.lendablePercent,
BorrowingLimitPercent: test.borrowingLimitPercent,
}
specPath := field.NewPath("spec").Child("limited")
errGot := ValidateLimitedPriorityLevelConfiguration(configuration, flowcontrolv1beta3.SchemeGroupVersion, specPath)
if !cmp.Equal(test.errExpected, errGot) {
t.Errorf("Expected error: %v, diff: %s", test.errExpected, cmp.Diff(test.errExpected, errGot))
}
})
}
}

View File

@@ -212,6 +212,16 @@ func (in *LimitResponse) DeepCopy() *LimitResponse {
func (in *LimitedPriorityLevelConfiguration) DeepCopyInto(out *LimitedPriorityLevelConfiguration) {
*out = *in
in.LimitResponse.DeepCopyInto(&out.LimitResponse)
if in.LendablePercent != nil {
in, out := &in.LendablePercent, &out.LendablePercent
*out = new(int32)
**out = **in
}
if in.BorrowingLimitPercent != nil {
in, out := &in.BorrowingLimitPercent, &out.BorrowingLimitPercent
*out = new(int32)
**out = **in
}
return
}

View File

@@ -31674,6 +31674,20 @@ func schema_k8sio_api_flowcontrol_v1alpha1_LimitedPriorityLevelConfiguration(ref
Ref: ref("k8s.io/api/flowcontrol/v1alpha1.LimitResponse"),
},
},
"lendablePercent": {
SchemaProps: spec.SchemaProps{
Description: "`lendablePercent` prescribes the fraction of the level's NominalCL that can be borrowed by other priority levels. The value of this field must be between 0 and 100, inclusive, and it defaults to 0. The number of seats that other levels can borrow from this level, known as this level's LendableConcurrencyLimit (LendableCL), is defined as follows.\n\nLendableCL(i) = round( NominalCL(i) * lendablePercent(i)/100.0 )",
Type: []string{"integer"},
Format: "int32",
},
},
"borrowingLimitPercent": {
SchemaProps: spec.SchemaProps{
Description: "`borrowingLimitPercent`, if present, configures a limit on how many seats this priority level can borrow from other priority levels. The limit is known as this level's BorrowingConcurrencyLimit (BorrowingCL) and is a limit on the total number of seats that this level may borrow at any one time. This field holds the ratio of that limit to the level's nominal concurrency limit. When this field is non-nil, it must hold a non-negative integer and the limit is calculated as follows.\n\nBorrowingCL(i) = round( NominalCL(i) * borrowingLimitPercent(i)/100.0 )\n\nThe value of this field can be more than 100, implying that this priority level can borrow a number of seats that is greater than its own nominal concurrency limit (NominalCL). When this field is left `nil`, the limit is effectively infinite.",
Type: []string{"integer"},
Format: "int32",
},
},
},
},
},
@@ -32659,6 +32673,20 @@ func schema_k8sio_api_flowcontrol_v1beta1_LimitedPriorityLevelConfiguration(ref
Ref: ref("k8s.io/api/flowcontrol/v1beta1.LimitResponse"),
},
},
"lendablePercent": {
SchemaProps: spec.SchemaProps{
Description: "`lendablePercent` prescribes the fraction of the level's NominalCL that can be borrowed by other priority levels. The value of this field must be between 0 and 100, inclusive, and it defaults to 0. The number of seats that other levels can borrow from this level, known as this level's LendableConcurrencyLimit (LendableCL), is defined as follows.\n\nLendableCL(i) = round( NominalCL(i) * lendablePercent(i)/100.0 )",
Type: []string{"integer"},
Format: "int32",
},
},
"borrowingLimitPercent": {
SchemaProps: spec.SchemaProps{
Description: "`borrowingLimitPercent`, if present, configures a limit on how many seats this priority level can borrow from other priority levels. The limit is known as this level's BorrowingConcurrencyLimit (BorrowingCL) and is a limit on the total number of seats that this level may borrow at any one time. This field holds the ratio of that limit to the level's nominal concurrency limit. When this field is non-nil, it must hold a non-negative integer and the limit is calculated as follows.\n\nBorrowingCL(i) = round( NominalCL(i) * borrowingLimitPercent(i)/100.0 )\n\nThe value of this field can be more than 100, implying that this priority level can borrow a number of seats that is greater than its own nominal concurrency limit (NominalCL). When this field is left `nil`, the limit is effectively infinite.",
Type: []string{"integer"},
Format: "int32",
},
},
},
},
},
@@ -33644,6 +33672,20 @@ func schema_k8sio_api_flowcontrol_v1beta2_LimitedPriorityLevelConfiguration(ref
Ref: ref("k8s.io/api/flowcontrol/v1beta2.LimitResponse"),
},
},
"lendablePercent": {
SchemaProps: spec.SchemaProps{
Description: "`lendablePercent` prescribes the fraction of the level's NominalCL that can be borrowed by other priority levels. The value of this field must be between 0 and 100, inclusive, and it defaults to 0. The number of seats that other levels can borrow from this level, known as this level's LendableConcurrencyLimit (LendableCL), is defined as follows.\n\nLendableCL(i) = round( NominalCL(i) * lendablePercent(i)/100.0 )",
Type: []string{"integer"},
Format: "int32",
},
},
"borrowingLimitPercent": {
SchemaProps: spec.SchemaProps{
Description: "`borrowingLimitPercent`, if present, configures a limit on how many seats this priority level can borrow from other priority levels. The limit is known as this level's BorrowingConcurrencyLimit (BorrowingCL) and is a limit on the total number of seats that this level may borrow at any one time. This field holds the ratio of that limit to the level's nominal concurrency limit. When this field is non-nil, it must hold a non-negative integer and the limit is calculated as follows.\n\nBorrowingCL(i) = round( NominalCL(i) * borrowingLimitPercent(i)/100.0 )\n\nThe value of this field can be more than 100, implying that this priority level can borrow a number of seats that is greater than its own nominal concurrency limit (NominalCL). When this field is left `nil`, the limit is effectively infinite.",
Type: []string{"integer"},
Format: "int32",
},
},
},
},
},
@@ -34631,6 +34673,20 @@ func schema_k8sio_api_flowcontrol_v1beta3_LimitedPriorityLevelConfiguration(ref
Ref: ref("k8s.io/api/flowcontrol/v1beta3.LimitResponse"),
},
},
"lendablePercent": {
SchemaProps: spec.SchemaProps{
Description: "`lendablePercent` prescribes the fraction of the level's NominalCL that can be borrowed by other priority levels. The value of this field must be between 0 and 100, inclusive, and it defaults to 0. The number of seats that other levels can borrow from this level, known as this level's LendableConcurrencyLimit (LendableCL), is defined as follows.\n\nLendableCL(i) = round( NominalCL(i) * lendablePercent(i)/100.0 )",
Type: []string{"integer"},
Format: "int32",
},
},
"borrowingLimitPercent": {
SchemaProps: spec.SchemaProps{
Description: "`borrowingLimitPercent`, if present, configures a limit on how many seats this priority level can borrow from other priority levels. The limit is known as this level's BorrowingConcurrencyLimit (BorrowingCL) and is a limit on the total number of seats that this level may borrow at any one time. This field holds the ratio of that limit to the level's nominal concurrency limit. When this field is non-nil, it must hold a non-negative integer and the limit is calculated as follows.\n\nBorrowingCL(i) = round( NominalCL(i) * borrowingLimitPercent(i)/100.0 )\n\nThe value of this field can be more than 100, implying that this priority level can borrow a number of seats that is greater than its own nominal concurrency limit (NominalCL). When this field is left `nil`, the limit is effectively infinite.",
Type: []string{"integer"},
Format: "int32",
},
},
},
},
},

View File

@@ -30,10 +30,10 @@ import (
flowcontrollisters "k8s.io/client-go/listers/flowcontrol/v1beta3"
"k8s.io/client-go/tools/cache"
flowcontrolapisv1beta3 "k8s.io/kubernetes/pkg/apis/flowcontrol/v1beta3"
"k8s.io/utils/pointer"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
)
func TestEnsurePriorityLevel(t *testing.T) {
@@ -257,6 +257,7 @@ func TestPriorityLevelSpecChanged(t *testing.T) {
Type: flowcontrolv1beta3.PriorityLevelEnablementLimited,
Limited: &flowcontrolv1beta3.LimitedPriorityLevelConfiguration{
NominalConcurrencyShares: flowcontrolapisv1beta3.PriorityLevelConfigurationDefaultNominalConcurrencyShares,
LendablePercent: pointer.Int32(0),
LimitResponse: flowcontrolv1beta3.LimitResponse{
Type: flowcontrolv1beta3.LimitResponseTypeReject,
},
@@ -291,7 +292,10 @@ func TestPriorityLevelSpecChanged(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
w := priorityLevelSpecChanged(testCase.expected, testCase.actual)
assert.Equal(t, testCase.specChanged, w)
if testCase.specChanged != w {
t.Errorf("Expected priorityLevelSpecChanged to return %t, but got: %t - diff: %s", testCase.specChanged, w,
cmp.Diff(testCase.expected, testCase.actual))
}
})
}
}
@@ -472,6 +476,7 @@ func (b *plBuilder) WithLimited(nominalConcurrencyShares int32) *plBuilder {
b.object.Spec.Type = flowcontrolv1beta3.PriorityLevelEnablementLimited
b.object.Spec.Limited = &flowcontrolv1beta3.LimitedPriorityLevelConfiguration{
NominalConcurrencyShares: nominalConcurrencyShares,
LendablePercent: pointer.Int32(0),
LimitResponse: flowcontrolv1beta3.LimitResponse{
Type: flowcontrolv1beta3.LimitResponseTypeReject,
},