Skip to content

Commit 3966c67

Browse files
🐛 fix loading CRDs from multiple directories in envtests (#1905)
* fix loading CRDs from multiple directories in envtests * add unit tests Co-authored-by: Christoph Mewes <[email protected]>
1 parent 2f77235 commit 3966c67

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

pkg/envtest/crd.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,6 @@ func CreateCRDs(config *rest.Config, crds []*apiextensionsv1.CustomResourceDefin
278278

279279
// renderCRDs iterate through options.Paths and extract all CRD files.
280280
func renderCRDs(options *CRDInstallOptions) ([]*apiextensionsv1.CustomResourceDefinition, error) {
281-
var (
282-
err error
283-
info os.FileInfo
284-
files []string
285-
)
286-
287281
type GVKN struct {
288282
GVK schema.GroupVersionKind
289283
Name string
@@ -292,7 +286,12 @@ func renderCRDs(options *CRDInstallOptions) ([]*apiextensionsv1.CustomResourceDe
292286
crds := map[GVKN]*apiextensionsv1.CustomResourceDefinition{}
293287

294288
for _, path := range options.Paths {
295-
var filePath = path
289+
var (
290+
err error
291+
info os.FileInfo
292+
files []string
293+
filePath = path
294+
)
296295

297296
// Return the error if ErrorIfPathMissing exists
298297
if info, err = os.Stat(path); os.IsNotExist(err) {

pkg/envtest/crd_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package envtest
18+
19+
import (
20+
. "github.com/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
"k8s.io/apimachinery/pkg/util/sets"
23+
)
24+
25+
var _ = Describe("Test", func() {
26+
Describe("readCRDFiles", func() {
27+
It("should not mix up files from different directories", func() {
28+
opt := CRDInstallOptions{
29+
Paths: []string{
30+
"testdata/crds",
31+
"testdata/crdv1_original",
32+
},
33+
}
34+
err := readCRDFiles(&opt)
35+
Expect(err).NotTo(HaveOccurred())
36+
37+
expectedCRDs := sets.NewString(
38+
"frigates.ship.example.com",
39+
"configs.foo.example.com",
40+
"drivers.crew.example.com",
41+
)
42+
43+
foundCRDs := sets.NewString()
44+
for _, crd := range opt.CRDs {
45+
foundCRDs.Insert(crd.Name)
46+
}
47+
48+
Expect(expectedCRDs).To(Equal(foundCRDs))
49+
})
50+
})
51+
})

0 commit comments

Comments
 (0)