-
Notifications
You must be signed in to change notification settings - Fork 97
/
Makefile
271 lines (236 loc) · 12.6 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
# Copyright 2021 Google LLC
#
# 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
#
# https://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.
# __ __ _ _ _
# \ \ / /_ _ _ __(_) __ _| |__ | | ___ ___
# \ \ / / _` | '__| |/ _` | '_ \| |/ _ \ __|
# \ V / (_| | | | | (_| | |_) | | __\__ \
# \_/ \__,_|_| |_|\__,_|_.__/|_|\___|___/
#
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
build_path := $(dir $(mkfile_path))
project_path := $(realpath $(build_path)/..)
CARGO_HOME ?= $(build_path)/.cargo
BUILD_IMAGE_TAG ?= quilkin-build
rust_toolchain := $(shell grep channel $(project_path)/rust-toolchain.toml | awk '{ print $$3 }')
# if this is a release, don't put the sha, otherwise, leave it off.
ifdef QUILKIN_RELEASE
package_version := $(shell grep -A1 -w "name = \"quilkin\"" $(project_path)/Cargo.toml | grep version -m 1 | awk '{print $$3}')
else
git_sha := $(shell git rev-parse --short=7 HEAD)
package_version := $(shell grep -A1 -w "name = \"quilkin\"" $(project_path)/Cargo.toml | grep version -m 1 | awk '{print $$3}')-${git_sha}
endif
# Set this value if you want to use an external registry
REPOSITORY ?= ""
IMAGE_TAG ?= ${REPOSITORY}quilkin:$(package_version)
MINIKUBE_PROFILE ?= quilkin
CARGO_TARGET_DIR ?= /workspace/target/build-image
common_rust_args := -v $(project_path):/workspace -w /workspace \
-v $(CARGO_HOME)/registry:/usr/local/cargo/registry \
-e "CARGO_TARGET_DIR=$(CARGO_TARGET_DIR)"
KUBECONFIG ?= ~/.kube/config
kubeconfig_path := $(dir $(KUBECONFIG))
helm_config := ~/.config/helm
helm_cache := ~/.cache/helm
kube_mount_args := -v $(kubeconfig_path):/root/.kube -v $(helm_config):/root/.config/helm -v $(helm_cache):/root/.cache/helm
minikube_args := --network=host -v ~/.minikube:$(HOME)/.minikube
gcloud_mount_args := -v $(build_path)/.config/gcloud:/root/.config/gcloud
cargo_build_x86_64_linux := build --release --target x86_64-unknown-linux-gnu
cargo_build_x86_64_apple := build --release --target x86_64-apple-darwin
cargo_build_aarch64-apple := build --release --target aarch64-apple-darwin
cargo_build_x86_64_windows := build --release --target x86_64-pc-windows-gnu
# _____ _
# |_ _|_ _ _ __ __ _ ___| |_ ___
# | |/ _` | '__/ _` |/ _ \ __/ __|
# | | (_| | | | (_| | __/ |_\__ \
# |_|\__,_|_| \__, |\___|\__|___/
# |___/
help: ensure-multi-arch
@cat $(MAKEFILE_LIST) | docker run --rm -i xanders/make-help
ensure-multi-arch:
@docker run --privileged --rm tonistiigi/binfmt --install linux/amd64,linux/arm64 > /dev/null 2>&1
# output the current build version
version:
@echo $(package_version)
# Run all tests
test: ensure-build-image test-quilkin test-examples test-docs
# test only the quilkin crate
test-quilkin: ensure-build-image
docker run --rm $(common_rust_args) \
--entrypoint=cargo $(BUILD_IMAGE_TAG) deny check
docker run --rm $(common_rust_args) \
--entrypoint=cargo $(BUILD_IMAGE_TAG) clippy --tests -- -D warnings
docker run --rm $(common_rust_args) \
--entrypoint=cargo $(BUILD_IMAGE_TAG) fmt -- --check
# --network=host because docker containers are not great at ipv6.
docker run --rm $(common_rust_args) \
--network=host \
-e RUST_BACKTRACE=1 --entrypoint=cargo $(BUILD_IMAGE_TAG) test -- --nocapture
# Run tests against the examples
test-examples: ensure-build-image
docker run --rm $(common_rust_args) -w /workspace/examples/quilkin-filter-example \
--entrypoint=cargo $(BUILD_IMAGE_TAG) clippy --tests -- -D warnings
docker run --rm $(common_rust_args) -w /workspace/examples/quilkin-filter-example \
--entrypoint=cargo $(BUILD_IMAGE_TAG) fmt -- --check
# Run tests against documentation
test-docs: ensure-build-image
test-docs: GITHUB_REF_NAME ?= main
test-docs: QUILKIN_VERSION ?= $(shell $(MAKE) version)
test-docs:
docker run --rm $(common_rust_args) \
-e GITHUB_REF_NAME=$(GITHUB_REF_NAME) -e QUILKIN_VERSION=$(QUILKIN_VERSION) \
--entrypoint=bash $(BUILD_IMAGE_TAG) -c \
'export RUSTDOCFLAGS="-Dwarnings" && mkdir /tmp/docs && \
mkdir -p "$(CARGO_TARGET_DIR)/doc"; \
cargo doc --workspace --no-deps && cd docs && mdbook build --dest-dir /tmp/docs/book && \
cp -r "$(CARGO_TARGET_DIR)/doc" /tmp/docs/api && \
rm /tmp/docs/book/print.html && \
echo "<!doctype html>" > /tmp/docs/book/print.html && \
htmltest -c /workspace/docs/htmltest.yaml /tmp/docs'
# Build all binaries, images and related artifacts
build: binary-archive build-image
# Build all binaries
build-all-binaries: ensure-build-image build-linux-binary build-macos-binary build-windows-binary
# Build an archive all binaries
binary-archive: ensure-build-image build-all-binaries
docker run --rm $(common_rust_args) -w $(CARGO_TARGET_DIR) \
--entrypoint=bash $(BUILD_IMAGE_TAG) -c 'zip ../../quilkin-$(package_version).zip ./*/release/quilkin ./*/release/quilkin.exe'
# Build binary for x86_64-unknown-linux-gnu.
# Use BUILD_LOCAL=1 to build through local cargo rather than through the build container.
build-linux-binary: ensure-build-image
ifdef BUILD_LOCAL
cargo $(cargo_build_x86_64_linux)
else
docker run --rm $(common_rust_args) -e "CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=/usr/bin/x86_64-linux-gnu-gcc" \
--entrypoint=cargo $(BUILD_IMAGE_TAG) $(cargo_build_x86_64_linux)
endif
# Build binary for x86_64-pc-windows-gnu
# Use BUILD_LOCAL=1 to build through local cargo rather than through the build container.
build-windows-binary: ensure-build-image
ifdef BUILD_LOCAL
cargo $(cargo_build_x86_64_windows)
else
docker run --rm $(common_rust_args) -e "CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER=/usr/bin/x86_64-w64-mingw32-gcc" \
--entrypoint=cargo $(BUILD_IMAGE_TAG) $(cargo_build_x86_64_windows)
endif
# Build binary for x86_64-apple-darwin and aarch64-apple-darwin
# Use BUILD_LOCAL=1 to build through local cargo rather than through the build container.
build-macos-binary:
ifdef BUILD_LOCAL
cargo $(cargo_build_x86_64_apple)
cargo $(cargo_build_aarch64-apple)
else
docker run --rm -v $(project_path):/workspace -w /workspace \
-v $(CARGO_HOME)/registry:/root/.cargo/registry \
-e "CARGO_TARGET_DIR=$(CARGO_TARGET_DIR)" \
-e "CC=o64-clang" -e "CXX=o64-clang++" \
-e "PROTOC=/opt/protoc/bin/protoc" \
joseluisq/rust-linux-darwin-builder:$(rust_toolchain) \
sh -c "/workspace/build/darwin-builder/setup.sh && cargo $(cargo_build_x86_64_apple) --no-default-features && \
CC=oa64-clang CXX=oa64-clang++ LIBZ_SYS_STATIC=1 cargo $(cargo_build_aarch64-apple) --no-default-features"
endif
# Build container image.
# Use either `REPOSITORY` to specify a container repository (defaults to blank/none), or use `IMAGE_TAG` argument to specify
# the entire image name and tag. Defaults to `quilkin:${version}-${git-sha}`.
# Use BUILD_LOCAL=1 to build the binary through local cargo rather than through the build container.
build-image: ensure-build-image build-linux-binary
build-image:
-mkdir -p "$(project_path)/target/image/"
ifdef BUILD_LOCAL
cp "$(project_path)/target/x86_64-unknown-linux-gnu/release/quilkin" "$(project_path)/target/image/"
else
cp "$(project_path)/target/build-image/x86_64-unknown-linux-gnu/release/quilkin" "$(project_path)/target/image/"
endif
docker run --rm $(common_rust_args) \
--entrypoint=bash $(BUILD_IMAGE_TAG) -c 'cargo about generate license.html.hbs > license.html'
docker run --rm $(common_rust_args) \
--entrypoint=bash $(BUILD_IMAGE_TAG) -c './image/archive_dependencies.sh'
docker build --platform=linux/amd64 -t $(IMAGE_TAG) -f $(project_path)/image/Dockerfile $(project_path)
# Builds Quilkin, pushes it to a repository (use REPOSITORY arg to set value)
# and then runs the Agones integration tests. See targets `build-images` and `push` for more options and details.
# Not part of `test` as it requires a Kubernetes cluster to be provisioned and running.
# To pass extra arguments to `cargo test`, to run only a single test, for example, use the `ARGS` variable
# to set those options.
# If a `kubectl` authentication failure occurs, run `kubectl get ns` to confirm access and refresh the Kubernetes
# authentication token, and try again if successful.
test-agones: push
test-agones:
$(MAKE) run-test-agones
run-test-agones: ensure-kube-dirs
run-test-agones:
docker run --rm $(DOCKER_RUN_ARGS) $(common_rust_args) -w /workspace/agones \
--entrypoint=cargo $(BUILD_IMAGE_TAG) clippy --tests -- -D warnings
docker run --rm $(DOCKER_RUN_ARGS) $(common_rust_args) -w /workspace/agones \
--entrypoint=cargo $(BUILD_IMAGE_TAG) fmt -- --check
# Confirm access to K8s, and force refresh of the auth token for the kube context
docker run --rm $(DOCKER_RUN_ARGS) $(common_rust_args) $(kube_mount_args) -w /workspace/agones \
--entrypoint=kubectl $(BUILD_IMAGE_TAG) get ns
docker run --rm $(DOCKER_RUN_ARGS) $(common_rust_args) $(kube_mount_args) -w /workspace/agones \
-e "RUST_BACKTRACE=1" -e "IMAGE_TAG=${IMAGE_TAG}" --entrypoint=cargo $(BUILD_IMAGE_TAG) test $(ARGS)
# Convenience target to build and push quilkin images to a repository.
# Use `REPOSITORY` arg to specify the repository to push to.
# USe `SKIP_BUILD_IMAGE` if you want to skip building the image if it has been already built.
# See `build-image` for more details.
push:
ifndef SKIP_BUILD_IMAGE
push: build-image
endif
docker push $(IMAGE_TAG)
# Convenience target to build and push quilkin images into a minikube instance
# Use `MINIKUBE_PROFILE` to specify the profile. Defaults to `quilkin`.
minikube-push: build-image
minikube image load $(IMAGE_TAG) -p $(MINIKUBE_PROFILE)
# Builds Quilkin, pushes it to a minikube instance (use `MINIKUBE_PROFILE` to specify the profile. Defaults to `quilkin`)
# and then runs the Agones integration tests. See targets `build-images` and `minikube-push` for more options and details.
# Not part of `test` as it requires a Kubernetes cluster to be provisioned and running.
# To pass extra arguments to `cargo test`, to run only a single test, for example, use the `ARGS` variable
# to set those options.
minikube-test-agones: minikube-push
$(MAKE) DOCKER_RUN_ARGS="$(minikube_args)" run-test-agones
# Runs mdbook and cargo doc in the same directory structure as what is hosted on Github pages.
# Open http://localhost:3000/book/index.html or http://localhost:3000/api/quilkin/index.html after running. Pages will live reload on change.
# (the .html extension is required for hot reload, but pages will display without it)
# Use `GITHUB_REF_NAME` (default: `main`) to specify the branch or tag to point Github links to and `QUILKIN_VERSION`
# (default `$(make version)) to specify which Quilkin version the documentation uses for examples.
docs: ensure-build-image
docs: GITHUB_REF_NAME ?= main
docs: QUILKIN_VERSION ?= $(shell $(MAKE) version)
docs:
@echo "📭 Open browser to http://localhost:3000/book/index.html or http://localhost:3000/api/quilkin/index.html (the .html extension is required for hot reload)"
docker run -it --rm $(common_rust_args) -p 3000:3000 \
-e GITHUB_REF_NAME=$(GITHUB_REF_NAME) -e QUILKIN_VERSION=$(QUILKIN_VERSION) \
--entrypoint=bash $(BUILD_IMAGE_TAG) -c \
'mkdir /tmp/docs && (live-server -p 3000 /tmp/docs &) && \
mkdir -p "$(CARGO_TARGET_DIR)/doc"; ln -s "$(CARGO_TARGET_DIR)/doc" /tmp/docs/api && \
cargo watch -s "cargo doc --workspace --no-deps && cd docs && mdbook build --dest-dir /tmp/docs/book"'
# Start an interactive shell inside the build image
# Useful for testing, or adhoc cargo, gcloud, kubectl or terraform commands
shell: ensure-gcloud-dirs ensure-kube-dirs ensure-build-image
# we --network=host because docker containers are not great at ipv6.
docker run --rm -it $(DOCKER_RUN_ARGS) $(common_rust_args) \
$(gcloud_mount_args) $(kube_mount_args) \
--network=host \
--entrypoint=bash $(BUILD_IMAGE_TAG)
ensure-build-image: ensure-cargo-registry
docker build $(BUILD_IMAGE_ARG) --build-arg RUST_TOOLCHAIN=$(rust_toolchain) --tag=$(BUILD_IMAGE_TAG) $(build_path)/build-image/
ensure-cargo-registry:
-mkdir -p $(CARGO_HOME)/registry
ensure-gcloud-dirs:
-mkdir -p $(build_path)/.gcloud
ensure-kube-dirs:
-mkdir -p ~/.config/helm
-mkdir -p ~/.kube
ci-gcloud-auth-cluster:
# Internal CI target. Used to authenticate against the integration test cluster.
docker run --rm $(DOCKER_RUN_ARGS) $(common_rust_args) $(kube_mount_args) --network=cloudbuild \
-e "USE_GKE_GCLOUD_AUTH_PLUGIN=True" --entrypoint=gcloud $(BUILD_IMAGE_TAG) container clusters get-credentials agones --zone us-west1-c