Show error in status if preserve unknown fields is true for nonstructural schemas

This commit is contained in:
Siva 2020-07-14 16:16:45 -05:00
parent d159ae3545
commit e643286c06
2 changed files with 72 additions and 0 deletions

View File

@ -90,6 +90,12 @@ func calculateCondition(in *apiextensionsv1.CustomResourceDefinition) *apiextens
allErrs := field.ErrorList{}
if in.Spec.PreserveUnknownFields {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "preserveUnknownFields"),
in.Spec.PreserveUnknownFields,
fmt.Sprint("must be false")))
}
for i, v := range in.Spec.Versions {
if v.Schema == nil || v.Schema.OpenAPIV3Schema == nil {
continue

View File

@ -0,0 +1,66 @@
/*
Copyright 2020 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 nonstructuralschema
import (
"fmt"
"reflect"
"testing"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
)
func Test_calculateCondition(t *testing.T) {
tests := []struct {
name string
args *apiextensionsv1.CustomResourceDefinition
want *apiextensionsv1.CustomResourceDefinitionCondition
}{
{
name: "preserve unknown fields is false",
args: &apiextensionsv1.CustomResourceDefinition{
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
PreserveUnknownFields: false,
},
},
},
{
name: "preserve unknown fields is true",
args: &apiextensionsv1.CustomResourceDefinition{
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
PreserveUnknownFields: true,
},
},
want: &apiextensionsv1.CustomResourceDefinitionCondition{
Type: apiextensionsv1.NonStructuralSchema,
Status: apiextensionsv1.ConditionTrue,
Reason: "Violations",
Message: field.Invalid(field.NewPath("spec", "preserveUnknownFields"),
true,
fmt.Sprint("must be false")).Error(),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := calculateCondition(tt.args); !reflect.DeepEqual(got, tt.want) {
t.Errorf("calculateCondition() = %v, want %v", got, tt.want)
}
})
}
}