// Package fixtures provides functions for loading embedded test fixtures used // by various tests. package fixtures import ( "embed" // "fmt" "path/filepath" v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" "k8s.io/apimachinery/pkg/util/yaml" ) //go:embed crds/**/*.yaml var crdFixtures embed.FS //go:embed warehouse/src/** var PalletFixtures embed.FS type options struct { only string } type Option func(*options) // Only makes LoadCRDs only return CRDs from directories with the provided name. // e.g., to only read the Edge CRDs, pass "edge" func Only(dir string) func(*options) { return func(o *options) { o.only = dir } } // LoadCRDs loads the embedded CRD test fixtures and marshals them // into client.Object structs that are ready to be applied to any K8s server func LoadCRDs(opts ...Option) ([]*v1.CustomResourceDefinition, error) { o := &options{} for _, opt := range opts { opt(o) } crdDir := "crds" var manifests []*v1.CustomResourceDefinition crdDirs, err := crdFixtures.ReadDir(crdDir) if err != nil { return nil, err } // loop over directories containing CRDs for _, crdDirEntry := range crdDirs { if !crdDirEntry.Type().IsDir() { continue } dirname := crdDirEntry.Name() // skip dirnames that dont match if o.only is set if o.only != "" && dirname != o.only { continue } crdEntries, err := crdFixtures.ReadDir(filepath.Join(crdDir, dirname)) if err != nil { return nil, err } // loop over CRDs in the CRD directory for _, crdEntry := range crdEntries { crdname := crdEntry.Name() //fmt.Println(crdname) data, err := crdFixtures.ReadFile(filepath.Join(crdDir, dirname, crdname)) if err != nil { return nil, err } crd := &v1.CustomResourceDefinition{} if err := yaml.Unmarshal(data, crd); err != nil { return nil, err } manifests = append(manifests, crd) } } return manifests, nil }