Skip to content

Instantly share code, notes, and snippets.

@AGou-ops
Created August 30, 2023 07:04
Show Gist options
  • Save AGou-ops/24d3e3b962e76c6bf4ffa0c5d5e17498 to your computer and use it in GitHub Desktop.
Save AGou-ops/24d3e3b962e76c6bf4ffa0c5d5e17498 to your computer and use it in GitHub Desktop.
ctr自用
#!/bin/bash
#
#**************************************************
# Author: AGou-ops *
# E-mail: agou-ops@foxmail.com *
# Date: 2023-08-30 *
# Description: *
# Copyright 2023 by AGou-ops.All Rights Reserved *
#**************************************************
#
HUB_USERNAME="suofeiya"
LOCALHUB_DOMINNAME="harbor.nblh.local/library"
IMAGE_NAME=$(cat <<EOF
quay.io/prometheus/node-exporter:v1.6.0
quay.io/kiwigrid/k8s-sidecar:1.24.6
docker.io/grafana/grafana:10.1.0
quay.io/prometheus-operator/prometheus-operator:v0.67.1
quay.io/prometheus/alertmanager:v0.26.0
quay.io/prometheus/prometheus:v2.46.0
docker.io/bats/bats:v1.4.1
EOF
)
OTHER_NAME=$(cat <<EOF
registry.k8s.io/kube-state-metrics/kube-state-metrics:v2.9.2
registry.k8s.io/ingress-nginx/kube-webhook-certgen:v20221220-controller-v1.5.1-58-g787ea74b6
EOF
)
new_name=$(echo "$IMAGE_NAME" | sed "s@^@$LOCALHUB_DOMINNAME/@")
pull_push2local(){
echo "------- pull image from docker hub"
echo "$IMAGE_NAME" | xargs -I {} -P 0 docker pull {}
sleep 1
for i in $IMAGE_NAME; do
docker tag "$i" $LOCALHUB_DOMINNAME/"$i"
done
echo "$new_name" | xargs -I {} -P 0 docker push {}
echo "------- finished"
check "$new_name"
}
check(){
echo "-------- check image"
local i_name=$1
for i in $i_name;do
docker inspect "$i" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "$i" " is ok"
else
echo "$i" " not found!!!"
fi
done
}
other_new_name=$(echo "$OTHER_NAME" | sed "s@/@jk@g" | sed "s@^@$HUB_USERNAME/@")
# used for VPS.(k8s.io)
other_pull_push2DockerHub(){
echo "------- pull image from docker hub and push to dockerhub"
echo "$OTHER_NAME" | xargs -I {} -P 0 docker pull {}
sleep 1
for i in $OTHER_NAME; do
tmp_name=$(echo "$i" | sed "s@/@jk@g")
docker tag "$i" $HUB_USERNAME/"$tmp_name"
done
echo "$other_new_name" | xargs -I {} -P 0 docker push {}
echo "------- finished"
check "$other_new_name"
}
new_name_retag=$(echo "$other_new_name" | sed "s@jk@/@g" | sed "s@^@$LOCALHUB_DOMINNAME/@")
other_pull_push2local(){
echo "------- other pull image from docker hub"
echo "$other_new_name" | xargs -I {} -P 0 docker pull {}
sleep 1
for i in $other_new_name; do
tmp_name=$(echo "$i" | sed "s@jk@/@g")
docker tag "$i" $LOCALHUB_DOMINNAME/"$tmp_name"
done
echo "$new_name_retag" | xargs -I {} -P 0 docker push {}
echo "------- finished"
check "$new_name_retag"
}
# used for client.
ctr_harborPull_retag(){
echo "------- pull image from local harbor and retag image"
echo "$new_name" | xargs -I {} -P 0 crictl pull {}
sleep 1
for i in $new_name; do
tag_name=$(echo "$i" | sed "s@$LOCALHUB_DOMINNAME/@@")
ctr -n k8s.io i tag "$i" "$tag_name"
done
}
# used for client.
other_ctr_harborPull_retag(){
echo "------- pull image from other local harbor and retag image"
echo "$new_name_retag" | xargs -I {} -P 0 crictl pull {}
sleep 1
for i in $new_name_retag; do
tag_name=$(echo "$i" | sed "s@$LOCALHUB_DOMINNAME/@@" | sed "s@$HUB_USERNAME/@@")
ctr -n k8s.io i tag "$i" "$tag_name"
done
}
echo "=========="
echo "Please enter your choice:"
echo
echo "1. local pull and push to harbor"
echo
echo "2. other local pull(retag) and push to harbor"
echo "3. **remote** pull(retag) and push to DockerHub(VPS)"
echo
echo "4. ctr pull form harbor and retag image(k8s)"
echo "5. other ctr pull form harbor and retag image(k8s)"
echo "=========="
read -r -t 15 -p "【Your choose】> " choice
# 根据用户的选择执行相应的函数
case $choice in
1)
pull_push2local
;;
2)
other_pull_push2local
;;
3)
other_pull_push2DockerHub
;;
4)
ctr_harborPull_retag
;;
5)
other_ctr_harborPull_retag
;;
*)
echo "Invalid."
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment