rename the roundtrip annotation, forbid it in v1

This commit is contained in:
Abu Kashem
2023-10-30 18:22:51 -04:00
parent 233bc2449d
commit b8cd792b36
15 changed files with 47 additions and 36 deletions

View File

@@ -420,7 +420,7 @@ type LimitedPriorityLevelConfiguration struct {
// at the expense of every other priority level.
// This field has a default value of 30.
//
// Setting this field to zero allows for the construction of a
// Setting this field to zero supports the construction of a
// "jail" for this priority level that is used to hold some request(s)
//
// +optional

View File

@@ -57,22 +57,22 @@ func Convert_flowcontrol_PriorityLevelConfiguration_To_v1beta3_PriorityLevelConf
}
func dropPriorityLevelConcurrencyShareDefaultAnnotation(in map[string]string) (map[string]string, bool) {
if _, ok := in[v1beta3.PriorityLevelConcurrencyShareDefaultKey]; !ok {
if _, ok := in[v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey]; !ok {
return in, false
}
out := copyStringMap(in)
delete(out, v1beta3.PriorityLevelConcurrencyShareDefaultKey)
delete(out, v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey)
return out, true
}
func addPriorityLevelConcurrencyShareDefaultAnnotation(in map[string]string) (map[string]string, bool) {
if _, ok := in[v1beta3.PriorityLevelConcurrencyShareDefaultKey]; ok {
if _, ok := in[v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey]; ok {
return in, false
}
out := copyStringMap(in)
out[v1beta3.PriorityLevelConcurrencyShareDefaultKey] = ""
out[v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey] = ""
return out, true
}

View File

@@ -69,7 +69,7 @@ func TestConvert_v1beta3_PriorityLevelConfiguration_To_flowcontrol_PriorityLevel
name: "v1beta3 object, the roundtrip annotation is set, NominalConcurrencyShares is zero; the internal object should not have the roundtrip annotation set",
in: inObjFn(0, map[string]string{
"foo": "bar",
v1beta3.PriorityLevelConcurrencyShareDefaultKey: "",
v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "",
}),
expected: outObjFn(0, map[string]string{
"foo": "bar",
@@ -81,7 +81,7 @@ func TestConvert_v1beta3_PriorityLevelConfiguration_To_flowcontrol_PriorityLevel
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"foo": "bar",
v1beta3.PriorityLevelConcurrencyShareDefaultKey: "",
v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "",
},
},
},
@@ -169,7 +169,7 @@ func TestConvert_flowcontrol_PriorityLevelConfiguration_To_v1beta3_PriorityLevel
}),
expected: outObjFn(0, map[string]string{
"foo": "bar",
v1beta3.PriorityLevelConcurrencyShareDefaultKey: "",
v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "",
}),
},
{
@@ -185,33 +185,33 @@ func TestConvert_flowcontrol_PriorityLevelConfiguration_To_v1beta3_PriorityLevel
name: "internal object, the roundtrip annotation is set, NominalConcurrencyShares is 0, no error expected",
in: inObjFn(0, map[string]string{
"foo": "bar",
v1beta3.PriorityLevelConcurrencyShareDefaultKey: "",
v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "",
}),
expected: outObjFn(0, map[string]string{
"foo": "bar",
v1beta3.PriorityLevelConcurrencyShareDefaultKey: "",
v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "",
}),
},
{
name: "internal object, the roundtrip annotation is set with a non-empty value, NominalConcurrencyShares is 0, the annotation value should be preserved",
in: inObjFn(0, map[string]string{
"foo": "bar",
v1beta3.PriorityLevelConcurrencyShareDefaultKey: "non-empty",
v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "non-empty",
}),
expected: outObjFn(0, map[string]string{
"foo": "bar",
v1beta3.PriorityLevelConcurrencyShareDefaultKey: "non-empty",
v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "non-empty",
}),
},
{
name: "internal object, the roundtrip annotation is set with a non-empty value, NominalConcurrencyShares is not 0, the annotation value should be preserved",
in: inObjFn(1, map[string]string{
"foo": "bar",
v1beta3.PriorityLevelConcurrencyShareDefaultKey: "non-empty",
v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "non-empty",
}),
expected: outObjFn(1, map[string]string{
"foo": "bar",
v1beta3.PriorityLevelConcurrencyShareDefaultKey: "non-empty",
v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "non-empty",
}),
},
}

View File

@@ -52,7 +52,7 @@ func SetDefaults_PriorityLevelConfiguration(in *v1beta3.PriorityLevelConfigurati
// field only when:
// a) NominalConcurrencyShares == 0, and
// b) the roundtrip annotation is not set
if _, ok := in.Annotations[v1beta3.PriorityLevelConcurrencyShareDefaultKey]; !ok && limited.NominalConcurrencyShares == 0 {
if _, ok := in.Annotations[v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey]; !ok && limited.NominalConcurrencyShares == 0 {
limited.NominalConcurrencyShares = PriorityLevelConfigurationDefaultNominalConcurrencyShares
}
}

View File

@@ -141,7 +141,7 @@ func TestDefaultWithPriorityLevelConfiguration(t *testing.T) {
original: &flowcontrolv1beta3.PriorityLevelConfiguration{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
flowcontrolv1beta3.PriorityLevelConcurrencyShareDefaultKey: "",
flowcontrolv1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "",
},
},
Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{
@@ -157,7 +157,7 @@ func TestDefaultWithPriorityLevelConfiguration(t *testing.T) {
expected: &flowcontrolv1beta3.PriorityLevelConfiguration{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
flowcontrolv1beta3.PriorityLevelConcurrencyShareDefaultKey: "",
flowcontrolv1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "",
},
},
Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{
@@ -177,7 +177,7 @@ func TestDefaultWithPriorityLevelConfiguration(t *testing.T) {
original: &flowcontrolv1beta3.PriorityLevelConfiguration{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
flowcontrolv1beta3.PriorityLevelConcurrencyShareDefaultKey: "",
flowcontrolv1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "",
},
},
Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{
@@ -193,7 +193,7 @@ func TestDefaultWithPriorityLevelConfiguration(t *testing.T) {
expected: &flowcontrolv1beta3.PriorityLevelConfiguration{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
flowcontrolv1beta3.PriorityLevelConcurrencyShareDefaultKey: "",
flowcontrolv1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "",
},
},
Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{
@@ -213,7 +213,7 @@ func TestDefaultWithPriorityLevelConfiguration(t *testing.T) {
original: &flowcontrolv1beta3.PriorityLevelConfiguration{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
flowcontrolv1beta3.PriorityLevelConcurrencyShareDefaultKey: "",
flowcontrolv1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "",
},
},
Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{
@@ -229,7 +229,7 @@ func TestDefaultWithPriorityLevelConfiguration(t *testing.T) {
expected: &flowcontrolv1beta3.PriorityLevelConfiguration{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
flowcontrolv1beta3.PriorityLevelConcurrencyShareDefaultKey: "",
flowcontrolv1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "",
},
},
Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{

View File

@@ -22,6 +22,7 @@ import (
flowcontrolv1beta1 "k8s.io/api/flowcontrol/v1beta1"
flowcontrolv1beta2 "k8s.io/api/flowcontrol/v1beta2"
flowcontrolv1beta3 "k8s.io/api/flowcontrol/v1beta3"
apiequality "k8s.io/apimachinery/pkg/api/equality"
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -351,6 +352,13 @@ func ValidateFlowSchemaCondition(condition *flowcontrol.FlowSchemaCondition, fld
func ValidatePriorityLevelConfiguration(pl *flowcontrol.PriorityLevelConfiguration, requestGV schema.GroupVersion, opts PriorityLevelValidationOptions) field.ErrorList {
allErrs := apivalidation.ValidateObjectMeta(&pl.ObjectMeta, false, ValidatePriorityLevelConfigurationName, field.NewPath("metadata"))
// the roundtrip annotation is only for use in v1beta3, and after
// conversion, the internal object should not have the roundtrip
// annotation, so we should forbid it, if it's set.
if _, ok := pl.ObjectMeta.Annotations[flowcontrolv1beta3.PriorityLevelPreserveZeroConcurrencySharesKey]; ok {
allErrs = append(allErrs, field.Forbidden(field.NewPath("metadata").Child("annotations"), fmt.Sprintf("annotation '%s' is forbidden", flowcontrolv1beta3.PriorityLevelPreserveZeroConcurrencySharesKey)))
}
specPath := field.NewPath("spec")
allErrs = append(allErrs, ValidatePriorityLevelConfigurationSpec(&pl.Spec, requestGV, pl.Name, specPath, opts)...)
allErrs = append(allErrs, ValidateIfMandatoryPriorityLevelConfigurationObject(pl, specPath)...)

View File

@@ -1107,12 +1107,12 @@ func TestPriorityLevelConfigurationValidation(t *testing.T) {
field.Invalid(field.NewPath("spec").Child("limited").Child("limitResponse").Child("queuing").Child("handSize"), int32(8), "should not be greater than queues (7)"),
},
}, {
name: "the roundtrip annotation is not forbidden in v1beta3",
name: "the roundtrip annotation is forbidden",
priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: "with-forbidden-annotation",
Annotations: map[string]string{
flowcontrolv1beta3.PriorityLevelConcurrencyShareDefaultKey: "",
flowcontrolv1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "",
},
},
Spec: flowcontrol.PriorityLevelConfigurationSpec{
@@ -1124,8 +1124,11 @@ func TestPriorityLevelConfigurationValidation(t *testing.T) {
},
},
},
requestGV: &flowcontrolv1beta3.SchemeGroupVersion,
expectedErrors: field.ErrorList{},
// the internal object should never have the round trip annotation
requestGV: &schema.GroupVersion{},
expectedErrors: field.ErrorList{
field.Forbidden(field.NewPath("metadata").Child("annotations"), fmt.Sprintf("annotation '%s' is forbidden", flowcontrolv1beta3.PriorityLevelPreserveZeroConcurrencySharesKey)),
},
}}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {