Skip to content

Commit 4c45f5c

Browse files
authored
Merge pull request #431 from droot/bugfix-envtest-multiversion-fix
:bugfix: support multiversion CRD in envtest
2 parents d6324a4 + ab41d11 commit 4c45f5c

File tree

7 files changed

+413
-12
lines changed

7 files changed

+413
-12
lines changed

pkg/envtest/crd.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,24 @@ func WaitForCRDs(config *rest.Config, crds []*apiextensionsv1beta1.CustomResourc
110110
// Add each CRD to a map of GroupVersion to Resource
111111
waitingFor := map[schema.GroupVersion]*sets.String{}
112112
for _, crd := range crds {
113-
gv := schema.GroupVersion{Group: crd.Spec.Group, Version: crd.Spec.Version}
114-
if _, found := waitingFor[gv]; !found {
115-
// Initialize the set
116-
waitingFor[gv] = &sets.String{}
113+
gvs := []schema.GroupVersion{}
114+
if crd.Spec.Version != "" {
115+
gvs = append(gvs, schema.GroupVersion{Group: crd.Spec.Group, Version: crd.Spec.Version})
116+
}
117+
for _, ver := range crd.Spec.Versions {
118+
if ver.Served {
119+
gvs = append(gvs, schema.GroupVersion{Group: crd.Spec.Group, Version: ver.Name})
120+
}
121+
}
122+
for _, gv := range gvs {
123+
log.V(1).Info("adding API in waitlist", "GV", gv)
124+
if _, found := waitingFor[gv]; !found {
125+
// Initialize the set
126+
waitingFor[gv] = &sets.String{}
127+
}
128+
// Add the Resource
129+
waitingFor[gv].Insert(crd.Spec.Names.Plural)
117130
}
118-
// Add the Resource
119-
waitingFor[gv].Insert(crd.Spec.Names.Plural)
120131
}
121132

122133
// Poll until all resources are found in discovery
@@ -225,7 +236,7 @@ func readCRDs(path string) ([]*apiextensionsv1beta1.CustomResourceDefinition, er
225236
crds = append(crds, crd)
226237
}
227238

228-
log.V(1).Info("read CRDs from file", "file", file)
239+
log.V(1).Info("read CRDs from file", "file", file.Name())
229240
}
230241
return crds, nil
231242
}

pkg/envtest/envtest_test.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package envtest
1818

1919
import (
2020
"context"
21+
"path/filepath"
2122

2223
"time"
2324

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

6263
crds, err = InstallCRDs(env.Config, CRDInstallOptions{
63-
Paths: []string{"."},
64+
Paths: []string{filepath.Join(".", "testdata")},
6465
})
6566
Expect(err).NotTo(HaveOccurred())
6667

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

90+
crd = &v1beta1.CustomResourceDefinition{}
91+
err = c.Get(context.TODO(), types.NamespacedName{Name: "drivers.crew.example.com"}, crd)
92+
Expect(err).NotTo(HaveOccurred())
93+
Expect(crd.Spec.Names.Kind).To(Equal("Driver"))
94+
8995
err = WaitForCRDs(env.Config, []*v1beta1.CustomResourceDefinition{
9096
{
9197
Spec: v1beta1.CustomResourceDefinitionSpec{
@@ -118,7 +124,27 @@ var _ = Describe("Test", func() {
118124
Names: v1beta1.CustomResourceDefinitionNames{
119125
Plural: "firstmates",
120126
}},
121-
}},
127+
},
128+
{
129+
Spec: v1beta1.CustomResourceDefinitionSpec{
130+
Group: "crew.example.com",
131+
Names: v1beta1.CustomResourceDefinitionNames{
132+
Plural: "drivers",
133+
},
134+
Versions: []v1beta1.CustomResourceDefinitionVersion{
135+
{
136+
Name: "v1",
137+
Storage: true,
138+
Served: true,
139+
},
140+
{
141+
Name: "v2",
142+
Storage: false,
143+
Served: true,
144+
},
145+
}},
146+
},
147+
},
122148
CRDInstallOptions{maxTime: 50 * time.Millisecond, pollInterval: 15 * time.Millisecond},
123149
)
124150
Expect(err).NotTo(HaveOccurred())
@@ -145,9 +171,11 @@ var _ = Describe("Test", func() {
145171
err := WaitForCRDs(env.Config,
146172
[]*v1beta1.CustomResourceDefinition{
147173
{
148-
Spec: v1beta1.CustomResourceDefinitionSpec{Names: v1beta1.CustomResourceDefinitionNames{
149-
Plural: "notfound",
150-
}},
174+
Spec: v1beta1.CustomResourceDefinitionSpec{
175+
Version: "v1",
176+
Names: v1beta1.CustomResourceDefinitionNames{
177+
Plural: "notfound",
178+
}},
151179
},
152180
},
153181
CRDInstallOptions{maxTime: 50 * time.Millisecond, pollInterval: 15 * time.Millisecond},

0 commit comments

Comments
 (0)