Skip to content

Commit 8fa5ae1

Browse files
bin: improve manage-offline-container-images script (#10857)
Fixes bug for retrieving images with tags containing image digests. Script now gets images from jobs and cronjobs as well. New env variable DESTINATION_REGISTRY to push to another registry instead of local registry. New env variable IMAGES_FROM_FILE to pull images listed in a file instead of getting images from a running k8s environment. New env variable REGISTRY_PORT to override port (default is 5000).
1 parent 65b0604 commit 8fa5ae1

File tree

3 files changed

+68
-27
lines changed

3 files changed

+68
-27
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
**/vagrant_ansible_inventory
44
*.iml
55
temp
6+
contrib/offline/container-images
7+
contrib/offline/container-images.tar.gz
68
contrib/offline/offline-files
79
contrib/offline/offline-files.tar.gz
810
.idea

Diff for: contrib/offline/README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
Container image collecting script for offline deployment
66

77
This script has two features:
8-
9-
(1) Get container images from an environment which is deployed online.
10-
8+
(1) Get container images from an environment which is deployed online, or set IMAGES_FROM_FILE
9+
environment variable to get images from a file (e.g. temp/images.list after running the
10+
./generate_list.sh script).
1111
(2) Deploy local container registry and register the container images to the registry.
1212

1313
Step(1) should be done online site as a preparation, then we bring the gotten images
1414
to the target offline environment. if images are from a private registry,
1515
you need to set `PRIVATE_REGISTRY` environment variable.
16-
Then we will run step(2) for registering the images to local registry.
16+
Then we will run step(2) for registering the images to local registry, or to an existing
17+
registry set by the `DESTINATION_REGISTRY` environment variable. By default, the local registry
18+
will run on port 5000. This can be changed with the `REGISTRY_PORT` environment variable
1719

1820
Step(1) can be operated with:
1921

Diff for: contrib/offline/manage-offline-container-images.sh

+60-23
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,24 @@ RETRY_COUNT=5
1212
function create_container_image_tar() {
1313
set -e
1414

15-
IMAGES=$(kubectl describe pods --all-namespaces | grep " Image:" | awk '{print $2}' | sort | uniq)
16-
# NOTE: etcd and pause cannot be seen as pods.
17-
# The pause image is used for --pod-infra-container-image option of kubelet.
18-
EXT_IMAGES=$(kubectl cluster-info dump | egrep "quay.io/coreos/etcd:|registry.k8s.io/pause:" | sed s@\"@@g)
19-
IMAGES="${IMAGES} ${EXT_IMAGES}"
15+
if [ -z "${IMAGES_FROM_FILE}" ]; then
16+
echo "Getting images from current \"$(kubectl config current-context)\""
17+
18+
IMAGES=$(mktemp --suffix=-images)
19+
trap 'rm -f "${IMAGES}"' EXIT
20+
21+
kubectl describe cronjobs,jobs,pods --all-namespaces | grep " Image:" | awk '{print $2}' | sort | uniq > "${IMAGES}"
22+
# NOTE: etcd and pause cannot be seen as pods.
23+
# The pause image is used for --pod-infra-container-image option of kubelet.
24+
kubectl cluster-info dump | grep -E "quay.io/coreos/etcd:|registry.k8s.io/pause:" | sed s@\"@@g >> "${IMAGES}"
25+
else
26+
echo "Getting images from file \"${IMAGES_FROM_FILE}\""
27+
if [ ! -f "${IMAGES_FROM_FILE}" ]; then
28+
echo "${IMAGES_FROM_FILE} is not a file"
29+
exit 1
30+
fi
31+
IMAGES=$(realpath $IMAGES_FROM_FILE)
32+
fi
2033

2134
rm -f ${IMAGE_TAR_FILE}
2235
rm -rf ${IMAGE_DIR}
@@ -26,9 +39,9 @@ function create_container_image_tar() {
2639
sudo ${runtime} pull registry:latest
2740
sudo ${runtime} save -o registry-latest.tar registry:latest
2841

29-
for image in ${IMAGES}
42+
while read -r image
3043
do
31-
FILE_NAME="$(echo ${image} | sed s@"/"@"-"@g | sed s/":"/"-"/g)".tar
44+
FILE_NAME="$(echo ${image} | sed s@"/"@"-"@g | sed s/":"/"-"/g | sed -E 's/\@.*//g')".tar
3245
set +e
3346
for step in $(seq 1 ${RETRY_COUNT})
3447
do
@@ -48,18 +61,20 @@ function create_container_image_tar() {
4861
# so that these parts will be replaced with Kubespray.
4962
# - kube_image_repo: "registry.k8s.io"
5063
# - gcr_image_repo: "gcr.io"
64+
# - ghcr_image_repo: "ghcr.io"
5165
# - docker_image_repo: "docker.io"
5266
# - quay_image_repo: "quay.io"
5367
FIRST_PART=$(echo ${image} | awk -F"/" '{print $1}')
5468
if [ "${FIRST_PART}" = "registry.k8s.io" ] ||
5569
[ "${FIRST_PART}" = "gcr.io" ] ||
70+
[ "${FIRST_PART}" = "ghcr.io" ] ||
5671
[ "${FIRST_PART}" = "docker.io" ] ||
5772
[ "${FIRST_PART}" = "quay.io" ] ||
5873
[ "${FIRST_PART}" = "${PRIVATE_REGISTRY}" ]; then
59-
image=$(echo ${image} | sed s@"${FIRST_PART}/"@@)
74+
image=$(echo ${image} | sed s@"${FIRST_PART}/"@@ | sed -E 's/\@.*/\n/g')
6075
fi
6176
echo "${FILE_NAME} ${image}" >> ${IMAGE_LIST}
62-
done
77+
done < "${IMAGES}"
6378

6479
cd ..
6580
sudo chown ${USER} ${IMAGE_DIR}/*
@@ -72,6 +87,16 @@ function create_container_image_tar() {
7287
}
7388

7489
function register_container_images() {
90+
create_registry=false
91+
REGISTRY_PORT=${REGISTRY_PORT:-"5000"}
92+
93+
if [ -z "${DESTINATION_REGISTRY}" ]; then
94+
echo "DESTINATION_REGISTRY not set, will create local registry"
95+
create_registry=true
96+
DESTINATION_REGISTRY="$(hostname):${REGISTRY_PORT}"
97+
fi
98+
echo "Images will be pushed to ${DESTINATION_REGISTRY}"
99+
75100
if [ ! -f ${IMAGE_TAR_FILE} ]; then
76101
echo "${IMAGE_TAR_FILE} should exist."
77102
exit 1
@@ -81,38 +106,46 @@ function register_container_images() {
81106
fi
82107

83108
# To avoid "http: server gave http response to https client" error.
84-
LOCALHOST_NAME=$(hostname)
85109
if [ -d /etc/docker/ ]; then
86110
set -e
87111
# Ubuntu18.04, RHEL7/CentOS7
88112
cp ${CURRENT_DIR}/docker-daemon.json ${TEMP_DIR}/docker-daemon.json
89-
sed -i s@"HOSTNAME"@"${LOCALHOST_NAME}"@ ${TEMP_DIR}/docker-daemon.json
113+
sed -i s@"HOSTNAME"@"$(hostname)"@ ${TEMP_DIR}/docker-daemon.json
90114
sudo cp ${TEMP_DIR}/docker-daemon.json /etc/docker/daemon.json
91115
elif [ -d /etc/containers/ ]; then
92116
set -e
93117
# RHEL8/CentOS8
94118
cp ${CURRENT_DIR}/registries.conf ${TEMP_DIR}/registries.conf
95-
sed -i s@"HOSTNAME"@"${LOCALHOST_NAME}"@ ${TEMP_DIR}/registries.conf
119+
sed -i s@"HOSTNAME"@"$(hostname)"@ ${TEMP_DIR}/registries.conf
96120
sudo cp ${TEMP_DIR}/registries.conf /etc/containers/registries.conf
97121
else
98122
echo "runtime package(docker-ce, podman, nerctl, etc.) should be installed"
99123
exit 1
100124
fi
101125

102126
tar -zxvf ${IMAGE_TAR_FILE}
103-
sudo ${runtime} load -i ${IMAGE_DIR}/registry-latest.tar
104-
set +e
105-
sudo ${runtime} container inspect registry >/dev/null 2>&1
106-
if [ $? -ne 0 ]; then
107-
sudo ${runtime} run --restart=always -d -p 5000:5000 --name registry registry:latest
127+
128+
if [ "${create_registry}" ]; then
129+
sudo ${runtime} load -i ${IMAGE_DIR}/registry-latest.tar
130+
set +e
131+
132+
sudo ${runtime} container inspect registry >/dev/null 2>&1
133+
if [ $? -ne 0 ]; then
134+
sudo ${runtime} run --restart=always -d -p "${REGISTRY_PORT}":"${REGISTRY_PORT}" --name registry registry:latest
135+
fi
136+
set -e
108137
fi
109-
set -e
110138

111139
while read -r line; do
112140
file_name=$(echo ${line} | awk '{print $1}')
113141
raw_image=$(echo ${line} | awk '{print $2}')
114-
new_image="${LOCALHOST_NAME}:5000/${raw_image}"
115-
org_image=$(sudo ${runtime} load -i ${IMAGE_DIR}/${file_name} | head -n1 | awk '{print $3}')
142+
new_image="${DESTINATION_REGISTRY}/${raw_image}"
143+
load_image=$(sudo ${runtime} load -i ${IMAGE_DIR}/${file_name} | head -n1)
144+
org_image=$(echo "${load_image}" | awk '{print $3}')
145+
# special case for tags containing the digest when using docker or podman as the container runtime
146+
if [ "${org_image}" == "ID:" ]; then
147+
org_image=$(echo "${load_image}" | awk '{print $4}')
148+
fi
116149
image_id=$(sudo ${runtime} image inspect ${org_image} | grep "\"Id\":" | awk -F: '{print $3}'| sed s/'\",'//)
117150
if [ -z "${file_name}" ]; then
118151
echo "Failed to get file_name for line ${line}"
@@ -136,7 +169,7 @@ function register_container_images() {
136169
done <<< "$(cat ${IMAGE_LIST})"
137170

138171
echo "Succeeded to register container images to local registry."
139-
echo "Please specify ${LOCALHOST_NAME}:5000 for the following options in your inventry:"
172+
echo "Please specify \"${DESTINATION_REGISTRY}\" for the following options in your inventry:"
140173
echo "- kube_image_repo"
141174
echo "- gcr_image_repo"
142175
echo "- docker_image_repo"
@@ -161,13 +194,17 @@ elif [ "${OPTION}" == "register" ]; then
161194
register_container_images
162195
else
163196
echo "This script has two features:"
164-
echo "(1) Get container images from an environment which is deployed online."
197+
echo "(1) Get container images from an environment which is deployed online, or set IMAGES_FROM_FILE"
198+
echo " environment variable to get images from a file (e.g. temp/images.list after running the"
199+
echo " ./generate_list.sh script)."
165200
echo "(2) Deploy local container registry and register the container images to the registry."
166201
echo ""
167202
echo "Step(1) should be done online site as a preparation, then we bring"
168203
echo "the gotten images to the target offline environment. if images are from"
169204
echo "a private registry, you need to set PRIVATE_REGISTRY environment variable."
170-
echo "Then we will run step(2) for registering the images to local registry."
205+
echo "Then we will run step(2) for registering the images to local registry, or to an existing"
206+
echo "registry set by the DESTINATION_REGISTRY environment variable. By default, the local registry"
207+
echo "will run on port 5000. This can be changed with the REGISTRY_PORT environment variable"
171208
echo ""
172209
echo "${IMAGE_TAR_FILE} is created to contain your container images."
173210
echo "Please keep this file and bring it to your offline environment."

0 commit comments

Comments
 (0)