Skip to content

Instantly share code, notes, and snippets.

@yezihack
Last active December 15, 2022 01:41
Show Gist options
  • Save yezihack/c57bcd84b81571faa5d2a229951c3534 to your computer and use it in GitHub Desktop.
Save yezihack/c57bcd84b81571faa5d2a229951c3534 to your computer and use it in GitHub Desktop.
k8s-upload-images.sh
#!/bin/bash
#####################
# 功能:下载官方 kubernetes 镜像 转存到 私有镜像中
####################
################### function #########################
# 警告
color::yello() {
local val=$1
echo -e "\033[33m ${val} \033[0m"
}
# 禁止
color::red() {
local val=$1
echo -e "\033[31m ${val} \033[0m"
}
# 提示
color::green() {
local val=$1
echo -e "\033[32m ${val} \033[0m"
}
# 青绿色
color::cyan() {
local val=$1
echo -e "\033[36m ${val} \033[0m"
}
k8s::image() {
version=$1 #版本
# 新镜像列表
new_list=()
# 操作
i=0
# 新地址
new_addr="me.hubor.io/google_containers/"
for addr in $(kubeadm config images list --kubernetes-version "${version}" --image-repository=registry.aliyuncs.com/google_containers);do
color::yello "原地址: ${addr}"
image_name=${addr##*/}
color::yello "镜像名称:${image_name}"
# 下载
color::yello "正在下载:${image_name}"
sudo docker pull "${addr}"
result=$?
if [ ${result} -ne 0 ]; then
color::red "pull is failed."
exit 0
fi
# 更改 Tag
new_image="${new_addr}${image_name}"
green "new image name: ${new_image}"
sudo docker tag "${addr}" "${new_image}"
# 推送
color::yello "正在上传: ${new_image}"
sudo docker push "${new_image}"
result=$?
if [ ${result} -ne 0 ]; then
color::red "push is failed."
exit 0
fi
# 添加新镜像地址
new_list[i]="${new_image}"
# i 自增
let i++
done
# 打印新的地址
color::yello "上传完成的新地址:"
for name in "${new_list[@]}"; do
color::green "${name}"
done
}
look::image() {
version=$1
kubeadm config images list --kubernetes-version "${version}" --image-repository=registry.aliyuncs.com/google_containers
}
main::help(){
scriptName=$0
scriptName=$(basename "${scriptName}")
help_list=(
"###############使用帮助###############"
"${scriptName} [action] [version]"
"实例: ${scriptName} download 1.16.0"
"目前支持:softName: download/see"
)
for name in "${help_list[@]}"; do
color::cyan "${name}"
done
}
main::check() {
version=$1
if [ -z "${version}" ];then
color::red "version 为空,如:1.16.0"
exit 0
fi
}
main() {
action=$1
version=$2
case "X${action}" in
"Xdownload")
main::check "${version}"
k8s::image "${version}"
;;
"Xsee")
main::check "${version}"
look::image "${version}"
;;
*)
main::help
;;
esac
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment