Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit 0cda57f

Browse files
committed
Improve gce bootstrapping in various ways.
Signed-off-by: Lantao Liu <[email protected]>
1 parent 77a33b1 commit 0cda57f

File tree

14 files changed

+174
-203
lines changed

14 files changed

+174
-203
lines changed

cluster/gce/cloud-init/master.yaml

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,6 @@ write_files:
2424
[Install]
2525
WantedBy=containerd.target
2626
27-
# containerd on master uses the cni binary and config in the
28-
# release tarball.
29-
- path: /etc/containerd/config.toml
30-
permissions: 0644
31-
owner: root
32-
content: |
33-
[plugins.linux]
34-
shim = "/home/containerd/usr/local/bin/containerd-shim"
35-
runtime = "/home/containerd/usr/local/sbin/runc"
36-
37-
[plugins.cri]
38-
enable_tls_streaming = true
39-
[plugins.cri.cni]
40-
bin_dir = "/home/containerd/opt/cni/bin"
41-
conf_dir = "/etc/cni/net.d"
42-
conf_template = "/home/containerd/opt/containerd/cluster/gce/cni.template"
43-
[plugins.cri.registry.mirrors."docker.io"]
44-
endpoint = ["https://mirror.gcr.io","https://registry-1.docker.io"]
45-
4627
- path: /etc/systemd/system/containerd.service
4728
permissions: 0644
4829
owner: root

cluster/gce/cloud-init/node.yaml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,6 @@ write_files:
2424
[Install]
2525
WantedBy=containerd.target
2626
27-
- path: /etc/containerd/config.toml
28-
permissions: 0644
29-
owner: root
30-
content: |
31-
[plugins.linux]
32-
shim = "/home/containerd/usr/local/bin/containerd-shim"
33-
runtime = "/home/containerd/usr/local/sbin/runc"
34-
35-
[plugins.cri]
36-
enable_tls_streaming = true
37-
[plugins.cri.cni]
38-
bin_dir = "/home/containerd/opt/cni/bin"
39-
conf_dir = "/etc/cni/net.d"
40-
conf_template = "/home/containerd/opt/containerd/cluster/gce/cni.template"
41-
[plugins.cri.registry.mirrors."docker.io"]
42-
endpoint = ["https://mirror.gcr.io","https://registry-1.docker.io"]
43-
4427
- path: /etc/systemd/system/containerd.service
4528
permissions: 0644
4629
owner: root

cluster/gce/configure.sh

Lines changed: 128 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ set -o pipefail
2222
# CONTAINERD_HOME is the directory for containerd.
2323
CONTAINERD_HOME="/home/containerd"
2424
cd "${CONTAINERD_HOME}"
25+
# KUBE_HOME is the directory for kubernetes.
26+
KUBE_HOME="/home/kubernetes"
2527

2628
# fetch_metadata fetches metadata from GCE metadata server.
2729
# Var set:
@@ -36,32 +38,143 @@ fetch_metadata() {
3638
fi
3739
}
3840

39-
# DEPLOY_PATH is the gcs path where cri-containerd tarball is stored.
40-
DEPLOY_PATH=${DEPLOY_PATH:-"cri-containerd-release"}
41+
# fetch_env fetches environment variables from GCE metadata server
42+
# and generate a env file under ${CONTAINERD_HOME}. It assumes that
43+
# the environment variables in metadata are in yaml format.
44+
fetch_env() {
45+
local -r env_name=$1
46+
(
47+
umask 700;
48+
local -r tmp_env="/tmp/${env_name}.yaml"
49+
tmp_env_content=$(fetch_metadata "${env_name}")
50+
if [ -z "${tmp_env_content}" ]; then
51+
echo "No environment variable is specified in ${env_name}"
52+
return
53+
fi
54+
echo "${tmp_env_content}" > "${tmp_env}"
55+
# Convert the yaml format file into a shell-style file.
56+
eval $(python -c '''
57+
import pipes,sys,yaml
58+
for k,v in yaml.load(sys.stdin).iteritems():
59+
print("readonly {var}={value}".format(var = k, value = pipes.quote(str(v))))
60+
''' < "${tmp_env}" > "${CONTAINERD_HOME}/${env_name}")
61+
rm -f "${tmp_env}"
62+
)
63+
}
64+
65+
# is_preloaded checks whether containerd is preloaded in the image.
66+
is_preloaded() {
67+
local -r tar=$1
68+
local -r sha1=$2
69+
grep -qs "${tar},${sha1}" "${KUBE_HOME}/preload_info"
70+
}
71+
72+
# KUBE_ENV_METADATA is the metadata key for kubernetes envs.
73+
KUBE_ENV_METADATA="kube-env"
74+
fetch_env ${KUBE_ENV_METADATA}
75+
if [ -f "${CONTAINERD_HOME}/${KUBE_ENV_METADATA}" ]; then
76+
source "${CONTAINERD_HOME}/${KUBE_ENV_METADATA}"
77+
fi
4178

42-
# PKG_PREFIX is the prefix of the cri-containerd tarball name.
79+
# CONTAINERD_ENV_METADATA is the metadata key for containerd envs.
80+
CONTAINERD_ENV_METADATA="containerd-env"
81+
fetch_env ${CONTAINERD_ENV_METADATA}
82+
if [ -f "${CONTAINERD_HOME}/${CONTAINERD_ENV_METADATA}" ]; then
83+
source "${CONTAINERD_HOME}/${CONTAINERD_ENV_METADATA}"
84+
fi
85+
86+
# CONTAINERD_PKG_PREFIX is the prefix of the cri-containerd tarball name.
4387
# By default use the release tarball with cni built in.
44-
PKG_PREFIX=${PKG_PREFIX:-"cri-containerd-cni"}
45-
46-
# VERSION is the cri-containerd version to use.
47-
VERSION_METADATA="version"
48-
VERSION=$(fetch_metadata "${VERSION_METADATA}")
49-
if [ -z "${VERSION}" ]; then
50-
echo "Version is not set."
51-
exit 1
88+
pkg_prefix=${CONTAINERD_PKG_PREFIX:-"cri-containerd-cni"}
89+
# Behave differently for test and production.
90+
if [ "${CONTAINERD_TEST:-"false"}" != "true" ]; then
91+
# CONTAINERD_DEPLOY_PATH is the gcs path where cri-containerd tarball is stored.
92+
deploy_path=${CONTAINERD_DEPLOY_PATH:-"cri-containerd-release"}
93+
# CONTAINERD_VERSION is the cri-containerd version to use.
94+
version=${CONTAINERD_VERSION:-""}
95+
if [ -z "${version}" ]; then
96+
echo "CONTAINERD_VERSION is not set."
97+
exit 1
98+
fi
99+
else
100+
deploy_path=${CONTAINERD_DEPLOY_PATH:-"cri-containerd-staging"}
101+
102+
# PULL_REFS_METADATA is the metadata key of PULL_REFS from prow.
103+
PULL_REFS_METADATA="PULL_REFS"
104+
pull_refs=$(fetch_metadata "${PULL_REFS_METADATA}")
105+
if [ ! -z "${pull_refs}" ]; then
106+
deploy_dir=$(echo "${pull_refs}" | sha1sum | awk '{print $1}')
107+
deploy_path="${deploy_path}/${deploy_dir}"
108+
fi
109+
110+
# TODO(random-liu): Put version into the metadata instead of
111+
# deciding it in cloud init. This may cause issue to reboot test.
112+
version=$(curl -f --ipv4 --retry 6 --retry-delay 3 --silent --show-error \
113+
https://storage.googleapis.com/${DEPLOY_PATH}/latest)
52114
fi
53115

116+
TARBALL_GCS_NAME="${pkg_prefix}-${version}.linux-amd64.tar.gz"
54117
# TARBALL_GCS_PATH is the path to download cri-containerd tarball for node e2e.
55-
TARBALL_GCS_PATH="https://storage.googleapis.com/${DEPLOY_PATH}/${PKG_PREFIX}-${VERSION}.linux-amd64.tar.gz"
118+
TARBALL_GCS_PATH="https://storage.googleapis.com/${deploy_path}/${TARBALL_GCS_NAME}"
56119
# TARBALL is the name of the tarball after being downloaded.
57120
TARBALL="cri-containerd.tar.gz"
58121

59-
# Download and untar the release tar ball.
60-
curl -f --ipv4 -Lo "${TARBALL}" --connect-timeout 20 --max-time 300 --retry 6 --retry-delay 10 "${TARBALL_GCS_PATH}"
61-
tar xvf "${TARBALL}"
122+
# CONTAINERD_TAR_SHA1 is the sha1sum of containerd tarball.
123+
if is_preloaded "${TARBALL_GCS_NAME}" "${CONTAINERD_TAR_SHA1:-""}"; then
124+
echo "${TARBALL_GCS_NAME} is preloaded"
125+
else
126+
# Download and untar the release tar ball.
127+
curl -f --ipv4 -Lo "${TARBALL}" --connect-timeout 20 --max-time 300 --retry 6 --retry-delay 10 "${TARBALL_GCS_PATH}"
128+
tar xvf "${TARBALL}"
129+
rm -f "${TARBALL}"
130+
fi
62131

132+
# Configure containerd.
63133
# Copy crictl config.
64134
cp "${CONTAINERD_HOME}/etc/crictl.yaml" /etc
65135

136+
# Generate containerd config
137+
config_path=${CONTAINERD_CONFIG_PATH:-"/etc/containerd/config.toml"}
138+
mkdir -p $(dirname ${config_path})
139+
cni_bin_dir="${CONTAINERD_HOME}/opt/cni/bin"
140+
cni_template_path="${CONTAINERD_HOME}/opt/containerd/cluster/gce/cni.template"
141+
network_policy_provider="${NETWORK_POLICY_PROVIDER:-"none"}"
142+
if [ -n "${network_policy_provider}" ] && [ "${network_policy_provider}" != "none" ] && [ "${KUBERNETES_MASTER:-}" != "true" ]; then
143+
# Use Kubernetes cni daemonset on node if network policy provider is specified.
144+
cni_bin_dir="${KUBE_HOME}/bin"
145+
cni_template_path=""
146+
fi
147+
cat > ${config_path} <<EOF
148+
[plugins.linux]
149+
shim = "${CONTAINERD_HOME}/usr/local/bin/containerd-shim"
150+
runtime = "${CONTAINERD_HOME}/usr/local/sbin/runc"
151+
152+
[plugins.cri]
153+
enable_tls_streaming = true
154+
[plugins.cri.cni]
155+
bin_dir = "${cni_bin_dir}"
156+
conf_dir = "/etc/cni/net.d"
157+
conf_template = "${cni_template_path}"
158+
[plugins.cri.registry.mirrors."docker.io"]
159+
endpoint = ["https://mirror.gcr.io","https://registry-1.docker.io"]
160+
EOF
161+
chmod 644 "${config_path}"
162+
66163
echo "export PATH=${CONTAINERD_HOME}/usr/local/bin/:${CONTAINERD_HOME}/usr/local/sbin/:\$PATH" > \
67164
/etc/profile.d/containerd_env.sh
165+
166+
# Run extra init script for test.
167+
if [ "${CONTAINERD_TEST:-"false"}" == "true" ]; then
168+
# EXTRA_INIT_SCRIPT is the name of the extra init script after being downloaded.
169+
EXTRA_INIT_SCRIPT="containerd-extra-init.sh"
170+
# EXTRA_INIT_SCRIPT_METADATA is the metadata key of init script.
171+
EXTRA_INIT_SCRIPT_METADATA="containerd-extra-init-sh"
172+
extra_init=$(fetch_metadata "${EXTRA_INIT_SCRIPT_METADATA}")
173+
# Return if containerd-extra-init-sh is not set.
174+
if [ -z "${extra_init}" ]; then
175+
exit 0
176+
fi
177+
echo "${extra_init}" > "${EXTRA_INIT_SCRIPT}"
178+
chmod 544 "${EXTRA_INIT_SCRIPT}"
179+
./${EXTRA_INIT_SCRIPT}
180+
fi

cluster/gce/env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ if [ ! -f "${version_file}" ]; then
88
echo "version file does not exist"
99
exit 1
1010
fi
11-
export KUBE_MASTER_EXTRA_METADATA="user-data=${GCE_DIR}/cloud-init/master.yaml,containerd-configure-sh=${GCE_DIR}/configure.sh,version=${version_file}"
12-
export KUBE_NODE_EXTRA_METADATA="user-data=${GCE_DIR}/cloud-init/node.yaml,containerd-configure-sh=${GCE_DIR}/configure.sh,version=${version_file}"
11+
export KUBE_MASTER_EXTRA_METADATA="user-data=${GCE_DIR}/cloud-init/master.yaml,containerd-configure-sh=${GCE_DIR}/configure.sh,containerd-env=${version_file}"
12+
export KUBE_NODE_EXTRA_METADATA="user-data=${GCE_DIR}/cloud-init/node.yaml,containerd-configure-sh=${GCE_DIR}/configure.sh,containerd-env=${version_file}"
1313
export KUBE_CONTAINER_RUNTIME="remote"
1414
export KUBE_CONTAINER_RUNTIME_ENDPOINT="/run/containerd/containerd.sock"
1515
export KUBE_LOAD_IMAGE_COMMAND="/home/containerd/usr/local/bin/ctr cri load"

hack/release.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ cp ${ROOT}/contrib/systemd-units/* ${destdir}/etc/systemd/system/
5656
mkdir -p ${destdir}/opt/containerd
5757
cp -r ${ROOT}/cluster ${destdir}/opt/containerd
5858
# Write a version file into the release tarball.
59-
echo ${VERSION} > ${destdir}/opt/containerd/cluster/version
59+
cat > ${destdir}/opt/containerd/cluster/version <<EOF
60+
CONTAINERD_VERSION: $(yaml-quote ${VERSION})
61+
EOF
6062

6163
# Create release tar
6264
tarball=${BUILD_DIR}/${TARBALL}

hack/utils.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,10 @@ from-vendor() {
9595
fi
9696
eval $setvars
9797
}
98+
99+
# yaml-quote quotes something appropriate for a yaml string.
100+
# This is the same with:
101+
# https://github.com/kubernetes/kubernetes/blob/v1.10.1/cluster/gce/util.sh#L471.
102+
yaml-quote() {
103+
echo "'$(echo "${@:-}" | sed -e "s/'/''/g")'"
104+
}

test/configure.sh

Lines changed: 0 additions & 99 deletions
This file was deleted.

test/containerd/deploy-path

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/containerd/env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONTAINERD_DEPLOY_PATH: 'cri-containerd-staging/containerd'
2+
CONTAINERD_PKG_PREFIX: 'containerd-cni'

test/containerd/image-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ images:
22
ubuntu:
33
image: ubuntu-gke-1604-xenial-v20170420-1
44
project: ubuntu-os-gke-cloud
5-
metadata: "user-data<test/e2e_node/init.yaml,containerd-configure-sh<test/configure.sh,pkg-prefix<test/containerd/pkg-prefix,deploy-path<test/containerd/deploy-path"
5+
metadata: "user-data<test/e2e_node/init.yaml,containerd-configure-sh<cluster/gce/configure.sh,containerd-env<test/containerd/env"
66
cos-stable:
77
image_regex: cos-stable-60-9592-84-0
88
project: cos-cloud
9-
metadata: "user-data<test/e2e_node/init.yaml,containerd-configure-sh<test/configure.sh,extra-init-sh<test/e2e_node/gci-init.sh,gci-update-strategy=update_disabled,pkg-prefix<test/containerd/pkg-prefix,deploy-path<test/containerd/deploy-path"
9+
metadata: "user-data<test/e2e_node/init.yaml,containerd-configure-sh<cluster/gce/configure.sh,containerd-extra-init-sh<test/e2e_node/gci-init.sh,containerd-env<test/containerd/env,gci-update-strategy=update_disabled"

0 commit comments

Comments
 (0)