forked from yunionio/kubecomps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_push.sh
More file actions
executable file
·251 lines (218 loc) · 7 KB
/
Copy pathdocker_push.sh
File metadata and controls
executable file
·251 lines (218 loc) · 7 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
#!/bin/bash
set -o errexit
set -o pipefail
if [ "$DEBUG" == "true" ]; then
set -ex ;export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
fi
readlink_mac() {
cd `dirname $1`
TARGET_FILE=`basename $1`
# Iterate down a (possible) chain of symlinks
while [ -L "$TARGET_FILE" ]
do
TARGET_FILE=`readlink $TARGET_FILE`
cd `dirname $TARGET_FILE`
TARGET_FILE=`basename $TARGET_FILE`
done
# Compute the canonicalized name by finding the physical path
# for the directory we're in and appending the target file.
PHYS_DIR=`pwd -P`
REAL_PATH=$PHYS_DIR/$TARGET_FILE
}
get_current_arch() {
local current_arch
case $(uname -m) in
x86_64)
current_arch=amd64
;;
aarch64)
current_arch=arm64
;;
riscv64)
current_arch=riscv64
;;
esac
echo $current_arch
}
ALLARCH=("amd64" "arm64" "riscv64")
pushd $(cd "$(dirname "$0")"; pwd) > /dev/null
readlink_mac $(basename "$0")
cd "$(dirname "$REAL_PATH")"
CUR_DIR=$(pwd)
SRC_DIR=$(cd .. && pwd)
popd > /dev/null
DOCKER_DIR="$SRC_DIR/build/docker"
# https://docs.docker.com/develop/develop-images/build_enhancements/
export DOCKER_BUILDKIT=1
REGISTRY=${REGISTRY:-docker.io/yunion}
TAG=${TAG:-latest}
CURRENT_ARCH=$(get_current_arch)
ARCH=${ARCH:-$CURRENT_ARCH}
build_bin() {
local BUILD_ARCH=$2
local BUILD_CGO=$3
case "$1" in
*)
docker run --rm \
-v $SRC_DIR:/root/go/src/yunion.io/x/kubecomps \
-v $SRC_DIR/_output/alpine-build:/root/go/src/yunion.io/x/kubecomps/_output \
-v $SRC_DIR/_output/alpine-build/_cache:/root/.cache \
registry.cn-beijing.aliyuncs.com/yunionio/kube-build:3.22.2-go-1.24.9-0 \
/bin/sh -c "set -ex; cd /root/go/src/yunion.io/x/kubecomps; $BUILD_ARCH $BUILD_CGO GOOS=linux make cmd/$1; chown -R $(id -u):$(id -g) _output"
;;
esac
}
build_image() {
local tag=$1
local file=$2
local path=$3
local arch
if [[ "$tag" == *:5000/* ]]; then
arch=$(arch)
case $arch in
x86_64)
docker buildx build -t "$tag" -f "$2" "$3" --output type=docker --platform linux/amd64
;;
aarch64)
docker buildx build -t "$tag" -f "$2" "$3" --output type=docker --platform linux/arm64
;;
riscv64)
docker buildx build -t "$tag" -f "$2" "$3" --output type=docker --platform linux/riscv64
;;
*)
echo wrong arch
exit 1
;;
esac
else
if [[ "$tag" == *"amd64" || "$ARCH" == "" || "$ARCH" == "amd64" || "$ARCH" == "x86_64" || "$ARCH" == "x86" ]]; then
docker buildx build -t "$tag" -f "$file" "$path" --push --platform linux/amd64
elif [[ "$tag" == *"arm64" || "$ARCH" == "arm64" ]]; then
docker buildx build -t "$tag" -f "$file" "$path" --push --platform linux/arm64
elif [[ "$tag" == *"riscv" || "$ARCH" == "riscv64" ]]; then
docker buildx build -t "$tag" -f "$file" "$path" --push --platform linux/riscv64
else
docker buildx build -t "$tag" -f "$file" "$path" --push
fi
docker pull "$tag"
fi
}
buildx_and_push() {
local tag=$1
local file=$2
local path=$3
local arch=$4
if [[ "$DRY_RUN" == "true" ]]; then
echo "[$(readlink -f ${BASH_SOURCE}):${LINENO} ${FUNCNAME[0]}] return for DRY_RUN"
return
fi
docker buildx build -t "$tag" --platform "linux/$arch" -f "$2" "$3" --push
docker pull --platform "linux/$arch" "$tag"
}
get_image_name() {
local component=$1
local arch=$2
local is_all_arch=$3
local img_name="$REGISTRY/$component:$TAG"
if [[ -n "$arch" && "$is_all_arch" == "true" ]]; then
img_name="${img_name}-$arch"
fi
echo $img_name
}
build_process() {
local component=$1
local image_name=$component
local is_all_arch=$3
local img_name=$(get_image_name $component $arch $is_all_arch)
build_bin $component
build_image $img_name $DOCKER_DIR/Dockerfile.$component $SRC_DIR
}
build_process_with_buildx() {
local component=$1
local arch=$2
local is_all_arch=$3
local img_name=$(get_image_name $component $arch $is_all_arch)
build_env="GOARCH=$arch "
if [[ "$DRY_RUN" == "true" ]]; then
build_bin $component $build_env
echo "[$(readlink -f ${BASH_SOURCE}):${LINENO} ${FUNCNAME[0]}] return for DRY_RUN"
return
fi
case "$component" in
kubeserver)
buildx_and_push $img_name $DOCKER_DIR/Dockerfile.$component-mularch $SRC_DIR $arch
;;
*)
build_bin $component $build_env
buildx_and_push $img_name $DOCKER_DIR/Dockerfile.$component $SRC_DIR $arch
;;
esac
}
general_build(){
local component=$1
# 如果未指定,则默认使用当前架构
local arch=${2:-$current_arch}
local is_all_arch=$3
build_process_with_buildx $component $arch $is_all_arch
}
make_manifest_image() {
local component=$1
local arch=$2
local img_name=$(get_image_name $component "" "false")
if [[ "$DRY_RUN" == "true" ]]; then
echo "[$(readlink -f ${BASH_SOURCE}):${LINENO} ${FUNCNAME[0]}] return for DRY_RUN"
return
fi
CMD="docker buildx imagetools create -t ${img_name} "
for ac in "${ALLARCH[@]}"; do
if [[ "${arch}" == "all" || "${arch}" == *"$ac"* ]]; then
CMD="${CMD} ${img_name}-${ac}"
fi
done
echo "$CMD"
$CMD
}
ALL_COMPONENTS=$(ls cmd | grep -v '.*cli$' | xargs)
if [ "$#" -lt 1 ]; then
echo "No component is specified~"
echo "You can specify a component in [$ALL_COMPONENTS]"
echo "If you want to build all components, specify the component to: all."
exit
elif [ "$#" -eq 1 ] && [ "$1" == "all" ]; then
echo "Build all onecloud-ee docker images"
COMPONENTS=$ALL_COMPONENTS
else
COMPONENTS=$@
fi
cd $SRC_DIR
mkdir -p $SRC_DIR/_output
show_update_cmd() {
local component=$1
local spec=$1
local name=$1
local tag=$TAG
echo "kubectl patch oc -n onecloud default --type='json' -p='[{op: replace, path: /spec/${spec}/imageName, value: ${name}},{"op": "replace", "path": "/spec/${spec}/repository", "value": "${REGISTRY}"},{"op": "add", "path": "/spec/${spec}/tag", "value": "${tag}"}]'"
}
for component in $COMPONENTS; do
if [[ $component == *cli ]]; then
echo "Please build image for climc"
continue
fi
echo "Start to build component: $component"
multiarch=""
for ac in "${ALLARCH[@]}"; do
if [[ "$ARCH" == "$ac" ]]; then
# single arch
general_build $component $ARCH "false"
elif [[ "$ARCH" == "all" || "$ARCH" == *"$ac"* ]]; then
multiarch="true"
general_build $component $ac "true"
fi
done
if [[ "$multiarch" == "true" ]]; then
make_manifest_image $component $ARCH
fi
done
for component in $COMPONENTS; do
show_update_cmd $component
done