Skip to content

Commit 3973134

Browse files
authored
Merge pull request #24 from njhale/op-disc-2
feat(operators): add v2alpha1 operator types
2 parents d9f7449 + 6acca52 commit 3973134

File tree

428 files changed

+77551
-4630
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

428 files changed

+77551
-4630
lines changed

Makefile

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ endif
1212
REPO = github.com/operator-framework/api
1313
BUILD_PATH = $(REPO)/cmd/operator-verify
1414
PKGS = $(shell go list ./... | grep -v /vendor/)
15+
YQ_INTERNAL := $(Q) go run $(MOD_FLAGS) ./vendor/github.com/mikefarah/yq/v2/
1516

1617
.PHONY: help
1718
help: ## Show this help screen
@@ -23,7 +24,7 @@ help: ## Show this help screen
2324

2425
.PHONY: install
2526

26-
install: ## Build & install the operator-verify
27+
install: ## Build & install operator-verify
2728

2829
$(Q)go install \
2930
-gcflags "all=-trimpath=${GOPATH}" \
@@ -35,22 +36,35 @@ install: ## Build & install the operator-verify
3536
$(BUILD_PATH)
3637

3738
# Code management.
38-
.PHONY: format tidy clean
39+
.PHONY: format tidy clean vendor generate
3940

4041
format: ## Format the source code
4142
$(Q)go fmt $(PKGS)
4243

4344
tidy: ## Update dependencies
4445
$(Q)go mod tidy -v
4546

47+
vendor: tidy ## Update vendor directory
48+
$(Q)go mod vendor
49+
4650
clean: ## Clean up the build artifacts
4751
$(Q)rm -rf build
4852

49-
##############################
50-
# Tests #
51-
##############################
53+
generate: controller-gen ## Generate code
54+
$(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths=./...
55+
56+
manifests: controller-gen ## Generate manifests e.g. CRD, RBAC etc
57+
@# Handle >v1 APIs
58+
$(CONTROLLER_GEN) crd paths=./pkg/operators/v2alpha1... output:crd:dir=./crds
59+
60+
@# Handle <=v1 APIs
61+
$(CONTROLLER_GEN) schemapatch:manifests=./crds output:dir=./crds paths=./pkg/operators/...
62+
63+
@# Preserve unknown fields on the CSV spec (prevents install strategy from being pruned)
64+
$(YQ_INTERNAL) w --inplace ./crds/operators.coreos.com_clusterserviceversions.yaml spec.validation.openAPIV3Schema.properties.spec.properties.install.properties.spec.properties.deployments.items.properties.spec.properties.template.properties.metadata.x-kubernetes-preserve-unknown-fields true
5265

53-
##@ Tests
66+
@# Update embedded CRD files.
67+
@go generate ./crds/...
5468

5569
# Static tests.
5670
.PHONY: test test-unit
@@ -59,4 +73,11 @@ test: test-unit ## Run the tests
5973

6074
TEST_PKGS:=$(shell go list ./...)
6175
test-unit: ## Run the unit tests
62-
$(Q)go test -short ${TEST_PKGS}
76+
$(Q)go test -count=1 -short ${TEST_PKGS}
77+
78+
# Utilities.
79+
.PHONY: controller-gen
80+
81+
controller-gen: vendor ## Find or download controller-gen
82+
CONTROLLER_GEN=$(Q)go run -mod=vendor ./vendor/sigs.k8s.io/controller-tools/cmd/controller-gen
83+

crds/defs.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
}

crds/defs_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package crds
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
8+
)
9+
10+
var emptyCRD = &apiextensionsv1.CustomResourceDefinition{}
11+
12+
func TestGetters(t *testing.T) {
13+
tests := []struct {
14+
description string
15+
get func() *apiextensionsv1.CustomResourceDefinition
16+
}{
17+
{
18+
description: "CatalogSource",
19+
get: CatalogSource,
20+
},
21+
{
22+
description: "ClusterServiceVersion",
23+
get: ClusterServiceVersion,
24+
},
25+
{
26+
description: "InstallPlan",
27+
get: InstallPlan,
28+
},
29+
{
30+
description: "OperatorGroup",
31+
get: OperatorGroup,
32+
},
33+
{
34+
description: "Operator",
35+
get: Operator,
36+
},
37+
{
38+
description: "Subscription",
39+
get: Subscription,
40+
},
41+
}
42+
for _, tt := range tests {
43+
t.Run(tt.description, func(t *testing.T) {
44+
defer func() {
45+
if x := recover(); x != nil {
46+
t.Errorf("panic loading crd: %v", x)
47+
}
48+
}()
49+
50+
crd := tt.get()
51+
if crd == nil || reflect.DeepEqual(crd, emptyCRD) {
52+
t.Error("loaded CustomResourceDefinition is empty")
53+
}
54+
})
55+
}
56+
}

crds/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package crds contains CustomResourceDefinition manifests for operator-framework APIs.
2+
package crds

0 commit comments

Comments
 (0)