Skip to content

Commit c1472c5

Browse files
Mengqi Yuvincepri
authored andcommitted
🐛 fix CRD filtering in envtest package
1 parent 817584d commit c1472c5

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

pkg/envtest/crd.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,16 @@ func renderCRDs(options *CRDInstallOptions) ([]runtime.Object, error) {
278278
var (
279279
err error
280280
info os.FileInfo
281-
crds []*unstructured.Unstructured
282281
files []os.FileInfo
283282
)
284283

284+
type GVKN struct {
285+
GVK schema.GroupVersionKind
286+
Name string
287+
}
288+
289+
crds := map[GVKN]*unstructured.Unstructured{}
290+
285291
for _, path := range options.Paths {
286292
var filePath = path
287293

@@ -294,7 +300,7 @@ func renderCRDs(options *CRDInstallOptions) ([]runtime.Object, error) {
294300
}
295301

296302
if !info.IsDir() {
297-
filePath, files = filepath.Dir(path), append(files, info)
303+
filePath, files = filepath.Dir(path), []os.FileInfo{info}
298304
} else {
299305
if files, err = ioutil.ReadDir(path); err != nil {
300306
return nil, err
@@ -307,14 +313,23 @@ func renderCRDs(options *CRDInstallOptions) ([]runtime.Object, error) {
307313
return nil, err
308314
}
309315

310-
// If CRD already in the list, skip it.
311-
if existsUnstructured(crds, crdList) {
312-
continue
316+
for i, crd := range crdList {
317+
gvkn := GVKN{GVK: crd.GroupVersionKind(), Name: crd.GetName()}
318+
if _, found := crds[gvkn]; found {
319+
// Currently, we only print a log when there are duplicates. We may want to error out if that makes more sense.
320+
log.Info("there are more than one CRD definitions with the same <Group, Version, Kind, Name>", "GVKN", gvkn)
321+
}
322+
// We always use the CRD definition that we found last.
323+
crds[gvkn] = crdList[i]
313324
}
314-
crds = append(crds, crdList...)
315325
}
316326

317-
return unstructuredCRDListToRuntime(crds), nil
327+
// Converting map to a list to return
328+
var res []runtime.Object
329+
for _, obj := range crds {
330+
res = append(res, obj)
331+
}
332+
return res, nil
318333
}
319334

320335
// readCRDs reads the CRDs from files and Unmarshals them into structs

pkg/envtest/envtest_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,19 @@ var _ = Describe("Test", func() {
213213
close(done)
214214
}, 10)
215215

216+
It("should be able to install CRDs using multiple files", func(done Done) {
217+
crds, err = InstallCRDs(env.Config, CRDInstallOptions{
218+
Paths: []string{
219+
filepath.Join(".", "testdata", "examplecrd.yaml"),
220+
filepath.Join(".", "testdata", "examplecrd_v1.yaml"),
221+
},
222+
})
223+
Expect(err).NotTo(HaveOccurred())
224+
Expect(crds).To(HaveLen(2))
225+
226+
close(done)
227+
}, 10)
228+
216229
It("should filter out already existent CRD", func(done Done) {
217230
crds, err = InstallCRDs(env.Config, CRDInstallOptions{
218231
Paths: []string{

pkg/envtest/helper.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package envtest
22

33
import (
4-
"reflect"
5-
64
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
75
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
86
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -57,18 +55,6 @@ func mergeCRDs(s1, s2 []runtime.Object) []runtime.Object {
5755
return merged
5856
}
5957

60-
// existsUnstructured verify if a any item is common between two lists.
61-
func existsUnstructured(s1, s2 []*unstructured.Unstructured) bool {
62-
for _, s1obj := range s1 {
63-
for _, s2obj := range s2 {
64-
if reflect.DeepEqual(s1obj, s2obj) {
65-
return true
66-
}
67-
}
68-
}
69-
return false
70-
}
71-
7258
func runtimeCRDListToUnstructured(l []runtime.Object) []*unstructured.Unstructured {
7359
res := []*unstructured.Unstructured{}
7460
for _, obj := range l {
@@ -81,11 +67,3 @@ func runtimeCRDListToUnstructured(l []runtime.Object) []*unstructured.Unstructur
8167
}
8268
return res
8369
}
84-
85-
func unstructuredCRDListToRuntime(l []*unstructured.Unstructured) []runtime.Object {
86-
res := []runtime.Object{}
87-
for _, obj := range l {
88-
res = append(res, obj.DeepCopy())
89-
}
90-
return res
91-
}

0 commit comments

Comments
 (0)