This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 531
/
validation.go
287 lines (239 loc) · 9.65 KB
/
validation.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
/*
Copyright 2019 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 e2e
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apiserver/pkg/storage/names"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/kubefed/pkg/apis/core/v1beta1"
"sigs.k8s.io/kubefed/pkg/controller/util"
ftc "sigs.k8s.io/kubefed/pkg/controller/webhook/federatedtypeconfig"
kfcluster "sigs.k8s.io/kubefed/pkg/controller/webhook/kubefedcluster"
kfconfig "sigs.k8s.io/kubefed/pkg/controller/webhook/kubefedconfig"
"sigs.k8s.io/kubefed/pkg/kubefedctl/enable"
"sigs.k8s.io/kubefed/test/common"
"sigs.k8s.io/kubefed/test/e2e/framework"
. "github.com/onsi/ginkgo" //nolint:stylecheck
)
var _ = Describe("Core API Validation", func() {
testBaseName := "core-api-validation"
f := framework.NewKubeFedFramework(testBaseName)
BeforeEach(func() {
if framework.TestContext.InMemoryControllers {
framework.Skipf("Running validation admission webhook outside of cluster not supported")
}
})
resourcesToValidate := []string{ftc.ResourceName, kfcluster.ResourceName, kfconfig.ResourceName}
for i := range resourcesToValidate {
resourceName := resourcesToValidate[i]
vrt := newValidationResourceTest(resourceName)
vrt.initialize()
runValidationResourceTests(f, vrt, resourceName, testBaseName)
}
})
func runValidationResourceTests(f framework.KubeFedFramework, vrt validationResourceTest, resourceName, testBaseName string) {
It(fmt.Sprintf("for %s should fail when an invalid %s is created or updated", resourceName, resourceName), func() {
userAgent := fmt.Sprintf("test-%s-validation", resourceName)
client := f.Client(userAgent)
namespace := f.TestNamespaceName()
vrt.getObjectMeta().Namespace = namespace
By(fmt.Sprintf("Creating an invalid %s", resourceName))
invalidObj := vrt.invalidObject("")
err := client.Create(context.TODO(), invalidObj)
if err == nil {
f.Logger().Fatalf("Expected error creating invalid %s = %+v", resourceName, invalidObj)
}
By(fmt.Sprintf("Creating a valid %s", resourceName))
validObj := vrt.validObject()
err = client.Create(context.TODO(), validObj)
if err != nil {
f.Logger().Fatalf("Unexpected error creating valid %s = %+v, err: %v", resourceName, validObj, err)
}
By(fmt.Sprintf("Updating with an invalid %s", resourceName))
invalidObj = vrt.invalidObjectFromValid(validObj)
err = client.Update(context.TODO(), invalidObj)
if err == nil {
f.Logger().Fatalf("Expected error updating invalid %s = %+v", resourceName, vrt)
}
By(fmt.Sprintf("Patching with an invalid %s", resourceName))
patch := runtimeclient.MergeFrom(validObj)
err = client.Patch(context.TODO(), invalidObj, patch)
if err == nil {
f.Logger().Fatalf("Expected error patching invalid %s = %+v", resourceName, vrt)
}
// Immediately delete the created test resource to avoid errors in
// other e2e tests that rely on the original e2e testing setup. For
// example for KubeFedCluster, delete the test cluster we just
// created as it's not a properly joined member cluster that's part
// of the original e2e test setup.
validObjName := vrt.getObjectMeta().Name
err = client.Delete(context.TODO(), validObj, namespace, validObjName)
if err != nil && !apierrors.IsNotFound(err) {
f.Logger().Errorf("Error deleting %s %s: %v", resourceName, validObjName, err)
}
})
// TODO(font): Consider removing once webhook singleton is implemented.
When("running with namespace scoped deployment", func() {
It(fmt.Sprintf("for %s should succeed when an invalid %s is created outside the kubefed system namespace", resourceName, resourceName), func() {
if !framework.TestContext.LimitedScope {
framework.Skipf("Cannot run validation admission webhook namespaced test in a cluster scoped deployment")
}
userAgent := fmt.Sprintf("test-%s-validation", resourceName)
client := f.Client(userAgent)
kubeClient := f.KubeClient(fmt.Sprintf("%s-create-namespace", testBaseName))
namespace := framework.CreateTestNamespace(kubeClient, testBaseName)
framework.AddCleanupAction(func() {
framework.DeleteNamespace(kubeClient, namespace)
})
By(fmt.Sprintf("Creating an invalid %s in the separate test namespace %s", resourceName, namespace))
invalidObj := vrt.invalidObject(namespace)
err := client.Create(context.TODO(), invalidObj)
if err != nil {
f.Logger().Fatalf("Unexpected error creating invalid %s = %+v in another test namespace %s, err: %v", resourceName, invalidObj, namespace, err)
}
})
})
}
type validationResourceTest interface {
initialize() runtimeclient.Object
getObjectMeta() *metav1.ObjectMeta
invalidObject(namespace string) runtimeclient.Object
setInvalidField(obj runtimeclient.Object)
validObject() runtimeclient.Object
invalidObjectFromValid(obj runtimeclient.Object) runtimeclient.Object
}
type ftcValidationTest struct {
object runtimeclient.Object
}
type kfClusterValidationTest struct {
object runtimeclient.Object
}
type kfConfigValidationTest struct {
object runtimeclient.Object
}
func newValidationResourceTest(resourceName string) validationResourceTest {
var vrt validationResourceTest
switch resourceName {
case ftc.ResourceName:
vrt = &ftcValidationTest{}
case kfcluster.ResourceName:
vrt = &kfClusterValidationTest{}
case kfconfig.ResourceName:
vrt = &kfConfigValidationTest{}
}
return vrt
}
func (ftc *ftcValidationTest) initialize() runtimeclient.Object {
if ftc.object != nil {
return ftc.object
}
// For the target API type, use an existing K8s API resource that
// is not currently enabled by default. This simplifies logic and
// avoids having to create a CRD that prevents validation tests
// from running with LimitedScope.
apiResource := metav1.APIResource{
Group: "apps",
Version: "v1",
Kind: "DaemonSet",
Name: "daemonsets",
Namespaced: true,
}
enableTypeDirective := enable.NewEnableTypeDirective()
ftc.object = enable.GenerateTypeConfigForTarget(apiResource, enableTypeDirective).(*v1beta1.FederatedTypeConfig)
return ftc.object
}
func (ftc *ftcValidationTest) getObjectMeta() *metav1.ObjectMeta {
return &ftc.object.(*v1beta1.FederatedTypeConfig).ObjectMeta
}
func (ftc *ftcValidationTest) invalidObject(namespace string) runtimeclient.Object {
invalidFtc := ftc.object.DeepCopyObject().(*v1beta1.FederatedTypeConfig)
if namespace != "" {
invalidFtc.Namespace = namespace
}
ftc.setInvalidField(invalidFtc)
return invalidFtc
}
func (ftc *ftcValidationTest) setInvalidField(obj runtimeclient.Object) {
obj.(*v1beta1.FederatedTypeConfig).Spec.FederatedType.Group = ""
}
func (ftc *ftcValidationTest) validObject() runtimeclient.Object {
return ftc.object.DeepCopyObject().(runtimeclient.Object)
}
func (ftc *ftcValidationTest) invalidObjectFromValid(obj runtimeclient.Object) runtimeclient.Object {
invalidFtc := obj.DeepCopyObject().(*v1beta1.FederatedTypeConfig)
ftc.setInvalidField(invalidFtc)
return invalidFtc
}
func (kfc *kfClusterValidationTest) initialize() runtimeclient.Object {
if kfc.object != nil {
return kfc.object
}
kfc.object = common.ValidKubeFedCluster()
return kfc.object
}
func (kfc *kfClusterValidationTest) getObjectMeta() *metav1.ObjectMeta {
return &kfc.object.(*v1beta1.KubeFedCluster).ObjectMeta
}
func (kfc *kfClusterValidationTest) invalidObject(namespace string) runtimeclient.Object {
invalidKfc := kfc.object.DeepCopyObject().(*v1beta1.KubeFedCluster)
if namespace != "" {
invalidKfc.Namespace = namespace
}
kfc.setInvalidField(invalidKfc)
return invalidKfc
}
func (kfc *kfClusterValidationTest) setInvalidField(obj runtimeclient.Object) {
obj.(*v1beta1.KubeFedCluster).Spec.APIEndpoint = ""
}
func (kfc *kfClusterValidationTest) validObject() runtimeclient.Object {
return kfc.object.DeepCopyObject().(runtimeclient.Object)
}
func (kfc *kfClusterValidationTest) invalidObjectFromValid(obj runtimeclient.Object) runtimeclient.Object {
invalidKfc := obj.DeepCopyObject().(*v1beta1.KubeFedCluster)
kfc.setInvalidField(invalidKfc)
return invalidKfc
}
func (kfc *kfConfigValidationTest) initialize() runtimeclient.Object {
if kfc.object != nil {
return kfc.object
}
kfconfig := common.ValidKubeFedConfig()
kfconfig.Name = names.SimpleNameGenerator.GenerateName(util.KubeFedConfigName + "-")
kfc.object = kfconfig
return kfc.object
}
func (kfc *kfConfigValidationTest) getObjectMeta() *metav1.ObjectMeta {
return &kfc.object.(*v1beta1.KubeFedConfig).ObjectMeta
}
func (kfc *kfConfigValidationTest) invalidObject(namespace string) runtimeclient.Object {
invalidKfc := kfc.object.DeepCopyObject().(*v1beta1.KubeFedConfig)
if namespace != "" {
invalidKfc.Namespace = namespace
}
kfc.setInvalidField(invalidKfc)
return invalidKfc
}
func (kfc *kfConfigValidationTest) setInvalidField(obj runtimeclient.Object) {
*obj.(*v1beta1.KubeFedConfig).Spec.SyncController.AdoptResources = "Unknown"
}
func (kfc *kfConfigValidationTest) validObject() runtimeclient.Object {
return kfc.object.DeepCopyObject().(runtimeclient.Object)
}
func (kfc *kfConfigValidationTest) invalidObjectFromValid(obj runtimeclient.Object) runtimeclient.Object {
invalidKfc := obj.DeepCopyObject().(*v1beta1.KubeFedConfig)
kfc.setInvalidField(invalidKfc)
return invalidKfc
}