Skip to content

Commit 2c5bca2

Browse files
authored
Merge pull request #397 from GrigoriyMikhalkin/issue/369
✨ envtest can load multiple CRDs from single file
2 parents faa41bc + f98781b commit 2c5bca2

File tree

4 files changed

+96
-11
lines changed

4 files changed

+96
-11
lines changed

Gopkg.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/envtest/crd.go

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ limitations under the License.
1717
package envtest
1818

1919
import (
20+
"bufio"
21+
"bytes"
22+
"io"
2023
"io/ioutil"
2124
"os"
2225
"path/filepath"
@@ -27,6 +30,7 @@ import (
2730
"k8s.io/apimachinery/pkg/runtime/schema"
2831
"k8s.io/apimachinery/pkg/util/sets"
2932
"k8s.io/apimachinery/pkg/util/wait"
33+
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
3034
"k8s.io/client-go/rest"
3135
"sigs.k8s.io/yaml"
3236
)
@@ -202,23 +206,52 @@ func readCRDs(path string) ([]*apiextensionsv1beta1.CustomResourceDefinition, er
202206
continue
203207
}
204208

205-
// Unmarshal the file into a struct
206-
b, err := ioutil.ReadFile(filepath.Join(path, file.Name()))
209+
// Unmarshal CRDs from file into structs
210+
docs, err := readDocuments(filepath.Join(path, file.Name()))
207211
if err != nil {
208212
return nil, err
209213
}
210-
crd := &apiextensionsv1beta1.CustomResourceDefinition{}
211-
if err = yaml.Unmarshal(b, crd); err != nil {
212-
return nil, err
213-
}
214214

215-
// Check that it is actually a CRD
216-
if crd.Spec.Names.Kind == "" || crd.Spec.Group == "" {
217-
continue
215+
for _, doc := range docs {
216+
crd := &apiextensionsv1beta1.CustomResourceDefinition{}
217+
if err = yaml.Unmarshal(doc, crd); err != nil {
218+
return nil, err
219+
}
220+
221+
// Check that it is actually a CRD
222+
if crd.Spec.Names.Kind == "" || crd.Spec.Group == "" {
223+
continue
224+
}
225+
crds = append(crds, crd)
218226
}
219227

220-
log.V(1).Info("read CRD from file", "file", file)
221-
crds = append(crds, crd)
228+
log.V(1).Info("read CRDs from file", "file", file)
222229
}
223230
return crds, nil
224231
}
232+
233+
// readDocuments reads documents from file
234+
func readDocuments(fp string) ([][]byte, error) {
235+
b, err := ioutil.ReadFile(fp)
236+
if err != nil {
237+
return nil, err
238+
}
239+
240+
docs := [][]byte{}
241+
reader := k8syaml.NewYAMLReader(bufio.NewReader(bytes.NewReader(b)))
242+
for {
243+
// Read document
244+
doc, err := reader.Read()
245+
if err != nil {
246+
if err == io.EOF {
247+
break
248+
}
249+
250+
return nil, err
251+
}
252+
253+
docs = append(docs, doc)
254+
}
255+
256+
return docs, nil
257+
}

pkg/envtest/envtest_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ var _ = Describe("Test", func() {
7676
Expect(err).NotTo(HaveOccurred())
7777
Expect(crd.Spec.Names.Kind).To(Equal("Baz"))
7878

79+
crd = &v1beta1.CustomResourceDefinition{}
80+
err = c.Get(context.TODO(), types.NamespacedName{Name: "captains.crew.example.com"}, crd)
81+
Expect(err).NotTo(HaveOccurred())
82+
Expect(crd.Spec.Names.Kind).To(Equal("Captain"))
83+
84+
crd = &v1beta1.CustomResourceDefinition{}
85+
err = c.Get(context.TODO(), types.NamespacedName{Name: "firstmates.crew.example.com"}, crd)
86+
Expect(err).NotTo(HaveOccurred())
87+
Expect(crd.Spec.Names.Kind).To(Equal("FirstMate"))
88+
7989
err = WaitForCRDs(env.Config, []*v1beta1.CustomResourceDefinition{
8090
{
8191
Spec: v1beta1.CustomResourceDefinitionSpec{
@@ -92,6 +102,22 @@ var _ = Describe("Test", func() {
92102
Names: v1beta1.CustomResourceDefinitionNames{
93103
Plural: "foos",
94104
}},
105+
},
106+
{
107+
Spec: v1beta1.CustomResourceDefinitionSpec{
108+
Group: "crew.example.com",
109+
Version: "v1beta1",
110+
Names: v1beta1.CustomResourceDefinitionNames{
111+
Plural: "captains",
112+
}},
113+
},
114+
{
115+
Spec: v1beta1.CustomResourceDefinitionSpec{
116+
Group: "crew.example.com",
117+
Version: "v1beta1",
118+
Names: v1beta1.CustomResourceDefinitionNames{
119+
Plural: "firstmates",
120+
}},
95121
}},
96122
CRDInstallOptions{maxTime: 50 * time.Millisecond, pollInterval: 15 * time.Millisecond},
97123
)

pkg/envtest/multiplecrds.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
apiVersion: apiextensions.k8s.io/v1beta1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
name: captains.crew.example.com
6+
spec:
7+
group: crew.example.com
8+
names:
9+
kind: Captain
10+
plural: captains
11+
scope: Namespaced
12+
version: "v1beta1"
13+
---
14+
apiVersion: apiextensions.k8s.io/v1beta1
15+
kind: CustomResourceDefinition
16+
metadata:
17+
name: firstmates.crew.example.com
18+
spec:
19+
group: crew.example.com
20+
names:
21+
kind: FirstMate
22+
plural: firstmates
23+
scope: Namespaced
24+
version: "v1beta1"
25+
---

0 commit comments

Comments
 (0)