Skip to content

Commit e643286

Browse files
committed
Show error in status if preserve unknown fields is true for nonstructural schemas
1 parent d159ae3 commit e643286

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

staging/src/k8s.io/apiextensions-apiserver/pkg/controller/nonstructuralschema/nonstructuralschema_controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ func calculateCondition(in *apiextensionsv1.CustomResourceDefinition) *apiextens
9090

9191
allErrs := field.ErrorList{}
9292

93+
if in.Spec.PreserveUnknownFields {
94+
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "preserveUnknownFields"),
95+
in.Spec.PreserveUnknownFields,
96+
fmt.Sprint("must be false")))
97+
}
98+
9399
for i, v := range in.Spec.Versions {
94100
if v.Schema == nil || v.Schema.OpenAPIV3Schema == nil {
95101
continue
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package nonstructuralschema
18+
19+
import (
20+
"fmt"
21+
"reflect"
22+
"testing"
23+
24+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
25+
"k8s.io/apimachinery/pkg/util/validation/field"
26+
)
27+
28+
func Test_calculateCondition(t *testing.T) {
29+
tests := []struct {
30+
name string
31+
args *apiextensionsv1.CustomResourceDefinition
32+
want *apiextensionsv1.CustomResourceDefinitionCondition
33+
}{
34+
{
35+
name: "preserve unknown fields is false",
36+
args: &apiextensionsv1.CustomResourceDefinition{
37+
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
38+
PreserveUnknownFields: false,
39+
},
40+
},
41+
},
42+
{
43+
name: "preserve unknown fields is true",
44+
args: &apiextensionsv1.CustomResourceDefinition{
45+
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
46+
PreserveUnknownFields: true,
47+
},
48+
},
49+
want: &apiextensionsv1.CustomResourceDefinitionCondition{
50+
Type: apiextensionsv1.NonStructuralSchema,
51+
Status: apiextensionsv1.ConditionTrue,
52+
Reason: "Violations",
53+
Message: field.Invalid(field.NewPath("spec", "preserveUnknownFields"),
54+
true,
55+
fmt.Sprint("must be false")).Error(),
56+
},
57+
},
58+
}
59+
for _, tt := range tests {
60+
t.Run(tt.name, func(t *testing.T) {
61+
if got := calculateCondition(tt.args); !reflect.DeepEqual(got, tt.want) {
62+
t.Errorf("calculateCondition() = %v, want %v", got, tt.want)
63+
}
64+
})
65+
}
66+
}

0 commit comments

Comments
 (0)