-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMakefile
185 lines (150 loc) · 8.09 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Gardener contributors.
#
# SPDX-License-Identifier: Apache-2.0
REPO_ROOT := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
EFFECTIVE_VERSION := $(shell $(REPO_ROOT)/hack/get-version.sh)
K8SYNCER_IMAGE_REPOSITORY := $(shell $(REPO_ROOT)/hack/get-registry.sh --image)/k8syncer
IMG ?= $(K8SYNCER_IMAGE_REPOSITORY):$(EFFECTIVE_VERSION)
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.27.1
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif
# CONTAINER_TOOL defines the container tool to be used for building images.
# Be aware that the target commands are only tested with Docker which is
# scaffolded by default. However, you might want to replace it to use other
# tools. (i.e. podman)
CONTAINER_TOOL ?= docker
# Setting SHELL to bash allows bash commands to be executed by recipes.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec
##@ General
.PHONY: help
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)
##@ Development
.PHONY: revendor
revendor: ## Runs 'go mod vendor' and 'go mod tidy'.
@go mod tidy
.PHONY: format
format: goimports ## Formats the imports.
@echo "> Formatting imports ..."
@FORMATTER=$(FORMATTER) $(REPO_ROOT)/hack/format.sh
.PHONY: check
check: jq goimports golangci-lint ## Runs 'go vet', linting checks, and verify that 'make format' has been called.
@[[ "$(SKIP_DOCS_INDEX_CHECK)" == "true" ]] || \
( echo "> Verifying documentation index ..."; \
$(REPO_ROOT)/hack/verify-docs-index.sh )
@echo "> Running 'go vet' ..."
@go vet $(REPO_ROOT)/...
@echo "> Running linter ..."
@$(LINTER) run -c $(REPO_ROOT)/.golangci.yaml $(REPO_ROOT)/...
@[[ "$(SKIP_FORMATTING_CHECK)" == "true" ]] || \
( echo "> Checking for unformatted files ..."; \
FORMATTER=$(FORMATTER) $(REPO_ROOT)/hack/format.sh --verify )
.PHONY: test
test: envtest ## Runs the tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
.PHONY: verify
verify: check ## Alias for check.
.PHONY: generate-docs
generate-docs: jq ## Generates the documentation index.
@$(REPO_ROOT)/hack/generate-docs-index.sh
.PHONY: generate
generate: format revendor generate-docs ## Runs format, revendor and generate-docs.
##@ Build
PLATFORMS ?= linux/arm64,linux/amd64
.PHONY: all
all: docker-multi helm-chart component ## Build and upload image, helm-chart, and component descriptor.
.PHONY: build
build: ## Build k8syncer binary for all os/arch combinations specified in PLATFORMS.
@PLATFORMS=$(PLATFORMS) $(REPO_ROOT)/hack/build.sh
# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
# architectures. (i.e. make docker-multi IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
# - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
# To properly provided solutions that supports more than one platform you should use this option.
ADDITIONAL_TAG ?= ""
.PHONY: docker-multi
docker-multi: build ## Build and push docker image for the manager for cross-platform support
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
@echo "Building docker image ${IMG} ..."
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
$(CONTAINER_TOOL) buildx use project-v3-builder
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
@test -z "$(ADDITIONAL_TAG)" || docker buildx imagetools create ${IMG} --tag $(K8SYNCER_IMAGE_REPOSITORY):$(ADDITIONAL_TAG)
- $(CONTAINER_TOOL) buildx rm project-v3-builder
rm Dockerfile.cross
.PHONY: helm-chart
helm-chart: helm ## Upload the helm chart into the registry.
@$(REPO_ROOT)/hack/build-chart.sh
@$(REPO_ROOT)/hack/push-chart.sh
.PHONY: component
component: component-build component-push ## Builds the components and pushes them into the registry. To overwrite existing versions, set the env var OVERWRITE_COMPONENTS to anything except 'false' or the empty string.
.PHONY: component-build
component-build: ocm ## Build the components.
OCM=$(OCM) $(REPO_ROOT)/hack/build-component.sh
.PHONY: component-push
component-push: ocm ## Upload the components into the registry. Must be called after 'make component-build'. To overwrite existing versions, set the env var OVERWRITE_COMPONENTS to anything except 'false' or the empty string.
OCM=$(OCM) $(REPO_ROOT)/hack/push-component.sh
##@ Build Dependencies
## Location to install dependencies to
LOCALBIN ?= $(REPO_ROOT)/bin
## Tool Binaries
KUBECTL ?= kubectl
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
FORMATTER ?= $(LOCALBIN)/goimports
LINTER ?= $(LOCALBIN)/golangci-lint
OCM ?= $(LOCALBIN)/ocm
HELM ?= $(LOCALBIN)/helm
JQ ?= $(LOCALBIN)/jq
## Tool Versions
CONTROLLER_TOOLS_VERSION ?= v0.12.0
FORMATTER_VERSION ?= v0.16.0
LINTER_VERSION ?= 1.55.2
OCM_VERSION ?= 0.8.0
HELM_VERSION ?= v3.13.2
JQ_VERSION ?= 1.6
SETUP_ENVTEST_VERSION ?= release-0.17
.PHONY: localbin
localbin:
@test -d $(LOCALBIN) || mkdir -p $(LOCALBIN)
.PHONY: goimports
goimports: localbin ## Download goimports locally if necessary. If wrong version is installed, it will be overwritten.
@test -s $(FORMATTER) && test -s $(LOCALBIN)/goimports_version && cat $(LOCALBIN)/goimports_version | grep -q $(FORMATTER_VERSION) || \
( echo "Installing goimports $(FORMATTER_VERSION) ..."; \
GOBIN=$(LOCALBIN) go install golang.org/x/tools/cmd/goimports@$(FORMATTER_VERSION) && \
echo $(FORMATTER_VERSION) > $(LOCALBIN)/goimports_version )
.PHONY: golangci-lint
golangci-lint: localbin ## Download golangci-lint locally if necessary. If wrong version is installed, it will be overwritten.
@test -s $(LINTER) && $(LINTER) --version | grep -q $(LINTER_VERSION) || \
( echo "Installing golangci-lint $(LINTER_VERSION) ..."; \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(LOCALBIN) v$(LINTER_VERSION) )
.PHONY: envtest
envtest: localbin ## Download envtest-setup locally if necessary.
@test -s $(LOCALBIN)/setup-envtest && test -s $(LOCALBIN)/setup-envtest_version && cat $(LOCALBIN)/setup-envtest_version | grep -q $(SETUP_ENVTEST_VERSION) || \
( echo "Installing setup-envtest $(SETUP_ENVTEST_VERSION) ..."; \
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@$(SETUP_ENVTEST_VERSION) && \
echo $(SETUP_ENVTEST_VERSION) > $(LOCALBIN)/setup-envtest_version )
.PHONY: ocm
ocm: localbin ## Install OCM CLI if necessary.
@test -s $(OCM) && $(OCM) --version | grep -q $(OCM_VERSION) || \
( echo "Installing OCM tooling $(OCM_VERSION) ..."; \
curl -sSfL https://ocm.software/install.sh | OCM_VERSION=$(OCM_VERSION) bash -s $(LOCALBIN) )
.PHONY: helm
helm: localbin ## Download helm locally if necessary.
@test -s $(HELM) && $(HELM) version --short | grep -q $(HELM_VERSION) || \
( echo "Installing helm $(HELM_VERSION) ..."; \
HELM=$(HELM) LOCALBIN=$(LOCALBIN) $(REPO_ROOT)/hack/install-helm.sh $(HELM_VERSION) )
.PHONY: jq
jq: localbin ## Download jq locally if necessary.
@test -s $(JQ) && $(JQ) --version | grep -q $(JQ_VERSION) || \
( echo "Installing jq $(JQ_VERSION) ..."; \
JQ=$(JQ) LOCALBIN=$(LOCALBIN) $(REPO_ROOT)/hack/install-jq.sh $(JQ_VERSION) )