-
Notifications
You must be signed in to change notification settings - Fork 22
/
Makefile
344 lines (286 loc) · 13.5 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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#
# Copyright Red Hat
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Current Operator version
VERSION ?= `cat $(PWD)/VERSION`
# Default bundle image tag
BUNDLE_IMG ?= quay.io/devfile/registry-operator-bundle:v$(VERSION)
CERT_MANAGER_VERSION ?= v1.15.1
ENABLE_WEBHOOKS ?= true
ENABLE_WEBHOOK_HTTP2 ?= false
# Options for 'bundle-build'
ifneq ($(origin CHANNELS), undefined)
BUNDLE_CHANNELS := --channels=$(CHANNELS)
endif
ifneq ($(origin DEFAULT_CHANNEL), undefined)
BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
endif
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
# Image URL to use all building/pushing image targets
IMG ?= quay.io/devfile/registry-operator:next
# ENVTEST_VERSION refers to the version of the setup-envtest binary to use
ENVTEST_VERSION=v0.0.0-20240405143037-c25fe2f5ca0f
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.26
# Controller tools version number
CONTROLLER_TOOLS_VERSION ?= v0.14.0
# Kustomize version number
KUSTOMIZE_VERSION ?= v3.8.7
# 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
# Check if oc or kubectl are installed and determine which of the two to use
ifeq ($(K8S_CLI),)
ifeq (,$(shell which kubectl))
ifeq (,$(shell which oc))
$(error oc or kubectl is required to proceed)
else
K8S_CLI := oc
endif
else
K8S_CLI := kubectl
endif
endif
# operator-sdk
OPERATOR_SDK_CLI ?= operator-sdk
# Setting SHELL to bash allows bash commands to be executed by recipes.
# This is a requirement for 'setup-envtest.sh' in the test target.
# 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
.PHONY: all
all: build
##@ General
# The help target prints out all targets with their descriptions organized
# beneath their categories. The categories are represented by '##@' and the
# target descriptions by '##'. The awk commands is responsible for reading the
# entire set of makefiles included in this invocation, looking for lines of the
# file as xyz: ## something, and then pretty-format the target and help. Then,
# if there's a line with ##@ something, that gets pretty-printed as a category.
# More info on the usage of ANSI control characters for terminal formatting:
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php
.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)
##@ Build Dependencies
## Local binary path
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)
## Tool binaries
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
## Target platform
TARGET_OS ?= linux # `make manager` only
TARGET_ARCH ?= amd64
##@ Development
.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test -timeout=1h `go list ./... | grep -v /tests/` -coverprofile cover.out -v
### test-integration: runs integration tests on the cluster set in context.
.PHONY: test-integration
test-integration:
CGO_ENABLED=0 go test -v -c -o $(LOCALBIN)/devfileregistry-operator-integration ./tests/integration/cmd/devfileregistry_test.go
# Create junit report output file if does not exist
mkdir -p /tmp/artifacts && touch /tmp/artifacts/junit-devfileregistry-operator.xml
$(LOCALBIN)/devfileregistry-operator-integration -ginkgo.fail-fast --ginkgo.junit-report=/tmp/artifacts/junit-devfileregistry-operator.xml
##@ Build
.PHONY: build manager
manager: manifests generate fmt vet ## Build manager binary.
GOOS=${TARGET_OS} GOARCH=${TARGET_ARCH} go build -o $(LOCALBIN)/manager ./main.go
.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
$(LOCALBIN)/manager
.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
### check_fmt: Checks the formatting on files in repo
.PHONY: check_fmt
check_fmt:
ifeq ($(shell command -v goimports 2> /dev/null),)
$(error "goimports must be installed for this rule" && exit 1)
endif
ifeq ($(shell command -v addlicense 2> /dev/null),)
$(error "error addlicense must be installed for this rule: go install github.com/google/addlicense")
endif
if [[ $$(find . -not -path '*/\.*' -not -name '*zz_generated*.go' -name '*.go' -exec goimports -l {} \;) != "" ]]; then \
echo "Files not formatted; run 'make fmt'"; exit 1 ;\
fi ;\
if ! addlicense -check -f license_header.txt $$(find . -not -path '*/\.*' -name '*.go'); then \
echo "Licenses are not formatted; run 'make fmt_license'"; exit 1 ;\
fi \
.PHONY: fmt
fmt: ## Run go fmt against code.
go fmt ./...
### fmt_license: ensure license header is set on all files
.PHONY: fmt_license
fmt_license:
ifneq ($(shell command -v addlicense 2> /dev/null),)
@echo 'addlicense -v -f license_header.txt **/*.go'
@addlicense -v -f license_header.txt $$(find . -name '*.go')
else
$(error addlicense must be installed for this rule: go install github.com/google/addlicense)
endif
.PHONY: vet
vet: ## Run go vet against code
go vet ./...
.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
# Build the docker image
.PHONY: docker-build
docker-build:
docker build . -t ${IMG} --build-arg TARGETARCH=${TARGET_ARCH} \
--build-arg ENABLE_WEBHOOKS=${ENABLE_WEBHOOKS} \
--build-arg ENABLE_WEBHOOK_HTTP2=${ENABLE_WEBHOOK_HTTP2}
# Push the docker image
.PHONY: docker-push
docker-push:
docker push ${IMG}
# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
# architectures. (i.e. make docker-buildx IMG=quay.io/devfile/registry-operator:next). 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=quay.io/<user>/registry-operator:next than the export will fail)
# To properly provided solutions that supports more than one platform you should use this option.
# **docker-buildx does not work with podman**
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
.PHONY: docker-buildx-build
docker-buildx-build: test
$(MAKE) docker-buildx-setup
- docker buildx build --platform=$(PLATFORMS) --tag ${IMG} --provenance=false -f Dockerfile.cross $(shell pwd)
$(MAKE) docker-buildx-cleanup
.PHONY: docker-buildx-push
docker-buildx-push: test
$(MAKE) docker-buildx-setup
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} --provenance=false -f Dockerfile.cross $(shell pwd)
$(MAKE) docker-buildx-cleanup
# INTERNAL: Used to setup the docker-buildx command, not to be called by users
.PHONY: docker-buildx-setup
docker-buildx-setup:
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
- docker buildx create --name registry-operator-builder
docker buildx use registry-operator-builder
# INTERNAL: Used to cleanup the docker-buildx command, not to be called by users
.PHONY: docker-buildx-cleanup
docker-buildx-cleanup:
- docker buildx rm registry-operator-builder
rm Dockerfile.cross
# Build the bundle image.
.PHONY: docker-bundle-buildx-build
docker-bundle-buildx-build:
$(MAKE) docker-bundle-buildx-setup
- docker buildx build --platform=${PLATFORMS} --tag ${BUNDLE_IMG} --provenance=false -f bundle.Dockerfile $(shell pwd)
$(MAKE) docker-bundle-buildx-cleanup
.PHONY: docker-bundle-buildx-push
docker-bundle-buildx-push:
$(MAKE) docker-bundle-buildx-setup
- docker buildx build --push --platform=${PLATFORMS} --tag ${BUNDLE_IMG} --provenance=false -f bundle.Dockerfile $(shell pwd)
$(MAKE) docker-bundle-buildx-cleanup
# INTERNAL: Used to setup the docker-bundle-buildx command, not to be called by users
.PHONY: docker-bundle-buildx-setup
docker-bundle-buildx-setup:
- docker buildx create --name bundle-builder
docker buildx use bundle-builder
# INTERNAL: Used to cleanup the docker-bundle-buildx command, not to be called by users
.PHONY: docker-bundle-buildx-cleanup
docker-bundle-buildx-cleanup:
- docker buildx rm bundle-builder
# Clone of docker-buildx command but redesigned to work with podman's workflow
# Designed to build and push multi architecture images of the registry operator
# This utilizes a new Dockerfile to insert platform args to preserve the original Dockerfile
.PHONY: podman-buildx-build
podman-buildx-build: test
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
- podman manifest create ${IMG}
- podman build --platform=$(PLATFORMS) --manifest ${IMG} -f Dockerfile.cross $(shell pwd)
# Contains cleanup steps to remove files and manifests created during build
.PHONY: podman-buildx-push
podman-buildx-push:
- podman manifest push ${IMG}
- podman manifest rm ${IMG}
rm Dockerfile.cross
# Build the podman image
.PHONY: podman-build
podman-build:
podman build . -t ${IMG} --build-arg TARGETARCH=${TARGET_ARCH} \
--build-arg ENABLE_WEBHOOKS=${ENABLE_WEBHOOKS} \
--build-arg ENABLE_WEBHOOK_HTTP2=${ENABLE_WEBHOOK_HTTP2}
# Push the podman image
.PHONY: podman-push
podman-push:
podman push ${IMG}
.PHONY: install-cert
install-cert: ## Install cert manager for webhooks
$(K8S_CLI) apply -f https://github.com/cert-manager/cert-manager/releases/download/$(CERT_MANAGER_VERSION)/cert-manager.yaml
.PHONY: uninstall-cert
uninstall-cert:
$(K8S_CLI) delete -f https://github.com/cert-manager/cert-manager/releases/download/$(CERT_MANAGER_VERSION)/cert-manager.yaml
# find or download controller-gen
# download controller-gen if necessary
.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN)
$(CONTROLLER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
GOBIN=$(LOCALBIN) GOFLAGS="" go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
##@ Deployment
.PHONY: install
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | $(K8S_CLI) apply -f -
.PHONY: uninstall
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.
$(KUSTOMIZE) build config/crd | $(K8S_CLI) delete -f -
.PHONY: deploy
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default | $(K8S_CLI) apply -f -
.PHONY: undeploy
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.
$(KUSTOMIZE) build config/default | $(K8S_CLI) delete -f -
KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
.PHONY: kustomize
kustomize: $(KUSTOMIZE)
$(KUSTOMIZE): $(LOCALBIN)
test -s $(LOCALBIN)/kustomize || { curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }
.PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) GOFLAGS="" go install sigs.k8s.io/controller-runtime/tools/setup-envtest@$(ENVTEST_VERSION)
# Generate bundle manifests and metadata, then validate generated files.
.PHONY: bundle
bundle: manifests
$(OPERATOR_SDK_CLI) generate kustomize manifests -q
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
$(KUSTOMIZE) build config/manifests | $(OPERATOR_SDK_CLI) generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
$(OPERATOR_SDK_CLI) bundle validate ./bundle
# Build the bundle image.
.PHONY: bundle-build
bundle-build:
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
# Push the bundle image.
.PHONY: bundle-push
bundle-push:
docker push $(BUNDLE_IMG)
### gosec - runs the gosec scanner for non-test files in this repo
.PHONY: gosec
gosec:
# Run this command to install gosec, if not installed:
# go install github.com/securego/gosec/v2/cmd/[email protected]
gosec -no-fail -fmt=sarif -out=gosec.sarif -exclude-dir pkg/test -exclude-dir tests ./...