Skip to content

Commit 54636d7

Browse files
refactor the Makefile and build scrtips, allow users to specify binary and (#113)
architecture combinations Example: # compile yurthub, yurt-controller-manager and yurtctl-servant with # architectures arm64 and arm in the mainland China make release WHAT="yurthub yurt-controller-manager yurtctl-servant" ARCH="arm64 arm" REGION=cn
1 parent 579a886 commit 54636d7

File tree

7 files changed

+230
-114
lines changed

7 files changed

+230
-114
lines changed

Makefile

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
# Copyright 2020 The OpenYurt Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
.PHONY: clean all release build
216

317
all: test build
418

5-
# Build binaries
19+
# Build binaries in the host environment
620
build:
7-
hack/make-rules/build.sh $(WHAT)
21+
bash hack/make-rules/build.sh $(WHAT)
822

923
# generate yaml files
1024
gen-yaml:
@@ -22,8 +36,24 @@ fmt:
2236
vet:
2337
go vet ./pkg/... ./cmd/...
2438

39+
# Build binaries and docker images.
40+
# NOTE: this rule can take time, as we build binaries inside containers
41+
#
42+
# ARGS:
43+
# WHAT: list of components that will be compiled.
44+
# ARCH: list of target architectures.
45+
# REGION: in which region this rule is executed, if in mainland China,
46+
# set it as cn.
47+
#
48+
# Examples:
49+
# # compile yurthub, yurt-controller-manager and yurtctl-servant with
50+
# # architectures arm64 and arm in the mainland China
51+
# make release WHAT="yurthub yurt-controller-manager yurtctl-servant" ARCH="arm64 arm" REGION=cn
52+
#
53+
# # compile all components with all architectures (i.e., amd64, arm64, arm)
54+
# make relase
2555
release:
26-
build/release-images.sh $(ARCH)
56+
hack/make-rules/release-images.sh
2757

2858
clean:
2959
-rm -Rf _output

build/release-images.sh

Lines changed: 0 additions & 102 deletions
This file was deleted.

hack/lib/build.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
#!/usr/bin/env bash
1616

17+
set -x
18+
1719
readonly YURT_ALL_TARGETS=(
18-
cmd/yurtctl
19-
cmd/yurthub
20-
cmd/yurt-controller-manager
21-
cmd/yurt-tunnel-server
22-
cmd/yurt-tunnel-agent
20+
yurtctl
21+
yurthub
22+
yurt-controller-manager
23+
yurt-tunnel-server
24+
yurt-tunnel-agent
2325
)
2426

2527
# we will generates setup yaml files for following components
@@ -51,7 +53,7 @@ project_info() {
5153
# is set, it subsitutes the prefix of the executable's name with the env,
5254
# otherwise the basename of the target is used
5355
get_output_name() {
54-
local oup_name=$(basename $1)
56+
local oup_name=$1
5557
PROJECT_PREFIX=${PROJECT_PREFIX:-}
5658
if [ ! -z $PROJECT_PREFIX ]; then
5759
oup_name=${oup_name/yurt/$PROJECT_PREFIX}
@@ -94,7 +96,7 @@ build_binaries() {
9496
echo "Building ${binary}"
9597
go build -o $(get_output_name $binary) \
9698
-ldflags "${goldflags:-}" \
97-
-gcflags "${gcflags:-}" ${goflags} $YURT_ROOT/${binary}
99+
-gcflags "${gcflags:-}" ${goflags} $YURT_ROOT/cmd/${binary}
98100
done
99101
}
100102

hack/lib/init.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ set -o pipefail
2020

2121
YURT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)"
2222
YURT_MOD="$(head -1 $YURT_ROOT/go.mod | awk '{print $2}')"
23-
YURT_OUTPUT_DIR=${YURT_ROOT}/_output/
24-
YURT_BIN_DIR=${YURT_OUTPUT_DIR}/bin/
23+
YURT_OUTPUT_DIR=${YURT_ROOT}/_output
24+
YURT_BIN_DIR=${YURT_OUTPUT_DIR}/bin
2525

2626
source "${YURT_ROOT}/hack/lib/build.sh"
27+
source "${YURT_ROOT}/hack/lib/release-images.sh"

hack/lib/release-images.sh

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# Copyright 2020 The OpenYurt Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
#!/usr/bin/env bash
16+
17+
YURT_IMAGE_DIR=${YURT_OUTPUT_DIR}/images
18+
YURTCTL_SERVANT_DIR=${YURT_ROOT}/config/yurtctl-servant
19+
DOCKER_BUILD_BASE_IDR=$YURT_ROOT/dockerbuild
20+
YURT_BUILD_IMAGE="golang:1.13-alpine"
21+
REPO="openyurt"
22+
TAG="v0.1.1"
23+
24+
readonly -a YURT_BIN_TARGETS=(
25+
yurthub
26+
yurt-controller-manager
27+
yurtctl-servant
28+
yurt-tunnel-server
29+
yurt-tunnel-agent
30+
)
31+
32+
readonly -a SUPPORTED_ARCH=(
33+
amd64
34+
arm
35+
arm64
36+
)
37+
38+
readonly SUPPORTED_OS=linux
39+
40+
readonly -a bin_targets=(${WHAT:-${YURT_BIN_TARGETS[@]}})
41+
readonly -a bin_targets_without_servant=(${bin_targets[@]/yurtctl-servant})
42+
readonly -a target_arch=(${ARCH:-${SUPPORTED_ARCH[@]}})
43+
readonly region=${REGION:-us}
44+
45+
function build_multi_arch_binaries() {
46+
local docker_run_opts=(
47+
"-i"
48+
"--rm"
49+
"--network host"
50+
"-v ${YURT_ROOT}:/opt/src"
51+
"--env CGO_ENABLED=0"
52+
"--env GOOS=${SUPPORTED_OS}"
53+
)
54+
# use goproxy if build from inside mainland China
55+
[[ $region == "cn" ]] && docker_run_opts+=("--env GOPROXY=https://goproxy.cn")
56+
57+
local docker_run_cmd=(
58+
"/bin/sh"
59+
"-xe"
60+
"-c"
61+
)
62+
63+
local sub_commands="sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories; \
64+
apk --no-cache add bash git; \
65+
cd /opt/src; umask 0022; \
66+
rm -rf ${YURT_BIN_DIR}/* ;"
67+
for arch in ${target_arch[@]}; do
68+
sub_commands+="GOARCH=$arch bash ./hack/make-rules/build.sh $(echo ${bin_targets_without_servant[@]}); "
69+
done
70+
sub_commands+="chown -R $(id -u):$(id -g) /opt/src/_output"
71+
72+
docker run ${docker_run_opts[@]} ${YURT_BUILD_IMAGE} ${docker_run_cmd[@]} "${sub_commands}"
73+
}
74+
75+
function build_docker_image() {
76+
for arch in ${target_arch[@]}; do
77+
for binary in "${bin_targets_without_servant[@]}"; do
78+
local binary_path=${YURT_BIN_DIR}/${SUPPORTED_OS}/${arch}/${binary}
79+
if [ -f ${binary_path} ]; then
80+
local docker_build_path=${DOCKER_BUILD_BASE_IDR}/${SUPPORTED_OS}/${arch}
81+
local docker_file_path=${docker_build_path}/Dockerfile.${binary}-${arch}
82+
mkdir -p ${docker_build_path}
83+
84+
local yurt_component_image="${REPO}/${binary}:${TAG}-${arch}"
85+
local base_image="k8s.gcr.io/debian-iptables-${arch}:v11.0.2"
86+
cat <<EOF > "${docker_file_path}"
87+
FROM ${base_image}
88+
COPY ${binary} /usr/local/bin/${binary}
89+
ENTRYPOINT ["/usr/local/bin/${binary}"]
90+
EOF
91+
92+
ln "${binary_path}" "${docker_build_path}/${binary}"
93+
docker build --no-cache -t "${yurt_component_image}" -f "${docker_file_path}" ${docker_build_path}
94+
docker save ${yurt_component_image} > ${YURT_IMAGE_DIR}/${binary}-${SUPPORTED_OS}-${arch}.tar
95+
rm -rf ${docker_build_path}
96+
fi
97+
done
98+
done
99+
}
100+
101+
function build_yurtctl_servant_image() {
102+
servant_script_path=$YURTCTL_SERVANT_DIR/setup_edgenode
103+
for arch in ${target_arch[@]}; do
104+
local docker_build_path=$DOCKER_BUILD_BASE_IDR/$SUPPORTED_OS/$arch
105+
local docker_file_path=$docker_build_path/Dockerfile.yurtctl-servant-$arch
106+
mkdir -p $docker_build_path
107+
108+
local yurtctl_servant_image=$REPO/yurtctl-servant:$TAG-$arch
109+
local base_image
110+
case $arch in
111+
amd64)
112+
base_image="amd64/alpine:3.9"
113+
;;
114+
arm64)
115+
base_image="arm64v8/alpine:3.9"
116+
;;
117+
arm)
118+
base_image="arm32v7/alpine:3.9"
119+
;;
120+
*)
121+
echo unknown arch $arch
122+
exit 1
123+
esac
124+
cat << EOF > $docker_file_path
125+
FROM $base_image
126+
RUN mkdir -p /var/lib/openyurt
127+
ADD setup_edgenode /var/lib/openyurt
128+
EOF
129+
ln $servant_script_path $docker_build_path/setup_edgenode
130+
docker build --no-cache -t $yurtctl_servant_image -f $docker_file_path $docker_build_path
131+
docker save $yurtctl_servant_image > $YURT_IMAGE_DIR/yurtctl-servant-$SUPPORTED_OS-$arch.tar
132+
rm -rf $docker_build_path
133+
done
134+
}
135+
136+
build_images() {
137+
# Always clean first
138+
rm -Rf ${YURT_OUTPUT_DIR}
139+
rm -Rf ${DOCKER_BUILD_BASE_IDR}
140+
mkdir -p ${YURT_BIN_DIR}
141+
mkdir -p ${YURT_IMAGE_DIR}
142+
mkdir -p ${DOCKER_BUILD_BASE_IDR}
143+
144+
build_multi_arch_binaries
145+
build_docker_image
146+
if [[ ${bin_targets[@]} =~ yurtctl-servant ]]; then
147+
build_yurtctl_servant_image
148+
fi
149+
}

hack/make-rules/build.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1+
# Copyright 2020 The OpenYurt Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
#!/usr/bin/env bash
216

17+
set -x
18+
319
YURT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)"
420
source "${YURT_ROOT}/hack/lib/init.sh"
521

hack/make-rules/release-images.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2020 The OpenYurt Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
#!/usr/bin/env bash
16+
17+
YURT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)"
18+
source "${YURT_ROOT}/hack/lib/init.sh"
19+
20+
build_images

0 commit comments

Comments
 (0)