#!/usr/bin/env bash # Copyright 2016 The Kubernetes Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Due to the GCE custom metadata size limit, we split the entire script into two # files configure.sh and configure-helper.sh. The functionality of downloading # kubernetes configuration, manifests, docker images, and binary files are # put in configure.sh, which is uploaded via GCE custom metadata. set -o errexit set -o nounset set -o pipefail ### Hardcoded constants DEFAULT_CNI_VERSION="v0.6.0" DEFAULT_CNI_SHA1="d595d3ded6499a64e8dac02466e2f5f2ce257c9f" DEFAULT_NPD_VERSION="v0.5.0" DEFAULT_NPD_SHA1="650ecfb2ae495175ee43706d0bd862a1ea7f1395" DEFAULT_CRICTL_VERSION="v1.11.0" DEFAULT_CRICTL_SHA1="8f5142b985d314cdebb51afd55054d5ec00c442a" DEFAULT_MOUNTER_TAR_SHA="8003b798cf33c7f91320cd6ee5cec4fa22244571" ### # Use --retry-connrefused opt only if it's supported by curl. CURL_RETRY_CONNREFUSED="" if curl --help | grep -q -- '--retry-connrefused'; then CURL_RETRY_CONNREFUSED='--retry-connrefused' fi function set-broken-motd { cat > /etc/motd < "${KUBE_HOME}/kube-env") rm -f "${tmp_kube_env}" ) } function download-kubelet-config { local -r dest="$1" echo "Downloading Kubelet config file, if it exists" # Fetch kubelet config file from GCE metadata server. ( umask 077 local -r tmp_kubelet_config="/tmp/kubelet-config.yaml" if curl --fail --retry 5 --retry-delay 3 ${CURL_RETRY_CONNREFUSED} --silent --show-error \ -H "X-Google-Metadata-Request: True" \ -o "${tmp_kubelet_config}" \ http://metadata.google.internal/computeMetadata/v1/instance/attributes/kubelet-config; then # only write to the final location if curl succeeds mv "${tmp_kubelet_config}" "${dest}" elif [[ "${REQUIRE_METADATA_KUBELET_CONFIG_FILE:-false}" == "true" ]]; then echo "== Failed to download required Kubelet config file from metadata server ==" exit 1 fi ) } function download-kube-master-certs { # Fetch kube-env from GCE metadata server. ( umask 077 local -r tmp_kube_master_certs="/tmp/kube-master-certs.yaml" curl --fail --retry 5 --retry-delay 3 ${CURL_RETRY_CONNREFUSED} --silent --show-error \ -H "X-Google-Metadata-Request: True" \ -o "${tmp_kube_master_certs}" \ http://metadata.google.internal/computeMetadata/v1/instance/attributes/kube-master-certs # Convert the yaml format file into a shell-style file. eval $(python -c ''' import pipes,sys,yaml for k,v in yaml.load(sys.stdin).iteritems(): print("readonly {var}={value}".format(var = k, value = pipes.quote(str(v)))) ''' < "${tmp_kube_master_certs}" > "${KUBE_HOME}/kube-master-certs") rm -f "${tmp_kube_master_certs}" ) } function validate-hash { local -r file="$1" local -r expected="$2" actual=$(sha1sum ${file} | awk '{ print $1 }') || true if [[ "${actual}" != "${expected}" ]]; then echo "== ${file} corrupted, sha1 ${actual} doesn't match expected ${expected} ==" return 1 fi } # Retry a download until we get it. Takes a hash and a set of URLs. # # $1 is the sha1 of the URL. Can be "" if the sha1 is unknown. # $2+ are the URLs to download. function download-or-bust { local -r hash="$1" shift 1 local -r urls=( $* ) while true; do for url in "${urls[@]}"; do local file="${url##*/}" rm -f "${file}" if ! curl -f --ipv4 -Lo "${file}" --connect-timeout 20 --max-time 300 --retry 6 --retry-delay 10 ${CURL_RETRY_CONNREFUSED} "${url}"; then echo "== Failed to download ${url}. Retrying. ==" elif [[ -n "${hash}" ]] && ! validate-hash "${file}" "${hash}"; then echo "== Hash validation of ${url} failed. Retrying. ==" else if [[ -n "${hash}" ]]; then echo "== Downloaded ${url} (SHA1 = ${hash}) ==" else echo "== Downloaded ${url} ==" fi return fi done done } function is-preloaded { local -r key=$1 local -r value=$2 grep -qs "${key},${value}" "${KUBE_HOME}/preload_info" } function split-commas { echo $1 | tr "," "\n" } function remount-flexvolume-directory { local -r flexvolume_plugin_dir=$1 mkdir -p $flexvolume_plugin_dir mount --bind $flexvolume_plugin_dir $flexvolume_plugin_dir mount -o remount,exec $flexvolume_plugin_dir } function install-gci-mounter-tools { CONTAINERIZED_MOUNTER_HOME="${KUBE_HOME}/containerized_mounter" local -r mounter_tar_sha="${DEFAULT_MOUNTER_TAR_SHA}" if is-preloaded "mounter" "${mounter_tar_sha}"; then echo "mounter is preloaded." return fi echo "Downloading gci mounter tools." mkdir -p "${CONTAINERIZED_MOUNTER_HOME}" chmod a+x "${CONTAINERIZED_MOUNTER_HOME}" mkdir -p "${CONTAINERIZED_MOUNTER_HOME}/rootfs" download-or-bust "${mounter_tar_sha}" "https://storage.googleapis.com/kubernetes-release/gci-mounter/mounter.tar" cp "${KUBE_HOME}/kubernetes/server/bin/mounter" "${CONTAINERIZED_MOUNTER_HOME}/mounter" chmod a+x "${CONTAINERIZED_MOUNTER_HOME}/mounter" mv "${KUBE_HOME}/mounter.tar" /tmp/mounter.tar tar xf /tmp/mounter.tar -C "${CONTAINERIZED_MOUNTER_HOME}/rootfs" rm /tmp/mounter.tar mkdir -p "${CONTAINERIZED_MOUNTER_HOME}/rootfs/var/lib/kubelet" } # Install node problem detector binary. function install-node-problem-detector { if [[ -n "${NODE_PROBLEM_DETECTOR_VERSION:-}" ]]; then local -r npd_version="${NODE_PROBLEM_DETECTOR_VERSION}" local -r npd_sha1="${NODE_PROBLEM_DETECTOR_TAR_HASH}" else local -r npd_version="${DEFAULT_NPD_VERSION}" local -r npd_sha1="${DEFAULT_NPD_SHA1}" fi local -r npd_tar="node-problem-detector-${npd_version}.tar.gz" if is-preloaded "${npd_tar}" "${npd_sha1}"; then echo "node-problem-detector is preloaded." return fi echo "Downloading node problem detector." local -r npd_release_path="https://storage.googleapis.com/kubernetes-release" download-or-bust "${npd_sha1}" "${npd_release_path}/node-problem-detector/${npd_tar}" local -r npd_dir="${KUBE_HOME}/node-problem-detector" mkdir -p "${npd_dir}" tar xzf "${KUBE_HOME}/${npd_tar}" -C "${npd_dir}" --overwrite mv "${npd_dir}/bin"/* "${KUBE_BIN}" chmod a+x "${KUBE_BIN}/node-problem-detector" rmdir "${npd_dir}/bin" rm -f "${KUBE_HOME}/${npd_tar}" } function install-cni-binaries { local -r cni_tar="cni-plugins-amd64-${DEFAULT_CNI_VERSION}.tgz" local -r cni_sha1="${DEFAULT_CNI_SHA1}" if is-preloaded "${cni_tar}" "${cni_sha1}"; then echo "${cni_tar} is preloaded." return fi echo "Downloading cni binaries" download-or-bust "${cni_sha1}" "https://storage.googleapis.com/kubernetes-release/network-plugins/${cni_tar}" local -r cni_dir="${KUBE_HOME}/cni" mkdir -p "${cni_dir}/bin" tar xzf "${KUBE_HOME}/${cni_tar}" -C "${cni_dir}/bin" --overwrite mv "${cni_dir}/bin"/* "${KUBE_BIN}" rmdir "${cni_dir}/bin" rm -f "${KUBE_HOME}/${cni_tar}" } # Install crictl binary. function install-crictl { if [[ -n "${CRICTL_VERSION:-}" ]]; then local -r crictl_version="${CRICTL_VERSION}" local -r crictl_sha1="${CRICTL_TAR_HASH}" else local -r crictl_version="${DEFAULT_CRICTL_VERSION}" local -r crictl_sha1="${DEFAULT_CRICTL_SHA1}" fi local -r crictl="crictl-${crictl_version}-linux-amd64" if is-preloaded "${crictl}" "${crictl_sha1}"; then echo "crictl is preloaded" return fi echo "Downloading crictl" local -r crictl_path="https://storage.googleapis.com/kubernetes-release/crictl" download-or-bust "${crictl_sha1}" "${crictl_path}/${crictl}" mv "${KUBE_HOME}/${crictl}" "${KUBE_BIN}/crictl" chmod a+x "${KUBE_BIN}/crictl" # Create crictl config file. cat > /etc/crictl.yaml <