-
Notifications
You must be signed in to change notification settings - Fork 38.6k
/
crd_validation_rules.go
338 lines (318 loc) · 14.2 KB
/
crd_validation_rules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
Copyright 2021 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 apimachinery
import (
"context"
"strings"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/runtime/schema"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apiextensions-apiserver/test/integration/fixtures"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/apiserver/pkg/storage/names"
"k8s.io/client-go/dynamic"
"k8s.io/kubernetes/test/e2e/framework"
admissionapi "k8s.io/pod-security-admission/api"
)
var _ = SIGDescribe("CustomResourceValidationRules [Privileged:ClusterAdmin]", func() {
f := framework.NewDefaultFramework("crd-validation-expressions")
f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged
var apiExtensionClient *clientset.Clientset
ginkgo.BeforeEach(func() {
var err error
apiExtensionClient, err = clientset.NewForConfig(f.ClientConfig())
framework.ExpectNoError(err, "initializing apiExtensionClient")
})
customResourceClient := func(crd *v1.CustomResourceDefinition) (dynamic.NamespaceableResourceInterface, schema.GroupVersionResource) {
gvrs := fixtures.GetGroupVersionResourcesOfCustomResource(crd)
if len(gvrs) != 1 {
ginkgo.Fail("Expected one version in custom resource definition")
}
gvr := gvrs[0]
return f.DynamicClient.Resource(gvr), gvr
}
unmarshallSchema := func(schemaJson []byte) *v1.JSONSchemaProps {
var c v1.JSONSchemaProps
err := json.Unmarshal(schemaJson, &c)
framework.ExpectNoError(err, "unmarshalling OpenAPIv3 schema")
return &c
}
// for all new CRD validation features that should be E2E-tested, add them
// into this schema and then add CR requests to the end of the test
// below ("MUST NOT fail validation...") instead of writing a new and
// separate test
var schemaWithValidationExpression = unmarshallSchema([]byte(`{
"type":"object",
"properties":{
"spec":{
"type":"object",
"x-kubernetes-validations":[
{ "rule":"self.x + self.y > 0" },
{ "rule":"self.firstArray.isSorted() && self.secondArray.isSorted() && ((self.firstArray.sum() + self.secondArray.sum()) % 2 == 0)" },
{ "rule":"self.largeArray.all(x, self.largeArray.all(y, y == x))" }
],
"properties":{
"x":{ "type":"integer" },
"y":{ "type":"integer" },
"firstArray":{ "type":"array", "maxItems": 1000, "items":{ "type": "integer"} },
"secondArray":{ "type":"array", "maxItems": 1000, "items":{ "type": "integer"} },
"largeArray":{ "type":"array", "maxItems": 725, "items":{ "type": "integer"} }
}
},
"status":{
"type":"object",
"x-kubernetes-validations":[
{ "rule":"self.health == 'ok' || self.health == 'unhealthy'" }
],
"properties":{
"health":{ "type":"string" }
}
}
}
}`))
ginkgo.It("MUST NOT fail validation for create of a custom resource that satisfies the x-kubernetes-validations rules", func(ctx context.Context) {
ginkgo.By("Creating a custom resource definition with validation rules")
crd := fixtures.NewRandomNameV1CustomResourceDefinitionWithSchema(v1.NamespaceScoped, schemaWithValidationExpression, false)
crd, err := fixtures.CreateNewV1CustomResourceDefinitionWatchUnsafe(crd, apiExtensionClient)
framework.ExpectNoError(err, "creating CustomResourceDefinition")
defer func() {
err = fixtures.DeleteV1CustomResourceDefinition(crd, apiExtensionClient)
framework.ExpectNoError(err, "deleting CustomResourceDefinition")
}()
ginkgo.By("Creating a custom resource with values that are allowed by the validation rules set on the custom resource definition")
crClient, gvr := customResourceClient(crd)
name1 := names.SimpleNameGenerator.GenerateName("cr-1")
_, err = crClient.Namespace(f.Namespace.Name).Create(ctx, &unstructured.Unstructured{Object: map[string]interface{}{
"apiVersion": gvr.Group + "/" + gvr.Version,
"kind": crd.Spec.Names.Kind,
"metadata": map[string]interface{}{
"name": name1,
"namespace": f.Namespace.Name,
},
"spec": map[string]interface{}{
"x": int64(1),
"y": int64(0),
"firstArray": []int64{3, 4},
"secondArray": []int64{5, 10},
"largeArray": []int64{2, 2},
},
}}, metav1.CreateOptions{})
framework.ExpectNoError(err, "validation rules satisfied")
})
ginkgo.It("MUST fail validation for create of a custom resource that does not satisfy the x-kubernetes-validations rules", func(ctx context.Context) {
ginkgo.By("Creating a custom resource definition with validation rules")
crd := fixtures.NewRandomNameV1CustomResourceDefinitionWithSchema(v1.NamespaceScoped, schemaWithValidationExpression, false)
crd, err := fixtures.CreateNewV1CustomResourceDefinitionWatchUnsafe(crd, apiExtensionClient)
framework.ExpectNoError(err, "creating CustomResourceDefinition")
defer func() {
err = fixtures.DeleteV1CustomResourceDefinition(crd, apiExtensionClient)
framework.ExpectNoError(err, "deleting CustomResourceDefinition")
}()
ginkgo.By("Creating a custom resource with values that fail the validation rules set on the custom resource definition")
crClient, gvr := customResourceClient(crd)
name1 := names.SimpleNameGenerator.GenerateName("cr-1")
_, err = crClient.Namespace(f.Namespace.Name).Create(ctx, &unstructured.Unstructured{Object: map[string]interface{}{
"apiVersion": gvr.Group + "/" + gvr.Version,
"kind": crd.Spec.Names.Kind,
"metadata": map[string]interface{}{
"name": name1,
"namespace": f.Namespace.Name,
},
"spec": map[string]interface{}{
"x": int64(0),
"y": int64(0),
},
}}, metav1.CreateOptions{})
gomega.Expect(err).To(gomega.HaveOccurred(), "validation rules not satisfied")
expectedErrMsg := "failed rule"
if !strings.Contains(err.Error(), expectedErrMsg) {
framework.Failf("expect error contains %q, got %q", expectedErrMsg, err.Error())
}
})
ginkgo.It("MUST fail create of a custom resource definition that contains a x-kubernetes-validations rule that refers to a property that do not exist", func(ctx context.Context) {
ginkgo.By("Defining a custom resource definition with a validation rule that refers to a property that do not exist")
var schemaWithInvalidValidationRule = unmarshallSchema([]byte(`{
"type":"object",
"properties":{
"spec":{
"type":"object",
"x-kubernetes-validations":[
{ "rule":"self.z == 100" }
],
"properties":{
"x":{ "type":"integer" }
}
}
}
}`))
crd := fixtures.NewRandomNameV1CustomResourceDefinitionWithSchema(v1.NamespaceScoped, schemaWithInvalidValidationRule, false)
_, err := fixtures.CreateNewV1CustomResourceDefinitionWatchUnsafe(crd, apiExtensionClient)
gomega.Expect(err).To(gomega.HaveOccurred(), "creating CustomResourceDefinition with a validation rule that refers to a property that do not exist")
expectedErrMsg := "undefined field 'z'"
if !strings.Contains(err.Error(), expectedErrMsg) {
framework.Failf("expect error contains %q, got %q", expectedErrMsg, err.Error())
}
})
ginkgo.It("MUST fail create of a custom resource definition that contains an x-kubernetes-validations rule that contains a syntax error", func(ctx context.Context) {
ginkgo.By("Defining a custom resource definition that contains a validation rule with a syntax error")
var schemaWithSyntaxErrorRule = unmarshallSchema([]byte(`{
"type":"object",
"properties":{
"spec":{
"type":"object",
"x-kubernetes-validations":[
{ "rule":"self = 42" }
]
}
}
}`))
crd := fixtures.NewRandomNameV1CustomResourceDefinitionWithSchema(v1.NamespaceScoped, schemaWithSyntaxErrorRule, false)
_, err := fixtures.CreateNewV1CustomResourceDefinitionWatchUnsafe(crd, apiExtensionClient)
gomega.Expect(err).To(gomega.HaveOccurred(), "creating a CustomResourceDefinition with a validation rule that contains a syntax error")
expectedErrMsg := "Syntax error"
if !strings.Contains(err.Error(), expectedErrMsg) {
framework.Failf("expected error message to contain %q, got %q", expectedErrMsg, err.Error())
}
})
ginkgo.It("MUST fail create of a custom resource definition that contains an x-kubernetes-validations rule that exceeds the estimated cost limit", func(ctx context.Context) {
ginkgo.By("Defining a custom resource definition that contains a validation rule that exceeds the cost limit")
var schemaWithExpensiveRule = unmarshallSchema([]byte(`{
"type":"object",
"properties":{
"spec":{
"type":"object",
"properties":{
"x":{
"type":"array",
"items":{
"type":"array",
"items":{
"type":"string"
},
"x-kubernetes-validations":[
{ "rule":"self.all(s, s == 'string constant')" }
]
}
}
}
}
}
}`))
crd := fixtures.NewRandomNameV1CustomResourceDefinitionWithSchema(v1.NamespaceScoped, schemaWithExpensiveRule, false)
_, err := fixtures.CreateNewV1CustomResourceDefinitionWatchUnsafe(crd, apiExtensionClient)
gomega.Expect(err).To(gomega.HaveOccurred(), "creating a CustomResourceDefinition with a validation rule that exceeds the cost limit")
expectedErrMsg := "exceeds budget"
if !strings.Contains(err.Error(), expectedErrMsg) {
framework.Failf("expected error message to contain %q, got %q", expectedErrMsg, err.Error())
}
})
ginkgo.It("MUST fail create of a custom resource that exceeds the runtime cost limit for x-kubernetes-validations rule execution", func(ctx context.Context) {
ginkgo.By("Defining a custom resource definition including an expensive rule on a large amount of data")
crd := fixtures.NewRandomNameV1CustomResourceDefinitionWithSchema(v1.NamespaceScoped, schemaWithValidationExpression, false)
_, err := fixtures.CreateNewV1CustomResourceDefinitionWatchUnsafe(crd, apiExtensionClient)
framework.ExpectNoError(err, "creating CustomResourceDefinition including an expensive rule on a large amount of data")
defer func() {
err = fixtures.DeleteV1CustomResourceDefinition(crd, apiExtensionClient)
framework.ExpectNoError(err, "deleting CustomResourceDefinition")
}()
ginkgo.By("Attempting to create a custom resource that will exceed the runtime cost limit")
crClient, gvr := customResourceClient(crd)
name1 := names.SimpleNameGenerator.GenerateName("cr-1")
_, err = crClient.Namespace(f.Namespace.Name).Create(ctx, &unstructured.Unstructured{Object: map[string]interface{}{
"apiVersion": gvr.Group + "/" + gvr.Version,
"kind": crd.Spec.Names.Kind,
"metadata": map[string]interface{}{
"name": name1,
"namespace": f.Namespace.Name,
},
"spec": map[string]interface{}{
"largeArray": genLargeArray(725, 20),
},
}}, metav1.CreateOptions{})
gomega.Expect(err).To(gomega.HaveOccurred(), "custom resource creation should be prohibited by runtime cost limit")
expectedErrMsg := "call cost exceeds limit"
if !strings.Contains(err.Error(), expectedErrMsg) {
framework.Failf("expect error contains %q, got %q", expectedErrMsg, err.Error())
}
})
ginkgo.It("MUST fail update of a custom resource that does not satisfy a x-kubernetes-validations transition rule", func(ctx context.Context) {
ginkgo.By("Defining a custom resource definition with a x-kubernetes-validations transition rule")
var schemaWithTransitionRule = unmarshallSchema([]byte(`{
"type":"object",
"properties":{
"spec":{
"type":"object",
"properties":{
"num":{
"type":"integer",
"x-kubernetes-validations":[
{ "rule":"self > oldSelf" }
]
}
}
}
}
}`))
crd := fixtures.NewRandomNameV1CustomResourceDefinitionWithSchema(v1.NamespaceScoped, schemaWithTransitionRule, false)
_, err := fixtures.CreateNewV1CustomResourceDefinitionWatchUnsafe(crd, apiExtensionClient)
framework.ExpectNoError(err, "creating CustomResourceDefinition including an x-kubernetes-validations transition rule")
defer func() {
err = fixtures.DeleteV1CustomResourceDefinition(crd, apiExtensionClient)
framework.ExpectNoError(err, "deleting CustomResourceDefinition")
}()
ginkgo.By("Attempting to create a custom resource")
crClient, gvr := customResourceClient(crd)
name1 := names.SimpleNameGenerator.GenerateName("cr-1")
unstruct, err := crClient.Namespace(f.Namespace.Name).Create(ctx, &unstructured.Unstructured{Object: map[string]interface{}{
"apiVersion": gvr.Group + "/" + gvr.Version,
"kind": crd.Spec.Names.Kind,
"metadata": map[string]interface{}{
"name": name1,
"namespace": f.Namespace.Name,
},
"spec": map[string]interface{}{
"num": int64(10),
},
}}, metav1.CreateOptions{})
framework.ExpectNoError(err, "transition rules do not apply to create operations")
ginkgo.By("Updating a custom resource with a value that does not satisfy an x-kubernetes-validations transition rule")
_, err = crClient.Namespace(f.Namespace.Name).Update(ctx, &unstructured.Unstructured{Object: map[string]interface{}{
"apiVersion": gvr.Group + "/" + gvr.Version,
"kind": crd.Spec.Names.Kind,
"metadata": map[string]interface{}{
"name": name1,
"namespace": f.Namespace.Name,
"resourceVersion": unstruct.GetResourceVersion(),
},
"spec": map[string]interface{}{
"num": int64(9),
},
}}, metav1.UpdateOptions{})
gomega.Expect(err).To(gomega.HaveOccurred(), "custom resource update should be prohibited by transition rule")
expectedErrMsg := "failed rule"
if !strings.Contains(err.Error(), expectedErrMsg) {
framework.Failf("expect error contains %q, got %q", expectedErrMsg, err.Error())
}
})
})
func genLargeArray(n, x int64) []int64 {
arr := make([]int64, n)
for i := int64(0); i < n; i++ {
arr[i] = x
}
return arr
}