|
| 1 | + |
| 2 | +# Image URL to use all building/pushing image targets |
| 3 | +BASE_IMAGE ?= gcr.io/distroless/static:nonroot |
| 4 | +DOCKER_BUILDX_CMD ?= docker buildx |
| 5 | +IMAGE_BUILD_CMD ?= $(DOCKER_BUILDX_CMD) build |
| 6 | +IMAGE_BUILD_EXTRA_OPTS ?= |
| 7 | +IMAGE_REGISTRY ?= inftyai |
| 8 | +IMAGE_NAME ?= vscheduler |
| 9 | +IMAGE_REPO := $(IMAGE_REGISTRY)/$(IMAGE_NAME) |
| 10 | +GIT_TAG ?= $(shell git describe --tags --dirty --always) |
| 11 | +IMG ?= $(IMAGE_REPO):$(GIT_TAG) |
| 12 | +GO_VERSION := $(shell awk '/^go /{print $$2}' go.mod|head -n1) |
| 13 | +BUILDER_IMAGE ?= golang:$(GO_VERSION) |
| 14 | + |
| 15 | +# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary. |
| 16 | +ENVTEST_K8S_VERSION = 1.28.0 |
| 17 | + |
| 18 | +# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) |
| 19 | +ifeq (,$(shell go env GOBIN)) |
| 20 | +GOBIN=$(shell go env GOPATH)/bin |
| 21 | +else |
| 22 | +GOBIN=$(shell go env GOBIN) |
| 23 | +endif |
| 24 | + |
| 25 | +# CONTAINER_TOOL defines the container tool to be used for building images. |
| 26 | +# Be aware that the target commands are only tested with Docker which is |
| 27 | +# scaffolded by default. However, you might want to replace it to use other |
| 28 | +# tools. (i.e. podman) |
| 29 | +CONTAINER_TOOL ?= docker |
| 30 | + |
| 31 | +# Setting SHELL to bash allows bash commands to be executed by recipes. |
| 32 | +# Options are set to exit when a recipe line exits non-zero or a piped command fails. |
| 33 | +SHELL = /usr/bin/env bash -o pipefail |
| 34 | +.SHELLFLAGS = -ec |
| 35 | + |
| 36 | +.PHONY: all |
| 37 | +all: build |
| 38 | + |
| 39 | +##@ General |
| 40 | + |
| 41 | +# The help target prints out all targets with their descriptions organized |
| 42 | +# beneath their categories. The categories are represented by '##@' and the |
| 43 | +# target descriptions by '##'. The awk command is responsible for reading the |
| 44 | +# entire set of makefiles included in this invocation, looking for lines of the |
| 45 | +# file as xyz: ## something, and then pretty-format the target and help. Then, |
| 46 | +# if there's a line with ##@ something, that gets pretty-printed as a category. |
| 47 | +# More info on the usage of ANSI control characters for terminal formatting: |
| 48 | +# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters |
| 49 | +# More info on the awk command: |
| 50 | +# http://linuxcommand.org/lc3_adv_awk.php |
| 51 | + |
| 52 | +.PHONY: help |
| 53 | +help: ## Display this help. |
| 54 | + @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) |
| 55 | + |
| 56 | +##@ Development |
| 57 | + |
| 58 | +.PHONY: manifests |
| 59 | +manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. |
| 60 | + $(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases |
| 61 | + |
| 62 | +.PHONY: generate |
| 63 | +generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. |
| 64 | + $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..." |
| 65 | + |
| 66 | +.PHONY: fmt |
| 67 | +fmt: ## Run go fmt against code. |
| 68 | + go fmt ./... |
| 69 | + |
| 70 | +.PHONY: vet |
| 71 | +vet: ## Run go vet against code. |
| 72 | + go vet ./... |
| 73 | + |
| 74 | +.PHONY: test |
| 75 | +test: manifests generate fmt vet envtest ## Run tests. |
| 76 | + KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out |
| 77 | + |
| 78 | +GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint |
| 79 | +GOLANGCI_LINT_VERSION ?= v1.54.2 |
| 80 | +golangci-lint: |
| 81 | + @[ -f $(GOLANGCI_LINT) ] || { \ |
| 82 | + set -e ;\ |
| 83 | + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) $(GOLANGCI_LINT_VERSION) ;\ |
| 84 | + } |
| 85 | + |
| 86 | +.PHONY: lint |
| 87 | +lint: golangci-lint ## Run golangci-lint linter & yamllint |
| 88 | + $(GOLANGCI_LINT) run |
| 89 | + |
| 90 | +.PHONY: lint-fix |
| 91 | +lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes |
| 92 | + $(GOLANGCI_LINT) run --fix |
| 93 | + |
| 94 | +##@ Build |
| 95 | + |
| 96 | +.PHONY: build |
| 97 | +build: manifests generate fmt vet ## Build manager binary. |
| 98 | + go build -o bin/manager cmd/main.go |
| 99 | + |
| 100 | +.PHONY: run |
| 101 | +run: manifests generate fmt vet ## Run a controller from your host. |
| 102 | + go run ./cmd/main.go |
| 103 | + |
| 104 | +# If you wish to build the manager image targeting other platforms you can use the --platform flag. |
| 105 | +# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it. |
| 106 | +# More info: https://docs.docker.com/develop/develop-images/build_enhancements/ |
| 107 | +.PHONY: docker-build |
| 108 | +docker-build: ## Build docker image with the manager. |
| 109 | + $(CONTAINER_TOOL) build -t ${IMG} . |
| 110 | + |
| 111 | +.PHONY: docker-push |
| 112 | +docker-push: ## Push docker image with the manager. |
| 113 | + $(CONTAINER_TOOL) push ${IMG} |
| 114 | + |
| 115 | +# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple |
| 116 | +# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to: |
| 117 | +# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/ |
| 118 | +# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/ |
| 119 | +# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail) |
| 120 | +# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option. |
| 121 | +PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le |
| 122 | +.PHONY: docker-buildx |
| 123 | +docker-buildx: ## Build and push docker image for the manager for cross-platform support |
| 124 | + # copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile |
| 125 | + sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross |
| 126 | + - $(CONTAINER_TOOL) buildx create --name project-v3-builder |
| 127 | + $(CONTAINER_TOOL) buildx use project-v3-builder |
| 128 | + - $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross . |
| 129 | + - $(CONTAINER_TOOL) buildx rm project-v3-builder |
| 130 | + rm Dockerfile.cross |
| 131 | + |
| 132 | +.PHONY: image-build |
| 133 | +image-build: |
| 134 | + $(IMAGE_BUILD_CMD) -t $(IMG) \ |
| 135 | + -f Dockerfile \ |
| 136 | + --build-arg BASE_IMAGE=$(BASE_IMAGE) \ |
| 137 | + --build-arg BUILDER_IMAGE=$(BUILDER_IMAGE) \ |
| 138 | + --build-arg CGO_ENABLED=$(CGO_ENABLED) \ |
| 139 | + $(IMAGE_BUILD_EXTRA_OPTS) ./ |
| 140 | +image-load: IMAGE_BUILD_EXTRA_OPTS=--load |
| 141 | +image-load: image-build |
| 142 | +image-push: IMAGE_BUILD_EXTRA_OPTS=--push |
| 143 | +image-push: image-build |
| 144 | + |
| 145 | +##@ Deployment |
| 146 | + |
| 147 | +ifndef ignore-not-found |
| 148 | + ignore-not-found = false |
| 149 | +endif |
| 150 | + |
| 151 | +.PHONY: install |
| 152 | +install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. |
| 153 | + $(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f - |
| 154 | + |
| 155 | +.PHONY: uninstall |
| 156 | +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. |
| 157 | + $(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f - |
| 158 | + |
| 159 | +.PHONY: deploy |
| 160 | +deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config. |
| 161 | + cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} |
| 162 | + $(KUSTOMIZE) build config/default | $(KUBECTL) apply -f - |
| 163 | + |
| 164 | +.PHONY: undeploy |
| 165 | +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. |
| 166 | + $(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f - |
| 167 | + |
| 168 | +##@ Build Dependencies |
| 169 | + |
| 170 | +## Location to install dependencies to |
| 171 | +LOCALBIN ?= $(shell pwd)/bin |
| 172 | +$(LOCALBIN): |
| 173 | + mkdir -p $(LOCALBIN) |
| 174 | + |
| 175 | +## Tool Binaries |
| 176 | +KUBECTL ?= kubectl |
| 177 | +KUSTOMIZE ?= $(LOCALBIN)/kustomize |
| 178 | +CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen |
| 179 | +ENVTEST ?= $(LOCALBIN)/setup-envtest |
| 180 | + |
| 181 | +## Tool Versions |
| 182 | +KUSTOMIZE_VERSION ?= v5.2.1 |
| 183 | +CONTROLLER_TOOLS_VERSION ?= v0.13.0 |
| 184 | + |
| 185 | +.PHONY: kustomize |
| 186 | +kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading. |
| 187 | +$(KUSTOMIZE): $(LOCALBIN) |
| 188 | + @if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \ |
| 189 | + echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \ |
| 190 | + rm -rf $(LOCALBIN)/kustomize; \ |
| 191 | + fi |
| 192 | + test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION) |
| 193 | + |
| 194 | +.PHONY: controller-gen |
| 195 | +controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten. |
| 196 | +$(CONTROLLER_GEN): $(LOCALBIN) |
| 197 | + test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \ |
| 198 | + GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION) |
| 199 | + |
| 200 | +.PHONY: envtest |
| 201 | +envtest: $(ENVTEST) ## Download envtest-setup locally if necessary. |
| 202 | +$(ENVTEST): $(LOCALBIN) |
| 203 | + test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest |
0 commit comments