mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-14 18:53:35 +00:00
vendor update for CSI 0.3.0
This commit is contained in:
274
vendor/k8s.io/kubernetes/cluster/images/etcd/migrate-if-needed.sh
generated
vendored
274
vendor/k8s.io/kubernetes/cluster/images/etcd/migrate-if-needed.sh
generated
vendored
@ -18,7 +18,7 @@
|
||||
# This script performs etcd upgrade based on the following environmental
|
||||
# variables:
|
||||
# TARGET_STORAGE - API of etcd to be used (supported: 'etcd2', 'etcd3')
|
||||
# TARGET_VERSION - etcd release to be used (supported: '2.2.1', '2.3.7', '3.0.17', '3.1.11', '3.2.14')
|
||||
# TARGET_VERSION - etcd release to be used (supported: '2.2.1', '2.3.7', '3.0.17', '3.1.12', '3.2.18')
|
||||
# DATA_DIRECTORY - directory with etcd data
|
||||
#
|
||||
# The current etcd version and storage format is detected based on the
|
||||
@ -28,8 +28,8 @@
|
||||
# The update workflow support the following upgrade steps:
|
||||
# - 2.2.1/etcd2 -> 2.3.7/etcd2
|
||||
# - 2.3.7/etcd2 -> 3.0.17/etcd2
|
||||
# - 3.0.17/etcd3 -> 3.1.11/etcd3
|
||||
# - 3.1.11/etcd3 -> 3.2.14/etcd3
|
||||
# - 3.0.17/etcd3 -> 3.1.12/etcd3
|
||||
# - 3.1.12/etcd3 -> 3.2.18/etcd3
|
||||
#
|
||||
# NOTE: The releases supported in this script has to match release binaries
|
||||
# present in the etcd image (to make this script work correctly).
|
||||
@ -40,95 +40,49 @@
|
||||
set -o errexit
|
||||
set -o nounset
|
||||
|
||||
source $(dirname "$0")/start-stop-etcd.sh
|
||||
# NOTE: BUNDLED_VERSION has to match release binaries present in the
|
||||
# etcd image (to make this script work correctly).
|
||||
BUNDLED_VERSIONS="2.2.1, 2.3.7, 3.0.17, 3.1.12, 3.2.18"
|
||||
|
||||
# Rollback to previous minor version of etcd 3.x, if needed.
|
||||
#
|
||||
# Warning: For HA etcd clusters (any cluster with more than one member), all members must be stopped before rolling back, zero
|
||||
# downtime rollbacks are not supported.
|
||||
rollback_etcd3_minor_version() {
|
||||
if [ ${TARGET_MINOR_VERSION} != $((${CURRENT_MINOR_VERSION}-1)) ]; then
|
||||
echo "Rollback from ${CURRENT_VERSION} to ${TARGET_VERSION} not supported, only rollbacks to the previous minor version are supported."
|
||||
exit 1
|
||||
fi
|
||||
echo "Performing etcd ${CURRENT_VERSION} -> ${TARGET_VERSION} rollback"
|
||||
ROLLBACK_BACKUP_DIR="${DATA_DIRECTORY}.bak"
|
||||
rm -rf "${ROLLBACK_BACKUP_DIR}"
|
||||
SNAPSHOT_FILE="${DATA_DIRECTORY}.snapshot.db"
|
||||
rm -rf "${SNAPSHOT_FILE}"
|
||||
ETCD_CMD="/usr/local/bin/etcd-${CURRENT_VERSION}"
|
||||
ETCDCTL_CMD="/usr/local/bin/etcdctl-${CURRENT_VERSION}"
|
||||
|
||||
# Start CURRENT_VERSION of etcd.
|
||||
START_VERSION="${CURRENT_VERSION}"
|
||||
START_STORAGE="${CURRENT_STORAGE}"
|
||||
echo "Starting etcd version ${START_VERSION} to capture rollback snapshot."
|
||||
if ! start_etcd; then
|
||||
echo "Unable to automatically downgrade etcd: starting etcd version ${START_VERSION} to capture rollback snapshot failed."
|
||||
echo "See https://coreos.com/etcd/docs/3.2.13/op-guide/recovery.html for manual downgrade options."
|
||||
exit 1
|
||||
else
|
||||
ETCDCTL_API=3 ${ETCDCTL_CMD} snapshot --endpoints "http://127.0.0.1:${ETCD_PORT}" save "${SNAPSHOT_FILE}"
|
||||
fi
|
||||
stop_etcd
|
||||
|
||||
# Backup the data before rolling back.
|
||||
mv "${DATA_DIRECTORY}" "${ROLLBACK_BACKUP_DIR}"
|
||||
ETCDCTL_CMD="/usr/local/bin/etcdctl-${TARGET_VERSION}"
|
||||
NAME="etcd-$(hostname)"
|
||||
ETCDCTL_API=3 ${ETCDCTL_CMD} snapshot restore "${SNAPSHOT_FILE}" \
|
||||
--data-dir "${DATA_DIRECTORY}" --name "${NAME}" --initial-cluster "${INITIAL_CLUSTER}"
|
||||
|
||||
CURRENT_VERSION="${TARGET_VERSION}"
|
||||
echo "${CURRENT_VERSION}/${CURRENT_STORAGE}" > "${DATA_DIRECTORY}/${VERSION_FILE}"
|
||||
}
|
||||
|
||||
# Rollback from "3.0.x" version in 'etcd3' mode to "2.2.1" version in 'etcd2' mode, if needed.
|
||||
rollback_to_etcd2() {
|
||||
if [ "$(echo ${CURRENT_VERSION} | cut -c1-4)" != "3.0." -o "${TARGET_VERSION}" != "2.2.1" ]; then
|
||||
echo "etcd3 -> etcd2 downgrade is supported only between 3.0.x and 2.2.1"
|
||||
return 0
|
||||
fi
|
||||
echo "Backup and remove all existing v2 data"
|
||||
ROLLBACK_BACKUP_DIR="${DATA_DIRECTORY}.bak"
|
||||
rm -rf "${ROLLBACK_BACKUP_DIR}"
|
||||
mkdir -p "${ROLLBACK_BACKUP_DIR}"
|
||||
cp -r "${DATA_DIRECTORY}" "${ROLLBACK_BACKUP_DIR}"
|
||||
echo "Performing etcd3 -> etcd2 rollback"
|
||||
${ROLLBACK} --data-dir "${DATA_DIRECTORY}"
|
||||
if [ "$?" -ne "0" ]; then
|
||||
echo "Rollback to etcd2 failed"
|
||||
exit 1
|
||||
fi
|
||||
CURRENT_STORAGE="etcd2"
|
||||
CURRENT_VERSION="2.2.1"
|
||||
echo "${CURRENT_VERSION}/${CURRENT_STORAGE}" > "${DATA_DIRECTORY}/${VERSION_FILE}"
|
||||
}
|
||||
|
||||
|
||||
if [ -z "${TARGET_STORAGE:-}" ]; then
|
||||
echo "TARGET_STORAGE variable unset - unexpected failure"
|
||||
ETCD_NAME="${ETCD_NAME:-etcd-$(hostname)}"
|
||||
if [ -z "${DATA_DIRECTORY:-}" ]; then
|
||||
echo "DATA_DIRECTORY variable unset - unexpected failure"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
case "${DATA_DIRECTORY}" in
|
||||
*event*)
|
||||
ETCD_PEER_PORT=2381
|
||||
ETCD_CLIENT_PORT=18631
|
||||
;;
|
||||
*)
|
||||
ETCD_PEER_PORT=2380
|
||||
ETCD_CLIENT_PORT=18629
|
||||
;;
|
||||
esac
|
||||
|
||||
if [ -z "${INITIAL_CLUSTER:-}" ]; then
|
||||
echo "Warn: INITIAL_CLUSTER variable unset - defaulting to ${ETCD_NAME}=http://localhost:${ETCD_PEER_PORT}"
|
||||
INITIAL_CLUSTER="${ETCD_NAME}=http://localhost:${ETCD_PEER_PORT}"
|
||||
fi
|
||||
if [ -z "${LISTEN_PEER_URLS:-}" ]; then
|
||||
echo "Warn: LISTEN_PEER_URLS variable unset - defaulting to http://localhost:${ETCD_PEER_PORT}"
|
||||
LISTEN_PEER_URLS="http://localhost:${ETCD_PEER_PORT}"
|
||||
fi
|
||||
if [ -z "${INITIAL_ADVERTISE_PEER_URLS:-}" ]; then
|
||||
echo "Warn: INITIAL_ADVERTISE_PEER_URLS variable unset - defaulting to http://localhost:${ETCD_PEER_PORT}"
|
||||
INITIAL_ADVERTISE_PEER_URLS="http://localhost:${ETCD_PEER_PORT}"
|
||||
fi
|
||||
if [ -z "${TARGET_VERSION:-}" ]; then
|
||||
echo "TARGET_VERSION variable unset - unexpected failure"
|
||||
exit 1
|
||||
fi
|
||||
if [ -z "${DATA_DIRECTORY:-}" ]; then
|
||||
echo "DATA_DIRECTORY variable unset - unexpected failure"
|
||||
exit 1
|
||||
fi
|
||||
if [ -z "${INITIAL_CLUSTER:-}" ]; then
|
||||
echo "Warn: INITIAL_CLUSTER variable unset - defaulting to etcd-$(hostname)=http://localhost:2380"
|
||||
INITIAL_CLUSTER="etcd-$(hostname)=http://localhost:2380"
|
||||
fi
|
||||
|
||||
echo "$(date +'%Y-%m-%d %H:%M:%S') Detecting if migration is needed"
|
||||
|
||||
if [ "${TARGET_STORAGE}" != "etcd2" -a "${TARGET_STORAGE}" != "etcd3" ]; then
|
||||
echo "Not supported version of storage: ${TARGET_STORAGE}"
|
||||
if [ -z "${TARGET_STORAGE:-}" ]; then
|
||||
echo "TARGET_STORAGE variable unset - unexpected failure"
|
||||
exit 1
|
||||
fi
|
||||
ETCD_DATA_PREFIX="${ETCD_DATA_PREFIX:-/registry}"
|
||||
ETCD_CREDS="${ETCD_CREDS:-}"
|
||||
|
||||
# Correctly support upgrade and rollback to non-default version.
|
||||
if [ "${DO_NOT_MOVE_BINARIES:-}" != "true" ]; then
|
||||
@ -136,144 +90,16 @@ if [ "${DO_NOT_MOVE_BINARIES:-}" != "true" ]; then
|
||||
cp "/usr/local/bin/etcdctl-${TARGET_VERSION}" "/usr/local/bin/etcdctl"
|
||||
fi
|
||||
|
||||
# NOTE: SUPPORTED_VERSION has to match release binaries present in the
|
||||
# etcd image (to make this script work correctly).
|
||||
# We cannot use array since sh doesn't support it.
|
||||
SUPPORTED_VERSIONS_STRING="2.2.1 2.3.7 3.0.17 3.1.11 3.2.14"
|
||||
SUPPORTED_VERSIONS=$(echo "${SUPPORTED_VERSIONS_STRING}" | tr " " "\n")
|
||||
|
||||
VERSION_FILE="version.txt"
|
||||
CURRENT_STORAGE="etcd2"
|
||||
CURRENT_VERSION="2.2.1"
|
||||
if [ -e "${DATA_DIRECTORY}/${VERSION_FILE}" ]; then
|
||||
VERSION_CONTENTS="$(cat ${DATA_DIRECTORY}/${VERSION_FILE})"
|
||||
# Example usage: if contents of VERSION_FILE is 2.3.7/etcd2, then
|
||||
# - CURRENT_VERSION would be '2.3.7'
|
||||
# - CURRENT_STORAGE would be 'etcd2'
|
||||
CURRENT_VERSION="$(echo $VERSION_CONTENTS | cut -d '/' -f 1)"
|
||||
CURRENT_STORAGE="$(echo $VERSION_CONTENTS | cut -d '/' -f 2)"
|
||||
fi
|
||||
ETCD_DATA_PREFIX="${ETCD_DATA_PREFIX:-/registry}"
|
||||
|
||||
# If there is no data in DATA_DIRECTORY, this means that we are
|
||||
# starting etcd from scratch. In that case, we don't need to do
|
||||
# any migration.
|
||||
if [ ! -d "${DATA_DIRECTORY}" ]; then
|
||||
mkdir -p "${DATA_DIRECTORY}"
|
||||
fi
|
||||
if [ -z "$(ls -A ${DATA_DIRECTORY})" ]; then
|
||||
echo "${DATA_DIRECTORY} is empty - skipping migration"
|
||||
echo "${TARGET_VERSION}/${TARGET_STORAGE}" > "${DATA_DIRECTORY}/${VERSION_FILE}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
ATTACHLEASE="${ATTACHLEASE:-/usr/local/bin/attachlease}"
|
||||
ROLLBACK="${ROLLBACK:-/usr/local/bin/rollback}"
|
||||
|
||||
# If we are upgrading from 2.2.1 and this is the first try for upgrade,
|
||||
# do the backup to allow restoring from it in case of failed upgrade.
|
||||
BACKUP_DIR="${DATA_DIRECTORY}/migration-backup"
|
||||
if [ "${CURRENT_VERSION}" = "2.2.1" -a "${CURRENT_VERSION}" != "${TARGET_VERSION}" -a ! -d "${BACKUP_DIR}" ]; then
|
||||
echo "Backup etcd before starting migration"
|
||||
mkdir ${BACKUP_DIR}
|
||||
ETCDCTL_CMD="/usr/local/bin/etcdctl-2.2.1"
|
||||
ETCDCTL_API=2 ${ETCDCTL_CMD} --debug backup --data-dir=${DATA_DIRECTORY} \
|
||||
--backup-dir=${BACKUP_DIR}
|
||||
echo "Backup done in ${BACKUP_DIR}"
|
||||
fi
|
||||
|
||||
CURRENT_MINOR_VERSION="$(echo ${CURRENT_VERSION} | awk -F'.' '{print $2}')"
|
||||
TARGET_MINOR_VERSION="$(echo ${TARGET_VERSION} | awk -F'.' '{print $2}')"
|
||||
|
||||
# "rollback-if-needed"
|
||||
case "${CURRENT_STORAGE}-${TARGET_STORAGE}" in
|
||||
"etcd3-etcd3")
|
||||
[ ${TARGET_MINOR_VERSION} -lt ${CURRENT_MINOR_VERSION} ] && rollback_etcd3_minor_version
|
||||
break
|
||||
;;
|
||||
"etcd3-etcd2")
|
||||
rollback_to_etcd2
|
||||
break
|
||||
;;
|
||||
*)
|
||||
break
|
||||
;;
|
||||
esac
|
||||
|
||||
# Do the roll-forward migration if needed.
|
||||
# The migration goes as following:
|
||||
# 1. for all versions starting one after the current version of etcd
|
||||
# we do "start, wait until healthy and stop etcd". This is the
|
||||
# procedure that etcd documentation suggests for upgrading binaries.
|
||||
# 2. For the first 3.0.x version that we encounter, if we are still in
|
||||
# v2 API, we do upgrade to v3 API using the "etcdct migrate" and
|
||||
# attachlease commands.
|
||||
SKIP_STEP=true
|
||||
for step in ${SUPPORTED_VERSIONS}; do
|
||||
if [ "${step}" = "${CURRENT_VERSION}" ]; then
|
||||
SKIP_STEP=false
|
||||
elif [ "${SKIP_STEP}" != "true" ]; then
|
||||
# Do the migration step, by just starting etcd in this version.
|
||||
START_VERSION="${step}"
|
||||
START_STORAGE="${CURRENT_STORAGE}"
|
||||
if ! start_etcd; then
|
||||
# Migration failed.
|
||||
echo "Starting etcd ${step} failed"
|
||||
exit 1
|
||||
fi
|
||||
# Kill etcd and wait until this is down.
|
||||
stop_etcd
|
||||
CURRENT_VERSION=${step}
|
||||
echo "${CURRENT_VERSION}/${CURRENT_STORAGE}" > "${DATA_DIRECTORY}/${VERSION_FILE}"
|
||||
fi
|
||||
if [ "$(echo ${CURRENT_VERSION} | cut -c1-2)" = "3." -a "${CURRENT_VERSION}" = "${step}" -a "${CURRENT_STORAGE}" = "etcd2" -a "${TARGET_STORAGE}" = "etcd3" ]; then
|
||||
# If it is the first 3.x release in the list and we are migrating
|
||||
# also from 'etcd2' to 'etcd3', do the migration now.
|
||||
echo "Performing etcd2 -> etcd3 migration"
|
||||
START_VERSION="${step}"
|
||||
START_STORAGE="etcd3"
|
||||
ETCDCTL_CMD="${ETCDCTL:-/usr/local/bin/etcdctl-${START_VERSION}}"
|
||||
ETCDCTL_API=3 ${ETCDCTL_CMD} migrate --data-dir=${DATA_DIRECTORY}
|
||||
echo "Attaching leases to TTL entries"
|
||||
# Now attach lease to all keys.
|
||||
# To do it, we temporarily start etcd on a random port (so that
|
||||
# apiserver actually cannot access it).
|
||||
if ! start_etcd; then
|
||||
echo "Starting etcd ${step} in v3 mode failed"
|
||||
exit 1
|
||||
fi
|
||||
# Create a lease and attach all keys to it.
|
||||
${ATTACHLEASE} \
|
||||
--etcd-address http://127.0.0.1:${ETCD_PORT} \
|
||||
--ttl-keys-prefix "${TTL_KEYS_DIRECTORY:-${ETCD_DATA_PREFIX}/events}" \
|
||||
--lease-duration 1h
|
||||
# Kill etcd and wait until this is down.
|
||||
stop_etcd
|
||||
CURRENT_STORAGE="etcd3"
|
||||
echo "${CURRENT_VERSION}/${CURRENT_STORAGE}" > "${DATA_DIRECTORY}/${VERSION_FILE}"
|
||||
fi
|
||||
if [ "$(echo ${CURRENT_VERSION} | cut -c1-4)" = "3.1." -a "${CURRENT_VERSION}" = "${step}" -a "${CURRENT_STORAGE}" = "etcd3" ]; then
|
||||
# If we are upgrading to 3.1.* release, if the cluster was migrated
|
||||
# from v2 version, the v2 data may still be around. So now is the
|
||||
# time to actually remove them.
|
||||
echo "Remove stale v2 data"
|
||||
START_VERSION="${step}"
|
||||
START_STORAGE="etcd3"
|
||||
ETCDCTL_CMD="${ETCDCTL:-/usr/local/bin/etcdctl-${START_VERSION}}"
|
||||
if ! start_etcd; then
|
||||
echo "Starting etcd ${step} in v3 mode failed"
|
||||
exit 1
|
||||
fi
|
||||
${ETCDCTL_CMD} --endpoints "http://127.0.0.1:${ETCD_PORT}" rm --recursive "${ETCD_DATA_PREFIX}"
|
||||
# Kill etcd and wait until this is down.
|
||||
stop_etcd
|
||||
echo "Successfully remove v2 data"
|
||||
# Also remove backup from v2->v3 migration.
|
||||
rm -rf "${BACKUP_DIR}"
|
||||
fi
|
||||
if [ "${CURRENT_VERSION}" = "${TARGET_VERSION}" -a "${CURRENT_STORAGE}" = "${TARGET_STORAGE}" ]; then
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
echo "$(date +'%Y-%m-%d %H:%M:%S') Migration finished"
|
||||
/usr/local/bin/migrate \
|
||||
--name "${ETCD_NAME}" \
|
||||
--port "${ETCD_CLIENT_PORT}" \
|
||||
--listen-peer-urls "${LISTEN_PEER_URLS}" \
|
||||
--initial-advertise-peer-urls "${INITIAL_ADVERTISE_PEER_URLS}" \
|
||||
--data-dir "${DATA_DIRECTORY}" \
|
||||
--bundled-versions "${BUNDLED_VERSIONS}" \
|
||||
--initial-cluster "${INITIAL_CLUSTER}" \
|
||||
--target-version "${TARGET_VERSION}" \
|
||||
--target-storage "${TARGET_STORAGE}" \
|
||||
--etcd-data-prefix "${ETCD_DATA_PREFIX}" \
|
||||
--ttl-keys-directory "${TTL_KEYS_DIRECTORY:-${ETCD_DATA_PREFIX}/events}" \
|
||||
--etcd-server-extra-args "${ETCD_CREDS}"
|
||||
|
Reference in New Issue
Block a user