Skip to content

Commit

Permalink
chore: update Makefile (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
nakamasato authored Apr 6, 2023
1 parent bde7858 commit f884409
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 64 deletions.
28 changes: 18 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ golangci-lint run ./...
```
kubectl get -k config/samples
NAME HOST ADMINUSER USERCOUNT
mysql.mysql.nakamasato.com/mysql-sample localhost root 1
NAME HOST ADMINUSER CONNECTED USERCOUNT DBCOUNT REASON
mysql.mysql.nakamasato.com/mysql-sample localhost root true 1 0 Ping succeded and updated MySQLClients
NAME PHASE REASON
mysqldb.mysql.nakamasato.com/sample-db Ready Database successfully created
NAME PHASE REASON
mysqluser.mysql.nakamasato.com/nakamasato Ready Both secret and mysql user are successfully created.
NAME MYSQLUSER SECRET PHASE REASON
mysqluser.mysql.nakamasato.com/nakamasato true true Ready Both secret and mysql user are successfully created.
```
1. Confirm MySQL user is created in MySQL container.
Expand Down Expand Up @@ -146,9 +146,13 @@ docker rm -f $(docker ps | grep mysql | head -1 |awk '{print $1}')
1. Check Custom resources
```bash
kubectl get -k config/samples-on-k8s
```
```
NAME HOST ADMINUSER CONNECTED REASON USERCOUNT DBCOUNT
mysql.mysql.nakamasato.com/mysql-sample mysql.default root true Ping succeded and updated MySQLClients 1 0
NAME HOST ADMINUSER CONNECTED USERCOUNT DBCOUNT REASON
mysql.mysql.nakamasato.com/mysql-sample mysql.default root true 1 0 Ping succeded and updated MySQLClients
NAME MYSQLUSER SECRET PHASE REASON
mysqluser.mysql.nakamasato.com/nakamasato true true Ready Both secret and mysql user are successfully created.
Expand All @@ -158,6 +162,8 @@ docker rm -f $(docker ps | grep mysql | head -1 |awk '{print $1}')
```bash
kubectl exec -it $(kubectl get po | grep mysql | head -1 | awk '{print $1}') -- mysql -uroot -ppassword -e 'select User, Host from mysql.user where User = "nakamasato";'
```
```
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+------+
| User | Host |
Expand All @@ -178,13 +184,15 @@ docker rm -f $(docker ps | grep mysql | head -1 |awk '{print $1}')
kubectl delete -k config/samples-on-k8s
```
If getting stuck in deletion:
<details><summary>If getting stuck in deletion</summary>
```
kubectl exec -it $(kubectl get po | grep mysql | head -1 | awk '{print $1}') -- mysql -uroot -ppassword -e 'delete from mysql.user where User = "nakamasato";'
kubectl patch mysqluser nakamasato -p '{"metadata":{"finalizers": []}}' --type=merge
```
</details>
1. Stop the `skaffold dev` by `ctrl-c` -> will clean up the controller, CRDs, and installed resources.
Expand Down Expand Up @@ -218,7 +226,7 @@ docker rm -f $(docker ps | grep mysql | head -1 |awk '{print $1}')
1. Install and run operator
```
make install
go run main.go --cloud-secret-manager gcp
PRJECT_ID=$PROJECT_ID go run main.go --cloud-secret-manager gcp
```
1. Create custom resources
```
Expand All @@ -227,8 +235,8 @@ docker rm -f $(docker ps | grep mysql | head -1 |awk '{print $1}')
1. Check
```
kubectl get -k config/samples-wtih-gcp-secretmanager
NAME HOST ADMINUSER CONNECTED REASON USERCOUNT DBCOUNT
mysql.mysql.nakamasato.com/mysql-sample localhost root true Ping succeded and updated MySQLClients 1 0
NAME HOST ADMINUSER CONNECTED USERCOUNT DBCOUNT REASON
mysql.mysql.nakamasato.com/mysql-sample localhost root true 1 0 Ping succeded and updated MySQLClients
NAME PHASE REASON
mysqldb.mysql.nakamasato.com/sample-db Ready Database successfully created
Expand Down
151 changes: 106 additions & 45 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,27 @@ BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
#
# For example, running 'make bundle-build bundle-push catalog-build catalog-push' will build and push both
# nakamasato.com/mysql-operator-bundle:$VERSION and nakamasato.com/mysql-operator-catalog:$VERSION.
IMAGE_TAG_BASE ?= nakamasato.com/mysql-operator
IMAGE_TAG_BASE ?= nakamasato/mysql-operator

# BUNDLE_IMG defines the image:tag used for the bundle.
# You can use it as an arg. (E.g make bundle-build BUNDLE_IMG=<some-registry>/<project-name-bundle>:<tag>)
BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION)

# BUNDLE_GEN_FLAGS are the flags passed to the operator-sdk generate bundle command
BUNDLE_GEN_FLAGS ?= -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)

# USE_IMAGE_DIGESTS defines if images are resolved via tags or digests
# You can enable this value if you would like to use SHA Based Digests
# To enable set flag to true
USE_IMAGE_DIGESTS ?= false
ifeq ($(USE_IMAGE_DIGESTS), true)
BUNDLE_GEN_FLAGS += --use-image-digests
endif

# Image URL to use all building/pushing image targets
IMG ?= ghcr.io/nakamasato/mysql-operator:latest
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd"
IMG ?= ghcr.io/nakamasato/mysql-operator
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.24
ENVTEST_K8S_VERSION = 1.26.0

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand All @@ -50,11 +59,11 @@ GOBIN=$(shell go env GOBIN)
endif

# Setting SHELL to bash allows bash commands to be executed by recipes.
# This is a requirement for 'setup-envtest.sh' in the test target.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec

.PHONY: all
all: build

##@ General
Expand All @@ -70,91 +79,143 @@ all: build
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php

.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Development

.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

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

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

test: ginkgo manifests generate fmt vet envtest ## Run tests.
.PHONY: test
test: manifests generate fmt vet envtest ginkgo ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" $(GINKGO) -cover -coverprofile cover.out -covermode=atomic -skip-package=e2e ./...
# KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out

##@ Build

build: generate fmt vet ## Build manager binary.
.PHONY: build
build: manifests generate fmt vet ## Build manager binary.
go build -o bin/manager main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
go run ./main.go

# If you wish built the manager image targeting other platforms you can use the --platform flag.
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
.PHONY: docker-build
docker-build: test ## Build docker image with the manager.
docker build -t ${IMG} .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
docker push ${IMG}

# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
# - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
# To properly provided solutions that supports more than one platform you should use this option.
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
.PHONY: docker-buildx
docker-buildx: test ## Build and push docker image for the manager for cross-platform support
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
- docker buildx create --name project-v3-builder
docker buildx use project-v3-builder
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
- docker buildx rm project-v3-builder
rm Dockerfile.cross

##@ Deployment

ifndef ignore-not-found
ignore-not-found = false
endif

.PHONY: install
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl apply -f -

uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl delete -f -
.PHONY: uninstall
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -

.PHONY: deploy
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default | kubectl apply -f -

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


CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
controller-gen: ## Download controller-gen locally if necessary.
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])

KUSTOMIZE = $(shell pwd)/bin/kustomize
kustomize: ## Download kustomize locally if necessary.
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])

ENVTEST = $(shell pwd)/bin/setup-envtest
envtest: ## Download envtest-setup locally if necessary.
$(call go-get-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)

GINKGO = $(shell pwd)/bin/ginkgo
.PHONY: undeploy
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -

##@ Build Dependencies

## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)

## Tool Binaries
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
GINKGO ?= $(LOCALBIN)/ginkgo

## Tool Versions
KUSTOMIZE_VERSION ?= v4.5.2
CONTROLLER_TOOLS_VERSION ?= v0.11.1
GINKGO_VERSION ?= v2.9.2

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
.PHONY: kustomize
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
$(KUSTOMIZE): $(LOCALBIN)
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
rm -rf $(LOCALBIN)/kustomize; \
fi
test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }

.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
$(CONTROLLER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)

.PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

.PHONY: ginkgo
ginkgo:
$(call go-get-tool,$(GINKGO),github.com/onsi/ginkgo/v2/[email protected])

# go-get-tool will 'go install' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-get-tool
@[ -f $(1) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
go mod init tmp ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef
test -s $(LOCALBIN)/ginkgo && $(LOCALBIN)/ginkgo version | grep -q $(GINKGO_VERSION) || \
GOBIN=$(LOCALBIN) go install github.com/onsi/ginkgo/v2/ginkgo@$(GINKGO_VERSION)

.PHONY: bundle
bundle: manifests kustomize ## Generate bundle manifests and metadata, then validate generated files.
operator-sdk generate kustomize manifests -q
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle $(BUNDLE_GEN_FLAGS)
operator-sdk bundle validate ./bundle

.PHONY: bundle-build
Expand All @@ -174,7 +235,7 @@ ifeq (,$(shell which opm 2>/dev/null))
set -e ;\
mkdir -p $(dir $(OPM)) ;\
OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \
curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.15.1/$${OS}-$${ARCH}-opm ;\
curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.23.0/$${OS}-$${ARCH}-opm ;\
chmod +x $(OPM) ;\
}
else
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/mysql_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ type MySQLStatus struct {
//+kubebuilder:printcolumn:name="Host",type=string,JSONPath=`.spec.host`
//+kubebuilder:printcolumn:name="AdminUser",type=string,JSONPath=`.spec.admin_user.name`
//+kubebuilder:printcolumn:name="Connected",type=boolean,JSONPath=`.status.connected`
//+kubebuilder:printcolumn:name="Reason",type=string,JSONPath=`.status.reason`
//+kubebuilder:printcolumn:name="UserCount",type="integer",JSONPath=".status.userCount",description="The number of MySQLUsers that belongs to the MySQL"
//+kubebuilder:printcolumn:name="DBCount",type="integer",JSONPath=".status.dbCount",description="The number of MySQLDBs that belongs to the MySQL"
//+kubebuilder:printcolumn:name="Reason",type=string,JSONPath=`.status.reason`

// MySQL is the Schema for the mysqls API
type MySQL struct {
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/mysql.nakamasato.com_mysqldbs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.9.0
controller-gen.kubebuilder.io/version: v0.11.1
creationTimestamp: null
name: mysqldbs.mysql.nakamasato.com
spec:
Expand Down
8 changes: 4 additions & 4 deletions config/crd/bases/mysql.nakamasato.com_mysqls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.9.0
controller-gen.kubebuilder.io/version: v0.11.1
creationTimestamp: null
name: mysqls.mysql.nakamasato.com
spec:
Expand All @@ -25,9 +25,6 @@ spec:
- jsonPath: .status.connected
name: Connected
type: boolean
- jsonPath: .status.reason
name: Reason
type: string
- description: The number of MySQLUsers that belongs to the MySQL
jsonPath: .status.userCount
name: UserCount
Expand All @@ -36,6 +33,9 @@ spec:
jsonPath: .status.dbCount
name: DBCount
type: integer
- jsonPath: .status.reason
name: Reason
type: string
name: v1alpha1
schema:
openAPIV3Schema:
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/mysql.nakamasato.com_mysqlusers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.9.0
controller-gen.kubebuilder.io/version: v0.11.1
creationTimestamp: null
name: mysqlusers.mysql.nakamasato.com
spec:
Expand Down
4 changes: 2 additions & 2 deletions controllers/mysql_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (r *MySQLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
mysql.Status.Reason = "Ping succeded and updated MySQLClients"
if err := r.Status().Update(ctx, mysql); err != nil {
log.Error(err, "failed to update status")
return ctrl.Result{RequeueAfter: 5 * time.Second}, nil
return ctrl.Result{RequeueAfter: time.Second}, nil
}

// Add a finalizer if not exists
Expand Down Expand Up @@ -135,7 +135,7 @@ func (r *MySQLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
}
} else {
log.Info("Could not complete finalizer. waiting another 5 seconds")
return ctrl.Result{RequeueAfter: 5 * time.Second}, nil
return ctrl.Result{RequeueAfter: time.Second}, nil
}
}
return ctrl.Result{}, nil
Expand Down
Loading

0 comments on commit f884409

Please sign in to comment.