Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:bugfix: support multiversion CRD in envtest #431

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions pkg/envtest/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,24 @@ 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 {
if ver.Served {
gvs = append(gvs, schema.GroupVersion{Group: crd.Spec.Group, Version: ver.Name})
}
}
for _, gv := range gvs {
log.V(1).Info("adding API in waitlist", "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 +236,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