#!/bin/sh
#****************************************************************#
# ScriptName: proloadimages.sh
# Author: xhy
# Create Date: 2021-06-01 16:54
# Modify Author: xhy
# Modify Date: 2021-06-01 16:54
# Version: 
#***************************************************************#
UCLOUD_URL=uhub.service.ucloud.cn/imxhy
QUAY_URL=quay.io

mkdir -p proimages/

# ýڵϢ
export ALL_HOSTS=(master02 master03 worker01 worker02 worker03)

proimages=(alertmanager:v0.21.0
blackbox-exporter:v0.18.0
node-exporter:v1.1.2
prometheus:v2.26.0
)

for proimagesName in ${proimages[@]} ; do
echo ${proimagesName}
ctr -n k8s.io images pull ${UCLOUD_URL}/${proimagesName}
ctr -n k8s.io images tag ${UCLOUD_URL}/${proimagesName} ${QUAY_URL}/prometheus/${proimagesName} 
ctr -n k8s.io images rm ${UCLOUD_URL}/${proimagesName}
ctr -n k8s.io images export proimages/${proimagesName}\.tar ${QUAY_URL}/prometheus/${proimagesName}
done

otherimages=(k8s-prometheus-adapter:v0.8.4
grafana:7.5.4
configmap-reload:v0.5.0
kube-state-metrics:v2.0.0
kube-rbac-proxy:v0.8.0
)

for otherimagesName in ${otherimages[@]} ; do
echo ${otherimagesName}
ctr -n k8s.io images pull ${UCLOUD_URL}/${otherimagesName}
done

ctr -n k8s.io images tag ${UCLOUD_URL}/k8s-prometheus-adapter:v0.8.4 directxman12/k8s-prometheus-adapter:v0.8.4
ctr -n k8s.io images tag ${UCLOUD_URL}/grafana:7.5.4 grafana/grafana:7.5.4
ctr -n k8s.io images tag ${UCLOUD_URL}/configmap-reload:v0.5.0 jimmidyson/configmap-reload:v0.5.0
ctr -n k8s.io images tag ${UCLOUD_URL}/kube-state-metrics:v2.0.0 k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.0.0
ctr -n k8s.io images tag ${UCLOUD_URL}/kube-rbac-proxy:v0.8.0 quay.io/brancz/kube-rbac-proxy:v0.8.0

for otherimagesName in ${otherimages[@]} ; do
echo ${otherimagesName}
ctr -n k8s.io images rm ${UCLOUD_URL}/${otherimagesName}
done

ctr -n k8s.io images export proimages/k8s-prometheus-adapter:v0.8.4.tar directxman12/k8s-prometheus-adapter:v0.8.4
ctr -n k8s.io images export proimages/grafana:7.5.4.tar grafana/grafana:7.5.4
ctr -n k8s.io images export proimages/configmap-reload:v0.5.0.tar jimmidyson/configmap-reload:v0.5.0
ctr -n k8s.io images export proimages/kube-state-metrics:v2.0.0.tar k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.0.0
ctr -n k8s.io images export proimages/kube-rbac-proxy:v0.8.0.tar quay.io/brancz/kube-rbac-proxy:v0.8.0

allimages=(k8s-prometheus-adapter:v0.8.4
grafana:7.5.4
configmap-reload:v0.5.0
kube-state-metrics:v2.0.0
kube-rbac-proxy:v0.8.0
alertmanager:v0.21.0
blackbox-exporter:v0.18.0
node-exporter:v1.1.2
prometheus:v2.26.0
)

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

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