forked from kubernetes-up-and-running/kuard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
318 lines (265 loc) · 11.2 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
# Copyright 2019 The KUARD Authors.
#
# 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.
# This is a pretty complicated Makefile that builds the go binary (in a
# container) and then automates packaging it up into an image and pushing it. It
# then allows you to do this across multiple architectures and "fake versions".
#
# There is a bunch of funkiness around creating volumes so that intermetiate
# files (such as go libraries and npm module downloads) are cached across builds
# to speed things up.
#
# Some of the ideas here are taken from
# https://github.com/thockin/go-build-template and
# https://github.com/bowei/go-build-template.
# We don't need make's built-in rules.
MAKEFLAGS += --no-builtin-rules
.SUFFIXES:
# Golang package.
PKG := github.com/kubernetes-up-and-running/kuard
# Registry to push to.
REGISTRY ?= gcr.io/kuar-demo
# For demo purposes, we want to build multiple versions. They will all be
# mostly the same but will let us demonstrate rollouts.
FAKEVER ?= blue
ALL_FAKEVER = blue green purple
# This is the real version. We'll grab it from git and use tags.
VERSION_BASE ?= $(shell git describe --tags --always --dirty)
# Set to 1 to print more verbose output from the build.
export VERBOSE ?= 0
# Default architecture to build for.
ARCH ?= amd64
ALL_ARCH := amd64 arm arm64 ppc64le
# Set default base image dynamically for each arch
ifeq ($(ARCH),amd64)
BASEIMAGE?=alpine
endif
ifeq ($(ARCH),arm)
BASEIMAGE?=arm32v6/alpine
endif
ifeq ($(ARCH),arm64)
BASEIMAGE?=arm64v8/alpine
endif
ifeq ($(ARCH),ppc64le)
BASEIMAGE?=ppc64le/alpine
endif
BUILD_IMAGE := kuard-build
DOCKER_RUN_FLAGS := --rm
DOCKER_BUILD_FLAGS := --rm
ifeq ($(VERBOSE), 1)
VERBOSE_OUTPUT := >&1
else
DOCKER_BUILD_FLAGS += -q
VERBOSE_OUTPUT := >/dev/null
MAKEFLAGS += -s
endif
DOCKER_MOUNTS:= \
-v $(BUILD_IMAGE)-data:/data:delegated \
-v $(BUILD_IMAGE)-node:/data/go/src/$(PKG)/client/node_modules:delegated \
-v $$(pwd):/data/go/src/$(PKG):delegated \
-v $$(pwd)/build:/build:delegated \
-v $$(pwd)/bin/$(FAKEVER)/$(ARCH):/data/go/bin:delegated \
-v $$(pwd)/bin/$(FAKEVER)/$(ARCH):/data/go/bin/linux_$(ARCH):delegated
DOCKER_ENVS:= \
-e VERBOSE=$(VERBOSE) \
-e ARCH=$(ARCH) \
-e PKG=$(PKG) \
-e VERSION=$(VERSION_BASE)-$(FAKEVER) \
##############################################################################
# Default rule
all: build
##############################################################################
# Build container image
# Build the build image. This depends on the dockerfile for this image and the
# "init_data" script that initializes some volumes. We use a "timestamp" to
# keep track of when this image was built and mirror the state of docker into
# the filesystem.
BUILD_IMAGE_BUILDSTAMP := .$(subst .,_,$(BUILD_IMAGE))-image
$(BUILD_IMAGE_BUILDSTAMP): build/init_data.sh Dockerfile.build
@echo "container image: $(BUILD_IMAGE)"
@echo " Building container image"
docker build \
$(DOCKER_BUILD_FLAGS) \
-t $(BUILD_IMAGE) \
--build-arg "ALL_ARCH=$(ALL_ARCH)" \
-f Dockerfile.build . \
$(VERBOSE_OUTPUT)
@echo " Creating volume $(BUILD_IMAGE)-data"
-docker volume rm $(BUILD_IMAGE)-data $(VERBOSE_OUTPUT) 2>&1
docker volume create $(BUILD_IMAGE)-data $(VERBOSE_OUTPUT)
@echo " Creating volume $(BUILD_IMAGE)-node"
-docker volume rm $(BUILD_IMAGE)-node $(VERBOSE_OUTPUT) 2>&1
docker volume create $(BUILD_IMAGE)-node $(VERBOSE_OUTPUT)
@echo " Running build/init_data.sh in build container to init volumes"
docker run $(DOCKER_RUN_FLAGS) \
-v $(BUILD_IMAGE)-data:/data:delegated \
-v $(BUILD_IMAGE)-node:/data/go/src/$(PKG)/client/node_modules:delegated \
-v $$(pwd)/build:/build:delegated \
-e TARGET_UIDGID=$$(id -u):$$(id -g) \
$(BUILD_IMAGE) \
/build/init_data.sh \
$(VERBOSE_OUTPUT)
echo "$(BUILD_IMAGE)" > $@
docker images -q $(BUILD_IMAGE) >> $@
build-env: $(BUILD_IMAGE_BUILDSTAMP)
@echo "Launching into build environment"
docker run -ti \
$(DOCKER_RUN_FLAGS) \
$(DOCKER_MOUNTS) \
--sig-proxy=true \
$(DOCKER_ENVS) \
-u $$(id -u):$$(id -g) \
-w /data/go/src/$(PKG) \
$(BUILD_IMAGE) \
ash
##############################################################################
# Build the kuard binary
BINARYPATH:=bin/$(FAKEVER)/$(ARCH)/kuard
.PHONY: build
build: $(BINARYPATH)
$(BINARYPATH): build/build.sh $(BUILD_IMAGE_BUILDSTAMP)
@echo "building binary: $@"
@mkdir -p $(shell pwd)/bin/$(FAKEVER)/$(ARCH)
docker run \
$(DOCKER_RUN_FLAGS) \
$(DOCKER_MOUNTS) \
--sig-proxy=true \
$(DOCKER_ENVS) \
-u $$(id -u):$$(id -g) \
-w /data/go/src/$(PKG) \
$(BUILD_IMAGE) \
./build/build.sh $(VERBOSE_OUTPUT)
##############################################################################
# Build the final container image
# Dockerfile for the final image. We use SED to slam a bunch of things in there.
BIN_DOCKERFILE:=.kuard-$(ARCH)-$(FAKEVER)-dockerfile
$(BIN_DOCKERFILE): Dockerfile.kuard
@echo "generating Dockerfile $@ from $<"
sed \
-e 's|ARG_ARCH|$(ARCH)|g' \
-e 's|ARG_FROM|$(BASEIMAGE)|g' \
-e 's|ARG_FAKEVER|$(FAKEVER)|g' \
$< > $@
CONTAINER_NAME := $(REGISTRY)/kuard-$(ARCH)
BUILDSTAMP_NAME := $(subst /,_,$(CONTAINER_NAME)-$(FAKEVER))
.$(BUILDSTAMP_NAME)-image: $(BIN_DOCKERFILE) $(BINARYPATH)
@echo "container image: $(CONTAINER_NAME):$(VERSION_BASE)-$(FAKEVER)"
docker build \
$(DOCKER_BUILD_FLAGS) \
-t $(CONTAINER_NAME):$(VERSION_BASE)-$(FAKEVER) \
-f .kuard-$(ARCH)-$(FAKEVER)-dockerfile . \
$(VERBOSE_OUTPUT)
echo "$(CONTAINER_NAME):$(VERSION_BASE)-$(FAKEVER)" > $@
@echo "container image tag: $(CONTAINER_NAME):$(FAKEVER)"
docker tag $(CONTAINER_NAME):$(VERSION_BASE)-$(FAKEVER) $(CONTAINER_NAME):$(FAKEVER)
echo "$(CONTAINER_NAME):$(FAKEVER)" >> $@
docker images -q $(CONTAINER_NAME):$(VERSION_BASE)-$(FAKEVER) >> $@
.PHONY: images
images: .$(BUILDSTAMP_NAME)-image
##############################################################################
# Push to the registry
PUSH_BUILDSTAMP:=.$(BUILDSTAMP_NAME)-push
.PHONY: push
push: $(PUSH_BUILDSTAMP)
.%-push: .%-image
@echo "pushing image: " $$(sed -n '1p' $<)
docker push $$(sed -n '1p' $<) $(VERBOSE_OUTPUT)
@echo "pushing image: " $$(sed -n '2p' $<)
docker push $$(sed -n '2p' $<) $(VERBOSE_OUTPUT)
cat $< > $@
##############################################################################
# Rules for dealing with fake versions
build-fakever-%:
$(MAKE) --no-print-directory FAKEVER=$* build
images-fakever-%:
$(MAKE) --no-print-directory FAKEVER=$* images
push-fakever-%:
$(MAKE) --no-print-directory FAKEVER=$* push
.PHONY: all-fakever-build
all-fakever-build: $(addprefix build-fakever-, $(ALL_FAKEVER))
.PHONY: all-fakever-containers
all-fakever-containers: $(addprefix containers-fakever-, $(ALL_FAKEVER))
.PHONY: all-fakever-push
all-fakever-push: $(addprefix push-fakever-, $(ALL_FAKEVER))
##############################################################################
# Rules for dealing with multiple/all architectures at once
build-arch-%:
$(MAKE) --no-print-directory ARCH=$* build
images-arch-%:
$(MAKE) --no-print-directory ARCH=$* images
push-arch-%:
$(MAKE) --no-print-directory ARCH=$* push
.PHONY: all-arch-build
all-arch-build: $(addprefix build-arch-, $(ALL_ARCH))
.PHONY: all-arch-containers
all-arch-containers: $(addprefix containers-arch-, $(ALL_ARCH))
.PHONY: all-arch-push
all-arch-push: $(addprefix push-arch-, $(ALL_ARCH))
##############################################################################
# Deal with all fakevers, all archs
.PHONY: all-build
all-build:
@$(foreach ARCH,$(ALL_ARCH),\
$(foreach FAKEVER,$(ALL_FAKEVER),\
$(MAKE) --no-print-directory ARCH=$(ARCH) FAKEVER=$(FAKEVER) build;))
.PHONY: all-images
all-images:
@$(foreach ARCH,$(ALL_ARCH),\
$(foreach FAKEVER,$(ALL_FAKEVER),\
$(MAKE) --no-print-directory ARCH=$(ARCH) FAKEVER=$(FAKEVER) images;))
.PHONY: all-push
all-push:
@$(foreach ARCH,$(ALL_ARCH),\
$(foreach FAKEVER,$(ALL_FAKEVER),\
$(MAKE) --no-print-directory ARCH=$(ARCH) FAKEVER=$(FAKEVER) push;))
##############################################################################
# Misc commands
.PHONY: version
version:
@echo $(VERSION_BASE)
.PHONY: clean
clean: container-clean bin-clean
.PHONY: container-clean
container-clean:
docker volume rm -f $(BUILD_IMAGE)-data $(BUILD_IMAGE)-node $(VERBOSE_OUTPUT)
rm -f .*-container .*-dockerfile .*-push .*-image
.PHONY: bin-clean
bin-clean:
rm -rf bin
.PHONY: client-clean
rm -rf client/node_modules sitedata/built
.PHONY: help
help:
@echo "make targets"
@echo
@echo " all, build build all binaries"
@echo " images build the container image"
@echo " push push images to the registry"
@echo " clean clean up all files and docker volumes/images"
@echo " help this help message"
@echo " version show package version"
@echo
@echo " {build,images,push}-arch-ARCH do action for specific ARCH"
@echo " all-arch-{build,images,push} do action for all arches"
@echo
@echo " {build,images,push}-fakever-FAKEVER do action for specific FAKEVER"
@echo " all-fakever-{build,images,push} do action for all fakevers"
@echo
@echo " all-{build,images,push} do action fo all arches and all fakevers"
@echo
@echo " Available ARCH: $(ALL_ARCH)"
@echo " Default FAKEVERS: $(ALL_FAKEVER)"
@echo
@echo " Setting VERBOSE=1 will show additional build logging."
@echo
@echo " Setting VERSION_BASE will override the container version tag."