Skip to content

Commit

Permalink
Bump operator-sdk version to v1.13
Browse files Browse the repository at this point in the history
Refactor structure and rewrite e2e test acconding to
https://sdk.operatorframework.io/docs/building-operators/golang/migration

Fixes: pravega#443

Signed-off-by: akamyshnikova <[email protected]>
  • Loading branch information
AKamyshnikova committed Mar 1, 2022
1 parent b150f81 commit 3dc56c8
Show file tree
Hide file tree
Showing 68 changed files with 6,505 additions and 2,136 deletions.
11 changes: 7 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ COPY go.sum go.sum
# Download all dependencies.
RUN go mod download

RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o /src/${PROJECT_NAME} \
-ldflags "-X ${REPO_PATH}/pkg/version.Version=${VERSION} -X ${REPO_PATH}/pkg/version.GitSHA=${GIT_SHA}" \
/src/cmd/manager
# Copy the go source
COPY main.go main.go
COPY api/ api/
COPY controllers/ controllers/

# =============================================================================
# Build
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o /src/${PROJECT_NAME} \
-ldflags "-X ${REPO_PATH}/pkg/version.Version=${VERSION} -X ${REPO_PATH}/pkg/version.GitSHA=${GIT_SHA}" main.go

FROM ${DOCKER_REGISTRY:+$DOCKER_REGISTRY/}alpine:${ALPINE_VERSION} AS final

Expand Down
145 changes: 133 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@
# http://www.apache.org/licenses/LICENSE-2.0

SHELL=/bin/bash -o pipefail
# Current Operator version
VERSION ?= 0.0.1
# Default bundle image tag
BUNDLE_IMG ?= controller-bundle:$(VERSION)
# Options for 'bundle-build'
ifneq ($(origin CHANNELS), undefined)
BUNDLE_CHANNELS := --channels=$(CHANNELS)
endif
ifneq ($(origin DEFAULT_CHANNEL), undefined)
BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
endif
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)

# Image URL to use all building/pushing image targets
IMG ?= controller:latest
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true"

PROJECT_NAME=zookeeper-operator
EXPORTER_NAME=zookeeper-exporter
Expand All @@ -24,17 +41,109 @@ TEST_IMAGE=$(TEST_REPO)-testimages:$(VERSION)
DOCKER_TEST_PASS=testzkop@123
DOCKER_TEST_USER=testzkop
.PHONY: all build check clean test
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

# Install CRDs into a cluster
install: manifests kustomize
$(KUSTOMIZE) build config/crd | kubectl apply -f -

# Uninstall CRDs from a cluster
uninstall: manifests kustomize
$(KUSTOMIZE) build config/crd | kubectl delete -f -

crds: ## Generate CRDs
- make controller-gen
- $(CONTROLLER_GEN) crd paths=./api/... output:dir=./config/crd/bases schemapatch:manifests=./config/crd/bases


# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
deploy: manifests kustomize
cd config/manager && $(KUSTOMIZE) edit set image pravega/zookeeper-operator=$(TEST_IMAGE)
$(KUSTOMIZE) build config/default | kubectl apply -f -


# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
deploy-test: manifests kustomize
cd config/test
$(KUSTOMIZE) build config/test | kubectl apply -f -

# Undeploy controller in the configured Kubernetes cluster in ~/.kube/config
undeploy-test: manifests kustomize
cd config/test
$(KUSTOMIZE) build config/test | kubectl apply -f -

# Undeploy controller in the configured Kubernetes cluster in ~/.kube/config
undeploy:
$(KUSTOMIZE) build config/default | kubectl delete -f -

# Generate manifests e.g. CRD, RBAC etc.
manifests: controller-gen
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases

# Run go fmt against code
fmt:
go fmt ./...

# Run go vet against code
vet:
go vet ./...


# Build the docker image
docker-build: test
docker build . -t ${IMG}

# Push the docker image
docker-push:
docker push ${IMG}

# find or download controller-gen
# download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
@{ \
set -e ;\
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CONTROLLER_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/controller-tools/cmd/[email protected] ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif

kustomize:
ifeq (, $(shell which kustomize))
@{ \
set -e ;\
KUSTOMIZE_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$KUSTOMIZE_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/kustomize/kustomize/[email protected] ;\
rm -rf $$KUSTOMIZE_GEN_TMP_DIR ;\
}
KUSTOMIZE=$(GOBIN)/kustomize
else
KUSTOMIZE=$(shell which kustomize)
endif

all: generate check build

generate:
[[ ${OSDK_VERSION} == v0.19* ]] || ( echo "operator-sdk version 0.19 required" ; exit 1 )
operator-sdk generate crds --crd-version v1
env GOROOT=$(shell go env GOROOT) operator-sdk generate k8s
controller-gen
$(CONTROLLER_GEN) object paths="./..."
$(CONTROLLER_GEN) crd paths=./api/... output:dir=./config/crd/bases schemapatch:manifests=./config/crd/bases
# sync crd generated to helm-chart
echo '{{- define "crd.openAPIV3Schema" }}' > charts/zookeeper-operator/templates/_crd_openapiv3schema.tpl
echo 'openAPIV3Schema:' >> charts/zookeeper-operator/templates/_crd_openapiv3schema.tpl
sed -e '1,/openAPIV3Schema/d' deploy/crds/zookeeper.pravega.io_zookeeperclusters_crd.yaml | sed -n '/served: true/!p;//q' >> charts/zookeeper-operator/templates/_crd_openapiv3schema.tpl
sed -e '1,/openAPIV3Schema/d' config/crd/bases/zookeeper.pravega.io_zookeeperclusters_crd.yaml | sed -n '/served: true/!p;//q' >> charts/zookeeper-operator/templates/_crd_openapiv3schema.tpl
echo '{{- end }}' >> charts/zookeeper-operator/templates/_crd_openapiv3schema.tpl


Expand Down Expand Up @@ -77,19 +186,21 @@ test:

test-e2e: test-e2e-remote

test-e2e-remote: test-login
operator-sdk build $(TEST_IMAGE)
test-e2e-remote:
make test-login
docker build . -t $(TEST_IMAGE)
docker push $(TEST_IMAGE)
operator-sdk test local ./test/e2e --operator-namespace default \
--namespaced-manifest ./test/e2e/resources/rbac-operator.yaml \
--global-manifest deploy/crds/zookeeper.pravega.io_zookeeperclusters_crd.yaml \
--image $(TEST_IMAGE) --go-test-flags "-v -timeout 0"
make deploy
RUN_LOCAL=false go test -timeout 1h ./test/e2e...
make undeploy

test-e2e-local:
operator-sdk test local ./test/e2e --operator-namespace default --up-local --go-test-flags "-v -timeout 0"
make deploy-test
RUN_LOCAL=true go test -timeout 1h ./test/e2e...
make undeploy-test

run-local:
operator-sdk run local
go run ./main.go

login:
@docker login -u "$(DOCKER_USER)" -p "$(DOCKER_PASS)"
Expand Down Expand Up @@ -124,3 +235,13 @@ check-license:

update-kube-version:
./scripts/update_kube_version.sh ${KUBE_VERSION}
# Generate bundle manifests and metadata, then validate generated files.
bundle: manifests
operator-sdk generate kustomize manifests -q
kustomize build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
operator-sdk bundle validate ./bundle

# Build the bundle image.
bundle-build:
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .

10 changes: 10 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
domain: zookeeper.pravega.io
layout: go.kubebuilder.io/v2
repo: github.com/pravega/zookeeper-operator
resources:
- group: zookeeper.pravega.io
kind: ZookeeperCluster
version: v1beta1
version: 3-alpha
plugins:
go.operator-sdk.io/v2-alpha: {}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/pravega/zookeeper-operator/pkg/apis/zookeeper/v1beta1"
"github.com/pravega/zookeeper-operator/api/v1beta1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down
File renamed without changes.
36 changes: 36 additions & 0 deletions api/v1beta1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package v1beta1 contains API Schema definitions for the zookeeper.pravega.io v1beta1 API group
// +kubebuilder:object:generate=true
// +groupName=zookeeper.pravega.io.zookeeper.pravega.io
package v1beta1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "zookeeper.pravega.io", Version: "v1beta1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/pravega/zookeeper-operator/pkg/apis/zookeeper/v1beta1"
"github.com/pravega/zookeeper-operator/api/v1beta1"
)

var _ = Describe("ZookeeperCluster Status", func() {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ type Probe struct {
// +kubebuilder:printcolumn:name="Internal Endpoint",type=string,JSONPath=`.status.internalClientEndpoint`,description="Client endpoint internal to cluster network"
// +kubebuilder:printcolumn:name="External Endpoint",type=string,JSONPath=`.status.externalClientEndpoint`,description="Client endpoint external to cluster network via LoadBalancer"
// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +k8s:openapi-gen=true

// ZookeeperCluster is the Schema for the zookeeperclusters API
Expand Down Expand Up @@ -775,7 +774,7 @@ const (
VolumeReclaimPolicyDelete VolumeReclaimPolicy = "Delete"
)

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root=true

// ZookeeperClusterList contains a list of ZookeeperCluster
type ZookeeperClusterList struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ package v1beta1_test
import (
"fmt"

"github.com/pravega/zookeeper-operator/pkg/apis/zookeeper/v1beta1"
"github.com/pravega/zookeeper-operator/api/v1beta1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down
Loading

0 comments on commit 3dc56c8

Please sign in to comment.