|
| 1 | +# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary. |
| 2 | +ENVTEST_K8S_VERSION = 1.31.0 |
| 3 | +# Set COVERAGE=true or COVERAGE=1 on make test-unit to print per-func coverage (cover.out is removed after). |
| 4 | +COVERAGE ?= |
| 5 | + |
| 6 | +# Setting SHELL to bash allows bash commands to be executed by recipes. |
| 7 | +# Options are set to exit when a recipe line exits non-zero or a piped command fails. |
| 8 | +SHELL = /usr/bin/env bash -o pipefail |
| 9 | +.SHELLFLAGS = -ec |
| 10 | + |
| 11 | +GIT_COMMIT_SHA ?= "$(shell git rev-parse HEAD 2>/dev/null)" |
| 12 | +GIT_TAG ?= $(shell git describe --tags --dirty --always) |
| 13 | +TARGETARCH ?= $(shell go env GOARCH) |
| 14 | +PLATFORMS ?= linux/$(TARGETARCH) |
| 15 | +DOCKER_BUILDX_CMD ?= docker buildx |
| 16 | +IMAGE_BUILD_CMD ?= $(DOCKER_BUILDX_CMD) build |
| 17 | +IMAGE_BUILD_EXTRA_OPTS ?= |
| 18 | + |
| 19 | +IMAGE_REGISTRY ?= quay.io/opendatahub-io |
| 20 | +IMAGE_NAME := ai-gateway-payload-processing |
| 21 | +IMAGE_REPO ?= $(IMAGE_REGISTRY)/$(IMAGE_NAME) |
| 22 | +IMAGE_TAG ?= $(IMAGE_REPO):$(GIT_TAG) |
| 23 | + |
| 24 | +BASE_IMAGE ?= gcr.io/distroless/static:nonroot |
| 25 | +BUILDER_IMAGE ?= golang:1.25 |
| 26 | +ifdef GO_VERSION |
| 27 | +BUILDER_IMAGE = golang:$(GO_VERSION) |
| 28 | +endif |
| 29 | + |
| 30 | +BUILD_REF ?= $(shell git describe --abbrev=0 2>/dev/null) |
| 31 | +ifdef EXTRA_TAG |
| 32 | +IMAGE_EXTRA_TAG ?= $(IMAGE_REPO):$(EXTRA_TAG) |
| 33 | +BUILD_REF = $(EXTRA_TAG) |
| 34 | +endif |
| 35 | +ifdef IMAGE_EXTRA_TAG |
| 36 | +IMAGE_BUILD_EXTRA_OPTS += -t $(IMAGE_EXTRA_TAG) |
| 37 | +endif |
| 38 | + |
| 39 | +# The name of the kind cluster to use for the "kind-load" target. |
| 40 | +KIND_CLUSTER ?= kind |
| 41 | + |
| 42 | +##@ General |
| 43 | + |
| 44 | +# The help target prints out all targets with their descriptions organized |
| 45 | +# beneath their categories. The categories are represented by '##@' and the |
| 46 | +# target descriptions by '##'. The awk command is responsible for reading the |
| 47 | +# entire set of makefiles included in this invocation, looking for lines of the |
| 48 | +# file as xyz: ## something, and then pretty-format the target and help. Then, |
| 49 | +# if there's a line with ##@ something, that gets pretty-printed as a category. |
| 50 | +# More info on the usage of ANSI control characters for terminal formatting: |
| 51 | +# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters |
| 52 | +# More info on the awk command: |
| 53 | +# http://linuxcommand.org/lc3_adv_awk.php |
| 54 | + |
| 55 | +.PHONY: help |
| 56 | +help: ## Display this help. |
| 57 | + @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) |
| 58 | + |
| 59 | +##@ Development |
| 60 | + |
| 61 | +.PHONY: fmt |
| 62 | +fmt: ## Run go fmt against code. |
| 63 | + go fmt ./... |
| 64 | + |
| 65 | +.PHONY: vet |
| 66 | +vet: ## Run go vet against code. |
| 67 | + go vet ./... |
| 68 | + |
| 69 | + |
| 70 | +.PHONY: lint |
| 71 | +lint: golangci-lint ## Run golangci-lint linter |
| 72 | + $(GOLANGCI_LINT) run --timeout 5m |
| 73 | + |
| 74 | +.PHONY: lint-fix |
| 75 | +lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes |
| 76 | + $(GOLANGCI_LINT) run --fix |
| 77 | + |
| 78 | +.PHONY: tidy |
| 79 | +tidy: ## Run go work sync (if go.work exists) and go mod tidy per module. |
| 80 | + @if [ -f go.work ]; then go work sync; fi |
| 81 | + find . -name go.mod -execdir sh -c 'go mod tidy' \; |
| 82 | + |
| 83 | +.PHONY: verify |
| 84 | +verify: tidy vet fmt lint ## Verify the codebase (tidy, vet, fmt, lint). |
| 85 | + |
| 86 | +.PHONY: test-unit |
| 87 | +test-unit: envtest ## Run unit tests. Optional: COVERAGE=true (or 1) for go tool cover summary. |
| 88 | + @set -e; \ |
| 89 | + kubebuilder_assets_path="$$($(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)"; \ |
| 90 | + if [ "$(COVERAGE)" = "true" ] || [ "$(COVERAGE)" = "1" ]; then \ |
| 91 | + CGO_ENABLED=1 KUBEBUILDER_ASSETS="$$kubebuilder_assets_path" go test ./pkg/... -race -count=1 -coverprofile=cover.out; \ |
| 92 | + go tool cover -func=cover.out; \ |
| 93 | + rm -f cover.out; \ |
| 94 | + else \ |
| 95 | + CGO_ENABLED=1 KUBEBUILDER_ASSETS="$$kubebuilder_assets_path" go test ./pkg/... -race -count=1; \ |
| 96 | + fi |
| 97 | + |
| 98 | +.PHONY: test |
| 99 | +test: test-unit ## Run unit tests (alias for test-unit). |
| 100 | + |
| 101 | +##@ Build |
| 102 | + |
| 103 | +# Build the container image |
| 104 | +.PHONY: image-local-build |
| 105 | +image-local-build: ## Build the image using Docker Buildx for local development. |
| 106 | + set -e; \ |
| 107 | + builder=$$($(DOCKER_BUILDX_CMD) create --use); \ |
| 108 | + trap '$(DOCKER_BUILDX_CMD) rm -f "$$builder"' EXIT; \ |
| 109 | + $(MAKE) image-build PUSH="$(PUSH)" LOAD="$(LOAD)" |
| 110 | + |
| 111 | +.PHONY: image-local-push |
| 112 | +image-local-push: PUSH=--push ## Build the image for local development and push it to $IMAGE_REPO. |
| 113 | +image-local-push: image-local-build |
| 114 | + |
| 115 | +.PHONY: image-local-load |
| 116 | +image-local-load: LOAD=--load ## Build the image for local development and load it in the local Docker registry. |
| 117 | +image-local-load: image-local-build |
| 118 | + |
| 119 | +.PHONY: image-build |
| 120 | +image-build: ## Build the image using Docker Buildx. |
| 121 | + $(IMAGE_BUILD_CMD) -t $(IMAGE_TAG) \ |
| 122 | + --platform=$(PLATFORMS) \ |
| 123 | + --build-arg BASE_IMAGE=$(BASE_IMAGE) \ |
| 124 | + --build-arg BUILDER_IMAGE=$(BUILDER_IMAGE) \ |
| 125 | + --build-arg COMMIT_SHA=${GIT_COMMIT_SHA} \ |
| 126 | + --build-arg BUILD_REF=${BUILD_REF} \ |
| 127 | + $(PUSH) \ |
| 128 | + $(LOAD) \ |
| 129 | + $(IMAGE_BUILD_EXTRA_OPTS) ./ |
| 130 | + |
| 131 | +.PHONY: image-push |
| 132 | +image-push: PUSH=--push ## Build the image and push it to $IMAGE_REPO. |
| 133 | +image-push: image-build |
| 134 | + |
| 135 | +.PHONY: image-load |
| 136 | +image-load: LOAD=--load ## Build the image and load it in the local Docker registry. |
| 137 | +image-load: image-build |
| 138 | + |
| 139 | +.PHONY: image-kind |
| 140 | +image-kind: image-build ## Build the image and load it to kind cluster $KIND_CLUSTER ("kind" by default). |
| 141 | + kind load docker-image $(IMAGE_TAG) --name $(KIND_CLUSTER) |
| 142 | + |
| 143 | +##@ Dependencies |
| 144 | + |
| 145 | +## Location to install dependencies to |
| 146 | +LOCALBIN ?= $(shell pwd)/bin |
| 147 | +$(LOCALBIN): |
| 148 | + [ -d $@ ] || mkdir -p $@ |
| 149 | + |
| 150 | +ENVTEST ?= $(LOCALBIN)/setup-envtest |
| 151 | +GOLANGCI_LINT = $(LOCALBIN)/golangci-lint |
| 152 | + |
| 153 | +ENVTEST_VERSION ?= release-0.19 |
| 154 | +GOLANGCI_LINT_VERSION ?= v2.9.0 |
| 155 | + |
| 156 | +.PHONY: envtest |
| 157 | +envtest: $(ENVTEST) ## Download setup-envtest locally if necessary. |
| 158 | +$(ENVTEST): $(LOCALBIN) |
| 159 | + $(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION)) |
| 160 | + |
| 161 | +.PHONY: golangci-lint |
| 162 | +golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary. |
| 163 | +$(GOLANGCI_LINT): $(LOCALBIN) |
| 164 | + $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) |
| 165 | + |
| 166 | +# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist |
| 167 | +# $1 - target path with name of binary |
| 168 | +# $2 - package url which can be installed |
| 169 | +# $3 - specific version of package |
| 170 | +define go-install-tool |
| 171 | +@[ -f "$(1)-$(3)" ] || { \ |
| 172 | +set -e; \ |
| 173 | +package=$(2)@$(3) ;\ |
| 174 | +echo "Downloading $${package}" ;\ |
| 175 | +rm -f $(1) || true ;\ |
| 176 | +GOBIN=$(LOCALBIN) go install $${package} ;\ |
| 177 | +mv $(1) $(1)-$(3) ;\ |
| 178 | +} ;\ |
| 179 | +ln -sf $(1)-$(3) $(1) |
| 180 | +endef |
0 commit comments