|
| 1 | +package crds |
| 2 | + |
| 3 | +// Generate embedded files from CRDs to avoid file path changes when this package is imported. |
| 4 | +//go:generate go run github.com/go-bindata/go-bindata/v3/go-bindata -pkg crds -o zz_defs.go -ignore=.*\.go . |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "fmt" |
| 9 | + "sync" |
| 10 | + |
| 11 | + "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" |
| 12 | + "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/install" |
| 13 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 14 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 15 | + "k8s.io/apimachinery/pkg/runtime" |
| 16 | + "k8s.io/apimachinery/pkg/util/yaml" |
| 17 | +) |
| 18 | + |
| 19 | +// crdFile is a descriptor of a file containing a CustomResourceDefinition. |
| 20 | +type crdFile string |
| 21 | + |
| 22 | +// path returns the path of the file. |
| 23 | +func (c crdFile) path() string { |
| 24 | + s := string(c) |
| 25 | + return s |
| 26 | +} |
| 27 | + |
| 28 | +// mustUnmarshal unmarshals the file into a CRD and panics on failure. |
| 29 | +func (c crdFile) mustUnmarshal() *apiextensionsv1.CustomResourceDefinition { |
| 30 | + path := c.path() |
| 31 | + data, err := Asset(path) |
| 32 | + if err != nil { |
| 33 | + panic(fmt.Errorf("unable to read crd file %s: %s", path, err)) |
| 34 | + } |
| 35 | + |
| 36 | + u := &unstructured.Unstructured{} |
| 37 | + reader := bytes.NewReader(data) |
| 38 | + decoder := yaml.NewYAMLOrJSONDecoder(reader, 30) |
| 39 | + if err = decoder.Decode(u); err != nil { |
| 40 | + panic(fmt.Errorf("crd unmarshaling failed: %s", err)) |
| 41 | + } |
| 42 | + |
| 43 | + // Step through unversioned type to support v1beta1 -> v1 |
| 44 | + unversioned := &apiextensions.CustomResourceDefinition{} |
| 45 | + if err = scheme.Convert(u, unversioned, nil); err != nil { |
| 46 | + panic(fmt.Errorf("failed to convert crd: %s\nto v1: %s", u, err)) |
| 47 | + } |
| 48 | + |
| 49 | + crd := &apiextensionsv1.CustomResourceDefinition{} |
| 50 | + if err = scheme.Convert(unversioned, crd, nil); err != nil { |
| 51 | + panic(fmt.Errorf("failed to convert crd: %s\nto v1: %s", u, err)) |
| 52 | + } |
| 53 | + |
| 54 | + return crd |
| 55 | +} |
| 56 | + |
| 57 | +var ( |
| 58 | + lock sync.Mutex |
| 59 | + |
| 60 | + // loaded stores previously unmarshaled CustomResourceDefinitions indexed by their file descriptor. |
| 61 | + loaded = map[crdFile]*apiextensionsv1.CustomResourceDefinition{} |
| 62 | + // scheme provides conversions between type versions. |
| 63 | + scheme = runtime.NewScheme() |
| 64 | +) |
| 65 | + |
| 66 | +func init() { |
| 67 | + // Add conversions between CRD versions |
| 68 | + install.Install(scheme) |
| 69 | +} |
| 70 | + |
| 71 | +// getCRD lazily loads and returns the CustomResourceDefinition unmarshaled from a file. |
| 72 | +func getCRD(file crdFile) *apiextensionsv1.CustomResourceDefinition { |
| 73 | + lock.Lock() |
| 74 | + defer lock.Unlock() |
| 75 | + |
| 76 | + if crd, ok := loaded[file]; ok && crd != nil { |
| 77 | + return crd |
| 78 | + } |
| 79 | + |
| 80 | + // Unmarshal and memoize |
| 81 | + crd := file.mustUnmarshal() |
| 82 | + loaded[file] = crd |
| 83 | + |
| 84 | + return crd |
| 85 | +} |
| 86 | + |
| 87 | +// TODO(njhale): codegen this. |
| 88 | + |
| 89 | +// CatalogSource returns a copy of the CustomResourceDefinition for the latest version of the CatalogSource API. |
| 90 | +func CatalogSource() *apiextensionsv1.CustomResourceDefinition { |
| 91 | + return getCRD("operators.coreos.com_catalogsources.yaml").DeepCopy() |
| 92 | +} |
| 93 | + |
| 94 | +// ClusterServiceVersion returns a copy of the CustomResourceDefinition for the latest version of the ClusterServiceVersion API. |
| 95 | +func ClusterServiceVersion() *apiextensionsv1.CustomResourceDefinition { |
| 96 | + return getCRD("operators.coreos.com_clusterserviceversions.yaml").DeepCopy() |
| 97 | +} |
| 98 | + |
| 99 | +// InstallPlan returns a copy of the CustomResourceDefinition for the latest version of the InstallPlan API. |
| 100 | +func InstallPlan() *apiextensionsv1.CustomResourceDefinition { |
| 101 | + return getCRD("operators.coreos.com_installplans.yaml").DeepCopy() |
| 102 | +} |
| 103 | + |
| 104 | +// OperatorGroup returns a copy of the CustomResourceDefinition for the latest version of the OperatorGroup API. |
| 105 | +func OperatorGroup() *apiextensionsv1.CustomResourceDefinition { |
| 106 | + return getCRD("operators.coreos.com_operatorgroups.yaml").DeepCopy() |
| 107 | +} |
| 108 | + |
| 109 | +// Operator returns a copy of the CustomResourceDefinition for the latest version of the Operator API. |
| 110 | +func Operator() *apiextensionsv1.CustomResourceDefinition { |
| 111 | + return getCRD("operators.coreos.com_operators.yaml").DeepCopy() |
| 112 | +} |
| 113 | + |
| 114 | +// Subscription returns a copy of the CustomResourceDefinition for the latest version of the Subscription API. |
| 115 | +func Subscription() *apiextensionsv1.CustomResourceDefinition { |
| 116 | + return getCRD("operators.coreos.com_subscriptions.yaml").DeepCopy() |
| 117 | +} |
0 commit comments