-
Notifications
You must be signed in to change notification settings - Fork 24
/
Makefile
328 lines (272 loc) · 9.74 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
# Alllow developer to override some defaults
-include devel.mk
# Current Operator version
VERSION?=0.0.1
# Default bundle image tag
BUNDLE_IMG?=samba-operator-bundle:$(VERSION)
# 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)
COMMIT_ID=$(shell git describe --abbrev=40 --always --exclude='*' --dirty=+ 2>/dev/null)
GIT_VERSION=$(shell git describe --match='v[0-9]*.[0-9]' --match='v[0-9]*.[0-9].[0-9]' 2>/dev/null || echo "(unset)")
CONFIG_KUST_DIR:=config/default
CRD_KUST_DIR:=config/crd
MGR_KUST_DIR:=config/manager
KUSTOMIZE_DEFAULT_BASE:=../default
ifneq ($(DEVELOPER),)
CONFIG_KUST_DIR:=config/developer
MGR_KUST_DIR:=config/developer
endif
GO_CMD:=go
GOFMT_CMD:=gofmt
KUBECTL_CMD?=kubectl
BUILDAH_CMD:=buildah
YAMLLINT_CMD:=yamllint
# Image URL to use all building/pushing image targets
TAG?=latest
IMG?=quay.io/samba.org/samba-operator:$(TAG)
# Produce CRDs that work on Kubernetes 1.16 or later
CRD_OPTIONS?="crd:crdVersions=v1"
CHECK_GOFMT_FLAGS?=-e -s -l
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell $(GO_CMD) env GOBIN))
GOBIN=$(shell $(GO_CMD) env GOPATH)/bin
else
GOBIN=$(shell $(GO_CMD) env GOBIN)
endif
# Get current GOARCH
GOARCH?=$(shell $(GO_CMD) env GOARCH)
# Local (alternative) GOBIN for auxiliary build tools
GOBIN_ALT:=$(CURDIR)/.bin
CONTAINER_BUILD_OPTS?=
CONTAINER_CMD?=
ifeq ($(CONTAINER_CMD),)
CONTAINER_CMD:=$(shell docker version >/dev/null 2>&1 && echo docker)
endif
ifeq ($(CONTAINER_CMD),)
CONTAINER_CMD:=$(shell podman version >/dev/null 2>&1 && echo podman)
endif
# handle the case where podman is present but is (defaulting) to remote and is
# not not functioning correctly. Example: mac platform but not 'podman machine'
# vms are ready
ifeq ($(CONTAINER_CMD),)
CONTAINER_CMD:=$(shell podman --version >/dev/null 2>&1 && echo podman)
ifneq ($(CONTAINER_CMD),)
$(warning podman detected but 'podman version' failed. \
this may mean your podman is set up for remote use, but is not working)
endif
endif
# Helper function to re-format yamls using helper script
define yamls_reformat
YQ=$(YQ) $(CURDIR)/hack/yq-fixup-yamls.sh $(1)
endef
all: manager build-integration-tests
# Run unit tests
test: generate manifests vet
$(GO_CMD) test ./... -coverprofile cover.out
.PHONY: test
cover.out: test
coverage.html: cover.out
$(GO_CMD) tool cover -html=cover.out -o coverage.html
.PHONY: coverage.html cover.out
# Build manager binary
manager: generate build vet
build:
CGO_ENABLED=0 $(GO_CMD) build -o bin/manager -ldflags "-X main.Version=$(GIT_VERSION) -X main.CommitID=$(COMMIT_ID)" main.go
.PHONY: build
build-integration-tests:
$(GO_CMD) test -c -o bin/integration-tests -tags integration ./tests/integration
.PHONY: build-integration-tests
# Run against the configured Kubernetes cluster in ~/.kube/config
run: generate vet manifests
$(GO_CMD) run ./main.go
# Install CRDs into a cluster
install: manifests kustomize
$(KUSTOMIZE) build $(CRD_KUST_DIR) | $(KUBECTL_CMD) apply -f -
# Uninstall CRDs from a cluster
uninstall: manifests kustomize
$(KUSTOMIZE) build $(CRD_KUST_DIR) | $(KUBECTL_CMD) delete -f -
# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
deploy: manifests kustomize set-image
$(KUSTOMIZE) build $(CONFIG_KUST_DIR) | $(KUBECTL_CMD) apply -f -
delete-deploy: manifests kustomize
$(KUSTOMIZE) build $(CONFIG_KUST_DIR) | $(KUBECTL_CMD) delete -f -
# the bar symbol below is an order only prerequisite
# https://www.gnu.org/software/make/manual/make.html#index-order_002donly-prerequisites
# this is needed because kustomize is phony but we really do not want to
# have that force the kustomization.yaml to be considered "dirty"
%/kustomization.yaml: | kustomize
mkdir -p $*
touch $@
cd $* && $(KUSTOMIZE) edit add base $(KUSTOMIZE_DEFAULT_BASE)
# We could make developer-dir always create a developer dir, but I want to be
# consistent and "train" the caller to use DEVELOPER=1 whenever "developer
# mode" is being invoked even when it is not strictly needed for
# implementation.
ifneq ($(DEVELOPER),)
developer-dir: $(MGR_KUST_DIR)/kustomization.yaml
else
developer-dir:
@echo "When creating a developer-dir, DEVELOPER=1 is required." && exit 1
endif
.PHONY: developer-dir
set-image: kustomize $(MGR_KUST_DIR)/kustomization.yaml
cd $(MGR_KUST_DIR) && $(KUSTOMIZE) edit set image quay.io/samba.org/samba-operator=$(IMG)
.PHONY: set-image
# Generate manifests e.g. CRD, RBAC etc.
manifests: yq controller-gen
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook \
paths="./..." output:crd:artifacts:config=$(CRD_KUST_DIR)/bases
$(call yamls_reformat, $(CURDIR)/config)
# Run go fmt to reformat code
reformat:
$(GO_CMD) fmt ./...
# Run go vet against code
vet:
$(GO_CMD) vet ./...
# Format yaml files for yamllint standard
.PHONY: yaml-fmt
yaml-fmt: yq
$(call yamls_reformat, $(CURDIR))
# Generate code
generate: controller-gen
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
# Build the container image
docker-build: image-build
image-build:
$(CONTAINER_CMD) build \
--build-arg=GIT_VERSION="$(GIT_VERSION)" \
--build-arg=COMMIT_ID="$(COMMIT_ID)" \
--build-arg=ARCH="$(GOARCH)" \
$(CONTAINER_BUILD_OPTS) . -t $(IMG)
.PHONY: image-build-buildah
image-build-buildah: build
cn=$$($(BUILDAH_CMD) from registry.access.redhat.com/ubi8/ubi-minimal:latest) && \
$(BUILDAH_CMD) copy $$cn bin/manager /manager && \
$(BUILDAH_CMD) config --cmd='[]' $$cn && \
$(BUILDAH_CMD) config --entrypoint='["/manager"]' $$cn && \
$(BUILDAH_CMD) commit $$cn $(IMG)
# Push the container image
docker-push: container-push
container-push:
$(CONTAINER_CMD) push $(IMG)
# Generate bundle manifests and metadata, then validate generated files.
.PHONY: bundle
bundle: manifests
@echo "This rule is currently disabled. It is retained for reference only."
@false
# See vcs history for how this could be restored or adapted in the future.
# Build the bundle image.
.PHONY: bundle-build
bundle-build:
@echo "This rule is currently disabled. It is retained for reference only."
@false
# See vcs history for how this could be restored or adapted in the future.
.PHONY: check check-revive check-golangci-lint check-format check-yaml check-gosec check-dockerfile-go-version
check: check-revive check-golangci-lint check-format vet check-yaml check-gosec check-dockerfile-go-version
check-format:
! $(GOFMT_CMD) $(CHECK_GOFMT_FLAGS) . | sed 's,^,formatting error: ,' | grep 'go$$'
check-revive: revive
# revive's checks are configured using .revive.toml
# See: https://github.com/mgechev/revive
$(REVIVE) -config .revive.toml $$($(GO_CMD) list ./... | grep -v /vendor/)
check-golangci-lint: golangci-lint
$(GOLANGCI_LINT) -c .golangci.yaml run ./...
check-yaml:
$(YAMLLINT_CMD) -c ./.yamllint.yaml ./
check-gosec: gosec
$(GOSEC) -quiet -exclude=G101 -fmt json ./...
check-dockerfile-go-version:
# use go-version-check.sh --show to list vaild golang builder images
$(CURDIR)/hack/go-version-check.sh --check
check-gitlint: gitlint
$(GITLINT) -C .gitlint --commits origin/master.. lint
# find or download auxiliary build tools
.PHONY: build-tools controller-gen kustomize revive golangci-lint yq
build-tools: controller-gen kustomize revive golangci-lint yq
define installtool
@GOBIN=$(GOBIN_ALT) GO_CMD=$(GO_CMD) $(CURDIR)/hack/install-tools.sh $(1)
endef
controller-gen:
ifeq (, $(shell command -v controller-gen ;))
@echo "controller-gen not found in PATH, checking $(GOBIN_ALT)"
ifeq (, $(shell command -v $(GOBIN_ALT)/controller-gen ;))
@$(call installtool, --controller-gen)
@echo "controller-gen installed in $(GOBIN_ALT)"
endif
CONTROLLER_GEN=$(GOBIN_ALT)/controller-gen
else
CONTROLLER_GEN=$(shell command -v controller-gen ;)
endif
kustomize:
ifeq (, $(shell command -v kustomize ;))
@echo "kustomize not found in PATH, checking $(GOBIN_ALT)"
ifeq (, $(shell command -v $(GOBIN_ALT)/kustomize ;))
@$(call installtool, --kustomize)
@echo "kustomize installed in $(GOBIN_ALT)"
endif
KUSTOMIZE=$(GOBIN_ALT)/kustomize
else
KUSTOMIZE=$(shell command -v kustomize ;)
endif
revive:
ifeq (, $(shell command -v revive ;))
@echo "revive not found in PATH, checking $(GOBIN_ALT)"
ifeq (, $(shell command -v $(GOBIN_ALT)/revive ;))
@$(call installtool, --revive)
@echo "revive installed in $(GOBIN_ALT)"
endif
REVIVE=$(GOBIN_ALT)/revive
else
@echo "revive found in PATH"
REVIVE=$(shell command -v revive ;)
endif
golangci-lint:
ifeq (, $(shell command -v golangci-lint ;))
@echo "golangci-lint not found in PATH, checking $(GOBIN_ALT)"
ifeq (, $(shell command -v $(GOBIN_ALT)/golangci-lint ;))
@$(call installtool, --golangci-lint)
@echo "golangci-lint installed in $(GOBIN_ALT)"
endif
GOLANGCI_LINT=$(GOBIN_ALT)/golangci-lint
else
GOLANGCI_LINT=$(shell command -v golangci-lint ;)
endif
yq:
ifeq (, $(shell command -v yq ;))
@echo "yq not found in PATH, checking $(GOBIN_ALT)"
ifeq (, $(shell command -v $(GOBIN_ALT)/yq ;))
@$(call installtool, --yq)
@echo "yq installed in $(GOBIN_ALT)"
endif
YQ=$(GOBIN_ALT)/yq
else
YQ=$(shell command -v yq ;)
endif
gosec:
ifeq (, $(shell command -v gosec ;))
@echo "gosec not found in PATH, checking $(GOBIN_ALT)"
ifeq (, $(shell command -v $(GOBIN_ALT)/gosec ;))
@$(call installtool, --gosec)
@echo "gosec installed in $(GOBIN_ALT)"
endif
GOSEC=$(GOBIN_ALT)/gosec
else
GOSEC=$(shell command -v gosec ;)
endif
gitlint:
ifeq (, $(shell command -v gitlint ;))
@echo "gitlint not found in PATH, checking $(GOBIN_ALT)"
ifeq (, $(shell command -v $(GOBIN_ALT)/gitlint ;))
@$(call installtool, --gitlint)
@echo "gitlint installed in $(GOBIN_ALT)"
endif
GITLINT=$(GOBIN_ALT)/gitlint
else
GITLINT=$(shell command -v gitlint ;)
endif