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

Commit ba9b075

Browse files
authored
Merge pull request #740 from Random-Liu/improve-gce-bootstrap
Improve gce bootstrapping in various ways.
2 parents daa9f60 + d1ba950 commit ba9b075

File tree

19 files changed

+246
-236
lines changed

19 files changed

+246
-236
lines changed

cluster/gce/cloud-init/master.yaml

Lines changed: 1 addition & 20 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
@@ -65,7 +46,7 @@ write_files:
6546
LimitNPROC=infinity
6647
LimitCORE=infinity
6748
ExecStartPre=/sbin/modprobe overlay
68-
ExecStart=/home/containerd/usr/local/bin/containerd --log-level debug
49+
ExecStart=/home/containerd/usr/local/bin/containerd
6950
7051
[Install]
7152
WantedBy=containerd.target

cluster/gce/cloud-init/node.yaml

Lines changed: 1 addition & 18 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
@@ -63,7 +46,7 @@ write_files:
6346
LimitNPROC=infinity
6447
LimitCORE=infinity
6548
ExecStartPre=/sbin/modprobe overlay
66-
ExecStart=/home/containerd/usr/local/bin/containerd --log-level debug
49+
ExecStart=/home/containerd/usr/local/bin/containerd
6750
6851
[Install]
6952
WantedBy=containerd.target

cluster/gce/configure.sh

Lines changed: 133 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,148 @@ 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_file_name=$1
46+
(
47+
umask 077;
48+
local -r tmp_env_file="/tmp/${env_file_name}.yaml"
49+
tmp_env_content=$(fetch_metadata "${env_file_name}")
50+
if [ -z "${tmp_env_content}" ]; then
51+
echo "No environment variable is specified in ${env_file_name}"
52+
return
53+
fi
54+
echo "${tmp_env_content}" > "${tmp_env_file}"
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_file}" > "${CONTAINERD_HOME}/${env_file_name}")
61+
rm -f "${tmp_env_file}"
62+
)
63+
}
64+
65+
# is_preloaded checks whether a package has been 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 is from kube-env.
142+
network_policy_provider="${NETWORK_POLICY_PROVIDER:-"none"}"
143+
if [ -n "${network_policy_provider}" ] && [ "${network_policy_provider}" != "none" ] && [ "${KUBERNETES_MASTER:-}" != "true" ]; then
144+
# Use Kubernetes cni daemonset on node if network policy provider is specified.
145+
cni_bin_dir="${KUBE_HOME}/bin"
146+
cni_template_path=""
147+
fi
148+
log_level="${CONTAINERD_LOG_LEVEL:-"info"}"
149+
cat > ${config_path} <<EOF
150+
[debug]
151+
level = "${log_level}"
152+
153+
[plugins.linux]
154+
shim = "${CONTAINERD_HOME}/usr/local/bin/containerd-shim"
155+
runtime = "${CONTAINERD_HOME}/usr/local/sbin/runc"
156+
157+
[plugins.cri]
158+
enable_tls_streaming = true
159+
[plugins.cri.cni]
160+
bin_dir = "${cni_bin_dir}"
161+
conf_dir = "/etc/cni/net.d"
162+
conf_template = "${cni_template_path}"
163+
[plugins.cri.registry.mirrors."docker.io"]
164+
endpoint = ["https://mirror.gcr.io","https://registry-1.docker.io"]
165+
EOF
166+
chmod 644 "${config_path}"
167+
66168
echo "export PATH=${CONTAINERD_HOME}/usr/local/bin/:${CONTAINERD_HOME}/usr/local/sbin/:\$PATH" > \
67169
/etc/profile.d/containerd_env.sh
170+
171+
# Run extra init script for test.
172+
if [ "${CONTAINERD_TEST:-"false"}" == "true" ]; then
173+
# EXTRA_INIT_SCRIPT is the name of the extra init script after being downloaded.
174+
EXTRA_INIT_SCRIPT="containerd-extra-init.sh"
175+
# EXTRA_INIT_SCRIPT_METADATA is the metadata key of init script.
176+
EXTRA_INIT_SCRIPT_METADATA="containerd-extra-init-sh"
177+
extra_init=$(fetch_metadata "${EXTRA_INIT_SCRIPT_METADATA}")
178+
# Return if containerd-extra-init-sh is not set.
179+
if [ -z "${extra_init}" ]; then
180+
exit 0
181+
fi
182+
echo "${extra_init}" > "${EXTRA_INIT_SCRIPT}"
183+
chmod 544 "${EXTRA_INIT_SCRIPT}"
184+
./${EXTRA_INIT_SCRIPT}
185+
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/install/install-cni-config.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
# Copyright 2018 The containerd Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
source $(dirname "${BASH_SOURCE[0]}")/utils.sh
22+
CNI_CONFIG_DIR=${DESTDIR}/etc/cni/net.d
23+
${SUDO} mkdir -p ${CNI_CONFIG_DIR}
24+
${SUDO} bash -c 'cat >'${CNI_CONFIG_DIR}'/10-containerd-net.conflist <<EOF
25+
{
26+
"cniVersion": "0.3.1",
27+
"name": "containerd-net",
28+
"plugins": [
29+
{
30+
"type": "bridge",
31+
"bridge": "cni0",
32+
"isGateway": true,
33+
"ipMasq": true,
34+
"promiscMode": true,
35+
"ipam": {
36+
"type": "host-local",
37+
"subnet": "10.88.0.0/16",
38+
"routes": [
39+
{ "dst": "0.0.0.0/0" }
40+
]
41+
}
42+
},
43+
{
44+
"type": "portmap",
45+
"capabilities": {"portMappings": true}
46+
}
47+
]
48+
}
49+
EOF'

hack/install/install-cni.sh

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ set -o pipefail
2020

2121
source $(dirname "${BASH_SOURCE[0]}")/utils.sh
2222
CNI_DIR=${DESTDIR}/opt/cni
23-
CNI_CONFIG_DIR=${DESTDIR}/etc/cni/net.d
2423
CNI_PKG=github.com/containernetworking/plugins
2524

2625
# Create a temporary GOPATH for cni installation.
@@ -34,33 +33,6 @@ cd ${GOPATH}/src/${CNI_PKG}
3433
FASTBUILD=true ./build.sh
3534
${SUDO} mkdir -p ${CNI_DIR}
3635
${SUDO} cp -r ./bin ${CNI_DIR}
37-
${SUDO} mkdir -p ${CNI_CONFIG_DIR}
38-
${SUDO} bash -c 'cat >'${CNI_CONFIG_DIR}'/10-containerd-net.conflist <<EOF
39-
{
40-
"cniVersion": "0.3.1",
41-
"name": "containerd-net",
42-
"plugins": [
43-
{
44-
"type": "bridge",
45-
"bridge": "cni0",
46-
"isGateway": true,
47-
"ipMasq": true,
48-
"promiscMode": true,
49-
"ipam": {
50-
"type": "host-local",
51-
"subnet": "10.88.0.0/16",
52-
"routes": [
53-
{ "dst": "0.0.0.0/0" }
54-
]
55-
}
56-
},
57-
{
58-
"type": "portmap",
59-
"capabilities": {"portMappings": true}
60-
}
61-
]
62-
}
63-
EOF'
6436

6537
# Clean the tmp GOPATH dir.
6638
rm -rf ${TMPGOPATH}

hack/install/install-deps.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ cd $(dirname "${BASH_SOURCE[0]}")
3434
# and configurations in cluster.
3535
INSTALL_CNI=${INSTALL_CNI:-true}
3636

37+
# INSTALL_CNI indicates whether to install CNI config.
38+
INSTALL_CNI_CONFIG=${INSTALL_CNI_CONFIG:-true}
39+
3740
# Install runc
3841
./install-runc.sh
3942

@@ -42,6 +45,11 @@ if ${INSTALL_CNI}; then
4245
./install-cni.sh
4346
fi
4447

48+
# Install cni config
49+
if ${INSTALL_CNI_CONFIG}; then
50+
./install-cni-config.sh
51+
fi
52+
4553
# Install containerd
4654
./install-containerd.sh
4755

hack/release.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ fi
4343
rm -rf ${destdir}
4444

4545
# Install dependencies into release stage.
46-
NOSUDO=true INSTALL_CNI=${INCLUDE_CNI} DESTDIR=${destdir} ./hack/install/install-deps.sh
46+
NOSUDO=true INSTALL_CNI=${INCLUDE_CNI} INSTALL_CNI_CONFIG=false DESTDIR=${destdir} \
47+
./hack/install/install-deps.sh
4748

4849
if ${CUSTOM_CONTAINERD}; then
4950
make install -e DESTDIR=${destdir}
@@ -56,7 +57,9 @@ cp ${ROOT}/contrib/systemd-units/* ${destdir}/etc/systemd/system/
5657
mkdir -p ${destdir}/opt/containerd
5758
cp -r ${ROOT}/cluster ${destdir}/opt/containerd
5859
# Write a version file into the release tarball.
59-
echo ${VERSION} > ${destdir}/opt/containerd/cluster/version
60+
cat > ${destdir}/opt/containerd/cluster/version <<EOF
61+
CONTAINERD_VERSION: $(yaml-quote ${VERSION})
62+
EOF
6063

6164
# Create release tar
6265
tarball=${BUILD_DIR}/${TARBALL}

hack/test-utils.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ test_setup() {
4040
exit 1
4141
fi
4242
sudo pkill -x containerd
43-
keepalive "sudo ${ROOT}/_output/containerd ${CONTAINERD_FLAGS}" \
43+
keepalive "sudo PATH=${PATH} ${ROOT}/_output/containerd ${CONTAINERD_FLAGS}" \
4444
${RESTART_WAIT_PERIOD} &> ${report_dir}/containerd.log &
4545
containerd_pid=$!
4646
# Wait for containerd to be running by using the containerd client ctr to check the version

0 commit comments

Comments
 (0)