Skip to content

Commit

Permalink
Evolve Makefile, improve developer experience, update documentation (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddenmarten committed Apr 2, 2024
1 parent 4f114fe commit 3e172b5
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/make-test-e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ jobs:
- uses: helm/kind-action@v1.9.0
with:
cluster_name: kind
- run: make test-e2e
- run: make test-e2e KIND_CLUSTER_NAME=kind
39 changes: 23 additions & 16 deletions HOW-TO-START-DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,29 @@
## Build your develop environment

### Easy way
1. Download and install [kind](https://kind.sigs.k8s.io/docs/user/quick-start/#creating-a-cluster).
2. Create lind cluster `kind create cluster --name etcd-operator-kind`.
3. Switch kubectl context to kind `kubectl config use-context kind-etcd-operator-kind`. Be attentive to avoid damaging your production environment.
4. Install cert-manager (it creates certificate k8s secrets). Refer to the [docs](https://cert-manager.io/docs/installation/helm/#4-install-cert-manager).
5. Build docker image *controller:latest* `make docker-build`.
6. Retag image to upload to kind cluster with correct name `docker tag controller:latest ghcr.io/aenix-io/etcd-operator:latest`.
7. Load image to kind cluster `kind load docker-image --name etcd-operator-kind ghcr.io/aenix-io/etcd-operator:latest`.
8. Install CRDs `make install`.
9. Deploy operator, RBAC, webhook certs `make deploy`.

To deploy your code changes
1. Rebuild the image `make docker-build`.
2. Retag image to upload to kind cluster with correct name `docker tag controller:latest ghcr.io/aenix-io/etcd-operator:latest`.
3. Load image to kind cluster `kind load docker-image --name etcd-operator-kind ghcr.io/aenix-io/etcd-operator:latest`.
4. Redeploy yaml manifests if necessary `make deploy`.
5. Restart etcd-operator `kubectl rollout restart -n etcd-operator-system deploy/etcd-operator-controller-manager`.
1. Create and prepare kind cluster:
```shell
make kind-prepare
```
2. Install CRDs into kind cluster
```shell
make install
```

3. Build image and load it into kind cluster, deploy etcd-operator, RBAC, webhook certs
```shell
make deploy
```

4. To deploy your code changes, redeploy etcd-operator:
```shell
make redeploy
```

5. To clean up after all, delete kind cluster:
```shell
make kind-delete
```

### Advanced way
Using *easy way* you will not be able to debug golang code locally and every change will be necessary to build as an image, upload to the cluster and then restart operator.
Expand Down
60 changes: 41 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ run: manifests generate fmt vet ## Run a controller from your host.
docker-build: ## Build docker image with the manager.
$(CONTAINER_TOOL) build -t ${IMG} .

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

# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
Expand Down Expand Up @@ -127,6 +123,12 @@ build-installer: manifests generate kustomize ## Generate a consolidated YAML wi

##@ Deployment

KIND_CLUSTER_NAME ?= etcd-operator-kind
NAMESPACE ?= etcd-operator-system

PROMETHEUS_OPERATOR_VERSION ?= v0.72.0
CERT_MANAGER_VERSION ?= v1.14.4

ifndef ignore-not-found
ignore-not-found = false
endif
Expand All @@ -137,30 +139,44 @@ install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~

.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 -
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete -n $(NAMESPACE) --ignore-not-found=$(ignore-not-found) -f -

.PHONY: deploy
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
deploy: manifests kustomize kind-load ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image ghcr.io/aenix-io/etcd-operator=${IMG}
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
$(KUSTOMIZE) build config/default | $(KUBECTL) -n $(NAMESPACE) apply -f -

.PHONY: undeploy
undeploy: kustomize ## 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 -
$(KUSTOMIZE) build config/default | $(KUBECTL) delete -n $(NAMESPACE) --ignore-not-found=$(ignore-not-found) -f -

# Build and upload docker image to the local Kind cluster
.PHONY: docker-load
docker-load: docker-build
kind load docker-image ${IMG} --name etcd-operator-kind

# Redeploy controller with new docker image
.PHONY: redeploy
redeploy: manifests kustomize docker-build docker-load
$(KUBECTL) config use-context kind-etcd-operator-kind
# deploy configs
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
redeploy: deploy ## Redeploy controller with new docker image.
# force recreate pods
$(KUBECTL) rollout restart -n etcd-operator-system deploy/etcd-operator-controller-manager
$(KUBECTL) rollout restart -n $(NAMESPACE) deploy/etcd-operator-controller-manager

.PHONY: kind-load
kind-load: docker-build kind ## Build and upload docker image to the local Kind cluster.
$(KIND) load docker-image ${IMG} --name $(KIND_CLUSTER_NAME)

.PHONY: kind-create
kind-create: kind ## Create kubernetes cluster using Kind.
@if ! $(KIND) get clusters | grep -q $(KIND_CLUSTER_NAME); then \
$(KIND) create cluster --name $(KIND_CLUSTER_NAME); \
fi

.PHONY: kind-delete
kind-delete: kind ## Create kubernetes cluster using Kind.
@if $(KIND) get clusters | grep -q $(KIND_CLUSTER_NAME); then \
$(KIND) delete cluster --name $(KIND_CLUSTER_NAME); \
fi

.PHONY: kind-prepare
kind-prepare: kind-create
# Install prometheus operator
$(KUBECTL) apply --server-side -f "https://github.com/prometheus-operator/prometheus-operator/releases/download/$(PROMETHEUS_OPERATOR_VERSION)/bundle.yaml"
# Install cert-manager operator
$(KUBECTL) apply --server-side -f "https://github.com/jetstack/cert-manager/releases/download/$(CERT_MANAGER_VERSION)/cert-manager.yaml"

##@ Dependencies

Expand All @@ -175,12 +191,14 @@ KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
KIND ?= $(LOCALBIN)/kind

## Tool Versions
KUSTOMIZE_VERSION ?= v5.3.0
CONTROLLER_TOOLS_VERSION ?= v0.14.0
ENVTEST_VERSION ?= latest
GOLANGCI_LINT_VERSION ?= v1.54.2
KIND_VERSION ?= v0.22.0

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"

Expand All @@ -204,3 +222,7 @@ envtest: $(LOCALBIN)
golangci-lint: $(LOCALBIN)
@test -x $(GOLANGCI_LINT) && $(GOLANGCI_LINT) version | grep -q $(GOLANGCI_LINT_VERSION) || \
GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)

kind: $(LOCALBIN)
@test -x $(KIND) && $(KIND) version | grep -q $(KIND_VERSION) || \
GOBIN=$(LOCALBIN) go install sigs.k8s.io/kind@$(KIND_VERSION)
19 changes: 1 addition & 18 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,8 @@ var _ = Describe("controller", Ordered, func() {
var controllerPodName string
var err error

// projectimage stores the name of the image used in the example
var projectimage = "example.com/etcd-operator:v0.0.1"

By("building the manager(Operator) image")
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectimage))
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("loading the the manager(Operator) image on Kind")
err = utils.LoadImageToKindClusterWithName(projectimage)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("installing CRDs")
cmd = exec.Command("make", "install")
// TODO: Handle CRD installation error
_, _ = utils.Run(cmd)

By("deploying the controller-manager")
cmd = exec.Command("make", "deploy", fmt.Sprintf("IMG=%s", projectimage))
cmd := exec.Command("make", "deploy")
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

Expand Down
4 changes: 2 additions & 2 deletions test/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import (
)

const (
prometheusOperatorVersion = "v0.68.0"
prometheusOperatorVersion = "v0.72.0"
prometheusOperatorURL = "https://github.com/prometheus-operator/prometheus-operator/" +
"releases/download/%s/bundle.yaml"

certmanagerVersion = "v1.5.3"
certmanagerVersion = "v1.14.4"
certmanagerURLTmpl = "https://github.com/jetstack/cert-manager/releases/download/%s/cert-manager.yaml"
)

Expand Down

0 comments on commit 3e172b5

Please sign in to comment.