#!/bin/sh
#****************************************************************#
# ScriptName: k8simage.sh
# Author: xhy
# Create Date: 2023-02-10 14:47
# Modify Author: xhy
# Modify Date: 2023-02-10 14:47
# Version: 
#***************************************************************#

KUBE_VERSION=v1.25.3
KUBE_PAUSE_VERSION=3.8
ETCD_VERSION=3.5.4-0
CORE_DNS_VERSION=v1.9.3
K8S_URL=registry.k8s.io
GCR_URL=k8s.gcr.io
UCLOUD_URL=uhub.service.ucloud.cn/imxhy
LONGHORN_URL=longhornio
CALICO_URL='docker.io/calico'
CALICO_VERSION=v3.24.5
METRICS_SERVER_VERSION=v0.6.1
INGRESS_VERSION=v1.5.1
INGRESS_WEBHOOK_VERSION=v20220916-gd32f8c343
LONGHORN1_VERSION=master-head
LONGHORN2_VERSION=v3_20221117
LONGHORN3_VERSION=v3_20220808
LONGHORN4_VERSION=v1_20220914
#CSI_PROVISIONER_VERSION=v1.6.0-lh1
#CSI_NODE_DRIVER_VERSION=v1.2.0-lh1
#CSI_ATTACHER_VERSION=v2.2.1-lh1
#CSI_RESIZER_VERSION=v0.5.1-lh1
#DEFAULTBACKENDVERSION=1.5
#ALIYUN_URL=registry.cn-hangzhou.aliyuncs.com/google_containers

mkdir -p k8simages/

# config node hostname
export ALL_NAMES=(master02 master03 worker01 worker02 worker03)

kubeimages=(kube-proxy:${KUBE_VERSION}
kube-scheduler:${KUBE_VERSION}
kube-controller-manager:${KUBE_VERSION}
kube-apiserver:${KUBE_VERSION}
pause:${KUBE_PAUSE_VERSION}
etcd:${ETCD_VERSION}
)

for kubeimageName in ${kubeimages[@]} ; do
echo ${kubeimageName}
ctr -n k8s.io images pull ${UCLOUD_URL}/${kubeimageName}
ctr -n k8s.io images tag ${UCLOUD_URL}/${kubeimageName} ${K8S_URL}/${kubeimageName}
ctr -n k8s.io images rm ${UCLOUD_URL}/${kubeimageName}
ctr -n k8s.io images export k8simages/${kubeimageName}\.tar ${K8S_URL}/${kubeimageName}
done

corednsimages=(coredns:${CORE_DNS_VERSION}
)

for corednsimageName in ${corednsimages[@]} ; do
echo ${corednsimageName}
ctr -n k8s.io images pull ${UCLOUD_URL}/${corednsimageName}
ctr -n k8s.io images tag ${UCLOUD_URL}/${corednsimageName} ${K8S_URL}/coredns/${corednsimageName}
ctr -n k8s.io images rm ${UCLOUD_URL}/${corednsimageName}
ctr -n k8s.io images export k8simages/${corednsimageName}\.tar ${K8S_URL}/coredns/${corednsimageName}
done

metricsimages=(metrics-server:${METRICS_SERVER_VERSION})

for metricsimageName in ${metricsimages[@]} ; do
echo ${metricsimageName}
ctr -n k8s.io images pull ${UCLOUD_URL}/${metricsimageName}
ctr -n k8s.io images tag ${UCLOUD_URL}/${metricsimageName} ${GCR_URL}/metrics-server/${metricsimageName}
ctr -n k8s.io images rm ${UCLOUD_URL}/${metricsimageName}
ctr -n k8s.io images export k8simages/${metricsimageName}\.tar ${GCR_URL}/metrics-server/${metricsimageName}
done

calimages=(cni:${CALICO_VERSION}
node:${CALICO_VERSION}
kube-controllers:${CALICO_VERSION})

for calimageName in ${calimages[@]} ; do
echo ${calimageName}
ctr -n k8s.io images pull ${UCLOUD_URL}/${calimageName}
ctr -n k8s.io images tag ${UCLOUD_URL}/${calimageName} ${CALICO_URL}/${calimageName}
ctr -n k8s.io images rm ${UCLOUD_URL}/${calimageName}
ctr -n k8s.io images export k8simages/${calimageName}\.tar ${CALICO_URL}/${calimageName}
done

ingressimages=(controller:${INGRESS_VERSION}
kube-webhook-certgen:${INGRESS_WEBHOOK_VERSION}
)

for ingressimageName in ${ingressimages[@]} ; do
echo ${ingressimageName}
ctr -n k8s.io images pull ${UCLOUD_URL}/${ingressimageName}
ctr -n k8s.io images tag ${UCLOUD_URL}/${ingressimageName} ${K8S_URL}/ingress-nginx/${ingressimageName}
ctr -n k8s.io images rm ${UCLOUD_URL}/${ingressimageName}
ctr -n k8s.io images export k8simages/${ingressimageName}\.tar ${K8S_URL}/ingress-nginx/${ingressimageName}
done

longhornimages01=(longhorn-manager:${LONGHORN1_VERSION}
longhorn-ui:${LONGHORN1_VERSION}
longhorn-engine:${LONGHORN1_VERSION}
)

for longhornimageNameA in ${longhornimages01[@]} ; do
echo ${longhornimageNameA}
ctr -n k8s.io images pull ${UCLOUD_URL}/${longhornimageNameA}
ctr -n k8s.io images tag ${UCLOUD_URL}/${longhornimageNameA} ${LONGHORN_URL}/${longhornimageNameA}
ctr -n k8s.io images rm ${UCLOUD_URL}/${longhornimageNameA}
ctr -n k8s.io images export k8simages/${longhornimageNameA}\.tar ${LONGHORN_URL}/${longhornimageNameA}
done

longhornimages02=(longhorn-instance-manager:${LONGHORN2_VERSION}
)

for longhornimageNameB in ${longhornimages02[@]} ; do
echo ${longhornimageNameB}
ctr -n k8s.io images pull ${UCLOUD_URL}/${longhornimageNameB}
ctr -n k8s.io images tag ${UCLOUD_URL}/${longhornimageNameB} ${LONGHORN_URL}/${longhornimageNameB}
ctr -n k8s.io images rm ${UCLOUD_URL}/${longhornimageNameB}
ctr -n k8s.io images export k8simages/${longhornimageNameB}\.tar ${LONGHORN_URL}/${longhornimageNameB}
done

longhornimages03=(backing-image-manager:${LONGHORN3_VERSION})

for longhornimageNameC in ${longhornimages03[@]} ; do
echo ${longhornimageNameC}
ctr -n k8s.io images pull ${UCLOUD_URL}/${longhornimageNameC}
ctr -n k8s.io images tag ${UCLOUD_URL}/${longhornimageNameC} ${LONGHORN_URL}/${longhornimageNameC}
ctr -n k8s.io images rm ${UCLOUD_URL}/${longhornimageNameC}
ctr -n k8s.io images export k8simages/${longhornimageNameC}\.tar ${LONGHORN_URL}/${longhornimageNameC}
done

longhornimages04=(longhorn-share-manager:${LONGHORN4_VERSION})

for longhornimageNameD in ${longhornimages04[@]} ; do
echo ${longhornimageNameD}
ctr -n k8s.io images pull ${UCLOUD_URL}/${longhornimageNameD}
ctr -n k8s.io images tag ${UCLOUD_URL}/${longhornimageNameD} ${LONGHORN_URL}/${longhornimageNameD}
ctr -n k8s.io images rm ${UCLOUD_URL}/${longhornimageNameD}
ctr -n k8s.io images export k8simages/${longhornimageNameD}\.tar ${LONGHORN_URL}/${longhornimageNameD}
done

#csiimages=(csi-provisioner:${CSI_PROVISIONER_VERSION}
#csi-node-driver-registrar:${CSI_NODE_DRIVER_VERSION}
#csi-attacher:${CSI_ATTACHER_VERSION}
#csi-resizer:${CSI_RESIZER_VERSION}
#)

#for csiimageName in ${csiimages[@]} ; do
#echo ${csiimageName}
#ctr -n k8s.io images pull ${UCLOUD_URL}/${csiimageName}
#ctr -n k8s.io images tag ${UCLOUD_URL}/${csiimageName} longhornio/${csiimageName}
#ctr -n k8s.io images rm ${UCLOUD_URL}/${csiimageName}
#ctr -n k8s.io images export k8simages/${csiimageName}\.tar longhornio/${csiimageName}
#done
#
#otherimages=(defaultbackend-amd64:${DEFAULTBACKENDVERSION})

#for otherimagesName in ${otherimages[@]} ; do
#echo ${otherimagesName}
#ctr -n k8s.io images pull ${UCLOUD_URL}/${otherimagesName}
#ctr -n k8s.io images tag ${UCLOUD_URL}/${otherimagesName} ${K8S_URL}/${otherimagesName}
#ctr -n k8s.io images rm ${UCLOUD_URL}/${otherimagesName}
#ctr -n k8s.io images export k8simages/${otherimagesName}\.tar ${K8S_URL}/${otherimagesName}
#done
#
allimages=(kube-proxy:${KUBE_VERSION}
kube-scheduler:${KUBE_VERSION}
kube-controller-manager:${KUBE_VERSION}
kube-apiserver:${KUBE_VERSION}
pause:${KUBE_PAUSE_VERSION}
etcd:${ETCD_VERSION}
coredns:${CORE_DNS_VERSION}
metrics-server:${METRICS_SERVER_VERSION}
cni:${CALICO_VERSION}
node:${CALICO_VERSION}
kube-controllers:${CALICO_VERSION}
controller:${INGRESS_VERSION}
kube-webhook-certgen:${INGRESS_WEBHOOK_VERSION}
longhorn-manager:${LONGHORN1_VERSION}
longhorn-ui:${LONGHORN1_VERSION}
longhorn-engine:${LONGHORN1_VERSION}
longhorn-instance-manager:${LONGHORN2_VERSION}
backing-image-manager:${LONGHORN3_VERSION}
longhorn-share-manager:${LONGHORN4_VERSION}
#csi-provisioner:${CSI_PROVISIONER_VERSION}
#csi-node-driver-registrar:${CSI_NODE_DRIVER_VERSION}
#csi-attacher:${CSI_ATTACHER_VERSION}
#csi-resizer:${CSI_RESIZER_VERSION}
#defaultbackend-amd64:${DEFAULTBACKENDVERSION}
)

for all_name in ${ALL_NAMES[@]}
  do  
    echo ">>> ${all_name}"
    ssh root@${all_name} "mkdir /root/k8simages"
    scp -rp k8simages/* root@${all_name}:/root/k8simages/
  done

for allimageName in ${allimages[@]}
  do
  for all_name in ${ALL_NAMES[@]}
    do
    echo "${allimageName} copy to ${all_name}"
    ssh root@${all_name} "ctr -n k8s.io images import k8simages/${allimageName}\.tar"
    done
  done
