2015-04-03 21:48:39 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2015-05-01 16:19:44 +00:00
|
|
|
# Copyright 2015 The Kubernetes Authors All rights reserved.
|
2015-04-03 21:48:39 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
# !!!EXPERIMENTAL !!! Upgrade script for GCE. Expect this to get
|
|
|
|
# rewritten in Go in relatively short order, but it allows us to start
|
|
|
|
# testing the concepts.
|
|
|
|
|
|
|
|
set -o errexit
|
|
|
|
set -o nounset
|
|
|
|
set -o pipefail
|
|
|
|
|
|
|
|
if [[ "${KUBERNETES_PROVIDER:-gce}" != "gce" ]]; then
|
|
|
|
echo "!!! ${1} only works on GCE" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../..
|
|
|
|
source "${KUBE_ROOT}/cluster/kube-env.sh"
|
2015-07-02 16:48:31 +00:00
|
|
|
source "${KUBE_ROOT}/cluster/kube-util.sh"
|
2015-04-03 21:48:39 +00:00
|
|
|
|
|
|
|
function usage() {
|
|
|
|
echo "!!! EXPERIMENTAL !!!"
|
|
|
|
echo ""
|
2015-06-17 07:13:26 +00:00
|
|
|
echo "${0} [-M|-N|-P] -l | <release or continuous integration version> | [latest_stable|latest_release|latest_ci]"
|
2015-04-03 21:48:39 +00:00
|
|
|
echo " Upgrades master and nodes by default"
|
|
|
|
echo " -M: Upgrade master only"
|
|
|
|
echo " -N: Upgrade nodes only"
|
2015-06-17 07:13:26 +00:00
|
|
|
echo " -P: Node upgrade prerequisites only (create a new instance template)"
|
2015-04-06 23:01:16 +00:00
|
|
|
echo " -l: Use local(dev) binaries"
|
2015-04-03 21:48:39 +00:00
|
|
|
echo ""
|
|
|
|
echo "(... Fetching current release versions ...)"
|
|
|
|
echo ""
|
|
|
|
|
2015-06-19 22:17:28 +00:00
|
|
|
# NOTE: IF YOU CHANGE THE FOLLOWING LIST, ALSO UPDATE test/e2e/cluster_upgrade.go
|
2015-04-03 21:48:39 +00:00
|
|
|
local latest_release
|
|
|
|
local latest_stable
|
|
|
|
local latest_ci
|
|
|
|
|
|
|
|
latest_stable=$(gsutil cat gs://kubernetes-release/release/stable.txt)
|
|
|
|
latest_release=$(gsutil cat gs://kubernetes-release/release/latest.txt)
|
|
|
|
latest_ci=$(gsutil cat gs://kubernetes-release/ci/latest.txt)
|
|
|
|
|
|
|
|
echo "To upgrade to:"
|
2015-06-19 22:17:28 +00:00
|
|
|
echo " latest stable: ${0} ${latest_stable}"
|
2015-04-03 21:48:39 +00:00
|
|
|
echo " latest release: ${0} ${latest_release}"
|
|
|
|
echo " latest ci: ${0} ${latest_ci}"
|
|
|
|
}
|
|
|
|
|
|
|
|
function upgrade-master() {
|
2015-04-06 23:01:16 +00:00
|
|
|
echo "== Upgrading master to '${SERVER_BINARY_TAR_URL}'. Do not interrupt, deleting master instance. =="
|
2015-04-03 21:48:39 +00:00
|
|
|
|
2015-06-10 05:33:34 +00:00
|
|
|
get-kubeconfig-basicauth
|
|
|
|
get-kubeconfig-bearertoken
|
2015-05-08 00:41:22 +00:00
|
|
|
|
|
|
|
detect-master
|
2015-04-03 21:48:39 +00:00
|
|
|
|
|
|
|
# Delete the master instance. Note that the master-pd is created
|
|
|
|
# with auto-delete=no, so it should not be deleted.
|
|
|
|
gcloud compute instances delete \
|
|
|
|
--project "${PROJECT}" \
|
|
|
|
--quiet \
|
|
|
|
--zone "${ZONE}" \
|
|
|
|
"${MASTER_NAME}"
|
|
|
|
|
2015-04-06 15:35:02 +00:00
|
|
|
create-master-instance "${MASTER_NAME}-ip"
|
2015-04-03 21:48:39 +00:00
|
|
|
wait-for-master
|
|
|
|
}
|
|
|
|
|
|
|
|
function wait-for-master() {
|
|
|
|
echo "== Waiting for new master to respond to API requests =="
|
|
|
|
|
2015-06-10 05:33:34 +00:00
|
|
|
local curl_auth_arg
|
|
|
|
if [[ -n ${KUBE_BEARER_TOKEN:-} ]]; then
|
|
|
|
curl_auth_arg=(-H "Authorization: Bearer ${KUBE_BEARER_TOKEN}")
|
|
|
|
elif [[ -n ${KUBE_PASSWORD:-} ]]; then
|
|
|
|
curl_auth_arg=(--user "${KUBE_USER}:${KUBE_PASSWORD}")
|
|
|
|
else
|
|
|
|
echo "can't get auth credentials for the current master"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
until curl --insecure "${curl_auth_arg[@]}" --max-time 5 \
|
2015-05-13 18:01:35 +00:00
|
|
|
--fail --output /dev/null --silent "https://${KUBE_MASTER_IP}/healthz"; do
|
2015-04-03 21:48:39 +00:00
|
|
|
printf "."
|
|
|
|
sleep 2
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "== Done =="
|
|
|
|
}
|
|
|
|
|
2015-04-06 23:01:16 +00:00
|
|
|
# Perform common upgrade setup tasks
|
|
|
|
#
|
|
|
|
# Assumed vars
|
2015-06-01 15:59:12 +00:00
|
|
|
# KUBE_VERSION
|
2015-04-06 23:01:16 +00:00
|
|
|
function prepare-upgrade() {
|
2015-04-03 21:48:39 +00:00
|
|
|
ensure-temp-dir
|
|
|
|
detect-project
|
2015-06-01 15:59:12 +00:00
|
|
|
tars_from_version
|
2015-04-06 23:01:16 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 07:13:26 +00:00
|
|
|
|
|
|
|
# Reads kube-env metadata from first node in MINION_NAMES.
|
2015-05-08 00:41:22 +00:00
|
|
|
#
|
|
|
|
# Assumed vars:
|
2015-06-17 07:13:26 +00:00
|
|
|
# MINION_NAMES
|
2015-06-15 23:57:32 +00:00
|
|
|
# PROJECT
|
2015-05-08 00:41:22 +00:00
|
|
|
# ZONE
|
2015-06-17 07:13:26 +00:00
|
|
|
function get-node-env() {
|
|
|
|
# TODO(mbforbes): Make this more reliable with retries.
|
|
|
|
gcloud compute --project ${PROJECT} ssh --zone ${ZONE} ${MINION_NAMES[0]} --command \
|
|
|
|
"curl --fail --silent -H 'Metadata-Flavor: Google' \
|
|
|
|
'http://metadata/computeMetadata/v1/instance/attributes/kube-env'" 2>/dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
# Using provided node env, extracts value from provided key.
|
2015-05-08 00:41:22 +00:00
|
|
|
#
|
|
|
|
# Args:
|
2015-06-17 07:13:26 +00:00
|
|
|
# $1 node env (kube-env of node; result of calling get-node-env)
|
|
|
|
# $2 env key to use
|
2015-05-08 00:41:22 +00:00
|
|
|
function get-env-val() {
|
2015-06-17 07:13:26 +00:00
|
|
|
echo "${1}" | grep ${2} | cut -d : -f 2 | cut -d \' -f 2
|
2015-05-08 00:41:22 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 15:59:12 +00:00
|
|
|
# Assumed vars:
|
|
|
|
# KUBE_VERSION
|
|
|
|
# MINION_SCOPES
|
|
|
|
# NODE_INSTANCE_PREFIX
|
|
|
|
# PROJECT
|
|
|
|
# ZONE
|
2015-06-17 07:13:26 +00:00
|
|
|
#
|
|
|
|
# Vars set:
|
|
|
|
# KUBELET_TOKEN
|
|
|
|
# KUBE_PROXY_TOKEN
|
|
|
|
# CA_CERT_BASE64
|
|
|
|
# EXTRA_DOCKER_OPTS
|
|
|
|
# KUBELET_CERT_BASE64
|
|
|
|
# KUBELET_KEY_BASE64
|
2015-04-06 23:01:16 +00:00
|
|
|
function upgrade-nodes() {
|
2015-06-17 07:13:26 +00:00
|
|
|
prepare-node-upgrade
|
|
|
|
do-node-upgrade
|
|
|
|
}
|
|
|
|
|
|
|
|
# prepare-node-upgrade creates a new instance template suitable for upgrading
|
|
|
|
# to KUBE_VERSION and echos a single line with the name of the new template.
|
|
|
|
#
|
|
|
|
# Assumed vars:
|
|
|
|
# KUBE_VERSION
|
|
|
|
# MINION_SCOPES
|
|
|
|
# NODE_INSTANCE_PREFIX
|
|
|
|
# PROJECT
|
|
|
|
# ZONE
|
|
|
|
#
|
|
|
|
# Vars set:
|
|
|
|
# SANITIZED_VERSION
|
|
|
|
# KUBELET_TOKEN
|
|
|
|
# KUBE_PROXY_TOKEN
|
|
|
|
# CA_CERT_BASE64
|
|
|
|
# EXTRA_DOCKER_OPTS
|
|
|
|
# KUBELET_CERT_BASE64
|
|
|
|
# KUBELET_KEY_BASE64
|
|
|
|
function prepare-node-upgrade() {
|
|
|
|
echo "== Preparing node upgrade (to ${KUBE_VERSION}). ==" >&2
|
|
|
|
SANITIZED_VERSION=$(echo ${KUBE_VERSION} | sed s/"\."/-/g)
|
2015-04-06 23:01:16 +00:00
|
|
|
|
2015-04-03 21:48:39 +00:00
|
|
|
detect-minion-names
|
2015-05-08 00:41:22 +00:00
|
|
|
|
|
|
|
# TODO(mbforbes): Refactor setting scope flags.
|
2015-06-29 23:47:36 +00:00
|
|
|
local scope_flags=
|
|
|
|
if [ -n "${MINION_SCOPES}" ]; then
|
|
|
|
scope_flags="--scopes ${MINION_SCOPES}"
|
2015-05-08 00:41:22 +00:00
|
|
|
else
|
2015-06-29 23:47:36 +00:00
|
|
|
scope_flags="--no-scopes"
|
2015-05-08 00:41:22 +00:00
|
|
|
fi
|
|
|
|
|
2015-06-17 07:13:26 +00:00
|
|
|
# Get required node env vars from exiting template.
|
|
|
|
local node_env=$(get-node-env)
|
|
|
|
KUBELET_TOKEN=$(get-env-val "${node_env}" "KUBELET_TOKEN")
|
|
|
|
KUBE_PROXY_TOKEN=$(get-env-val "${node_env}" "KUBE_PROXY_TOKEN")
|
|
|
|
CA_CERT_BASE64=$(get-env-val "${node_env}" "CA_CERT")
|
|
|
|
EXTRA_DOCKER_OPTS=$(get-env-val "${node_env}" "EXTRA_DOCKER_OPTS")
|
|
|
|
KUBELET_CERT_BASE64=$(get-env-val "${node_env}" "KUBELET_CERT")
|
|
|
|
KUBELET_KEY_BASE64=$(get-env-val "${node_env}" "KUBELET_KEY")
|
2015-05-08 00:41:22 +00:00
|
|
|
|
|
|
|
# TODO(mbforbes): How do we ensure kube-env is written in a ${version}-
|
|
|
|
# compatible way?
|
|
|
|
write-node-env
|
2015-06-17 07:13:26 +00:00
|
|
|
|
2015-05-08 00:41:22 +00:00
|
|
|
# TODO(mbforbes): Get configure-vm script from ${version}. (Must plumb this
|
|
|
|
# through all create-node-instance-template implementations).
|
2015-06-17 07:13:26 +00:00
|
|
|
create-node-instance-template ${SANITIZED_VERSION}
|
|
|
|
# The following is echo'd so that callers can get the template name.
|
|
|
|
echo "${NODE_INSTANCE_PREFIX}-template-${SANITIZED_VERSION}"
|
|
|
|
echo "== Finished preparing node upgrade (to ${KUBE_VERSION}). ==" >&2
|
|
|
|
}
|
2015-05-08 00:41:22 +00:00
|
|
|
|
2015-06-17 07:13:26 +00:00
|
|
|
# Prereqs:
|
|
|
|
# - prepare-node-upgrade should have been called successfully
|
|
|
|
function do-node-upgrade() {
|
|
|
|
echo "== Upgrading nodes to ${KUBE_VERSION}. ==" >&2
|
2015-05-08 00:41:22 +00:00
|
|
|
# Do the actual upgrade.
|
2015-06-17 07:13:26 +00:00
|
|
|
# NOTE(mbforbes): If you are changing this gcloud command, update
|
2015-07-23 00:07:15 +00:00
|
|
|
# test/e2e/cluster_upgrade.go to match this EXACTLY.
|
|
|
|
# TODO(mbforbes): Remove this hack on July 29, 2015, when the migration to
|
|
|
|
# `gcloud alpha compute rolling-updates` is complete.
|
|
|
|
local subgroup="preview"
|
|
|
|
local exists=$(gcloud ${subgroup} rolling-updates -h &>/dev/null; echo $?) || true
|
|
|
|
if [[ "${exists}" != "0" ]]; then
|
|
|
|
subgroup="alpha compute"
|
|
|
|
fi
|
|
|
|
gcloud ${subgroup} rolling-updates \
|
2015-06-17 07:13:26 +00:00
|
|
|
--project="${PROJECT}" \
|
|
|
|
--zone="${ZONE}" \
|
|
|
|
start \
|
|
|
|
--group="${NODE_INSTANCE_PREFIX}-group" \
|
|
|
|
--template="${NODE_INSTANCE_PREFIX}-template-${SANITIZED_VERSION}" \
|
|
|
|
--instance-startup-timeout=300s \
|
|
|
|
--max-num-concurrent-instances=1 \
|
|
|
|
--max-num-failed-instances=0 \
|
|
|
|
--min-instance-update-time=0s
|
|
|
|
|
|
|
|
# TODO(mbforbes): Wait for the rolling-update to finish.
|
|
|
|
|
|
|
|
echo "== Finished upgrading nodes to ${KUBE_VERSION}. ==" >&2
|
2015-04-03 21:48:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
master_upgrade=true
|
|
|
|
node_upgrade=true
|
2015-06-17 07:13:26 +00:00
|
|
|
node_prereqs=false
|
2015-04-06 23:01:16 +00:00
|
|
|
local_binaries=false
|
2015-04-03 21:48:39 +00:00
|
|
|
|
2015-06-17 07:13:26 +00:00
|
|
|
while getopts ":MNPlh" opt; do
|
2015-04-03 21:48:39 +00:00
|
|
|
case ${opt} in
|
|
|
|
M)
|
|
|
|
node_upgrade=false
|
|
|
|
;;
|
|
|
|
N)
|
|
|
|
master_upgrade=false
|
|
|
|
;;
|
2015-06-17 07:13:26 +00:00
|
|
|
P)
|
|
|
|
node_prereqs=true
|
|
|
|
;;
|
2015-04-06 23:01:16 +00:00
|
|
|
l)
|
|
|
|
local_binaries=true
|
|
|
|
;;
|
2015-04-03 21:48:39 +00:00
|
|
|
h)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
\?)
|
|
|
|
echo "Invalid option: -$OPTARG" >&2
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $((OPTIND-1))
|
|
|
|
|
2015-04-06 23:01:16 +00:00
|
|
|
if [[ $# -lt 1 ]] && [[ "${local_binaries}" == "false" ]]; then
|
2015-04-03 21:48:39 +00:00
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${master_upgrade}" == "false" ]] && [[ "${node_upgrade}" == "false" ]]; then
|
|
|
|
echo "Can't specify both -M and -N" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2015-04-06 23:01:16 +00:00
|
|
|
if [[ "${local_binaries}" == "false" ]]; then
|
2015-05-22 22:57:35 +00:00
|
|
|
set_binary_version ${1}
|
2015-04-06 23:01:16 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
prepare-upgrade
|
2015-04-03 21:48:39 +00:00
|
|
|
|
2015-06-17 07:13:26 +00:00
|
|
|
if [[ "${node_prereqs}" == "true" ]]; then
|
|
|
|
prepare-node-upgrade
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2015-04-03 21:48:39 +00:00
|
|
|
if [[ "${master_upgrade}" == "true" ]]; then
|
|
|
|
upgrade-master
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${node_upgrade}" == "true" ]]; then
|
2015-05-08 00:41:22 +00:00
|
|
|
if [[ "${local_binaries}" == "true" ]]; then
|
|
|
|
echo "Upgrading nodes to local binaries is not yet supported." >&2
|
2015-06-17 07:13:26 +00:00
|
|
|
exit 1
|
2015-05-08 00:41:22 +00:00
|
|
|
else
|
2015-06-01 15:59:12 +00:00
|
|
|
upgrade-nodes
|
2015-05-08 00:41:22 +00:00
|
|
|
fi
|
2015-04-03 21:48:39 +00:00
|
|
|
fi
|
|
|
|
|
2015-05-08 00:41:22 +00:00
|
|
|
echo "== Validating cluster post-upgrade =="
|
2015-04-03 21:48:39 +00:00
|
|
|
"${KUBE_ROOT}/cluster/validate-cluster.sh"
|