#!/usr/bin/env bash

package="kubernetes-1.20.15-images.$(uname -m).tar.gz"
image_path="k8s_images"

image_repo=registry.cn-shenzhen.aliyuncs.com/shuhui

# 终端颜色定义
txtbld=$(tput bold)             # Bold
bldred=${txtbld}$(tput setaf 1) # red
bldgre=${txtbld}$(tput setaf 2) # green
bldylw=${txtbld}$(tput setaf 3) # yellow
txtrst=$(tput sgr0)             # Reset
err=${bldred}ERROR${txtrst}
info=${bldgre}INFO${txtrst}
warn=${bldylw}WARNING${txtrst}


k8s_images=(
    registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.20.15
    registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.20.15
    registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.20.15
    registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.20.15
    registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2
    registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0
    registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.7.0
)

imgSuccessful=0
imgFailed=0


install -d ${image_path}
for img in ${k8s_images[@]}
do
    printf "$info: 镜像导出 [ ${bldgre}${img}${txtrst} ]"

    skopeo copy --multi-arch=all docker://${img} docker://${image_repo}/${img##*/} >/dev/null 2>/dev/null

    if [[ "$?" -eq "0" ]]; then
        printf " | ${bldgre}√${txtrst}\n"
        let imgSuccessful=${imgSuccessful}+1
    else
        printf " | ${bldred}×${txtrst}\n"
        let imgFailed=${imgFailed}+1
    fi
done

#cd $(realpath .)/${image_path}/ && tar acvf ${package} *.tar  --remove-files
