2021-01-07 00:05:52 +08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
OPTION=$1
|
|
|
|
CURRENT_DIR=$(cd $(dirname $0); pwd)
|
|
|
|
TEMP_DIR="${CURRENT_DIR}/temp"
|
|
|
|
|
|
|
|
IMAGE_TAR_FILE="${CURRENT_DIR}/container-images.tar.gz"
|
|
|
|
IMAGE_DIR="${CURRENT_DIR}/container-images"
|
|
|
|
IMAGE_LIST="${IMAGE_DIR}/container-images.txt"
|
|
|
|
RETRY_COUNT=5
|
|
|
|
|
|
|
|
function create_container_image_tar() {
|
|
|
|
set -e
|
|
|
|
|
2024-02-18 11:34:29 +08:00
|
|
|
if [ -z "${IMAGES_FROM_FILE}" ]; then
|
|
|
|
echo "Getting images from current \"$(kubectl config current-context)\""
|
|
|
|
|
|
|
|
IMAGES=$(mktemp --suffix=-images)
|
|
|
|
trap 'rm -f "${IMAGES}"' EXIT
|
|
|
|
|
|
|
|
kubectl describe cronjobs,jobs,pods --all-namespaces | grep " Image:" | awk '{print $2}' | sort | uniq > "${IMAGES}"
|
|
|
|
# NOTE: etcd and pause cannot be seen as pods.
|
|
|
|
# The pause image is used for --pod-infra-container-image option of kubelet.
|
|
|
|
kubectl cluster-info dump | grep -E "quay.io/coreos/etcd:|registry.k8s.io/pause:" | sed s@\"@@g >> "${IMAGES}"
|
|
|
|
else
|
|
|
|
echo "Getting images from file \"${IMAGES_FROM_FILE}\""
|
|
|
|
if [ ! -f "${IMAGES_FROM_FILE}" ]; then
|
|
|
|
echo "${IMAGES_FROM_FILE} is not a file"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
IMAGES=$(realpath $IMAGES_FROM_FILE)
|
|
|
|
fi
|
2021-01-07 00:05:52 +08:00
|
|
|
|
|
|
|
rm -f ${IMAGE_TAR_FILE}
|
|
|
|
rm -rf ${IMAGE_DIR}
|
|
|
|
mkdir ${IMAGE_DIR}
|
|
|
|
cd ${IMAGE_DIR}
|
|
|
|
|
2023-12-21 23:45:09 +08:00
|
|
|
sudo ${runtime} pull registry:latest
|
|
|
|
sudo ${runtime} save -o registry-latest.tar registry:latest
|
2021-01-07 00:05:52 +08:00
|
|
|
|
2024-02-18 11:34:29 +08:00
|
|
|
while read -r image
|
2021-01-07 00:05:52 +08:00
|
|
|
do
|
2024-02-18 11:34:29 +08:00
|
|
|
FILE_NAME="$(echo ${image} | sed s@"/"@"-"@g | sed s/":"/"-"/g | sed -E 's/\@.*//g')".tar
|
2021-01-07 00:05:52 +08:00
|
|
|
set +e
|
|
|
|
for step in $(seq 1 ${RETRY_COUNT})
|
|
|
|
do
|
2023-12-21 23:45:09 +08:00
|
|
|
sudo ${runtime} pull ${image}
|
2021-01-07 00:05:52 +08:00
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
echo "Failed to pull ${image} at step ${step}"
|
|
|
|
if [ ${step} -eq ${RETRY_COUNT} ]; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
set -e
|
2023-12-21 23:45:09 +08:00
|
|
|
sudo ${runtime} save -o ${FILE_NAME} ${image}
|
2021-01-07 00:05:52 +08:00
|
|
|
|
|
|
|
# NOTE: Here removes the following repo parts from each image
|
|
|
|
# so that these parts will be replaced with Kubespray.
|
2022-06-07 15:55:42 +08:00
|
|
|
# - kube_image_repo: "registry.k8s.io"
|
2021-01-07 00:05:52 +08:00
|
|
|
# - gcr_image_repo: "gcr.io"
|
2024-02-18 11:34:29 +08:00
|
|
|
# - ghcr_image_repo: "ghcr.io"
|
2021-01-07 00:05:52 +08:00
|
|
|
# - docker_image_repo: "docker.io"
|
|
|
|
# - quay_image_repo: "quay.io"
|
|
|
|
FIRST_PART=$(echo ${image} | awk -F"/" '{print $1}')
|
2022-06-07 15:55:42 +08:00
|
|
|
if [ "${FIRST_PART}" = "registry.k8s.io" ] ||
|
2021-01-07 00:05:52 +08:00
|
|
|
[ "${FIRST_PART}" = "gcr.io" ] ||
|
2024-02-18 11:34:29 +08:00
|
|
|
[ "${FIRST_PART}" = "ghcr.io" ] ||
|
2021-01-07 00:05:52 +08:00
|
|
|
[ "${FIRST_PART}" = "docker.io" ] ||
|
2022-05-13 14:23:14 +08:00
|
|
|
[ "${FIRST_PART}" = "quay.io" ] ||
|
|
|
|
[ "${FIRST_PART}" = "${PRIVATE_REGISTRY}" ]; then
|
2024-02-18 11:34:29 +08:00
|
|
|
image=$(echo ${image} | sed s@"${FIRST_PART}/"@@ | sed -E 's/\@.*/\n/g')
|
2021-01-07 00:05:52 +08:00
|
|
|
fi
|
|
|
|
echo "${FILE_NAME} ${image}" >> ${IMAGE_LIST}
|
2024-02-18 11:34:29 +08:00
|
|
|
done < "${IMAGES}"
|
2021-01-07 00:05:52 +08:00
|
|
|
|
|
|
|
cd ..
|
|
|
|
sudo chown ${USER} ${IMAGE_DIR}/*
|
|
|
|
tar -zcvf ${IMAGE_TAR_FILE} ./container-images
|
|
|
|
rm -rf ${IMAGE_DIR}
|
|
|
|
|
|
|
|
echo ""
|
|
|
|
echo "${IMAGE_TAR_FILE} is created to contain your container images."
|
|
|
|
echo "Please keep this file and bring it to your offline environment."
|
|
|
|
}
|
|
|
|
|
|
|
|
function register_container_images() {
|
2024-02-18 11:34:29 +08:00
|
|
|
create_registry=false
|
|
|
|
REGISTRY_PORT=${REGISTRY_PORT:-"5000"}
|
|
|
|
|
|
|
|
if [ -z "${DESTINATION_REGISTRY}" ]; then
|
|
|
|
echo "DESTINATION_REGISTRY not set, will create local registry"
|
|
|
|
create_registry=true
|
|
|
|
DESTINATION_REGISTRY="$(hostname):${REGISTRY_PORT}"
|
|
|
|
fi
|
|
|
|
echo "Images will be pushed to ${DESTINATION_REGISTRY}"
|
|
|
|
|
2021-01-07 00:05:52 +08:00
|
|
|
if [ ! -f ${IMAGE_TAR_FILE} ]; then
|
|
|
|
echo "${IMAGE_TAR_FILE} should exist."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [ ! -d ${TEMP_DIR} ]; then
|
|
|
|
mkdir ${TEMP_DIR}
|
|
|
|
fi
|
|
|
|
|
|
|
|
# To avoid "http: server gave http response to https client" error.
|
|
|
|
if [ -d /etc/docker/ ]; then
|
|
|
|
set -e
|
|
|
|
# Ubuntu18.04, RHEL7/CentOS7
|
|
|
|
cp ${CURRENT_DIR}/docker-daemon.json ${TEMP_DIR}/docker-daemon.json
|
2024-02-18 11:34:29 +08:00
|
|
|
sed -i s@"HOSTNAME"@"$(hostname)"@ ${TEMP_DIR}/docker-daemon.json
|
2021-01-07 00:05:52 +08:00
|
|
|
sudo cp ${TEMP_DIR}/docker-daemon.json /etc/docker/daemon.json
|
|
|
|
elif [ -d /etc/containers/ ]; then
|
|
|
|
set -e
|
|
|
|
# RHEL8/CentOS8
|
|
|
|
cp ${CURRENT_DIR}/registries.conf ${TEMP_DIR}/registries.conf
|
2024-02-18 11:34:29 +08:00
|
|
|
sed -i s@"HOSTNAME"@"$(hostname)"@ ${TEMP_DIR}/registries.conf
|
2021-01-07 00:05:52 +08:00
|
|
|
sudo cp ${TEMP_DIR}/registries.conf /etc/containers/registries.conf
|
|
|
|
else
|
2023-12-21 23:45:09 +08:00
|
|
|
echo "runtime package(docker-ce, podman, nerctl, etc.) should be installed"
|
2021-01-07 00:05:52 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
tar -zxvf ${IMAGE_TAR_FILE}
|
2024-02-18 11:34:29 +08:00
|
|
|
|
|
|
|
if [ "${create_registry}" ]; then
|
|
|
|
sudo ${runtime} load -i ${IMAGE_DIR}/registry-latest.tar
|
|
|
|
set +e
|
|
|
|
|
|
|
|
sudo ${runtime} container inspect registry >/dev/null 2>&1
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
sudo ${runtime} run --restart=always -d -p "${REGISTRY_PORT}":"${REGISTRY_PORT}" --name registry registry:latest
|
|
|
|
fi
|
|
|
|
set -e
|
2021-07-26 15:56:33 +08:00
|
|
|
fi
|
|
|
|
|
2021-01-07 00:05:52 +08:00
|
|
|
while read -r line; do
|
|
|
|
file_name=$(echo ${line} | awk '{print $1}')
|
2021-07-26 15:56:33 +08:00
|
|
|
raw_image=$(echo ${line} | awk '{print $2}')
|
2024-02-18 11:34:29 +08:00
|
|
|
new_image="${DESTINATION_REGISTRY}/${raw_image}"
|
|
|
|
load_image=$(sudo ${runtime} load -i ${IMAGE_DIR}/${file_name} | head -n1)
|
|
|
|
org_image=$(echo "${load_image}" | awk '{print $3}')
|
|
|
|
# special case for tags containing the digest when using docker or podman as the container runtime
|
|
|
|
if [ "${org_image}" == "ID:" ]; then
|
|
|
|
org_image=$(echo "${load_image}" | awk '{print $4}')
|
|
|
|
fi
|
2023-12-21 23:45:09 +08:00
|
|
|
image_id=$(sudo ${runtime} image inspect ${org_image} | grep "\"Id\":" | awk -F: '{print $3}'| sed s/'\",'//)
|
2021-07-19 08:58:51 +08:00
|
|
|
if [ -z "${file_name}" ]; then
|
|
|
|
echo "Failed to get file_name for line ${line}"
|
|
|
|
exit 1
|
|
|
|
fi
|
2021-07-26 15:56:33 +08:00
|
|
|
if [ -z "${raw_image}" ]; then
|
|
|
|
echo "Failed to get raw_image for line ${line}"
|
|
|
|
exit 1
|
|
|
|
fi
|
2021-07-19 08:58:51 +08:00
|
|
|
if [ -z "${org_image}" ]; then
|
|
|
|
echo "Failed to get org_image for line ${line}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [ -z "${image_id}" ]; then
|
|
|
|
echo "Failed to get image_id for file ${file_name}"
|
|
|
|
exit 1
|
|
|
|
fi
|
2023-12-21 23:45:09 +08:00
|
|
|
sudo ${runtime} load -i ${IMAGE_DIR}/${file_name}
|
|
|
|
sudo ${runtime} tag ${image_id} ${new_image}
|
|
|
|
sudo ${runtime} push ${new_image}
|
2021-01-07 00:05:52 +08:00
|
|
|
done <<< "$(cat ${IMAGE_LIST})"
|
|
|
|
|
|
|
|
echo "Succeeded to register container images to local registry."
|
2024-02-18 11:34:29 +08:00
|
|
|
echo "Please specify \"${DESTINATION_REGISTRY}\" for the following options in your inventry:"
|
2021-01-07 00:05:52 +08:00
|
|
|
echo "- kube_image_repo"
|
|
|
|
echo "- gcr_image_repo"
|
|
|
|
echo "- docker_image_repo"
|
|
|
|
echo "- quay_image_repo"
|
|
|
|
}
|
|
|
|
|
2023-12-21 23:45:09 +08:00
|
|
|
# get runtime command
|
|
|
|
if command -v nerdctl 1>/dev/null 2>&1; then
|
|
|
|
runtime="nerdctl"
|
|
|
|
elif command -v podman 1>/dev/null 2>&1; then
|
|
|
|
runtime="podman"
|
|
|
|
elif command -v docker 1>/dev/null 2>&1; then
|
|
|
|
runtime="docker"
|
|
|
|
else
|
|
|
|
echo "No supported container runtime found"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-01-07 00:05:52 +08:00
|
|
|
if [ "${OPTION}" == "create" ]; then
|
|
|
|
create_container_image_tar
|
|
|
|
elif [ "${OPTION}" == "register" ]; then
|
|
|
|
register_container_images
|
|
|
|
else
|
|
|
|
echo "This script has two features:"
|
2024-02-18 11:34:29 +08:00
|
|
|
echo "(1) Get container images from an environment which is deployed online, or set IMAGES_FROM_FILE"
|
|
|
|
echo " environment variable to get images from a file (e.g. temp/images.list after running the"
|
|
|
|
echo " ./generate_list.sh script)."
|
2021-01-07 00:05:52 +08:00
|
|
|
echo "(2) Deploy local container registry and register the container images to the registry."
|
|
|
|
echo ""
|
|
|
|
echo "Step(1) should be done online site as a preparation, then we bring"
|
2022-05-13 14:23:14 +08:00
|
|
|
echo "the gotten images to the target offline environment. if images are from"
|
|
|
|
echo "a private registry, you need to set PRIVATE_REGISTRY environment variable."
|
2024-02-18 11:34:29 +08:00
|
|
|
echo "Then we will run step(2) for registering the images to local registry, or to an existing"
|
|
|
|
echo "registry set by the DESTINATION_REGISTRY environment variable. By default, the local registry"
|
|
|
|
echo "will run on port 5000. This can be changed with the REGISTRY_PORT environment variable"
|
2021-01-07 00:05:52 +08:00
|
|
|
echo ""
|
|
|
|
echo "${IMAGE_TAR_FILE} is created to contain your container images."
|
|
|
|
echo "Please keep this file and bring it to your offline environment."
|
|
|
|
echo ""
|
|
|
|
echo "Step(1) can be operated with:"
|
|
|
|
echo " $ ./manage-offline-container-images.sh create"
|
|
|
|
echo ""
|
|
|
|
echo "Step(2) can be operated with:"
|
|
|
|
echo " $ ./manage-offline-container-images.sh register"
|
|
|
|
echo ""
|
|
|
|
echo "Please specify 'create' or 'register'."
|
|
|
|
echo ""
|
|
|
|
exit 1
|
|
|
|
fi
|