Skip to content

Commit a5dbff9

Browse files
authored
Merge pull request #565 from yanggangtony/fix-wrong-etcd-install
fix etcd install hint info.
2 parents aeb7ff2 + e4d4400 commit a5dbff9

File tree

2 files changed

+98
-17
lines changed

2 files changed

+98
-17
lines changed

hack/install-etcd.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2014 The Kubernetes 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+
# This script is convenience to download and install etcd in third_party.
18+
# Mostly just used by CI.
19+
# Usage: `hack/install-etcd.sh`.
20+
21+
set -o errexit
22+
set -o nounset
23+
set -o pipefail
24+
25+
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
26+
source "${KUBE_ROOT}/hack/lib/init.sh"
27+
28+
kube::etcd::install

hack/lib/etcd.sh

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616

1717
# A set of helpers for starting/running etcd for tests
1818

19-
ETCD_VERSION=${ETCD_VERSION:-3.5.0}
19+
ETCD_VERSION=${ETCD_VERSION:-3.5.7}
2020
ETCD_HOST=${ETCD_HOST:-127.0.0.1}
2121
ETCD_PORT=${ETCD_PORT:-2379}
22+
# This is intentionally not called ETCD_LOG_LEVEL:
23+
# etcd checks that and compains when it is set in addition
24+
# to the command line argument, even when both have the same value.
25+
ETCD_LOGLEVEL=${ETCD_LOGLEVEL:-warn}
2226
export KUBE_INTEGRATION_ETCD_URL="http://${ETCD_HOST}:${ETCD_PORT}"
2327

2428
kube::etcd::validate() {
@@ -29,25 +33,39 @@ kube::etcd::validate() {
2933
exit 1
3034
}
3135

32-
# validate if etcd is running and $ETCD_PORT is in use
33-
if ps -ef | grep "etcd " | grep ${ETCD_PORT} &> /dev/null; then
36+
# validate etcd port is free
37+
local port_check_command
38+
if command -v ss &> /dev/null && ss -Version | grep 'iproute2' &> /dev/null; then
39+
port_check_command="ss"
40+
elif command -v netstat &>/dev/null; then
41+
port_check_command="netstat"
42+
else
43+
kube::log::usage "unable to identify if etcd is bound to port ${ETCD_PORT}. unable to find ss or netstat utilities."
44+
exit 1
45+
fi
46+
if ${port_check_command} -nat | grep "LISTEN" | grep "[\.:]${ETCD_PORT:?}" >/dev/null 2>&1; then
3447
kube::log::usage "unable to start etcd as port ${ETCD_PORT} is in use. please stop the process listening on this port and retry."
48+
kube::log::usage "$(netstat -nat | grep "[\.:]${ETCD_PORT:?} .*LISTEN")"
3549
exit 1
3650
fi
3751

3852
# need set the env of "ETCD_UNSUPPORTED_ARCH" on unstable arch.
3953
arch=$(uname -m)
40-
if [[ $arch =~ aarch* ]]; then
41-
export ETCD_UNSUPPORTED_ARCH=arm64
42-
elif [[ $arch =~ arm* ]]; then
54+
if [[ $arch =~ arm* ]]; then
4355
export ETCD_UNSUPPORTED_ARCH=arm
4456
fi
4557
# validate installed version is at least equal to minimum
4658
version=$(etcd --version | grep Version | head -n 1 | cut -d " " -f 3)
4759
if [[ $(kube::etcd::version "${ETCD_VERSION}") -gt $(kube::etcd::version "${version}") ]]; then
60+
export PATH=${KUBE_ROOT}/third_party/etcd:${PATH}
61+
hash etcd
62+
echo "${PATH}"
63+
version=$(etcd --version | grep Version | head -n 1 | cut -d " " -f 3)
64+
if [[ $(kube::etcd::version "${ETCD_VERSION}") -gt $(kube::etcd::version "${version}") ]]; then
4865
kube::log::usage "etcd version ${ETCD_VERSION} or greater required."
49-
kube::log::info "You can use 'hack/install-etcd.sh' to install."
66+
kube::log::info "You can use 'hack/install-etcd.sh' to install a copy in third_party/."
5067
exit 1
68+
fi
5169
fi
5270
}
5371

@@ -66,16 +84,48 @@ kube::etcd::start() {
6684
else
6785
ETCD_LOGFILE=${ETCD_LOGFILE:-"/dev/null"}
6886
fi
69-
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=debug > \"${ETCD_LOGFILE}\" 2>/dev/null"
70-
etcd --advertise-client-urls "${KUBE_INTEGRATION_ETCD_URL}" --data-dir "${ETCD_DIR}" --listen-client-urls "${KUBE_INTEGRATION_ETCD_URL}" --log-level=debug 2> "${ETCD_LOGFILE}" >/dev/null &
87+
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"
88+
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 &
7189
ETCD_PID=$!
7290

7391
echo "Waiting for etcd to come up."
7492
kube::util::wait_for_url "${KUBE_INTEGRATION_ETCD_URL}/health" "etcd: " 0.25 80
7593
curl -fs -X POST "${KUBE_INTEGRATION_ETCD_URL}/v3/kv/put" -d '{"key": "X3Rlc3Q=", "value": ""}'
7694
}
7795

96+
kube::etcd::start_scraping() {
97+
if [[ -d "${ARTIFACTS:-}" ]]; then
98+
ETCD_SCRAPE_DIR="${ARTIFACTS}/etcd-scrapes"
99+
else
100+
ETCD_SCRAPE_DIR=$(mktemp -d -t test.XXXXXX)/etcd-scrapes
101+
fi
102+
kube::log::info "Periodically scraping etcd to ${ETCD_SCRAPE_DIR} ."
103+
mkdir -p "${ETCD_SCRAPE_DIR}"
104+
(
105+
while sleep 30; do
106+
kube::etcd::scrape
107+
done
108+
) &
109+
ETCD_SCRAPE_PID=$!
110+
}
111+
112+
kube::etcd::scrape() {
113+
curl -s -S "${KUBE_INTEGRATION_ETCD_URL}/metrics" > "${ETCD_SCRAPE_DIR}/next" && mv "${ETCD_SCRAPE_DIR}/next" "${ETCD_SCRAPE_DIR}/$(date +%s).scrape"
114+
}
115+
116+
78117
kube::etcd::stop() {
118+
if [[ -n "${ETCD_SCRAPE_PID:-}" ]] && [[ -n "${ETCD_SCRAPE_DIR:-}" ]] ; then
119+
kill "${ETCD_SCRAPE_PID}" &>/dev/null || :
120+
wait "${ETCD_SCRAPE_PID}" &>/dev/null || :
121+
kube::etcd::scrape || :
122+
(
123+
# shellcheck disable=SC2015
124+
cd "${ETCD_SCRAPE_DIR}"/.. && \
125+
tar czf etcd-scrapes.tgz etcd-scrapes && \
126+
rm -rf etcd-scrapes || :
127+
)
128+
fi
79129
if [[ -n "${ETCD_PID-}" ]]; then
80130
kill "${ETCD_PID}" &>/dev/null || :
81131
wait "${ETCD_PID}" &>/dev/null || :
@@ -100,27 +150,30 @@ kube::etcd::install() {
100150

101151
os=$(kube::util::host_os)
102152
arch=$(kube::util::host_arch)
103-
153+
mkdir -p ${KUBE_ROOT}/third_party
154+
cd "${KUBE_ROOT}/third_party" || return 1
104155
if [[ $(readlink etcd) == etcd-v${ETCD_VERSION}-${os}-* ]]; then
105156
kube::log::info "etcd v${ETCD_VERSION} already installed. To use:"
106157
kube::log::info "export PATH=\"$(pwd)/etcd:\${PATH}\""
107158
return #already installed
108159
fi
109160

110161
if [[ ${os} == "darwin" ]]; then
111-
download_file="etcd-v${ETCD_VERSION}-darwin-amd64.zip"
112-
url="https://github.com/etcd-io/etcd/releases/download/v${ETCD_VERSION}/${download_file}"
162+
download_file="etcd-v${ETCD_VERSION}-${os}-${arch}.zip"
163+
url="https://github.com/coreos/etcd/releases/download/v${ETCD_VERSION}/${download_file}"
113164
kube::util::download_file "${url}" "${download_file}"
114165
unzip -o "${download_file}"
115-
ln -fns "etcd-v${ETCD_VERSION}-darwin-amd64" etcd
166+
ln -fns "etcd-v${ETCD_VERSION}-${os}-${arch}" etcd
116167
rm "${download_file}"
117-
else
118-
url="https://github.com/etcd-io/etcd/releases/download/v${ETCD_VERSION}/etcd-v${ETCD_VERSION}-linux-${arch}.tar.gz"
119-
download_file="etcd-v${ETCD_VERSION}-linux-${arch}.tar.gz"
168+
elif [[ ${os} == "linux" ]]; then
169+
url="https://github.com/coreos/etcd/releases/download/v${ETCD_VERSION}/etcd-v${ETCD_VERSION}-${os}-${arch}.tar.gz"
170+
download_file="etcd-v${ETCD_VERSION}-${os}-${arch}.tar.gz"
120171
kube::util::download_file "${url}" "${download_file}"
121172
tar xzf "${download_file}"
122-
ln -fns "etcd-v${ETCD_VERSION}-linux-${arch}" etcd
173+
ln -fns "etcd-v${ETCD_VERSION}-${os}-${arch}" etcd
123174
rm "${download_file}"
175+
else
176+
kube::log::info "${os} is NOT supported."
124177
fi
125178
kube::log::info "etcd v${ETCD_VERSION} installed. To use:"
126179
kube::log::info "export PATH=\"$(pwd)/etcd:\${PATH}\""

0 commit comments

Comments
 (0)