|
| 1 | +# Copyright 2020 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +.DEFAULT_GOAL := help |
| 16 | +.SUFFIXES: # remove legacy builtin suffixes to allow easier make debugging |
| 17 | +SHELL = /usr/bin/env bash |
| 18 | + |
| 19 | +.PHONY: help |
| 20 | +help: ## Display this help. |
| 21 | + @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) |
| 22 | + |
| 23 | +# If GOARCH is not set in the env, find it |
| 24 | +GOARCH ?= $(shell go env GOARCH) |
| 25 | + |
| 26 | +## |
| 27 | +## ==== ARGS ===== # |
| 28 | + |
| 29 | +## Container build tool compatible with `docker` API |
| 30 | +DOCKER ?= docker |
| 31 | + |
| 32 | +## Platform for 'build' |
| 33 | +PLATFORM ?= linux/$(GOARCH) |
| 34 | + |
| 35 | +## Additional args for 'build' |
| 36 | +BUILD_ARGS ?= |
| 37 | + |
| 38 | +## Image tag for controller image build |
| 39 | +CONTROLLER_TAG ?= cosi-controller:latest |
| 40 | + |
| 41 | +## Image tag for sidecar image build |
| 42 | +SIDECAR_TAG ?= cosi-provisioner-sidecar:latest |
| 43 | + |
| 44 | +## Location to install dependencies to |
| 45 | +TOOLBIN ?= $(CURDIR)/.cache/tools |
| 46 | +$(TOOLBIN): |
| 47 | + mkdir -p $(TOOLBIN) |
| 48 | + |
| 49 | +##@ Development |
| 50 | + |
| 51 | +.PHONY: all .gen |
| 52 | +.gen: generate codegen # can be done in parallel with 'make -j' |
| 53 | +.NOTPARALLEL: all # codegen must be finished before fmt/vet |
| 54 | +all: .gen fmt vet build ## Build all targets, plus their prerequisites (faster with 'make -j') |
| 55 | + |
| 56 | +.PHONY: generate |
| 57 | +generate: controller/Dockerfile sidecar/Dockerfile ## Generate files |
| 58 | + $(MAKE) -C client crds |
| 59 | + $(MAKE) -C proto generate |
| 60 | + |
| 61 | +.PHONY: codegen |
| 62 | +codegen: codegen.client codegen.proto ## Generate code |
| 63 | + |
| 64 | +.PHONY: fmt |
| 65 | +fmt: fmt.client fmt.controller fmt.sidecar ## Format code |
| 66 | + |
| 67 | +.PHONY: vet |
| 68 | +vet: vet.client vet.controller vet.sidecar ## Vet code |
| 69 | + |
| 70 | +.PHONY: test |
| 71 | +test: .test.proto test.client test.controller test.sidecar ## Run tests including unit tests |
| 72 | + |
| 73 | +.PHONY: test-e2e |
| 74 | +test-e2e: chainsaw # Run e2e tests against the K8s cluster specified in ~/.kube/config. It requires both controller and driver deployed. If you need to create a cluster beforehand, consider using 'cluster' and 'deploy' targets. |
| 75 | + $(CHAINSAW) test --values ./test/e2e/values.yaml |
| 76 | + |
| 77 | +.PHONY: lint |
| 78 | +lint: golangci-lint.client golangci-lint.controller golangci-lint.sidecar ## Run all linters (suggest `make -k`) |
| 79 | + |
| 80 | +.PHONY: lint-fix |
| 81 | +lint-fix: golangci-lint-fix.client golangci-lint-fix.controller golangci-lint-fix.sidecar ## Run all linters and perform fixes where possible (suggest `make -k`) |
| 82 | + |
| 83 | +##@ Build |
| 84 | + |
| 85 | +.PHONY: build |
| 86 | +build: build.controller build.sidecar ## Build all container images for development |
| 87 | + |
| 88 | +.PHONY: build.controller build.sidecar |
| 89 | +build.controller: controller/Dockerfile ## Build only the controller container image |
| 90 | + $(DOCKER) build --file controller/Dockerfile --platform $(PLATFORM) $(BUILD_ARGS) --tag $(CONTROLLER_TAG) . |
| 91 | +build.sidecar: sidecar/Dockerfile ## Build only the sidecar container image |
| 92 | + $(DOCKER) build --file sidecar/Dockerfile --platform $(PLATFORM) $(BUILD_ARGS) --tag $(SIDECAR_TAG) . |
| 93 | + |
| 94 | +.PHONY: clean |
| 95 | +## Clean build environment |
| 96 | +clean: |
| 97 | + $(MAKE) -C proto clean |
| 98 | + |
| 99 | +.PHONY: clobber |
| 100 | +## Clean build environment and cached tools |
| 101 | +clobber: |
| 102 | + $(MAKE) -C proto clobber |
| 103 | + rm -rf $(TOOLBIN) |
| 104 | + rm -rf $(CURDIR)/.cache |
| 105 | + |
| 106 | +## |
| 107 | +## === INTERMEDIATES === # |
| 108 | + |
| 109 | +%/Dockerfile: hack/Dockerfile.in hack/gen-dockerfile.sh |
| 110 | + hack/gen-dockerfile.sh $* > "$@" |
| 111 | + |
| 112 | +codegen.%: FORCE |
| 113 | + $(MAKE) -C $* codegen |
| 114 | + |
| 115 | +fmt.%: FORCE |
| 116 | + cd $* && go fmt ./... |
| 117 | + |
| 118 | +vet.%: FORCE |
| 119 | + cd $* && go vet ./... |
| 120 | + |
| 121 | +test.%: fmt.% vet.% FORCE |
| 122 | + cd $* && go test ./... |
| 123 | + |
| 124 | +# golangci-lint --new flag only complains about new code |
| 125 | +golangci-lint.%: $(GOLANGCI_LINT) |
| 126 | + cd $* && $(GOLANGCI_LINT) run --config $(CURDIR)/.golangci.yaml --new |
| 127 | + |
| 128 | +golangci-lint-fix.%: $(GOLANGCI_LINT) |
| 129 | + cd $* && $(GOLANGCI_LINT) run --config $(CURDIR)/.golangci.yaml --new --fix |
| 130 | + |
| 131 | +.PHONY: .test.proto |
| 132 | +.test.proto: # gRPC proto has a special unit test |
| 133 | + $(MAKE) -C proto check |
| 134 | + |
| 135 | +.PHONY: FORCE # use this to force phony behavior for targets with pattern rules |
| 136 | +FORCE: |
| 137 | + |
| 138 | +##@ Deployment |
| 139 | + |
| 140 | +.PHONY: cluster |
| 141 | +cluster: kind ctlptl ## Create Kind cluster and local registry |
| 142 | + $(CTLPTL) apply -f ctlptl.yaml |
| 143 | + |
| 144 | +.PHONY: cluster-reset |
| 145 | +cluster-reset: kind ctlptl ## Delete Kind cluster |
| 146 | + $(CTLPTL) delete -f ctlptl.yaml |
| 147 | + |
| 148 | +.PHONY: deploy |
| 149 | +deploy: kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config. The 'generate' and 'codegen' targets should be run manually, and are expected to be run at least once before the 'deploy' target, as those are not cached. |
| 150 | + $(KUSTOMIZE) build . | $(KUBECTL) apply -f - |
| 151 | + |
| 152 | +.PHONY: undeploy |
| 153 | +undeploy: kustomize ## Undeploy controller from the K8s cluster specified in ~/.kube/config. |
| 154 | + $(KUSTOMIZE) build . | $(KUBECTL) delete --ignore-not-found=true -f - |
| 155 | + |
| 156 | +##@ Tools |
| 157 | + |
| 158 | +## Tool Binaries |
| 159 | +CHAINSAW ?= $(TOOLBIN)/chainsaw |
| 160 | +CTLPTL ?= $(TOOLBIN)/ctlptl |
| 161 | +GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint |
| 162 | +KIND ?= $(TOOLBIN)/kind |
| 163 | +KUBECTL ?= kubectl ## Special case, we do not manage it via tools.go |
| 164 | +KUSTOMIZE ?= $(TOOLBIN)/kustomize |
| 165 | + |
| 166 | +## Tool Versions |
| 167 | +CHAINSAW_VERSION ?= $(shell grep 'github.com/kyverno/chainsaw ' ./hack/tools/go.mod | cut -d ' ' -f 2) |
| 168 | +CTLPTL_VERSION ?= $(shell grep 'github.com/tilt-dev/ctlptl ' ./hack/tools/go.mod | cut -d ' ' -f 2) |
| 169 | +GOLANGCI_LINT_VERSION ?= $(shell grep 'github.com/golangci/golangci-lint ' ./hack/tools/go.mod | cut -d ' ' -f 2) |
| 170 | +KIND_VERSION ?= $(shell grep 'sigs.k8s.io/kind ' ./hack/tools/go.mod | cut -d ' ' -f 2) |
| 171 | +KUSTOMIZE_VERSION ?= $(shell grep 'sigs.k8s.io/kustomize/kustomize/v5 ' ./hack/tools/go.mod | cut -d ' ' -f 2) |
| 172 | + |
| 173 | +.PHONY: chainsaw |
| 174 | +chainsaw: $(CHAINSAW)$(CHAINSAW_VERSION) ## Download chainsaw locally if necessary. |
| 175 | +$(CHAINSAW)$(CHAINSAW_VERSION): $(TOOLBIN) |
| 176 | + $(call go-install-tool,$(CHAINSAW),github.com/kyverno/chainsaw,$(CHAINSAW_VERSION)) |
| 177 | + |
| 178 | +.PHONY: ctlptl |
| 179 | +ctlptl: $(CTLPTL)$(CTLPTL_VERSION) ## Download ctlptl locally if necessary. |
| 180 | +$(CTLPTL)$(CTLPTL_VERSION): $(TOOLBIN) |
| 181 | + $(call go-install-tool,$(CTLPTL),github.com/tilt-dev/ctlptl/cmd/ctlptl,$(CTLPTL_VERSION)) |
| 182 | + |
| 183 | +.PHONY: golangci-lint |
| 184 | +golangci-lint: $(GOLANGCI_LINT)$(GOLANGCI_LINT_VERSION) ## Download golangci-lint locally if necessary. |
| 185 | +$(GOLANGCI_LINT)$(GOLANGCI_LINT_VERSION): $(LOCALBIN) |
| 186 | + $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) |
| 187 | + |
| 188 | +.PHONY: kind |
| 189 | +kind: $(KIND)$(KIND_VERSION) ## Download kind locally if necessary. |
| 190 | +$(KIND)$(KIND_VERSION): $(TOOLBIN) |
| 191 | + $(call go-install-tool,$(KIND),sigs.k8s.io/kind,$(KIND_VERSION)) |
| 192 | + |
| 193 | +.PHONY: kustomize |
| 194 | +kustomize: $(KUSTOMIZE)$(KUSTOMIZE_VERSION) ## Download kustomize locally if necessary. |
| 195 | +$(KUSTOMIZE)$(KUSTOMIZE_VERSION): $(TOOLBIN) |
| 196 | + $(call go-install-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION)) |
| 197 | + |
| 198 | +# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist |
| 199 | +# $1 - target path with name of binary |
| 200 | +# $2 - package url which can be installed |
| 201 | +# $3 - specific version of package |
| 202 | +define go-install-tool |
| 203 | +@[ -f "$(1)-$(3)" ] || { \ |
| 204 | +set -e; \ |
| 205 | +package=$(2)@$(3) ;\ |
| 206 | +echo "Downloading $${package}" ;\ |
| 207 | +rm -f $(1) || true ;\ |
| 208 | +GOBIN=$(TOOLBIN) go install $${package} ;\ |
| 209 | +mv $(1) $(1)-$(3) ;\ |
| 210 | +} ;\ |
| 211 | +ln -sf $(1)-$(3) $(1) |
| 212 | +endef |
0 commit comments