2018-04-16 12:31:44 -04:00
|
|
|
#!/usr/bin/env bash
|
2014-10-22 19:26:59 -04:00
|
|
|
|
2016-06-02 20:25:58 -04:00
|
|
|
# Copyright 2014 The Kubernetes Authors.
|
2014-10-22 19:26:59 -04: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.
|
|
|
|
|
|
|
|
|
|
# A set of helpers for starting/running etcd for tests
|
|
|
|
|
|
2024-11-12 21:19:08 -05:00
|
|
|
ETCD_VERSION=${ETCD_VERSION:-3.5.17}
|
2016-05-27 14:13:24 -04:00
|
|
|
ETCD_HOST=${ETCD_HOST:-127.0.0.1}
|
2016-08-10 16:39:36 -04:00
|
|
|
ETCD_PORT=${ETCD_PORT:-2379}
|
2022-01-20 06:06:22 -05:00
|
|
|
# This is intentionally not called ETCD_LOG_LEVEL:
|
|
|
|
|
# etcd checks that and compains when it is set in addition
|
|
|
|
|
# to the command line argument, even when both have the same value.
|
2022-03-27 15:21:28 -04:00
|
|
|
ETCD_LOGLEVEL=${ETCD_LOGLEVEL:-warn}
|
2018-05-09 16:51:40 -04:00
|
|
|
export KUBE_INTEGRATION_ETCD_URL="http://${ETCD_HOST}:${ETCD_PORT}"
|
2016-02-25 22:26:02 -05:00
|
|
|
|
2016-12-13 12:29:47 -05:00
|
|
|
kube::etcd::validate() {
|
|
|
|
|
# validate if in path
|
2018-02-14 13:33:04 -05:00
|
|
|
command -v etcd >/dev/null || {
|
2014-10-22 19:26:59 -04:00
|
|
|
kube::log::usage "etcd must be in your PATH"
|
2018-08-14 15:48:07 -04:00
|
|
|
kube::log::info "You can use 'hack/install-etcd.sh' to install a copy in third_party/."
|
2014-10-22 19:26:59 -04:00
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-09 08:59:56 -05:00
|
|
|
# validate etcd port is free
|
2018-02-14 13:33:04 -05:00
|
|
|
local port_check_command
|
|
|
|
|
if command -v ss &> /dev/null && ss -Version | grep 'iproute2' &> /dev/null; then
|
|
|
|
|
port_check_command="ss"
|
|
|
|
|
elif command -v netstat &>/dev/null; then
|
|
|
|
|
port_check_command="netstat"
|
|
|
|
|
else
|
|
|
|
|
kube::log::usage "unable to identify if etcd is bound to port ${ETCD_PORT}. unable to find ss or netstat utilities."
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
if ${port_check_command} -nat | grep "LISTEN" | grep "[\.:]${ETCD_PORT:?}" >/dev/null 2>&1; then
|
2018-02-09 08:59:56 -05:00
|
|
|
kube::log::usage "unable to start etcd as port ${ETCD_PORT} is in use. please stop the process listening on this port and retry."
|
2023-05-05 11:09:49 -04:00
|
|
|
kube::log::usage "$(${port_check_command} -nat | grep "LISTEN" | grep "[\.:]${ETCD_PORT:?}")"
|
2014-10-22 19:26:59 -04:00
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2020-02-21 03:17:11 -05:00
|
|
|
# need set the env of "ETCD_UNSUPPORTED_ARCH" on unstable arch.
|
|
|
|
|
arch=$(uname -m)
|
2023-01-22 19:27:25 -05:00
|
|
|
if [[ $arch =~ arm* ]]; then
|
2020-02-21 03:17:11 -05:00
|
|
|
export ETCD_UNSUPPORTED_ARCH=arm
|
|
|
|
|
fi
|
2016-12-13 12:29:47 -05:00
|
|
|
# validate installed version is at least equal to minimum
|
2020-02-21 03:17:11 -05:00
|
|
|
version=$(etcd --version | grep Version | head -n 1 | cut -d " " -f 3)
|
2019-01-31 09:51:20 -05:00
|
|
|
if [[ $(kube::etcd::version "${ETCD_VERSION}") -gt $(kube::etcd::version "${version}") ]]; then
|
2019-01-21 17:11:58 -05:00
|
|
|
export PATH=${KUBE_ROOT}/third_party/etcd:${PATH}
|
2016-11-06 21:08:34 -05:00
|
|
|
hash etcd
|
2019-01-21 17:11:58 -05:00
|
|
|
echo "${PATH}"
|
2020-02-21 03:17:11 -05:00
|
|
|
version=$(etcd --version | grep Version | head -n 1 | cut -d " " -f 3)
|
2019-01-31 09:51:20 -05:00
|
|
|
if [[ $(kube::etcd::version "${ETCD_VERSION}") -gt $(kube::etcd::version "${version}") ]]; then
|
2016-11-06 21:08:34 -05:00
|
|
|
kube::log::usage "etcd version ${ETCD_VERSION} or greater required."
|
|
|
|
|
kube::log::info "You can use 'hack/install-etcd.sh' to install a copy in third_party/."
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2015-01-29 18:48:04 -05:00
|
|
|
fi
|
2016-12-13 12:29:47 -05:00
|
|
|
}
|
|
|
|
|
|
2017-04-12 12:12:05 -04:00
|
|
|
kube::etcd::version() {
|
|
|
|
|
printf '%s\n' "${@}" | awk -F . '{ printf("%d%03d%03d\n", $1, $2, $3) }'
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-13 12:29:47 -05:00
|
|
|
kube::etcd::start() {
|
|
|
|
|
# validate before running
|
|
|
|
|
kube::etcd::validate
|
2015-01-29 18:48:04 -05:00
|
|
|
|
2014-10-22 19:26:59 -04:00
|
|
|
# Start etcd
|
2016-08-16 07:59:08 -04:00
|
|
|
ETCD_DIR=${ETCD_DIR:-$(mktemp -d 2>/dev/null || mktemp -d -t test-etcd.XXXXXX)}
|
2018-12-19 18:17:13 -05:00
|
|
|
if [[ -d "${ARTIFACTS:-}" ]]; then
|
|
|
|
|
ETCD_LOGFILE="${ARTIFACTS}/etcd.$(uname -n).$(id -un).log.DEBUG.$(date +%Y%m%d-%H%M%S).$$"
|
2016-06-06 20:22:45 -04:00
|
|
|
else
|
2018-07-27 14:50:33 -04:00
|
|
|
ETCD_LOGFILE=${ETCD_LOGFILE:-"/dev/null"}
|
2016-06-06 20:22:45 -04:00
|
|
|
fi
|
2022-01-20 07:38:07 -05:00
|
|
|
kube::log::info "etcd --advertise-client-urls ${KUBE_INTEGRATION_ETCD_URL} --data-dir ${ETCD_DIR} --listen-client-urls http://${ETCD_HOST}:${ETCD_PORT} --log-level=${ETCD_LOGLEVEL} 2> \"${ETCD_LOGFILE}\" >/dev/null"
|
2022-01-20 06:06:22 -05:00
|
|
|
etcd --advertise-client-urls "${KUBE_INTEGRATION_ETCD_URL}" --data-dir "${ETCD_DIR}" --listen-client-urls "${KUBE_INTEGRATION_ETCD_URL}" --log-level="${ETCD_LOGLEVEL}" 2> "${ETCD_LOGFILE}" >/dev/null &
|
2014-10-22 19:26:59 -04:00
|
|
|
ETCD_PID=$!
|
2015-02-04 16:37:28 -05:00
|
|
|
|
|
|
|
|
echo "Waiting for etcd to come up."
|
2019-10-29 16:10:29 -04:00
|
|
|
kube::util::wait_for_url "${KUBE_INTEGRATION_ETCD_URL}/health" "etcd: " 0.25 80
|
|
|
|
|
curl -fs -X POST "${KUBE_INTEGRATION_ETCD_URL}/v3/kv/put" -d '{"key": "X3Rlc3Q=", "value": ""}'
|
2014-10-22 19:26:59 -04:00
|
|
|
}
|
|
|
|
|
|
2021-11-05 15:58:48 -04:00
|
|
|
kube::etcd::start_scraping() {
|
|
|
|
|
if [[ -d "${ARTIFACTS:-}" ]]; then
|
|
|
|
|
ETCD_SCRAPE_DIR="${ARTIFACTS}/etcd-scrapes"
|
|
|
|
|
else
|
2021-11-06 02:10:05 -04:00
|
|
|
ETCD_SCRAPE_DIR=$(mktemp -d -t test.XXXXXX)/etcd-scrapes
|
2021-11-05 15:58:48 -04:00
|
|
|
fi
|
|
|
|
|
kube::log::info "Periodically scraping etcd to ${ETCD_SCRAPE_DIR} ."
|
|
|
|
|
mkdir -p "${ETCD_SCRAPE_DIR}"
|
|
|
|
|
(
|
|
|
|
|
while sleep 30; do
|
|
|
|
|
kube::etcd::scrape
|
|
|
|
|
done
|
|
|
|
|
) &
|
|
|
|
|
ETCD_SCRAPE_PID=$!
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
kube::etcd::scrape() {
|
2021-11-06 02:10:05 -04:00
|
|
|
curl -s -S "${KUBE_INTEGRATION_ETCD_URL}/metrics" > "${ETCD_SCRAPE_DIR}/next" && mv "${ETCD_SCRAPE_DIR}/next" "${ETCD_SCRAPE_DIR}/$(date +%s).scrape"
|
2021-11-05 15:58:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-04-05 03:16:27 -04:00
|
|
|
kube::etcd::stop() {
|
2021-11-06 02:10:05 -04:00
|
|
|
if [[ -n "${ETCD_SCRAPE_PID:-}" ]] && [[ -n "${ETCD_SCRAPE_DIR:-}" ]] ; then
|
2021-11-05 15:58:48 -04:00
|
|
|
kill "${ETCD_SCRAPE_PID}" &>/dev/null || :
|
|
|
|
|
wait "${ETCD_SCRAPE_PID}" &>/dev/null || :
|
|
|
|
|
kube::etcd::scrape || :
|
2021-11-06 02:10:05 -04:00
|
|
|
(
|
|
|
|
|
# shellcheck disable=SC2015
|
|
|
|
|
cd "${ETCD_SCRAPE_DIR}"/.. && \
|
|
|
|
|
tar czf etcd-scrapes.tgz etcd-scrapes && \
|
|
|
|
|
rm -rf etcd-scrapes || :
|
|
|
|
|
)
|
2021-11-05 15:58:48 -04:00
|
|
|
fi
|
2018-01-15 14:38:23 -05:00
|
|
|
if [[ -n "${ETCD_PID-}" ]]; then
|
|
|
|
|
kill "${ETCD_PID}" &>/dev/null || :
|
|
|
|
|
wait "${ETCD_PID}" &>/dev/null || :
|
|
|
|
|
fi
|
2015-04-05 03:16:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
kube::etcd::clean_etcd_dir() {
|
2018-01-15 14:38:23 -05:00
|
|
|
if [[ -n "${ETCD_DIR-}" ]]; then
|
|
|
|
|
rm -rf "${ETCD_DIR}"
|
|
|
|
|
fi
|
2014-10-22 19:26:59 -04:00
|
|
|
}
|
2015-04-05 03:16:27 -04:00
|
|
|
|
|
|
|
|
kube::etcd::cleanup() {
|
|
|
|
|
kube::etcd::stop
|
|
|
|
|
kube::etcd::clean_etcd_dir
|
|
|
|
|
}
|
2016-02-25 22:26:02 -05:00
|
|
|
|
|
|
|
|
kube::etcd::install() {
|
2023-12-24 21:10:48 -05:00
|
|
|
# Make sure that we will abort if the inner shell fails.
|
|
|
|
|
set -o errexit
|
|
|
|
|
set -o pipefail
|
|
|
|
|
set -o nounset
|
2023-10-24 17:10:06 -04:00
|
|
|
|
2023-12-24 21:10:48 -05:00
|
|
|
# We change directories below, so this subshell is needed.
|
|
|
|
|
(
|
|
|
|
|
local os
|
|
|
|
|
local arch
|
|
|
|
|
|
|
|
|
|
os=$(kube::util::host_os)
|
|
|
|
|
arch=$(kube::util::host_arch)
|
|
|
|
|
|
|
|
|
|
cd "${KUBE_ROOT}/third_party" || return 1
|
|
|
|
|
if [[ $(readlink etcd) == etcd-v${ETCD_VERSION}-${os}-* ]]; then
|
2024-09-24 15:02:54 -04:00
|
|
|
V=4 kube::log::info "etcd v${ETCD_VERSION} is already installed"
|
2023-12-24 21:10:48 -05:00
|
|
|
return 0 # already installed
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ ${os} == "darwin" ]]; then
|
|
|
|
|
download_file="etcd-v${ETCD_VERSION}-${os}-${arch}.zip"
|
|
|
|
|
url="https://github.com/etcd-io/etcd/releases/download/v${ETCD_VERSION}/${download_file}"
|
|
|
|
|
kube::util::download_file "${url}" "${download_file}"
|
|
|
|
|
unzip -o "${download_file}"
|
|
|
|
|
ln -fns "etcd-v${ETCD_VERSION}-${os}-${arch}" etcd
|
|
|
|
|
rm "${download_file}"
|
|
|
|
|
elif [[ ${os} == "linux" ]]; then
|
|
|
|
|
url="https://github.com/etcd-io/etcd/releases/download/v${ETCD_VERSION}/etcd-v${ETCD_VERSION}-${os}-${arch}.tar.gz"
|
|
|
|
|
download_file="etcd-v${ETCD_VERSION}-${os}-${arch}.tar.gz"
|
|
|
|
|
kube::util::download_file "${url}" "${download_file}"
|
|
|
|
|
tar xzf "${download_file}"
|
|
|
|
|
ln -fns "etcd-v${ETCD_VERSION}-${os}-${arch}" etcd
|
|
|
|
|
rm "${download_file}"
|
|
|
|
|
else
|
|
|
|
|
kube::log::info "${os} is NOT supported."
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
V=4 kube::log::info "installed etcd v${ETCD_VERSION}"
|
|
|
|
|
return 0 # newly installed
|
|
|
|
|
)
|
|
|
|
|
# Through the magic of errexit, we will not get here if the above shell
|
|
|
|
|
# fails!
|
|
|
|
|
PATH="${KUBE_ROOT}/third_party/etcd:${PATH}" # export into current process
|
2023-10-24 17:10:06 -04:00
|
|
|
export PATH
|
2023-12-24 21:10:48 -05:00
|
|
|
V=3 kube::log::info "added etcd to PATH: ${KUBE_ROOT}/third_party/etcd"
|
2016-02-25 22:26:02 -05:00
|
|
|
}
|