Skip to content

Commit

Permalink
Refactor Makefile to better support make help
Browse files Browse the repository at this point in the history
Add local `kind` target
  • Loading branch information
Baarsgaard authored and theSuess committed Oct 16, 2024
1 parent eb1e1a8 commit 0516043
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 48 deletions.
88 changes: 48 additions & 40 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
###
# NOTE: this section almost matches outputs out kubebuilder v3.7.0
###
# Current Operator version
VERSION ?= 5.14.0

Expand All @@ -15,13 +12,6 @@ ifeq ($(USE_IMAGE_DIGESTS), true)
BUNDLE_GEN_FLAGS += --use-image-digests
endif

# Set the Operator SDK version to use. By default, what is installed on the system is used.
# This is useful for CI or a project to utilize a specific version of the operator-sdk toolkit.
OPERATOR_SDK_VERSION ?= v1.32.0

OPM_VERSION ?= v1.23.2
YQ_VERSION ?= v4.35.2

# Read Grafana Image and Version from go code
GRAFANA_IMAGE := $(shell grep 'GrafanaImage' controllers/config/operator_constants.go | sed 's/.*"\(.*\)".*/\1/')
GRAFANA_VERSION := $(shell grep 'GrafanaVersion' controllers/config/operator_constants.go | sed 's/.*"\(.*\)".*/\1/')
Expand Down Expand Up @@ -74,23 +64,6 @@ all: manifests test api-docs
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)

.PHONY: yq
YQ = ./bin/yq
yq: ## Download yq locally if necessary.
ifeq (,$(wildcard $(YQ)))
ifeq (,$(shell which yq 2>/dev/null))
@{ \
set -e ;\
mkdir -p $(dir $(YQ)) ;\
OSTYPE=$(shell uname | awk '{print tolower($$0)}') && ARCH=$(shell go env GOARCH) && \
curl -sSLo $(YQ) https://github.com/mikefarah/yq/releases/download/$(YQ_VERSION)/yq_$${OSTYPE}_$${ARCH} ;\
chmod +x $(YQ) ;\
}
else
YQ = $(shell which yq)
endif
endif

##@ Development

.PHONY: manifests
Expand Down Expand Up @@ -161,6 +134,10 @@ deploy-chainsaw: manifests kustomize ## Deploy controller to the K8s cluster spe
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 -

.PHONY: start-kind
start-kind: kind ## Start kind cluster locally
@hack/kind/start-kind.sh

##@ Build Dependencies

## Location to install dependencies to
Expand All @@ -169,13 +146,22 @@ $(LOCALBIN):
mkdir -p $(LOCALBIN)

## Tool Binaries
OPERATOR_SDK ?= $(LOCALBIN)/operator-sdk
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
YQ = $(LOCALBIN)/yq
KIND = $(LOCALBIN)/kind

## Tool Versions
# Set the Operator SDK version to use. By default, what is installed on the system is used.
# This is useful for CI or a project to utilize a specific version of the operator-sdk toolkit.
OPERATOR_SDK_VERSION ?= v1.32.0
KUSTOMIZE_VERSION ?= v5.1.1
CONTROLLER_TOOLS_VERSION ?= v0.16.3
OPM_VERSION ?= v1.23.2
YQ_VERSION ?= v4.35.2
KIND_VERSION ?= v0.24.0

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
.PHONY: kustomize
Expand All @@ -194,7 +180,6 @@ $(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

.PHONY: operator-sdk
OPERATOR_SDK ?= $(LOCALBIN)/operator-sdk
operator-sdk: ## Download operator-sdk locally if necessary.
ifeq (,$(wildcard $(OPERATOR_SDK)))
ifeq (, $(shell which operator-sdk 2>/dev/null))
Expand All @@ -210,9 +195,37 @@ OPERATOR_SDK = $(shell which operator-sdk)
endif
endif

###
# END OF kubebuilder SECTION
###
.PHONY: yq
yq: ## Download yq locally if necessary.
ifeq (,$(wildcard $(YQ)))
ifeq (,$(shell which yq 2>/dev/null))
@{ \
set -e ;\
mkdir -p $(dir $(YQ)) ;\
OSTYPE=$(shell uname | awk '{print tolower($$0)}') && ARCH=$(shell go env GOARCH) && \
curl -sSLo $(YQ) https://github.com/mikefarah/yq/releases/download/$(YQ_VERSION)/yq_$${OSTYPE}_$${ARCH} ;\
chmod +x $(YQ) ;\
}
else
YQ = $(shell which yq)
endif
endif

.PHONY: kind
kind: ## Download kind locally if necessary.
ifeq (,$(wildcard $(KIND)))
ifeq (,$(shell which kind 2>/dev/null))
@{ \
set -e ;\
mkdir -p $(dir $(KIND)) ;\
OSTYPE=$(shell uname | awk '{print tolower($$0)}') && ARCH=$(shell go env GOARCH) && \
curl -sSLo $(KIND) https://github.com/kubernetes-sigs/kind/releases/download/$(KIND_VERSION)/kind-$${OSTYPE}-$${ARCH} ;\
chmod +x $(KIND) ;\
}
else
KIND = $(shell which kind)
endif
endif

# CHANNELS define the bundle channels used in the bundle.
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "candidate,fast,stable")
Expand Down Expand Up @@ -309,16 +322,14 @@ export KO_DOCKER_REPO ?= ko.local/grafana/grafana-operator
export KIND_CLUSTER_NAME ?= kind-grafana
export KUBECONFIG ?= ${HOME}/.kube/kind-grafana-operator

# If you want to push ko to your local Docker daemon
.PHONY: ko-build-local
ko-build-local: ko
ko-build-local: ko ## Build Docker image with KO
$(KO) build --sbom=none --bare

# If you want to push ko to your kind cluster
.PHONY: ko-build-kind
ko-build-kind: ko
$(KO) build --sbom=none --bare
kind load docker-image $(KO_DOCKER_REPO) --name $(KIND_CLUSTER_NAME)
ko-build-kind: ko-build-local ## Build and Load Docker image into kind cluster
$(KIND) load docker-image $(KO_DOCKER_REPO) --name $(KIND_CLUSTER_NAME)

helm-docs:
ifeq (, $(shell which helm-docs))
@{ \
Expand All @@ -330,9 +341,6 @@ else
HELM_DOCS=$(shell which helm-docs)
endif

start-kind:
@hack/kind/start-kind.sh

.PHONY: helm/docs
helm/docs: helm-docs
$(HELM_DOCS)
Expand Down
17 changes: 9 additions & 8 deletions hack/kind/start-kind.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
#!/usr/bin/env bash
KIND=${KIND:-kind}
KIND_CLUSTER_NAME=${KIND_CLUSTER_NAME:-kind-grafana}
KUBECONFIG=${KUBECONFIG:-~/.kube/kind-grafana-operator}
CRD_NS=${CRD_NS:-grafana-crds}

set -eu

# Find the script directory
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)

# Make sure there is no current cluster
echo "Delete existing cluster"
kind --kubeconfig="${KUBECONFIG}" delete cluster --name "${KIND_CLUSTER_NAME}" \
|| echo "There was no existing cluster"
${KIND} --kubeconfig="${KUBECONFIG}" delete cluster --name "${KIND_CLUSTER_NAME}" ||
echo "There was no existing cluster"

# Start kind cluster
echo ""
echo "###############################"
echo "# 1. Start kind cluster #"
echo "###############################"
kind --kubeconfig "${KUBECONFIG}" create cluster \
${KIND} --kubeconfig "${KUBECONFIG}" create cluster \
--name "${KIND_CLUSTER_NAME}" \
--wait 120s \
--config="${SCRIPT_DIR}/resources/cluster.yaml"
Expand All @@ -34,10 +35,10 @@ echo "###############################"
kubectl --kubeconfig="${KUBECONFIG}" \
apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/kind/deploy.yaml
kubectl --kubeconfig="${KUBECONFIG}" \
-n ingress-nginx \
wait deploy ingress-nginx-controller \
--for condition=Available \
--timeout=90s
-n ingress-nginx \
wait deploy ingress-nginx-controller \
--for condition=Available \
--timeout=90s

# Will install the CRD:s
echo ""
Expand Down

0 comments on commit 0516043

Please sign in to comment.