-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from njhale/op-disc-2
feat(operators): add v2alpha1 operator types
- Loading branch information
Showing
428 changed files
with
77,551 additions
and
4,630 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package crds | ||
|
||
// Generate embedded files from CRDs to avoid file path changes when this package is imported. | ||
//go:generate go run github.com/go-bindata/go-bindata/v3/go-bindata -pkg crds -o zz_defs.go -ignore=.*\.go . | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"sync" | ||
|
||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" | ||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/install" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/yaml" | ||
) | ||
|
||
// crdFile is a descriptor of a file containing a CustomResourceDefinition. | ||
type crdFile string | ||
|
||
// path returns the path of the file. | ||
func (c crdFile) path() string { | ||
s := string(c) | ||
return s | ||
} | ||
|
||
// mustUnmarshal unmarshals the file into a CRD and panics on failure. | ||
func (c crdFile) mustUnmarshal() *apiextensionsv1.CustomResourceDefinition { | ||
path := c.path() | ||
data, err := Asset(path) | ||
if err != nil { | ||
panic(fmt.Errorf("unable to read crd file %s: %s", path, err)) | ||
} | ||
|
||
u := &unstructured.Unstructured{} | ||
reader := bytes.NewReader(data) | ||
decoder := yaml.NewYAMLOrJSONDecoder(reader, 30) | ||
if err = decoder.Decode(u); err != nil { | ||
panic(fmt.Errorf("crd unmarshaling failed: %s", err)) | ||
} | ||
|
||
// Step through unversioned type to support v1beta1 -> v1 | ||
unversioned := &apiextensions.CustomResourceDefinition{} | ||
if err = scheme.Convert(u, unversioned, nil); err != nil { | ||
panic(fmt.Errorf("failed to convert crd: %s\nto v1: %s", u, err)) | ||
} | ||
|
||
crd := &apiextensionsv1.CustomResourceDefinition{} | ||
if err = scheme.Convert(unversioned, crd, nil); err != nil { | ||
panic(fmt.Errorf("failed to convert crd: %s\nto v1: %s", u, err)) | ||
} | ||
|
||
return crd | ||
} | ||
|
||
var ( | ||
lock sync.Mutex | ||
|
||
// loaded stores previously unmarshaled CustomResourceDefinitions indexed by their file descriptor. | ||
loaded = map[crdFile]*apiextensionsv1.CustomResourceDefinition{} | ||
// scheme provides conversions between type versions. | ||
scheme = runtime.NewScheme() | ||
) | ||
|
||
func init() { | ||
// Add conversions between CRD versions | ||
install.Install(scheme) | ||
} | ||
|
||
// getCRD lazily loads and returns the CustomResourceDefinition unmarshaled from a file. | ||
func getCRD(file crdFile) *apiextensionsv1.CustomResourceDefinition { | ||
lock.Lock() | ||
defer lock.Unlock() | ||
|
||
if crd, ok := loaded[file]; ok && crd != nil { | ||
return crd | ||
} | ||
|
||
// Unmarshal and memoize | ||
crd := file.mustUnmarshal() | ||
loaded[file] = crd | ||
|
||
return crd | ||
} | ||
|
||
// TODO(njhale): codegen this. | ||
|
||
// CatalogSource returns a copy of the CustomResourceDefinition for the latest version of the CatalogSource API. | ||
func CatalogSource() *apiextensionsv1.CustomResourceDefinition { | ||
return getCRD("operators.coreos.com_catalogsources.yaml").DeepCopy() | ||
} | ||
|
||
// ClusterServiceVersion returns a copy of the CustomResourceDefinition for the latest version of the ClusterServiceVersion API. | ||
func ClusterServiceVersion() *apiextensionsv1.CustomResourceDefinition { | ||
return getCRD("operators.coreos.com_clusterserviceversions.yaml").DeepCopy() | ||
} | ||
|
||
// InstallPlan returns a copy of the CustomResourceDefinition for the latest version of the InstallPlan API. | ||
func InstallPlan() *apiextensionsv1.CustomResourceDefinition { | ||
return getCRD("operators.coreos.com_installplans.yaml").DeepCopy() | ||
} | ||
|
||
// OperatorGroup returns a copy of the CustomResourceDefinition for the latest version of the OperatorGroup API. | ||
func OperatorGroup() *apiextensionsv1.CustomResourceDefinition { | ||
return getCRD("operators.coreos.com_operatorgroups.yaml").DeepCopy() | ||
} | ||
|
||
// Operator returns a copy of the CustomResourceDefinition for the latest version of the Operator API. | ||
func Operator() *apiextensionsv1.CustomResourceDefinition { | ||
return getCRD("operators.coreos.com_operators.yaml").DeepCopy() | ||
} | ||
|
||
// Subscription returns a copy of the CustomResourceDefinition for the latest version of the Subscription API. | ||
func Subscription() *apiextensionsv1.CustomResourceDefinition { | ||
return getCRD("operators.coreos.com_subscriptions.yaml").DeepCopy() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package crds | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
) | ||
|
||
var emptyCRD = &apiextensionsv1.CustomResourceDefinition{} | ||
|
||
func TestGetters(t *testing.T) { | ||
tests := []struct { | ||
description string | ||
get func() *apiextensionsv1.CustomResourceDefinition | ||
}{ | ||
{ | ||
description: "CatalogSource", | ||
get: CatalogSource, | ||
}, | ||
{ | ||
description: "ClusterServiceVersion", | ||
get: ClusterServiceVersion, | ||
}, | ||
{ | ||
description: "InstallPlan", | ||
get: InstallPlan, | ||
}, | ||
{ | ||
description: "OperatorGroup", | ||
get: OperatorGroup, | ||
}, | ||
{ | ||
description: "Operator", | ||
get: Operator, | ||
}, | ||
{ | ||
description: "Subscription", | ||
get: Subscription, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.description, func(t *testing.T) { | ||
defer func() { | ||
if x := recover(); x != nil { | ||
t.Errorf("panic loading crd: %v", x) | ||
} | ||
}() | ||
|
||
crd := tt.get() | ||
if crd == nil || reflect.DeepEqual(crd, emptyCRD) { | ||
t.Error("loaded CustomResourceDefinition is empty") | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package crds contains CustomResourceDefinition manifests for operator-framework APIs. | ||
package crds |
Oops, something went wrong.