-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.sh
More file actions
executable file
·497 lines (430 loc) · 19 KB
/
Copy pathcommon.sh
File metadata and controls
executable file
·497 lines (430 loc) · 19 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#!/usr/bin/env bash
# Copyright 2020 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
# Use of this source code is governed by a MIT style
# license that can be found in the LICENSE file.
# shellcheck disable=SC2034 # Variables sourced in other scripts.
# Common utilities, variables and checks for all build scripts.
set -o errexit
set -o nounset
set -o pipefail
# Unset CDPATH, having it set messes up with script import paths
unset CDPATH
USER_ID=$(id -u)
GROUP_ID=$(id -g)
DOCKER_OPTS=${DOCKER_OPTS:-""}
IFS=" " read -r -a DOCKER <<< "docker ${DOCKER_OPTS}"
DOCKER_HOST=${DOCKER_HOST:-""}
DOCKER_MACHINE_NAME=${DOCKER_MACHINE_NAME:-"kubecub-dev"}
readonly DOCKER_MACHINE_DRIVER=${DOCKER_MACHINE_DRIVER:-"virtualbox --virtualbox-cpu-count -1"}
# This will canonicalize the path
KUBECUB_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd -P)
source "${KUBECUB_ROOT}/scripts/lib/init.sh"
# Constants
readonly KUBECUB_BUILD_IMAGE_REPO=kubecub-build
#readonly KUBECUB_BUILD_IMAGE_CROSS_TAG="$(cat "${KUBECUB_ROOT}/build/build-image/cross/VERSION")"
readonly KUBECUB_DOCKER_REGISTRY="${KUBECUB_DOCKER_REGISTRY:-k8s.gcr.io}"
readonly KUBECUB_BASE_IMAGE_REGISTRY="${KUBECUB_BASE_IMAGE_REGISTRY:-us.gcr.io/k8s-artifacts-prod/build-image}"
# This version number is used to cause everyone to rebuild their data containers
# and build image. This is especially useful for automated build systems like
# Jenkins.
#
# Increment/change this number if you change the build image (anything under
# build/build-image) or change the set of volumes in the data container.
#readonly KUBECUB_BUILD_IMAGE_VERSION_BASE="$(cat "${KUBECUB_ROOT}/build/build-image/VERSION")"
#readonly KUBECUB_BUILD_IMAGE_VERSION="${KUBECUB_BUILD_IMAGE_VERSION_BASE}-${KUBECUB_BUILD_IMAGE_CROSS_TAG}"
# Here we map the output directories across both the local and remote _output
# directories:
#
# *_OUTPUT_ROOT - the base of all output in that environment.
# *_OUTPUT_SUBPATH - location where golang stuff is built/cached. Also
# persisted across docker runs with a volume mount.
# *_OUTPUT_BINPATH - location where final binaries are placed. If the remote
# is really remote, this is the stuff that has to be copied
# back.
# OUT_DIR can come in from the Makefile, so honor it.
readonly LOCAL_OUTPUT_ROOT="${KUBECUB_ROOT}/${OUT_DIR:-_output}"
readonly LOCAL_OUTPUT_SUBPATH="${LOCAL_OUTPUT_ROOT}/platforms"
readonly LOCAL_OUTPUT_BINPATH="${LOCAL_OUTPUT_SUBPATH}"
readonly LOCAL_OUTPUT_GOPATH="${LOCAL_OUTPUT_SUBPATH}/go"
readonly LOCAL_OUTPUT_IMAGE_STAGING="${LOCAL_OUTPUT_ROOT}/images"
# This is the port on the workstation host to expose RSYNC on. Set this if you
# are doing something fancy with ssh tunneling.
readonly KUBECUB_RSYNC_PORT="${KUBECUB_RSYNC_PORT:-}"
# This is the port that rsync is running on *inside* the container. This may be
# mapped to KUBECUB_RSYNC_PORT via docker networking.
readonly KUBECUB_CONTAINER_RSYNC_PORT=8730
# Get the set of master binaries that run in Docker (on Linux)
# Entry format is "<name-of-binary>,<base-image>".
# Binaries are placed in /usr/local/bin inside the image.
#
# $1 - server architecture
kubecub::build::get_docker_wrapped_binaries() {
local arch=$1
local debian_base_version=v2.1.0
local debian_iptables_version=v12.1.0
### If you change any of these lists, please also update DOCKERIZED_BINARIES
### in build/BUILD. And kubecub::golang::server_image_targets
local targets=(
"kubecub-apiserver,${KUBECUB_BASE_IMAGE_REGISTRY}/debian-base-${arch}:${debian_base_version}"
"kubecub-controller-manager,${KUBECUB_BASE_IMAGE_REGISTRY}/debian-base-${arch}:${debian_base_version}"
"kubecub-scheduler,${KUBECUB_BASE_IMAGE_REGISTRY}/debian-base-${arch}:${debian_base_version}"
"kubecub-proxy,${KUBECUB_BASE_IMAGE_REGISTRY}/debian-iptables-${arch}:${debian_iptables_version}"
)
echo "${targets[@]}"
}
# ---------------------------------------------------------------------------
# Basic setup functions
# Verify that the right utilities and such are installed for building kubecub. Set
# up some dynamic constants.
# Args:
# $1 - boolean of whether to require functioning docker (default true)
#
# Vars set:
# KUBECUB_ROOT_HASH
# KUBECUB_BUILD_IMAGE_TAG_BASE
# KUBECUB_BUILD_IMAGE_TAG
# KUBECUB_BUILD_IMAGE
# KUBECUB_BUILD_CONTAINER_NAME_BASE
# KUBECUB_BUILD_CONTAINER_NAME
# KUBECUB_DATA_CONTAINER_NAME_BASE
# KUBECUB_DATA_CONTAINER_NAME
# KUBECUB_RSYNC_CONTAINER_NAME_BASE
# KUBECUB_RSYNC_CONTAINER_NAME
# DOCKER_MOUNT_ARGS
# LOCAL_OUTPUT_BUILD_CONTEXT
function kubecub::build::verify_prereqs() {
local -r require_docker=${1:-true}
kubecub::log::status "Verifying Prerequisites...."
kubecub::build::ensure_tar || return 1
kubecub::build::ensure_rsync || return 1
if ${require_docker}; then
kubecub::build::ensure_docker_in_path || return 1
kubecub::util::ensure_docker_daemon_connectivity || return 1
if (( KUBECUB_VERBOSE > 6 )); then
kubecub::log::status "Docker Version:"
"${DOCKER[@]}" version | kubecub::log::info_from_stdin
fi
fi
KUBECUB_GIT_BRANCH=$(git symbolic-ref --short -q HEAD 2>/dev/null || true)
KUBECUB_ROOT_HASH=$(kubecub::build::short_hash "${HOSTNAME:-}:${KUBECUB_ROOT}:${KUBECUB_GIT_BRANCH}")
KUBECUB_BUILD_IMAGE_TAG_BASE="build-${KUBECUB_ROOT_HASH}"
#KUBECUB_BUILD_IMAGE_TAG="${KUBECUB_BUILD_IMAGE_TAG_BASE}-${KUBECUB_BUILD_IMAGE_VERSION}"
#KUBECUB_BUILD_IMAGE="${KUBECUB_BUILD_IMAGE_REPO}:${KUBECUB_BUILD_IMAGE_TAG}"
KUBECUB_BUILD_CONTAINER_NAME_BASE="kubecub-build-${KUBECUB_ROOT_HASH}"
#KUBECUB_BUILD_CONTAINER_NAME="${KUBECUB_BUILD_CONTAINER_NAME_BASE}-${KUBECUB_BUILD_IMAGE_VERSION}"
KUBECUB_RSYNC_CONTAINER_NAME_BASE="kubecub-rsync-${KUBECUB_ROOT_HASH}"
#KUBECUB_RSYNC_CONTAINER_NAME="${KUBECUB_RSYNC_CONTAINER_NAME_BASE}-${KUBECUB_BUILD_IMAGE_VERSION}"
KUBECUB_DATA_CONTAINER_NAME_BASE="kubecub-build-data-${KUBECUB_ROOT_HASH}"
#KUBECUB_DATA_CONTAINER_NAME="${KUBECUB_DATA_CONTAINER_NAME_BASE}-${KUBECUB_BUILD_IMAGE_VERSION}"
#DOCKER_MOUNT_ARGS=(--volumes-from "${KUBECUB_DATA_CONTAINER_NAME}")
#LOCAL_OUTPUT_BUILD_CONTEXT="${LOCAL_OUTPUT_IMAGE_STAGING}/${KUBECUB_BUILD_IMAGE}"
kubecub::version::get_version_vars
#kubecub::version::save_version_vars "${KUBECUB_ROOT}/.dockerized-kubecub-version-defs"
}
# ---------------------------------------------------------------------------
# Utility functions
function kubecub::build::docker_available_on_osx() {
if [[ -z "${DOCKER_HOST}" ]]; then
if [[ -S "/var/run/docker.sock" ]]; then
kubecub::log::status "Using Docker for MacOS"
return 0
fi
kubecub::log::status "No docker host is set. Checking options for setting one..."
if [[ -z "$(which docker-machine)" ]]; then
kubecub::log::status "It looks like you're running Mac OS X, yet neither Docker for Mac nor docker-machine can be found."
kubecub::log::status "See: https://docs.docker.com/engine/installation/mac/ for installation instructions."
return 1
elif [[ -n "$(which docker-machine)" ]]; then
kubecub::build::prepare_docker_machine
fi
fi
}
function kubecub::build::prepare_docker_machine() {
kubecub::log::status "docker-machine was found."
local available_memory_bytes
available_memory_bytes=$(sysctl -n hw.memsize 2>/dev/null)
local bytes_in_mb=1048576
# Give virtualbox 1/2 the system memory. Its necessary to divide by 2, instead
# of multiple by .5, because bash can only multiply by ints.
local memory_divisor=2
local virtualbox_memory_mb=$(( available_memory_bytes / (bytes_in_mb * memory_divisor) ))
docker-machine inspect "${DOCKER_MACHINE_NAME}" &> /dev/null || {
kubecub::log::status "Creating a machine to build kubecub"
docker-machine create --driver "${DOCKER_MACHINE_DRIVER}" \
--virtualbox-memory "${virtualbox_memory_mb}" \
--engine-env HTTP_PROXY="${kubecubRNETES_HTTP_PROXY:-}" \
--engine-env HTTPS_PROXY="${kubecubRNETES_HTTPS_PROXY:-}" \
--engine-env NO_PROXY="${kubecubRNETES_NO_PROXY:-127.0.0.1}" \
"${DOCKER_MACHINE_NAME}" > /dev/null || {
kubecub::log::error "Something went wrong creating a machine."
kubecub::log::error "Try the following: "
kubecub::log::error "docker-machine create -d ${DOCKER_MACHINE_DRIVER} --virtualbox-memory ${virtualbox_memory_mb} ${DOCKER_MACHINE_NAME}"
return 1
}
}
docker-machine start "${DOCKER_MACHINE_NAME}" &> /dev/null
# it takes `docker-machine env` a few seconds to work if the machine was just started
local docker_machine_out
while ! docker_machine_out=$(docker-machine env "${DOCKER_MACHINE_NAME}" 2>&1); do
if [[ ${docker_machine_out} =~ "Error checking TLS connection" ]]; then
echo "${docker_machine_out}"
docker-machine regenerate-certs "${DOCKER_MACHINE_NAME}"
else
sleep 1
fi
done
eval "$(docker-machine env "${DOCKER_MACHINE_NAME}")"
kubecub::log::status "A Docker host using docker-machine named '${DOCKER_MACHINE_NAME}' is ready to go!"
return 0
}
function kubecub::build::is_gnu_sed() {
[[ $(sed --version 2>&1) == *GNU* ]]
}
function kubecub::build::ensure_rsync() {
if [[ -z "$(which rsync)" ]]; then
kubecub::log::error "Can't find 'rsync' in PATH, please fix and retry."
return 1
fi
}
function kubecub::build::update_dockerfile() {
if kubecub::build::is_gnu_sed; then
sed_opts=(-i)
else
sed_opts=(-i '')
fi
sed "${sed_opts[@]}" "s/KUBECUB_BUILD_IMAGE_CROSS_TAG/${KUBECUB_BUILD_IMAGE_CROSS_TAG}/" "${LOCAL_OUTPUT_BUILD_CONTEXT}/Dockerfile"
}
function kubecub::build::set_proxy() {
if [[ -n "${kubecubRNETES_HTTPS_PROXY:-}" ]]; then
echo "ENV https_proxy $kubecubRNETES_HTTPS_PROXY" >> "${LOCAL_OUTPUT_BUILD_CONTEXT}/Dockerfile"
fi
if [[ -n "${kubecubRNETES_HTTP_PROXY:-}" ]]; then
echo "ENV http_proxy $kubecubRNETES_HTTP_PROXY" >> "${LOCAL_OUTPUT_BUILD_CONTEXT}/Dockerfile"
fi
if [[ -n "${kubecubRNETES_NO_PROXY:-}" ]]; then
echo "ENV no_proxy $kubecubRNETES_NO_PROXY" >> "${LOCAL_OUTPUT_BUILD_CONTEXT}/Dockerfile"
fi
}
function kubecub::build::ensure_docker_in_path() {
if [[ -z "$(which docker)" ]]; then
kubecub::log::error "Can't find 'docker' in PATH, please fix and retry."
kubecub::log::error "See https://docs.docker.com/installation/#installation for installation instructions."
return 1
fi
}
function kubecub::build::ensure_tar() {
if [[ -n "${TAR:-}" ]]; then
return
fi
# Find gnu tar if it is available, bomb out if not.
TAR=tar
if which gtar &>/dev/null; then
TAR=gtar
else
if which gnutar &>/dev/null; then
TAR=gnutar
fi
fi
if ! "${TAR}" --version | grep -q GNU; then
echo " !!! Cannot find GNU tar. Build on Linux or install GNU tar"
echo " on Mac OS X (brew install gnu-tar)."
return 1
fi
}
function kubecub::build::has_docker() {
which docker &> /dev/null
}
function kubecub::build::has_ip() {
which ip &> /dev/null && ip -Version | grep 'iproute2' &> /dev/null
}
# Detect if a specific image exists
#
# $1 - image repo name
# $2 - image tag
function kubecub::build::docker_image_exists() {
[[ -n $1 && -n $2 ]] || {
kubecub::log::error "Internal error. Image not specified in docker_image_exists."
exit 2
}
[[ $("${DOCKER[@]}" images -q "${1}:${2}") ]]
}
# Delete all images that match a tag prefix except for the "current" version
#
# $1: The image repo/name
# $2: The tag base. We consider any image that matches $2*
# $3: The current image not to delete if provided
function kubecub::build::docker_delete_old_images() {
# In Docker 1.12, we can replace this with
# docker images "$1" --format "{{.Tag}}"
for tag in $("${DOCKER[@]}" images "${1}" | tail -n +2 | awk '{print $2}') ; do
if [[ "${tag}" != "${2}"* ]] ; then
V=3 kubecub::log::status "Keeping image ${1}:${tag}"
continue
fi
if [[ -z "${3:-}" || "${tag}" != "${3}" ]] ; then
V=2 kubecub::log::status "Deleting image ${1}:${tag}"
"${DOCKER[@]}" rmi "${1}:${tag}" >/dev/null
else
V=3 kubecub::log::status "Keeping image ${1}:${tag}"
fi
done
}
# Stop and delete all containers that match a pattern
#
# $1: The base container prefix
# $2: The current container to keep, if provided
function kubecub::build::docker_delete_old_containers() {
# In Docker 1.12 we can replace this line with
# docker ps -a --format="{{.Names}}"
for container in $("${DOCKER[@]}" ps -a | tail -n +2 | awk '{print $NF}') ; do
if [[ "${container}" != "${1}"* ]] ; then
V=3 kubecub::log::status "Keeping container ${container}"
continue
fi
if [[ -z "${2:-}" || "${container}" != "${2}" ]] ; then
V=2 kubecub::log::status "Deleting container ${container}"
kubecub::build::destroy_container "${container}"
else
V=3 kubecub::log::status "Keeping container ${container}"
fi
done
}
# Takes $1 and computes a short has for it. Useful for unique tag generation
function kubecub::build::short_hash() {
[[ $# -eq 1 ]] || {
kubecub::log::error "Internal error. No data based to short_hash."
exit 2
}
local short_hash
if which md5 >/dev/null 2>&1; then
short_hash=$(md5 -q -s "$1")
else
short_hash=$(echo -n "$1" | md5sum)
fi
echo "${short_hash:0:10}"
}
# Pedantically kill, wait-on and remove a container. The -f -v options
# to rm don't actually seem to get the job done, so force kill the
# container, wait to ensure it's stopped, then try the remove. This is
# a workaround for bug https://github.com/docker/docker/issues/3968.
function kubecub::build::destroy_container() {
"${DOCKER[@]}" kill "$1" >/dev/null 2>&1 || true
if [[ $("${DOCKER[@]}" version --format '{{.Server.Version}}') = 17.06.0* ]]; then
# Workaround https://github.com/moby/moby/issues/33948.
# TODO: remove when 17.06.0 is not relevant anymore
DOCKER_API_VERSION=v1.29 "${DOCKER[@]}" wait "$1" >/dev/null 2>&1 || true
else
"${DOCKER[@]}" wait "$1" >/dev/null 2>&1 || true
fi
"${DOCKER[@]}" rm -f -v "$1" >/dev/null 2>&1 || true
}
# ---------------------------------------------------------------------------
# Building
function kubecub::build::clean() {
if kubecub::build::has_docker ; then
kubecub::build::docker_delete_old_containers "${KUBECUB_BUILD_CONTAINER_NAME_BASE}"
kubecub::build::docker_delete_old_containers "${KUBECUB_RSYNC_CONTAINER_NAME_BASE}"
kubecub::build::docker_delete_old_containers "${KUBECUB_DATA_CONTAINER_NAME_BASE}"
kubecub::build::docker_delete_old_images "${KUBECUB_BUILD_IMAGE_REPO}" "${KUBECUB_BUILD_IMAGE_TAG_BASE}"
V=2 kubecub::log::status "Cleaning all untagged docker images"
"${DOCKER[@]}" rmi "$("${DOCKER[@]}" images -q --filter 'dangling=true')" 2> /dev/null || true
fi
if [[ -d "${LOCAL_OUTPUT_ROOT}" ]]; then
kubecub::log::status "Removing _output directory"
rm -rf "${LOCAL_OUTPUT_ROOT}"
fi
}
# Set up the context directory for the kubecub-build image and build it.
function kubecub::build::build_image() {
mkdir -p "${LOCAL_OUTPUT_BUILD_CONTEXT}"
# Make sure the context directory owned by the right user for syncing sources to container.
chown -R "${USER_ID}":"${GROUP_ID}" "${LOCAL_OUTPUT_BUILD_CONTEXT}"
cp /etc/localtime "${LOCAL_OUTPUT_BUILD_CONTEXT}/"
cp "${KUBECUB_ROOT}/build/build-image/Dockerfile" "${LOCAL_OUTPUT_BUILD_CONTEXT}/Dockerfile"
cp "${KUBECUB_ROOT}/build/build-image/rsyncd.sh" "${LOCAL_OUTPUT_BUILD_CONTEXT}/"
dd if=/dev/urandom bs=512 count=1 2>/dev/null | LC_ALL=C tr -dc 'A-Za-z0-9' | dd bs=32 count=1 2>/dev/null > "${LOCAL_OUTPUT_BUILD_CONTEXT}/rsyncd.password"
chmod go= "${LOCAL_OUTPUT_BUILD_CONTEXT}/rsyncd.password"
kubecub::build::update_dockerfile
kubecub::build::set_proxy
kubecub::build::docker_build "${KUBECUB_BUILD_IMAGE}" "${LOCAL_OUTPUT_BUILD_CONTEXT}" 'false'
# Clean up old versions of everything
kubecub::build::docker_delete_old_containers "${KUBECUB_BUILD_CONTAINER_NAME_BASE}" "${KUBECUB_BUILD_CONTAINER_NAME}"
kubecub::build::docker_delete_old_containers "${KUBECUB_RSYNC_CONTAINER_NAME_BASE}" "${KUBECUB_RSYNC_CONTAINER_NAME}"
kubecub::build::docker_delete_old_containers "${KUBECUB_DATA_CONTAINER_NAME_BASE}" "${KUBECUB_DATA_CONTAINER_NAME}"
kubecub::build::docker_delete_old_images "${KUBECUB_BUILD_IMAGE_REPO}" "${KUBECUB_BUILD_IMAGE_TAG_BASE}" "${KUBECUB_BUILD_IMAGE_TAG}"
kubecub::build::ensure_data_container
kubecub::build::sync_to_container
}
# Build a docker image from a Dockerfile.
# $1 is the name of the image to build
# $2 is the location of the "context" directory, with the Dockerfile at the root.
# $3 is the value to set the --pull flag for docker build; true by default
function kubecub::build::docker_build() {
local -r image=$1
local -r context_dir=$2
local -r pull="${3:-true}"
local -ra build_cmd=("${DOCKER[@]}" build -t "${image}" "--pull=${pull}" "${context_dir}")
kubecub::log::status "Building Docker image ${image}"
local docker_output
docker_output=$("${build_cmd[@]}" 2>&1) || {
cat <<EOF >&2
+++ Docker build command failed for ${image}
${docker_output}
To retry manually, run:
${build_cmd[*]}
EOF
return 1
}
}
function kubecub::build::ensure_data_container() {
# If the data container exists AND exited successfully, we can use it.
# Otherwise nuke it and start over.
local ret=0
local code=0
code=$(docker inspect \
-f '{{.State.ExitCode}}' \
"${KUBECUB_DATA_CONTAINER_NAME}" 2>/dev/null) || ret=$?
if [[ "${ret}" == 0 && "${code}" != 0 ]]; then
kubecub::build::destroy_container "${KUBECUB_DATA_CONTAINER_NAME}"
ret=1
fi
if [[ "${ret}" != 0 ]]; then
kubecub::log::status "Creating data container ${KUBECUB_DATA_CONTAINER_NAME}"
# We have to ensure the directory exists, or else the docker run will
# create it as root.
mkdir -p "${LOCAL_OUTPUT_GOPATH}"
# We want this to run as root to be able to chown, so non-root users can
# later use the result as a data container. This run both creates the data
# container and chowns the GOPATH.
#
# The data container creates volumes for all of the directories that store
# intermediates for the Go build. This enables incremental builds across
# Docker sessions. The *_cgo paths are re-compiled versions of the go std
# libraries for true static building.
local -ra docker_cmd=(
"${DOCKER[@]}" run
--volume "${REMOTE_ROOT}" # white-out the whole output dir
--volume /usr/local/go/pkg/linux_386_cgo
--volume /usr/local/go/pkg/linux_amd64_cgo
--volume /usr/local/go/pkg/linux_arm_cgo
--volume /usr/local/go/pkg/linux_arm64_cgo
--volume /usr/local/go/pkg/linux_ppc64le_cgo
--volume /usr/local/go/pkg/darwin_amd64_cgo
--volume /usr/local/go/pkg/darwin_386_cgo
--volume /usr/local/go/pkg/windows_amd64_cgo
--volume /usr/local/go/pkg/windows_386_cgo
--name "${KUBECUB_DATA_CONTAINER_NAME}"
--hostname "${HOSTNAME}"
"${KUBECUB_BUILD_IMAGE}"
chown -R "${USER_ID}":"${GROUP_ID}"
"${REMOTE_ROOT}"
/usr/local/go/pkg/
)
"${docker_cmd[@]}"
fi
}
# Build all kubecub commands.
function kubecub::build::build_command() {
kubecub::log::status "Running build command..."
make -C "${KUBECUB_ROOT}" build.multiarch BINS="kubecubctl kubecub-apiserver kubecub-authz-server kubecub-pump kubecub-watcher"
}