This repository has been archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
292 lines (249 loc) · 8.15 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
# TODOs
# relocate IMAGE_ORG
IMAGE_ORG?=quay.io/openshift
MODULE:=github.com/openshift/addon-operator
KIND_KUBECONFIG:=bin/e2e/kubeconfig
# Dependency Versions
CONTROLLER_GEN_VERSION:=v0.5.0
OLM_VERSION:=v0.17.0
KIND_VERSION:=v0.10.0
YQ_VERSION:[email protected]
GOIMPORTS_VERSION:=v0.1.0
SHELL=/bin/bash
.SHELLFLAGS=-euo pipefail -c
# Build Flags
export CGO_ENABLED:=0
BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
SHORT_SHA=$(shell git rev-parse --short HEAD)
VERSION?=${BRANCH}-${SHORT_SHA}
BUILD_DATE=$(shell date +%s)
LD_FLAGS=-X $(MODULE)/internal/version.Version=$(VERSION) \
-X $(MODULE)/internal/version.Branch=$(BRANCH) \
-X $(MODULE)/internal/version.Commit=$(SHORT_SHA) \
-X $(MODULE)/internal/version.BuildDate=$(BUILD_DATE)
UNAME_OS:=$(shell uname -s)
UNAME_ARCH:=$(shell uname -m)
# PATH/Bin
DEPENDENCIES:=bin/dependencies/$(UNAME_OS)/$(UNAME_ARCH)
export GOBIN?=$(abspath bin/dependencies/bin)
export PATH:=$(GOBIN):$(PATH)
# -------
# Compile
# -------
all: \
bin/linux_amd64/addon-operator-manager
bin/linux_amd64/%: GOARGS = GOOS=linux GOARCH=amd64
bin/%: generate manifests FORCE
$(eval COMPONENT=$(shell basename $*))
@echo -e -n "compiling cmd/$(COMPONENT)...\n "
$(GOARGS) go build -ldflags "-w $(LD_FLAGS)" -o bin/$* cmd/$(COMPONENT)/main.go
@echo
FORCE:
clean:
rm -rf bin/$*
.PHONY: clean
# ------------
# Dependencies
# ------------
# setup kind
KIND:=$(DEPENDENCIES)/kind/$(KIND_VERSION)
$(KIND):
@echo "installing kind $(KIND_VERSION)..."
$(eval KIND_TMP := $(shell mktemp -d))
@(cd "$(KIND_TMP)" \
&& go mod init tmp \
&& go get "sigs.k8s.io/kind@$(KIND_VERSION)" \
) 2>&1 | sed 's/^/ /'
@rm -rf "$(KIND_TMP)" "$(dir $(KIND))" \
&& mkdir -p "$(dir $(KIND))" \
&& touch "$(KIND)" \
&& echo
# setup controller-gen
CONTROLLER_GEN:=$(DEPENDENCIES)/controller-gen/$(CONTROLLER_GEN_VERSION)
$(CONTROLLER_GEN):
@echo "installing controller-gen $(CONTROLLER_GEN_VERSION)..."
$(eval CONTROLLER_GEN_TMP := $(shell mktemp -d))
@(cd "$(CONTROLLER_GEN_TMP)" \
&& go mod init tmp \
&& go get "sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION)" \
) 2>&1 | sed 's/^/ /'
@rm -rf "$(CONTROLLER_GEN_TMP)" "$(dir $(CONTROLLER_GEN))" \
&& mkdir -p "$(dir $(CONTROLLER_GEN))" \
&& touch "$(CONTROLLER_GEN)" \
&& echo
# setup yq
YQ:=$(DEPENDENCIES)/yq/$(YQ_VERSION)
$(YQ):
@echo "installing yq $(YQ_VERSION)..."
$(eval YQ_TMP := $(shell mktemp -d))
@(cd "$(YQ_TMP)" \
&& go mod init tmp \
&& go get "github.com/mikefarah/yq/$(YQ_VERSION)" \
) 2>&1 | sed 's/^/ /'
@rm -rf "$(YQ_TMP)" "$(dir $(YQ))" \
&& mkdir -p "$(dir $(YQ))" \
&& touch "$(YQ)" \
&& echo
# setup goimports
GOIMPORTS:=$(DEPENDENCIES)/GOIMPORTS/$(GOIMPORTS_VERSION)
$(GOIMPORTS):
@echo "installing GOIMPORTS $(GOIMPORTS_VERSION)..."
$(eval GOIMPORTS_TMP := $(shell mktemp -d))
@(cd "$(GOIMPORTS_TMP)" \
&& go mod init tmp \
&& go get "golang.org/x/tools/cmd/goimports@$(GOIMPORTS_VERSION)" \
) 2>&1 | sed 's/^/ /'
@rm -rf "$(GOIMPORTS_TMP)" "$(dir $(GOIMPORTS))" \
&& mkdir -p "$(dir $(GOIMPORTS))" \
&& touch "$(GOIMPORTS)" \
&& echo
setup-dependencies: \
$(KIND) \
$(CONTROLLER_GEN) \
$(YQ) \
$(GOIMPORTS)
# ----------
# Deployment
# ----------
# Run against the configured Kubernetes cluster in ~/.kube/config or $KUBECONFIG
run: generate fmt vet manifests
go run -ldflags "-w $(LD_FLAGS)" \
./cmd/addon-operator-manager/main.go \
-pprof-addr="127.0.0.1:8065"
.PHONY: run
# ----------
# Generators
# ----------
# Generate manifests e.g. CRD, RBAC etc.
manifests: $(CONTROLLER_GEN)
@echo "generating kubernetes manifests..."
@controller-gen crd:crdVersions=v1 \
rbac:roleName=addon-operator-manager \
paths="./..." \
output:crd:artifacts:config=config/deploy 2>&1 | sed 's/^/ /'
@echo
# Generate code
generate: $(CONTROLLER_GEN)
@echo "generating code..."
@controller-gen object paths=./apis/... 2>&1 | sed 's/^/ /'
@echo
# Makes sandwich
# https://xkcd.com/149/
sandwich:
ifneq ($(shell id -u), 0)
@echo "What? Make it yourself."
else
@echo "Okay."
endif
# -------------------
# Testing and Linting
# -------------------
test: generate fmt vet manifests
CGO_ENABLED=1 go test -race -v ./internal/... ./cmd/...
.PHONY: test
ci-test: test
hack/validate-directory-clean.sh
.PHONY: ci-test
e2e-test:
@echo "running e2e tests..."
@export KUBECONFIG=$(abspath $(KIND_KUBECONFIG)) \
&& kubectl get pod -A \
&& echo \
&& go test -v ./e2e/...
.PHONY: e2e-test
e2e: | setup-e2e-kind e2e-test
.PHONY: e2e
fmt:
go fmt ./...
.PHONY: fmt
vet:
go vet ./...
.PHONY: vet
pre-commit-install:
@echo "installing pre-commit hooks using https://pre-commit.com/"
@pre-commit install
.PHONY: pre-commit-install
create-kind-cluster: $(KIND)
@echo "creating kind cluster addon-operator-e2e..."
@mkdir -p bin/e2e
@(source hack/determine-container-runtime.sh \
&& $$KIND_COMMAND create cluster \
--kubeconfig=$(KIND_KUBECONFIG) \
--name="addon-operator-e2e" \
&& sudo chown $$USER: $(KIND_KUBECONFIG) \
&& echo) 2>&1 | sed 's/^/ /'
.PHONY: create-kind-cluster
delete-kind-cluster: $(KIND)
@echo "deleting kind cluster addon-operator-e2e..."
@(source hack/determine-container-runtime.sh \
&& $$KIND_COMMAND delete cluster \
--kubeconfig="$(KIND_KUBECONFIG)" \
--name "addon-operator-e2e" \
&& rm -rf "$(KIND_KUBECONFIG)" \
&& echo) 2>&1 | sed 's/^/ /'
.PHONY: delete-kind-cluster
setup-e2e-kind: | \
create-kind-cluster \
apply-olm \
apply-openshift-console \
apply-ao
apply-olm:
@echo "installing OLM $(OLM_VERSION)..."
@(export KUBECONFIG=$(KIND_KUBECONFIG) \
&& kubectl apply -f https://github.com/operator-framework/operator-lifecycle-manager/releases/download/$(OLM_VERSION)/crds.yaml \
&& kubectl apply -f https://github.com/operator-framework/operator-lifecycle-manager/releases/download/$(OLM_VERSION)/olm.yaml \
&& echo -e "\nwaiting for deployment/olm-operator..." \
&& kubectl wait --for=condition=available deployment/olm-operator -n olm --timeout=240s \
&& echo -e "\nwaiting for deployment/catalog-operator..." \
&& kubectl wait --for=condition=available deployment/catalog-operator -n olm --timeout=240s \
&& echo) 2>&1 | sed 's/^/ /'
.PHONY: apply-olm
apply-openshift-console:
@echo "installing OpenShift console :latest..."
@(export KUBECONFIG=$(KIND_KUBECONFIG) \
&& kubectl apply -f hack/openshift-console.yaml \
&& echo) 2>&1 | sed 's/^/ /'
.PHONY: apply-openshift-console
apply-ao: $(YQ) build-image-addon-operator-manager
@echo "installing Addon Operator $(VERSION)..."
@(source hack/determine-container-runtime.sh \
&& export KUBECONFIG=$(KIND_KUBECONFIG) \
&& $$KIND_COMMAND load image-archive \
bin/image/addon-operator-manager.tar \
--name addon-operator-e2e \
&& kubectl apply -f config/deploy \
&& yq eval '.spec.template.spec.containers[0].image = "$(IMAGE_ORG)/addon-operator-manager:$(VERSION)"' \
config/deploy/deployment.yaml.tpl \
| kubectl apply -f - \
&& echo -e "\nwaiting for deployment/addon-operator..." \
&& kubectl wait --for=condition=available deployment/addon-operator -n addon-operator --timeout=240s \
&& echo) 2>&1 | sed 's/^/ /'
.PHONY: apply-ao
# ----------------
# Container Images
# ----------------
build-images: \
build-image-addon-operator-manager
.PHONY: build-images
push-images: \
push-image-addon-operator-manager
.PHONY: push-images
.SECONDEXPANSION:
build-image-%: bin/linux_amd64/$$*
@echo "building image ${IMAGE_ORG}/$*:${VERSION}..."
@(source hack/determine-container-runtime.sh \
&& rm -rf "bin/image/$*" "bin/image/$*.tar" \
&& mkdir -p "bin/image/$*" \
&& cp -a "bin/linux_amd64/$*" "bin/image/$*" \
&& cp -a "config/docker/$*.Dockerfile" "bin/image/$*/Dockerfile" \
&& cp -a "config/docker/passwd" "bin/image/$*/passwd" \
&& echo "building ${IMAGE_ORG}/$*:${VERSION}" \
&& $$CONTAINER_COMMAND build -t "${IMAGE_ORG}/$*:${VERSION}" "bin/image/$*" \
&& $$CONTAINER_COMMAND image save -o "bin/image/$*.tar" "${IMAGE_ORG}/$*:${VERSION}" \
&& echo) 2>&1 | sed 's/^/ /'
push-image-%: build-image-$$*
@echo "pushing image ${IMAGE_ORG}/$*:${VERSION}..."
@(source hack/determine-container-runtime.sh \
&& $$CONTAINER_COMMAND push "${IMAGE_ORG}/$*:${VERSION}" \
&& echo pushed "${IMAGE_ORG}/$*:${VERSION}" \
&& echo) 2>&1 | sed 's/^/ /'