Skip to content

Commit 79c06b1

Browse files
committed
Updated CRD if already present
Signed-off-by: Pires <pjpires@gmail.com>
1 parent a0e9c1d commit 79c06b1

File tree

4 files changed

+1016
-2
lines changed

4 files changed

+1016
-2
lines changed

pkg/envtest/crd.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,18 @@ func CreateCRDs(config *rest.Config, crds []*apiextensionsv1beta1.CustomResource
225225
// Create each CRD
226226
for _, crd := range crds {
227227
log.V(1).Info("installing CRD", "crd", crd.Name)
228-
if _, err := cs.ApiextensionsV1beta1().CustomResourceDefinitions().Create(crd); err != nil {
229-
return err
228+
if existingCrd, err := cs.ApiextensionsV1beta1().CustomResourceDefinitions().Get(crd.Name, metav1.GetOptions{}); err != nil {
229+
if apierrors.IsNotFound(err) {
230+
if _, err := cs.ApiextensionsV1beta1().CustomResourceDefinitions().Create(crd); err != nil {
231+
return err
232+
}
233+
}
234+
} else {
235+
log.V(1).Info("CRD already exists, updating", "crd", crd.Name)
236+
crd.ResourceVersion = existingCrd.ResourceVersion
237+
if _, err = cs.ApiextensionsV1beta1().CustomResourceDefinitions().Update(crd); err != nil {
238+
return err
239+
}
230240
}
231241
}
232242
return nil

pkg/envtest/envtest_test.go

+277
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,285 @@ var _ = Describe("Test", func() {
226226

227227
close(done)
228228
}, 5)
229+
230+
It("should reinstall the CRDs if already present in the cluster", func(done Done) {
231+
232+
crds, err = InstallCRDs(env.Config, CRDInstallOptions{
233+
Paths: []string{filepath.Join(".", "testdata")},
234+
})
235+
Expect(err).NotTo(HaveOccurred())
236+
237+
// Expect to find the CRDs
238+
239+
crd := &v1beta1.CustomResourceDefinition{}
240+
err = c.Get(context.TODO(), types.NamespacedName{Name: "foos.bar.example.com"}, crd)
241+
Expect(err).NotTo(HaveOccurred())
242+
Expect(crd.Spec.Names.Kind).To(Equal("Foo"))
243+
244+
crd = &v1beta1.CustomResourceDefinition{}
245+
err = c.Get(context.TODO(), types.NamespacedName{Name: "bazs.qux.example.com"}, crd)
246+
Expect(err).NotTo(HaveOccurred())
247+
Expect(crd.Spec.Names.Kind).To(Equal("Baz"))
248+
249+
crd = &v1beta1.CustomResourceDefinition{}
250+
err = c.Get(context.TODO(), types.NamespacedName{Name: "captains.crew.example.com"}, crd)
251+
Expect(err).NotTo(HaveOccurred())
252+
Expect(crd.Spec.Names.Kind).To(Equal("Captain"))
253+
254+
crd = &v1beta1.CustomResourceDefinition{}
255+
err = c.Get(context.TODO(), types.NamespacedName{Name: "firstmates.crew.example.com"}, crd)
256+
Expect(err).NotTo(HaveOccurred())
257+
Expect(crd.Spec.Names.Kind).To(Equal("FirstMate"))
258+
259+
crd = &v1beta1.CustomResourceDefinition{}
260+
err = c.Get(context.TODO(), types.NamespacedName{Name: "drivers.crew.example.com"}, crd)
261+
Expect(err).NotTo(HaveOccurred())
262+
Expect(crd.Spec.Names.Kind).To(Equal("Driver"))
263+
264+
err = WaitForCRDs(env.Config, []*v1beta1.CustomResourceDefinition{
265+
{
266+
Spec: v1beta1.CustomResourceDefinitionSpec{
267+
Group: "qux.example.com",
268+
Version: "v1beta1",
269+
Names: v1beta1.CustomResourceDefinitionNames{
270+
Plural: "bazs",
271+
}},
272+
},
273+
{
274+
Spec: v1beta1.CustomResourceDefinitionSpec{
275+
Group: "bar.example.com",
276+
Version: "v1beta1",
277+
Names: v1beta1.CustomResourceDefinitionNames{
278+
Plural: "foos",
279+
}},
280+
},
281+
{
282+
Spec: v1beta1.CustomResourceDefinitionSpec{
283+
Group: "crew.example.com",
284+
Version: "v1beta1",
285+
Names: v1beta1.CustomResourceDefinitionNames{
286+
Plural: "captains",
287+
}},
288+
},
289+
{
290+
Spec: v1beta1.CustomResourceDefinitionSpec{
291+
Group: "crew.example.com",
292+
Version: "v1beta1",
293+
Names: v1beta1.CustomResourceDefinitionNames{
294+
Plural: "firstmates",
295+
}},
296+
},
297+
{
298+
Spec: v1beta1.CustomResourceDefinitionSpec{
299+
Group: "crew.example.com",
300+
Names: v1beta1.CustomResourceDefinitionNames{
301+
Plural: "drivers",
302+
},
303+
Versions: []v1beta1.CustomResourceDefinitionVersion{
304+
{
305+
Name: "v1",
306+
Storage: true,
307+
Served: true,
308+
},
309+
{
310+
Name: "v2",
311+
Storage: false,
312+
Served: true,
313+
},
314+
}},
315+
},
316+
},
317+
CRDInstallOptions{MaxTime: 50 * time.Millisecond, PollInterval: 15 * time.Millisecond},
318+
)
319+
Expect(err).NotTo(HaveOccurred())
320+
321+
// Try to re-install the CRDs
322+
323+
crds, err = InstallCRDs(env.Config, CRDInstallOptions{
324+
Paths: []string{filepath.Join(".", "testdata")},
325+
})
326+
Expect(err).NotTo(HaveOccurred())
327+
328+
// Expect to find the CRDs
329+
330+
crd = &v1beta1.CustomResourceDefinition{}
331+
err = c.Get(context.TODO(), types.NamespacedName{Name: "foos.bar.example.com"}, crd)
332+
Expect(err).NotTo(HaveOccurred())
333+
Expect(crd.Spec.Names.Kind).To(Equal("Foo"))
334+
335+
crd = &v1beta1.CustomResourceDefinition{}
336+
err = c.Get(context.TODO(), types.NamespacedName{Name: "bazs.qux.example.com"}, crd)
337+
Expect(err).NotTo(HaveOccurred())
338+
Expect(crd.Spec.Names.Kind).To(Equal("Baz"))
339+
340+
crd = &v1beta1.CustomResourceDefinition{}
341+
err = c.Get(context.TODO(), types.NamespacedName{Name: "captains.crew.example.com"}, crd)
342+
Expect(err).NotTo(HaveOccurred())
343+
Expect(crd.Spec.Names.Kind).To(Equal("Captain"))
344+
345+
crd = &v1beta1.CustomResourceDefinition{}
346+
err = c.Get(context.TODO(), types.NamespacedName{Name: "firstmates.crew.example.com"}, crd)
347+
Expect(err).NotTo(HaveOccurred())
348+
Expect(crd.Spec.Names.Kind).To(Equal("FirstMate"))
349+
350+
crd = &v1beta1.CustomResourceDefinition{}
351+
err = c.Get(context.TODO(), types.NamespacedName{Name: "drivers.crew.example.com"}, crd)
352+
Expect(err).NotTo(HaveOccurred())
353+
Expect(crd.Spec.Names.Kind).To(Equal("Driver"))
354+
355+
err = WaitForCRDs(env.Config, []*v1beta1.CustomResourceDefinition{
356+
{
357+
Spec: v1beta1.CustomResourceDefinitionSpec{
358+
Group: "qux.example.com",
359+
Version: "v1beta1",
360+
Names: v1beta1.CustomResourceDefinitionNames{
361+
Plural: "bazs",
362+
}},
363+
},
364+
{
365+
Spec: v1beta1.CustomResourceDefinitionSpec{
366+
Group: "bar.example.com",
367+
Version: "v1beta1",
368+
Names: v1beta1.CustomResourceDefinitionNames{
369+
Plural: "foos",
370+
}},
371+
},
372+
{
373+
Spec: v1beta1.CustomResourceDefinitionSpec{
374+
Group: "crew.example.com",
375+
Version: "v1beta1",
376+
Names: v1beta1.CustomResourceDefinitionNames{
377+
Plural: "captains",
378+
}},
379+
},
380+
{
381+
Spec: v1beta1.CustomResourceDefinitionSpec{
382+
Group: "crew.example.com",
383+
Version: "v1beta1",
384+
Names: v1beta1.CustomResourceDefinitionNames{
385+
Plural: "firstmates",
386+
}},
387+
},
388+
{
389+
Spec: v1beta1.CustomResourceDefinitionSpec{
390+
Group: "crew.example.com",
391+
Names: v1beta1.CustomResourceDefinitionNames{
392+
Plural: "drivers",
393+
},
394+
Versions: []v1beta1.CustomResourceDefinitionVersion{
395+
{
396+
Name: "v1",
397+
Storage: true,
398+
Served: true,
399+
},
400+
{
401+
Name: "v2",
402+
Storage: false,
403+
Served: true,
404+
},
405+
}},
406+
},
407+
},
408+
CRDInstallOptions{MaxTime: 50 * time.Millisecond, PollInterval: 15 * time.Millisecond},
409+
)
410+
Expect(err).NotTo(HaveOccurred())
411+
412+
close(done)
413+
}, 5)
229414
})
230415

416+
It("should update CRDs if already present in the cluster", func(done Done) {
417+
418+
// Install only the CRDv1 multi-version example
419+
crds, err = InstallCRDs(env.Config, CRDInstallOptions{
420+
Paths: []string{filepath.Join(".", "testdata", "crdv1_original")},
421+
})
422+
Expect(err).NotTo(HaveOccurred())
423+
424+
// Expect to find the CRDs
425+
426+
crd := &v1beta1.CustomResourceDefinition{}
427+
err = c.Get(context.TODO(), types.NamespacedName{Name: "drivers.crew.example.com"}, crd)
428+
Expect(err).NotTo(HaveOccurred())
429+
Expect(crd.Spec.Names.Kind).To(Equal("Driver"))
430+
Expect(len(crd.Spec.Versions)).To(BeEquivalentTo(2))
431+
432+
// Store resource version for comparison later on
433+
firstRV := crd.ResourceVersion
434+
435+
err = WaitForCRDs(env.Config, []*v1beta1.CustomResourceDefinition{
436+
{
437+
Spec: v1beta1.CustomResourceDefinitionSpec{
438+
Group: "crew.example.com",
439+
Names: v1beta1.CustomResourceDefinitionNames{
440+
Plural: "drivers",
441+
},
442+
Versions: []v1beta1.CustomResourceDefinitionVersion{
443+
{
444+
Name: "v1",
445+
Storage: true,
446+
Served: true,
447+
},
448+
{
449+
Name: "v2",
450+
Storage: false,
451+
Served: true,
452+
},
453+
}},
454+
},
455+
},
456+
CRDInstallOptions{MaxTime: 50 * time.Millisecond, PollInterval: 15 * time.Millisecond},
457+
)
458+
Expect(err).NotTo(HaveOccurred())
459+
460+
// Add one more version and update
461+
crds, err = InstallCRDs(env.Config, CRDInstallOptions{
462+
Paths: []string{filepath.Join(".", "testdata", "crdv1_updated")},
463+
})
464+
Expect(err).NotTo(HaveOccurred())
465+
466+
// Expect to find updated CRD
467+
468+
crd = &v1beta1.CustomResourceDefinition{}
469+
err = c.Get(context.TODO(), types.NamespacedName{Name: "drivers.crew.example.com"}, crd)
470+
Expect(err).NotTo(HaveOccurred())
471+
Expect(crd.Spec.Names.Kind).To(Equal("Driver"))
472+
Expect(len(crd.Spec.Versions)).To(BeEquivalentTo(3))
473+
Expect(crd.ResourceVersion).NotTo(BeEquivalentTo(firstRV))
474+
475+
err = WaitForCRDs(env.Config, []*v1beta1.CustomResourceDefinition{
476+
{
477+
Spec: v1beta1.CustomResourceDefinitionSpec{
478+
Group: "crew.example.com",
479+
Names: v1beta1.CustomResourceDefinitionNames{
480+
Plural: "drivers",
481+
},
482+
Versions: []v1beta1.CustomResourceDefinitionVersion{
483+
{
484+
Name: "v1",
485+
Storage: true,
486+
Served: true,
487+
},
488+
{
489+
Name: "v2",
490+
Storage: false,
491+
Served: true,
492+
},
493+
{
494+
Name: "v3",
495+
Storage: false,
496+
Served: true,
497+
},
498+
}},
499+
},
500+
},
501+
CRDInstallOptions{MaxTime: 50 * time.Millisecond, PollInterval: 15 * time.Millisecond},
502+
)
503+
Expect(err).NotTo(HaveOccurred())
504+
505+
close(done)
506+
}, 5)
507+
231508
Describe("UninstallCRDs", func() {
232509
It("should uninstall the CRDs from the cluster", func(done Done) {
233510

0 commit comments

Comments
 (0)