@@ -12,11 +12,24 @@ RETRY_COUNT=5
12
12
function create_container_image_tar() {
13
13
set -e
14
14
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
20
33
21
34
rm -f ${IMAGE_TAR_FILE}
22
35
rm -rf ${IMAGE_DIR}
@@ -26,9 +39,9 @@ function create_container_image_tar() {
26
39
sudo ${runtime} pull registry:latest
27
40
sudo ${runtime} save -o registry-latest.tar registry:latest
28
41
29
- for image in ${IMAGES}
42
+ while read -r image
30
43
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
32
45
set +e
33
46
for step in $( seq 1 ${RETRY_COUNT} )
34
47
do
@@ -48,18 +61,20 @@ function create_container_image_tar() {
48
61
# so that these parts will be replaced with Kubespray.
49
62
# - kube_image_repo: "registry.k8s.io"
50
63
# - gcr_image_repo: "gcr.io"
64
+ # - ghcr_image_repo: "ghcr.io"
51
65
# - docker_image_repo: "docker.io"
52
66
# - quay_image_repo: "quay.io"
53
67
FIRST_PART=$( echo ${image} | awk -F" /" ' {print $1}' )
54
68
if [ " ${FIRST_PART} " = " registry.k8s.io" ] ||
55
69
[ " ${FIRST_PART} " = " gcr.io" ] ||
70
+ [ " ${FIRST_PART} " = " ghcr.io" ] ||
56
71
[ " ${FIRST_PART} " = " docker.io" ] ||
57
72
[ " ${FIRST_PART} " = " quay.io" ] ||
58
73
[ " ${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 ' )
60
75
fi
61
76
echo " ${FILE_NAME} ${image} " >> ${IMAGE_LIST}
62
- done
77
+ done < " ${IMAGES} "
63
78
64
79
cd ..
65
80
sudo chown ${USER} ${IMAGE_DIR} /*
@@ -72,6 +87,16 @@ function create_container_image_tar() {
72
87
}
73
88
74
89
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
+
75
100
if [ ! -f ${IMAGE_TAR_FILE} ]; then
76
101
echo " ${IMAGE_TAR_FILE} should exist."
77
102
exit 1
@@ -81,38 +106,46 @@ function register_container_images() {
81
106
fi
82
107
83
108
# To avoid "http: server gave http response to https client" error.
84
- LOCALHOST_NAME=$( hostname)
85
109
if [ -d /etc/docker/ ]; then
86
110
set -e
87
111
# Ubuntu18.04, RHEL7/CentOS7
88
112
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
90
114
sudo cp ${TEMP_DIR} /docker-daemon.json /etc/docker/daemon.json
91
115
elif [ -d /etc/containers/ ]; then
92
116
set -e
93
117
# RHEL8/CentOS8
94
118
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
96
120
sudo cp ${TEMP_DIR} /registries.conf /etc/containers/registries.conf
97
121
else
98
122
echo " runtime package(docker-ce, podman, nerctl, etc.) should be installed"
99
123
exit 1
100
124
fi
101
125
102
126
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
108
137
fi
109
- set -e
110
138
111
139
while read -r line; do
112
140
file_name=$( echo ${line} | awk ' {print $1}' )
113
141
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
116
149
image_id=$( sudo ${runtime} image inspect ${org_image} | grep " \" Id\" :" | awk -F: ' {print $3}' | sed s/' \",' //)
117
150
if [ -z " ${file_name} " ]; then
118
151
echo " Failed to get file_name for line ${line} "
@@ -136,7 +169,7 @@ function register_container_images() {
136
169
done <<< " $(cat ${IMAGE_LIST})"
137
170
138
171
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:"
140
173
echo " - kube_image_repo"
141
174
echo " - gcr_image_repo"
142
175
echo " - docker_image_repo"
@@ -161,13 +194,17 @@ elif [ "${OPTION}" == "register" ]; then
161
194
register_container_images
162
195
else
163
196
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)."
165
200
echo " (2) Deploy local container registry and register the container images to the registry."
166
201
echo " "
167
202
echo " Step(1) should be done online site as a preparation, then we bring"
168
203
echo " the gotten images to the target offline environment. if images are from"
169
204
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"
171
208
echo " "
172
209
echo " ${IMAGE_TAR_FILE} is created to contain your container images."
173
210
echo " Please keep this file and bring it to your offline environment."
0 commit comments