-
Notifications
You must be signed in to change notification settings - Fork 20
/
utils.Makefile
298 lines (250 loc) · 7.99 KB
/
utils.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
comma := ,
space := $(subst ,, )
UNAME_S := $(shell uname -s)
UNAME := $(shell echo `uname` | tr '[:upper:]' '[:lower:]')
VERSION ?= $(shell $(SVU) next --prefix "")
TAG ?= $(VERSION)
export VERSION
export TAG
BINDIR ?= ./bin
# GO_VERSION can be major version only, latest stable minor version will be retrieved by base.Dockerfile
GO_VERSION ?= 1.21
ENVTEST_K8S_VERSION ?= 1.27.x
ARCHS ?= amd64 ppc64le s390x arm64
BUILDX ?= true
ARCH ?= amd64
IMAGE_PUSH ?= true
DOCKER_BUILD := docker build
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
DOCKERBUILDXCACHE ?=
KUBE_RBAC_PROXY_IMAGE ?= registry.redhat.io/openshift4/ose-kube-rbac-proxy-rhel9:v4.17
clean-bin:
rm -rf $(PROJECT_DIR)/bin
# --TOOLS--
#
# find or download controller-gen
# download controller-gen if necessary
CONTROLLER_GEN_VERSION=v0.12.1
CONTROLLER_GEN=$(PROJECT_DIR)/bin/controller-gen
controller-gen:
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION),$(CONTROLLER_GEN_VERSION))
CODEGEN_VERSION=kubernetes-1.22.3
CODEGEN_PKG=$(GOPATH)/src/k8s.io/code-generator
code-generator:
@[ -d $(CODEGEN_PKG) ] && { \
cd $(CODEGEN_PKG) && \
if [ "$$(git describe --tags)" != "$(CODEGEN_VERSION)" ]; \
then git fetch --tags && git checkout tags/$(CODEGEN_VERSION) ; \
fi;
}
@[ -d $(CODEGEN_PKG) ] || { \
git clone -b tags/$(CODEGEN_VERSION) git@github.com:kubernetes/code-generator $(GOPATH)/k8s.io/code-generator ;\
}
KUSTOMIZE_VERSION=v5.0.3
KUSTOMIZE=$(PROJECT_DIR)/bin/kustomize
kustomize:
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION),$(KUSTOMIZE_VERSION))
OMT_VERSION=v0.2.2
OMT=$(PROJECT_DIR)/bin/operator-manifest-tools
omt:
$(call go-get-tool,$(OMT),github.com/operator-framework/operator-manifest-tools@$(OMT_VERSION),$(OMT_VERSION))
export KUSTOMIZE
OPENAPI_GEN=$(PROJECT_DIR)/bin/openapi-gen
openapi-gen:
$(call go-get-tool,$(OPENAPI_GEN),github.com/kubernetes/kube-openapi/cmd/openapi-gen@690f563a49b523b7e87ea117b6bf448aead23b09,690f563)
HELM_VERSION=v3.8.2
HELM=$(PROJECT_DIR)/bin/helm
helm:
@[ -f $(HELM)-$(HELM_VERSION) ] || { \
HELM_INSTALL_DIR=$$(dirname $(HELM)) $(PROJECT_DIR)/hack/get_helm.sh --version $(HELM_VERSION) && touch $(HELM)-$(HELM_VERSION) ;\
}
GINKGO_VERSION=v2.13.0
GINKGO=$(PROJECT_DIR)/bin/ginkgo
ginkgo:
$(call go-get-tool,$(GINKGO),github.com/onsi/ginkgo/v2/ginkgo@$(GINKGO_VERSION),$(GINKGO_VERSION))
LICENSE=$(PROJECT_DIR)/bin/addlicense
addlicense:
$(call go-install-tool,$(LICENSE),github.com/google/addlicense@v1.1.1,v1.1.1)
GO_MOD_OUTDATED=$(PROJECT_DIR)/bin/go-mod-outdated
go-mod-outdated:
$(shell GOBIN=$(PROJECT_DIR)/bin go install github.com/psampaz/go-mod-outdated@latest)
GO_LICENSES=$(PROJECT_DIR)/bin/go-licenses
golicense:
$(call go-get-tool,$(GO_LICENSES),github.com/google/go-licenses@v1.6.0,v1.6.0)
YQ_VERSION=v4.40.1
YQ=$(PROJECT_DIR)/bin/yq
yq:
$(call go-get-tool,$(YQ),github.com/mikefarah/yq/v4@$(YQ_VERSION),$(YQ_VERSION))
OPERATOR_SDK_VERSION=v1.28.1
OPERATOR_SDK=$(PROJECT_DIR)/bin/operator-sdk
operator-sdk:
$(call install-binary,https://github.com/operator-framework/operator-sdk/releases/download/$(OPERATOR_SDK_VERSION),operator-sdk_$(UNAME)_$(ARCH),$(OPERATOR_SDK),$(OPERATOR_SDK_VERSION))
OPM_VERSION=v1.43.1
OPM=$(PROJECT_DIR)/bin/opm
opm:
$(call install-binary,https://github.com/operator-framework/operator-registry/releases/download/$(OPM_VERSION),$(UNAME)-$(ARCH)-opm,$(OPM),$(OPM_VERSION))
SVU_VERSION=v1.8.0
.SILENT: svu
SVU=$(PROJECT_DIR)/bin/svu
svu:
$(call go-get-tool,$(SVU),github.com/caarlos0/svu@$(SVU_VERSION),$(SVU_VERSION))
VERSION_TOOL=$(PROJECT_DIR)/bin/version-tool
version-tool:
@[ -f $(VERSION_TOOL) ] || { \
cd v2/tools/version && go build -o $(VERSION_TOOL) ./main.go ; \
}
PC_TOOL=$(PROJECT_DIR)/bin/partner-connect-tool
pc-tool:
@[ -f $(VERSION_TOOL) ] || { \
cd v2/tools/connect && go build -o $(PC_TOOL) ./main.go ; \
}
SKAFFOLD=$(PROJECT_DIR)/bin/skaffold
SKAFFOLD_VERSION=v2.3.1
skaffold:
$(call install-binary,https://storage.googleapis.com/skaffold/releases/$(SKAFFOLD_VERSION),skaffold-$(UNAME)-amd64,$(SKAFFOLD),$(SKAFFOLD_VERSION))
BUF=$(PROJECT_DIR)/bin/buf
BUF_VERSION=v1.25.0
ifeq ($(UNAME_S),Linux)
WILDCARDS=--wildcards
endif
buf:
$(call install-targz,https://github.com/bufbuild/buf/releases/download/$(BUF_VERSION)/buf-$(shell uname -s)-$(shell uname -m).tar.gz,$(BUF),$(BUF_VERSION),$(PROJECT_DIR)/bin,--strip-components 2 $(WILDCARDS) "*/bin/*")
ENVTEST=$(PROJECT_DIR)/bin/setup-envtest
envtest:
$(shell GOBIN=$(PROJECT_DIR)/bin go install sigs.k8s.io/controller-runtime/tools/setup-envtest@c7e1dc9b5302d649d5531e19168dd7ea0013736d)
.PHONY: source-envtest
source-envtest:
@echo export KUBEBUILDER_ASSETS="'$(shell $(ENVTEST) use -p path $(ENVTEST_K8S_VERSION))'"
# --COMMON--
# Run go mod tidy against code
tidy:
go mod tidy
# Run go mod tidy against code
download:
go mod download
# func(name)
define multiarch-build
{ \
mkdir -p $(BINDIR) && \
for arch in $(ARCHS); do \
GOOS=linux GOARCH=$${arch} go build -o $(BINDIR)/$(1)-linux-$${arch} $(2) ; \
done \
}
endef
# func(ARCHS, OS) TARGETS
define build-targets
$(subst $(space),$(comma),$(addprefix $(2)/,$(1)))
endef
ifeq ($(BUILDX),true)
DOCKER_BUILD := docker buildx build --platform $(call build-targets,$(ARCHS),linux)
endif
ifneq ($(DOCKERBUILDXCACHE),)
DOCKER_EXTRA_ARGS = --cache-from "type=local,src=$(DOCKERBUILDXCACHE)" --cache-to "type=local,dest=$(DOCKERBUILDXCACHE)" --output "type=image,push=$(IMAGE_PUSH)" --provenance=false
else
DOCKER_EXTRA_ARGS =
ifneq ($(PODMAN),true)
ifeq ($(IMAGE_PUSH),true)
DOCKER_BUILD += --push
else
DOCKER_BUILD += --load
endif
endif
endif
# func(image,args)
define docker-build
$(DOCKER_BUILD) \
-t "$(2)" \
$(DOCKER_EXTRA_ARGS) \
-f $(1) $(3) .
endef
# func(image,name,path,exec,bin,args)
define docker-templated-build
$(DOCKER_BUILD) \
-f ./Dockerfile \
--tag $(1) \
$(DOCKER_EXTRA_ARGS) \
--build-arg ARCHS='$(ARCHS)' \
--build-arg REGISTRY=$(IMAGE_REGISTRY) \
--build-arg GO_VERSION=$(GO_VERSION) \
--build-arg name=$(2) \
--build-arg path=$(3) \
--build-arg exec=$(4) \
--build-arg bin=$(5) \
--build-arg app_version=$(VERSION) \
--build-arg quay_expiration=$(QUAY_EXPIRATION) \
$(6)
endef
# go-get-tool will 'go get' any package $2 and install it to $1.
define go-get-tool
@[ -f $(1)-$(3) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
go mod init tmp ;\
echo $(1) ;\
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
rm -rf $$TMP_DIR ;\
mkdir -p $$(dirname $(1)) ;\
touch $(1)-$(3) ;\
}
endef
# go-install-tool will 'go install' any package $2 and install it to $1.
define go-install-tool
@[ -f $(1)-$(3) ] || { \
set -e ;\
GOBIN=$$(dirname $(1)) go install $(2) ;\
touch $(1)-$(3) ;\
}
endef
# install-binary will 'curl' any package url $1 with file $2 and install it to $3
define install-binary
@[ -f $(3)-$(4) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
echo "Downloading $(1)" ;\
curl -o $(3) -LO $(1)/$(2) ;\
chmod +x $(3) ;\
mkdir -p $$(dirname $(1)) ;\
rm -rf $$TMP_DIR ;\
touch $(3)-$(4) ;\
}
endef
# $1 url $2 bin path $3 version $4 is the bin path to extract to $5 is optional extract args
define install-targz
[ -f $(2)-$(3) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
echo "Downloading $(1)"; \
mkdir -p $$(dirname $(1)) ;\
curl -sSL $(1) | tar -xvzf - -C "$(4)" $(5) ;\
rm -rf $$TMP_DIR ;\
touch $(2)-$(3); \
}
endef
# $1 is the image
define get-image-sha
$$(IMAGE=$(1); echo $${IMAGE%:*}@sha256:$$(skopeo inspect --raw docker://$(1) | sha256sum | cut -d " " -f 1))
endef
IBM_TWISTLOCK_TOOL=$(PROJECT_DIR)/bin/tt
TT_OS=linux
ifeq ($(UNAME_S),Darwin)
TT_OS=macos
endif
define install-twistlock-tool
[ -f $(IBM_TWISTLOCK_TOOL) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
echo "Downloading IBM twistlock tool"; \
wget --tries=3 --no-check-certificate $(TWISTLOCK_URL)/download/tt_latest.zip ; \
unzip tt_latest.zip; \
mkdir -p $(PROJECT_DIR)/bin ; \
cp tt_v*/$(TT_OS)*/tt $(IBM_TWISTLOCK_TOOL) ; \
chmod +x $(IBM_TWISTLOCK_TOOL) ; \
rm -rf $$TMP_DIR ;\
}
endef
install-twistlock-tool:
$(call install-twistlock-tool)