Skip to content

Commit

Permalink
:bugfix: support multiversion CRD in envtest
Browse files Browse the repository at this point in the history
  • Loading branch information
droot committed May 15, 2019
1 parent d6324a4 commit 88c1439
Show file tree
Hide file tree
Showing 7 changed files with 411 additions and 12 deletions.
23 changes: 16 additions & 7 deletions pkg/envtest/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,22 @@ func WaitForCRDs(config *rest.Config, crds []*apiextensionsv1beta1.CustomResourc
// Add each CRD to a map of GroupVersion to Resource
waitingFor := map[schema.GroupVersion]*sets.String{}
for _, crd := range crds {
gv := schema.GroupVersion{Group: crd.Spec.Group, Version: crd.Spec.Version}
if _, found := waitingFor[gv]; !found {
// Initialize the set
waitingFor[gv] = &sets.String{}
gvs := []schema.GroupVersion{}
if crd.Spec.Version != "" {
gvs = append(gvs, schema.GroupVersion{Group: crd.Spec.Group, Version: crd.Spec.Version})
}
for _, ver := range crd.Spec.Versions {
gvs = append(gvs, schema.GroupVersion{Group: crd.Spec.Group, Version: ver.Name})
}
for _, gv := range gvs {
log.V(1).Info("adding API in waiting for list", "gv", gv)
if _, found := waitingFor[gv]; !found {
// Initialize the set
waitingFor[gv] = &sets.String{}
}
// Add the Resource
waitingFor[gv].Insert(crd.Spec.Names.Plural)
}
// Add the Resource
waitingFor[gv].Insert(crd.Spec.Names.Plural)
}

// Poll until all resources are found in discovery
Expand Down Expand Up @@ -225,7 +234,7 @@ func readCRDs(path string) ([]*apiextensionsv1beta1.CustomResourceDefinition, er
crds = append(crds, crd)
}

log.V(1).Info("read CRDs from file", "file", file)
log.V(1).Info("read CRDs from file", "file", file.Name())
}
return crds, nil
}
Expand Down
38 changes: 33 additions & 5 deletions pkg/envtest/envtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package envtest

import (
"context"
"path/filepath"

"time"

Expand Down Expand Up @@ -60,7 +61,7 @@ var _ = Describe("Test", func() {
It("should install the CRDs into the cluster", func(done Done) {

crds, err = InstallCRDs(env.Config, CRDInstallOptions{
Paths: []string{"."},
Paths: []string{filepath.Join(".", "testdata")},
})
Expect(err).NotTo(HaveOccurred())

Expand All @@ -86,6 +87,11 @@ var _ = Describe("Test", func() {
Expect(err).NotTo(HaveOccurred())
Expect(crd.Spec.Names.Kind).To(Equal("FirstMate"))

crd = &v1beta1.CustomResourceDefinition{}
err = c.Get(context.TODO(), types.NamespacedName{Name: "drivers.crew.example.com"}, crd)
Expect(err).NotTo(HaveOccurred())
Expect(crd.Spec.Names.Kind).To(Equal("Driver"))

err = WaitForCRDs(env.Config, []*v1beta1.CustomResourceDefinition{
{
Spec: v1beta1.CustomResourceDefinitionSpec{
Expand Down Expand Up @@ -118,7 +124,27 @@ var _ = Describe("Test", func() {
Names: v1beta1.CustomResourceDefinitionNames{
Plural: "firstmates",
}},
}},
},
{
Spec: v1beta1.CustomResourceDefinitionSpec{
Group: "crew.example.com",
Names: v1beta1.CustomResourceDefinitionNames{
Plural: "drivers",
},
Versions: []v1beta1.CustomResourceDefinitionVersion{
{
Name: "v1",
Storage: true,
Served: true,
},
{
Name: "v2",
Storage: false,
Served: true,
},
}},
},
},
CRDInstallOptions{maxTime: 50 * time.Millisecond, pollInterval: 15 * time.Millisecond},
)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -145,9 +171,11 @@ var _ = Describe("Test", func() {
err := WaitForCRDs(env.Config,
[]*v1beta1.CustomResourceDefinition{
{
Spec: v1beta1.CustomResourceDefinitionSpec{Names: v1beta1.CustomResourceDefinitionNames{
Plural: "notfound",
}},
Spec: v1beta1.CustomResourceDefinitionSpec{
Version: "v1",
Names: v1beta1.CustomResourceDefinitionNames{
Plural: "notfound",
}},
},
},
CRDInstallOptions{maxTime: 50 * time.Millisecond, pollInterval: 15 * time.Millisecond},
Expand Down
Loading

0 comments on commit 88c1439

Please sign in to comment.