Skip to content

Commit

Permalink
envtest can load multiple CRDs from single file
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigoriyMikhalkin committed May 9, 2019
1 parent 4276f38 commit d048e2a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 11 deletions.
1 change: 1 addition & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 44 additions & 11 deletions pkg/envtest/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package envtest

import (
"bufio"
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -28,6 +31,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/rest"
)

Expand Down Expand Up @@ -202,23 +206,52 @@ func readCRDs(path string) ([]*apiextensionsv1beta1.CustomResourceDefinition, er
continue
}

// Unmarshal the file into a struct
b, err := ioutil.ReadFile(filepath.Join(path, file.Name()))
// Unmarshal CRDs from file into structs
docs, err := readDocuments(filepath.Join(path, file.Name()))
if err != nil {
return nil, err
}
crd := &apiextensionsv1beta1.CustomResourceDefinition{}
if err = yaml.Unmarshal(b, crd); err != nil {
return nil, err
}

// Check that it is actually a CRD
if crd.Spec.Names.Kind == "" || crd.Spec.Group == "" {
continue
for _, doc := range docs {
crd := &apiextensionsv1beta1.CustomResourceDefinition{}
if err = yaml.Unmarshal(doc, crd); err != nil {
return nil, err
}

// Check that it is actually a CRD
if crd.Spec.Names.Kind == "" || crd.Spec.Group == "" {
continue
}
crds = append(crds, crd)
}

log.V(1).Info("read CRD from file", "file", file)
crds = append(crds, crd)
log.V(1).Info("read CRDs from file", "file", file)
}
return crds, nil
}

// readDocuments reads documents from file
func readDocuments(fp string) ([][]byte, error) {
b, err := ioutil.ReadFile(fp)
if err != nil {
return nil, err
}

docs := [][]byte{}
reader := k8syaml.NewYAMLReader(bufio.NewReader(bytes.NewReader(b)))
for {
// Read document
doc, err := reader.Read()
if err != nil {
if err == io.EOF {
break
}

return nil, err
}

docs = append(docs, doc)
}

return docs, nil
}
26 changes: 26 additions & 0 deletions pkg/envtest/envtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ var _ = Describe("Test", func() {
Expect(err).NotTo(HaveOccurred())
Expect(crd.Spec.Names.Kind).To(Equal("Baz"))

crd = &v1beta1.CustomResourceDefinition{}
err = c.Get(context.TODO(), types.NamespacedName{Name: "captains.crew.example.com"}, crd)
Expect(err).NotTo(HaveOccurred())
Expect(crd.Spec.Names.Kind).To(Equal("Captain"))

crd = &v1beta1.CustomResourceDefinition{}
err = c.Get(context.TODO(), types.NamespacedName{Name: "firstmates.crew.example.com"}, crd)
Expect(err).NotTo(HaveOccurred())
Expect(crd.Spec.Names.Kind).To(Equal("FirstMate"))

err = WaitForCRDs(env.Config, []*v1beta1.CustomResourceDefinition{
{
Spec: v1beta1.CustomResourceDefinitionSpec{
Expand All @@ -92,6 +102,22 @@ var _ = Describe("Test", func() {
Names: v1beta1.CustomResourceDefinitionNames{
Plural: "foos",
}},
},
{
Spec: v1beta1.CustomResourceDefinitionSpec{
Group: "crew.example.com",
Version: "v1beta1",
Names: v1beta1.CustomResourceDefinitionNames{
Plural: "captains",
}},
},
{
Spec: v1beta1.CustomResourceDefinitionSpec{
Group: "crew.example.com",
Version: "v1beta1",
Names: v1beta1.CustomResourceDefinitionNames{
Plural: "firstmates",
}},
}},
CRDInstallOptions{maxTime: 50 * time.Millisecond, pollInterval: 15 * time.Millisecond},
)
Expand Down
25 changes: 25 additions & 0 deletions pkg/envtest/multiplecrds.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: captains.crew.example.com
spec:
group: crew.example.com
names:
kind: Captain
plural: captains
scope: Namespaced
version: "v1beta1"
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: firstmates.crew.example.com
spec:
group: crew.example.com
names:
kind: FirstMate
plural: firstmates
scope: Namespaced
version: "v1beta1"
---

0 comments on commit d048e2a

Please sign in to comment.