diff --git a/Makefile b/Makefile index a75634481d..e2b353051d 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,7 @@ -# All make targets should be defined in Makefile.targets.mk # All make targets should be implemented in tools/make/*.mk # ==================================================================================================== # Supported Targets: (Run `make help` to see more information) # ==================================================================================================== -## build: Build envoy-gateway for host platform. See Option PLATFORM and BINS. -## image: Build docker images for host platform. See Option PLATFORM and BINS. -## push: Push docker images to registry. -## build.multiarch: Build envoy-gateway for multiple platforms. See Option PLATFORMS and IMAGES. -## image.multiarch: Build docker images for multiple platforms. See Option PLATFORMS and IMAGES. -## push.multiarch: Push docker images for multiple platforms to registry. -## lint: Run all linter of code sources, including golint, yamllint and codespell. -## test: Run all Go test of code sources. -## format: Format codes style with mod tidy, gofmt and goimports. -## clean: Remove all files that are created by building. -# ==================================================================================================== # This is a wrapper around `make` so it can run # directly on the host or inside a container @@ -39,5 +27,5 @@ ifeq ($(MAKE_IN_DOCKER), 1) @$(DOCKER_RUN_CMD) $(DOCKER_BUILDER_IMAGE):$(DOCKER_BUILDER_TAG) MAKE_IN_DOCKER=0 $@ else # Run make locally - @$(MAKE) -f Makefile.targets.mk $@ + @$(MAKE) -f tools/make/common.mk $@ endif diff --git a/Makefile.targets.mk b/Makefile.targets.mk deleted file mode 100644 index 27aaacfe46..0000000000 --- a/Makefile.targets.mk +++ /dev/null @@ -1,123 +0,0 @@ -# ==================================================================================================== -# ROOT Options: -# ==================================================================================================== - -ROOT_PACKAGE=github.com/envoyproxy/gateway - -# ==================================================================================================== -# Includes: -# ==================================================================================================== -include tools/make/common.mk -include tools/make/golang.mk -include tools/make/image.mk -include tools/make/lint.mk - -# ==================================================================================================== -# Targets: -# ==================================================================================================== - -# ==================================================================================================== -## build: Build envoy-gateway for host platform. See Option PLATFORM and BINS. -# ==================================================================================================== -.PHONY: build -build: - @$(MAKE) go.build - -# ==================================================================================================== -## image: Build docker images for host platform. See Option PLATFORM and BINS. -# ==================================================================================================== -.PHONY: image -image: - @$(MAKE) image.build - -# ==================================================================================================== -## push: Push docker images to registry. -# ==================================================================================================== -.PHONY: push -push: - @$(MAKE) image.push - -# ==================================================================================================== -## build.multiarch: Build envoy-gateway for multiple platforms. See Option PLATFORMS and IMAGES. -# ==================================================================================================== -.PHONY: build.multiarch -build.multiarch: - @$(MAKE) go.build.multiarch - -# ==================================================================================================== -## image.multiarch: Build docker images for multiple platforms. See Option PLATFORMS and IMAGES. -# ==================================================================================================== -.PHONY: image.multiarch -image.multiarch: - @$(MAKE) image.build.multiarch - -# ==================================================================================================== -## push.multiarch: Push docker images for multiple platforms to registry. -# ==================================================================================================== -.PHONY: push.multiarch -push.multiarch: - @$(MAKE) image.push.multiarch - -# ==================================================================================================== -## lint: Run all linter of code sources, including golint, yamllint and codespell. -# ==================================================================================================== -.PHONY: lint -lint: - @$(MAKE) lint.golint lint.yamllint lint.codespell - -# ==================================================================================================== -## test: Run all Go test of code sources. -# ==================================================================================================== -.PHONY: test -test: - @$(MAKE) go.test.unit - -# ==================================================================================================== -## format: Format codes style with mod tidy, gofmt and goimports. -# ==================================================================================================== -.PHONY: format -format: - @$(MAKE) go.format - -# ==================================================================================================== -## clean: Remove all files that are created by building. -# ==================================================================================================== -.PHONY: clean -clean: - @$(MAKE) go.clean - -# ==================================================================================================== -# Usage -# ==================================================================================================== - -define USAGE_OPTIONS - -Options: - BINS The binaries to build. Default is all of cmd. - This option is available when using: make build/build.multiarch - Example: make build BINS="envoy-gateway" - IMAGES Backend images to make. Default is all of cmds. - This option is available when using: make image/image.multiarch/push/push.multiarch - Example: make image.multiarch IMAGES="envoy-gateway" - PLATFORM The specified platform to build. - This option is available when using: make build/image - Example: make build BINS="envoy-gateway" PLATFORM="linux_amd64" - Supported Platforms: linux_amd64 linux_arm64 darwin_amd64 darwin_arm64 - PLATFORMS The multiple platforms to build. - This option is available when using: make build.multiarch - Example: make image.multiarch IMAGES="envoy-gateway" PLATFORMS="linux_amd64 linux_arm64" - Default is "linux_amd64 linux_arm64 darwin_amd64 darwin_arm64". -endef -export USAGE_OPTIONS - -# ==================================================================================================== -# Help -# ==================================================================================================== - -## help: Show this help info. -.PHONY: help -help: Makefile - @echo "Envoy Gateway is an open source project for managing Envoy Proxy as a standalone or Kubernetes-based application gateway\n" - @echo "Usage: make ...\n\nTargets:" - @sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /' - @echo "$$USAGE_OPTIONS" diff --git a/tools/make/common.mk b/tools/make/common.mk index ba430c73e7..8d044c1bab 100644 --- a/tools/make/common.mk +++ b/tools/make/common.mk @@ -2,6 +2,20 @@ # # All make targets related to common variables are defined in this file. +# ==================================================================================================== +# ROOT Options: +# ==================================================================================================== + +ROOT_PACKAGE=github.com/envoyproxy/gateway + +# ==================================================================================================== +# Includes: +# ==================================================================================================== +include tools/make/golang.mk +include tools/make/image.mk +include tools/make/lint.mk +include tools/make/kube.mk + # Set Root Directory Path ifeq ($(origin ROOT_DIR),undefined) ROOT_DIR := $(abspath $(shell pwd -P)) @@ -51,3 +65,31 @@ endif ifeq (${BINS},) $(error Could not determine BINS, set ROOT_DIR or run in source dir) endif + +define USAGE_OPTIONS + +Options: + BINS The binaries to build. Default is all of cmd. + This option is available when using: make build/build.multiarch + Example: make build BINS="envoy-gateway" + IMAGES Backend images to make. Default is all of cmds. + This option is available when using: make image/image.multiarch/push/push.multiarch + Example: make image.multiarch IMAGES="envoy-gateway" + PLATFORM The specified platform to build. + This option is available when using: make build/image + Example: make build BINS="envoy-gateway" PLATFORM="linux_amd64" + Supported Platforms: linux_amd64 linux_arm64 darwin_amd64 darwin_arm64 + PLATFORMS The multiple platforms to build. + This option is available when using: make build.multiarch + Example: make image.multiarch IMAGES="envoy-gateway" PLATFORMS="linux_amd64 linux_arm64" + Default is "linux_amd64 linux_arm64 darwin_amd64 darwin_arm64". +endef +export USAGE_OPTIONS + +## help: Show this help info. +.PHONY: help +help: + @echo "Envoy Gateway is an open source project for managing Envoy Proxy as a standalone or Kubernetes-based application gateway\n" + @echo "Usage: make ...\n\nTargets:" + @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\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) + @echo "$$USAGE_OPTIONS" diff --git a/tools/make/golang.mk b/tools/make/golang.mk index 0a49d54415..7600d0a165 100644 --- a/tools/make/golang.mk +++ b/tools/make/golang.mk @@ -1,4 +1,4 @@ -# This is a wrapper to build and push golang binaries +# This is a wrapper to build golang binaries # # All make targets related to golang are defined in this file. @@ -22,12 +22,14 @@ go.build.%: # Build the envoy-gateway binaries in the hosted platforms. .PHONY: go.build -go.build: $(addprefix go.build., $(addprefix $(PLATFORM)., $(BINS))) +go.build: + @$(MAKE) $(addprefix go.build., $(addprefix $(PLATFORM)., $(BINS))) # Build the envoy-gateway binaries in multi platforms # It will build the linux/amd64, linux/arm64, darwin/amd64, darwin/arm64 binaries out. .PHONY: go.build.multiarch -go.build.multiarch: $(foreach p,$(PLATFORMS),$(addprefix go.build., $(addprefix $(p)., $(BINS)))) +go.build.multiarch: + @$(MAKE) $(foreach p,$(PLATFORMS),$(addprefix go.build., $(addprefix $(p)., $(BINS)))) .PHONY: go.test.unit go.test.unit: ## Run go unit tests @@ -56,3 +58,25 @@ go.format: go.format.verify @gofmt -s -w . @goimports -w -local $(ROOT_PACKAGE) . @go mod tidy + +##@ Golang + +.PHONY: build +build: ## Build envoy-gateway for host platform. See Option PLATFORM and BINS. + @$(MAKE) go.build + +.PHONY: build.multiarch +build.multiarch: ## Build envoy-gateway for multiple platforms. See Option PLATFORMS and IMAGES. + @$(MAKE) go.build.multiarch + +.PHONY: test +test: ## Run all Go test of code sources. + @$(MAKE) go.test.unit + +.PHONY: format +format: ## Format codes style with mod tidy, gofmt and goimports. + @$(MAKE) go.format + +.PHONY: clean +clean: ## Remove all files that are created during builds. + @$(MAKE) go.clean diff --git a/tools/make/image.mk b/tools/make/image.mk index f13aa4d9a1..2bcbb2faf3 100644 --- a/tools/make/image.mk +++ b/tools/make/image.mk @@ -23,6 +23,24 @@ ifeq (${IMAGES},) $(error Could not determine IMAGES, set ROOT_DIR or run in source dir) endif +##@ Image + +.PHONY: image +image: ## Build docker images for host platform. See Option PLATFORM and BINS. + @$(MAKE) image.build + +.PHONY: push +push: ## Push docker images to registry. + @$(MAKE) image.push + +.PHONY: image.multiarch +image.multiarch: ## Build docker images for multiple platforms. See Option PLATFORMS and IMAGES. + @$(MAKE) image.build.multiarch + +.PHONY: push.multiarch +push.multiarch: ## Push docker images for multiple platforms to registry. + @$(MAKE) image.push.multiarch + .PHONY: image.verify image.verify: $(eval API_VERSION := $(shell $(DOCKER) version | grep -E 'API version: {1,6}[0-9]' | head -n1 | awk '{print $$3} END { if (NR==0) print 0}' )) @@ -34,10 +52,12 @@ image.verify: fi .PHONY: image.build -image.build: image.verify $(addprefix image.build., $(addprefix $(IMAGE_PLAT)., $(IMAGES))) +image.build: image.verify + @$(MAKE) $(addprefix image.build., $(addprefix $(IMAGE_PLAT)., $(IMAGES))) .PHONY: image.build.multiarch -image.build.multiarch: image.verify $(foreach p,$(IMAGE_PLATFORMS),$(addprefix image.build., $(addprefix $(p)., $(IMAGES)))) +image.build.multiarch: image.verify + # TODO: use buildx .PHONY: image.build.% image.build.%: go.build.% diff --git a/tools/make/kube.mk b/tools/make/kube.mk new file mode 100644 index 0000000000..66750f308e --- /dev/null +++ b/tools/make/kube.mk @@ -0,0 +1,60 @@ +##@ Kubernetes Development + +.PHONY: manifests +manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. + $(CONTROLLER_GEN) rbac:roleName=envoy-gateway-role crd webhook paths="./..." output:crd:artifacts:config=pkg/provider/kubernetes/config/crd/bases + +.PHONY: generate +generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. + $(CONTROLLER_GEN) object paths="./..." + +.PHONY: kube-test +kube-test: manifests generate lint envtest ## Run Kubernetes provider tests. + KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out + + +##@ Kubernetes Deployment + +ifndef ignore-not-found + ignore-not-found = false +endif + +.PHONY: kube-install +kube-install: manifests kustomize ## Install CRDs into the Kubernetes cluster specified in ~/.kube/config. + $(KUSTOMIZE) build pkg/provider/kubernetes/config/crd | kubectl apply -f - + +.PHONY: kube-uninstall +kube-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 pkg/provider/kubernetes/config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f - + +##@ Kubernetes 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 + +## Tool Versions +KUSTOMIZE_VERSION ?= v3.8.7 +CONTROLLER_TOOLS_VERSION ?= v0.8.0 + +KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" +.PHONY: kustomize +kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. +$(KUSTOMIZE): $(LOCALBIN) + curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN) + +.PHONY: controller-gen +controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. +$(CONTROLLER_GEN): $(LOCALBIN) + 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) + GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest diff --git a/tools/make/lint.mk b/tools/make/lint.mk index 4d1cabbebc..52e83e80cb 100644 --- a/tools/make/lint.mk +++ b/tools/make/lint.mk @@ -2,6 +2,12 @@ # # All make targets related to lint are defined in this file. +##@ Lint + +.PHONY: lint +lint: ## Run all linter of code sources, including golint, yamllint and codespell. + @$(MAKE) lint.golint lint.yamllint lint.codespell + .PHONY: lint.golint lint.golint: @echo Running Go linter ... @@ -15,4 +21,5 @@ lint.yamllint: .PHONY: lint.codespell lint.codespell: CODESPELL_SKIP := $(shell cat tools/linter/codespell/.codespell.skip | tr \\n ',') lint.codespell: + @echo Running Codespell linter ... @codespell --skip $(CODESPELL_SKIP) --ignore-words tools/linter/codespell/.codespell.ignorewords --check-filenames --check-hidden -q2