@@ -25,6 +25,7 @@ import (
25
25
. "github.com/onsi/ginkgo"
26
26
. "github.com/onsi/gomega"
27
27
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
28
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
28
29
"k8s.io/apimachinery/pkg/runtime"
29
30
"k8s.io/apimachinery/pkg/types"
30
31
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -52,6 +53,18 @@ var _ = Describe("Test", func() {
52
53
// Cleanup CRDs
53
54
AfterEach (func (done Done ) {
54
55
for _ , crd := range crds {
56
+ // Delete only if CRD exists.
57
+ crdObjectKey := client.ObjectKey {
58
+ Namespace : crd .Namespace ,
59
+ Name : crd .Name ,
60
+ }
61
+ var placeholder v1beta1.CustomResourceDefinition
62
+ err := c .Get (context .TODO (), crdObjectKey , & placeholder )
63
+ if err != nil && apierrors .IsNotFound (err ) {
64
+ // CRD doesn't need to be deleted.
65
+ continue
66
+ }
67
+ Expect (err ).NotTo (HaveOccurred ())
55
68
Expect (c .Delete (context .TODO (), crd )).To (Succeed ())
56
69
}
57
70
close (done )
@@ -216,4 +229,105 @@ var _ = Describe("Test", func() {
216
229
close (done )
217
230
}, 5 )
218
231
})
232
+
233
+ Describe ("UninstallCRDs" , func () {
234
+ It ("should uninstall the CRDs from the cluster" , func (done Done ) {
235
+
236
+ crds , err = InstallCRDs (env .Config , CRDInstallOptions {
237
+ Paths : []string {filepath .Join ("." , "testdata" )},
238
+ })
239
+ Expect (err ).NotTo (HaveOccurred ())
240
+
241
+ // Expect to find the CRDs
242
+
243
+ crd := & v1beta1.CustomResourceDefinition {}
244
+ err = c .Get (context .TODO (), types.NamespacedName {Name : "foos.bar.example.com" }, crd )
245
+ Expect (err ).NotTo (HaveOccurred ())
246
+ Expect (crd .Spec .Names .Kind ).To (Equal ("Foo" ))
247
+
248
+ crd = & v1beta1.CustomResourceDefinition {}
249
+ err = c .Get (context .TODO (), types.NamespacedName {Name : "bazs.qux.example.com" }, crd )
250
+ Expect (err ).NotTo (HaveOccurred ())
251
+ Expect (crd .Spec .Names .Kind ).To (Equal ("Baz" ))
252
+
253
+ crd = & v1beta1.CustomResourceDefinition {}
254
+ err = c .Get (context .TODO (), types.NamespacedName {Name : "captains.crew.example.com" }, crd )
255
+ Expect (err ).NotTo (HaveOccurred ())
256
+ Expect (crd .Spec .Names .Kind ).To (Equal ("Captain" ))
257
+
258
+ crd = & v1beta1.CustomResourceDefinition {}
259
+ err = c .Get (context .TODO (), types.NamespacedName {Name : "firstmates.crew.example.com" }, crd )
260
+ Expect (err ).NotTo (HaveOccurred ())
261
+ Expect (crd .Spec .Names .Kind ).To (Equal ("FirstMate" ))
262
+
263
+ crd = & v1beta1.CustomResourceDefinition {}
264
+ err = c .Get (context .TODO (), types.NamespacedName {Name : "drivers.crew.example.com" }, crd )
265
+ Expect (err ).NotTo (HaveOccurred ())
266
+ Expect (crd .Spec .Names .Kind ).To (Equal ("Driver" ))
267
+
268
+ err = WaitForCRDs (env .Config , []* v1beta1.CustomResourceDefinition {
269
+ {
270
+ Spec : v1beta1.CustomResourceDefinitionSpec {
271
+ Group : "qux.example.com" ,
272
+ Version : "v1beta1" ,
273
+ Names : v1beta1.CustomResourceDefinitionNames {
274
+ Plural : "bazs" ,
275
+ }},
276
+ },
277
+ {
278
+ Spec : v1beta1.CustomResourceDefinitionSpec {
279
+ Group : "bar.example.com" ,
280
+ Version : "v1beta1" ,
281
+ Names : v1beta1.CustomResourceDefinitionNames {
282
+ Plural : "foos" ,
283
+ }},
284
+ },
285
+ {
286
+ Spec : v1beta1.CustomResourceDefinitionSpec {
287
+ Group : "crew.example.com" ,
288
+ Version : "v1beta1" ,
289
+ Names : v1beta1.CustomResourceDefinitionNames {
290
+ Plural : "captains" ,
291
+ }},
292
+ },
293
+ {
294
+ Spec : v1beta1.CustomResourceDefinitionSpec {
295
+ Group : "crew.example.com" ,
296
+ Version : "v1beta1" ,
297
+ Names : v1beta1.CustomResourceDefinitionNames {
298
+ Plural : "firstmates" ,
299
+ }},
300
+ },
301
+ {
302
+ Spec : v1beta1.CustomResourceDefinitionSpec {
303
+ Group : "crew.example.com" ,
304
+ Names : v1beta1.CustomResourceDefinitionNames {
305
+ Plural : "drivers" ,
306
+ },
307
+ Versions : []v1beta1.CustomResourceDefinitionVersion {
308
+ {
309
+ Name : "v1" ,
310
+ Storage : true ,
311
+ Served : true ,
312
+ },
313
+ {
314
+ Name : "v2" ,
315
+ Storage : false ,
316
+ Served : true ,
317
+ },
318
+ }},
319
+ },
320
+ },
321
+ CRDInstallOptions {MaxTime : 50 * time .Millisecond , PollInterval : 15 * time .Millisecond },
322
+ )
323
+ Expect (err ).NotTo (HaveOccurred ())
324
+
325
+ err = UninstallCRDs (env .Config , CRDInstallOptions {
326
+ Paths : []string {filepath .Join ("." , "testdata" )},
327
+ })
328
+ Expect (err ).NotTo (HaveOccurred ())
329
+
330
+ close (done )
331
+ }, 10 )
332
+ })
219
333
})
0 commit comments