Merge pull request #14789 from JanetKuo/fix-e2e-test-flake-kubectl-proxy

Use random ports when doing kubectl proxy in testing
This commit is contained in:
Dawn Chen 2015-10-12 11:46:24 -07:00
commit b07e29b03c
2 changed files with 39 additions and 4 deletions

View File

@ -48,6 +48,32 @@ kube::util::wait_for_url() {
return 1
}
# returns a random port
kube::util::get_random_port() {
awk -v min=1 -v max=65535 'BEGIN{srand(); print int(min+rand()*(max-min+1))}'
}
# use netcat to check if the host($1):port($2) is free (return 0 means free, 1 means used)
kube::util::test_host_port_free() {
local host=$1
local port=$2
local success=0
local fail=1
which nc >/dev/null || {
kube::log::usage "netcat isn't installed, can't verify if ${host}:${port} is free, skipping the check..."
return ${success}
}
if [ ! $(nc -vz "${host} ${port}") ]; then
kube::log::status "${host}:${port} is free, proceeding..."
return ${success}
else
kube::log::status "${host}:${port} is already used"
return ${fail}
fi
}
# Example: kube::util::trap_add 'echo "in trap DEBUG"' DEBUG
# See: http://stackoverflow.com/questions/3338030/multiple-bash-traps-for-the-same-signal
kube::util::trap_add() {

View File

@ -30,6 +30,7 @@ function stop-proxy()
{
[[ -n "${PROXY_PID-}" ]] && kill "${PROXY_PID}" 1>&2 2>/dev/null
PROXY_PID=
PROXY_PORT=
}
# Starts "kubect proxy" to test the client proxy. You may pass options, e.g.
@ -39,9 +40,18 @@ function start-proxy()
stop-proxy
kube::log::status "Starting kubectl proxy"
# the --www and --www-prefix are just to make something definitely show up for
# wait_for_url to see.
kubectl proxy -p ${PROXY_PORT} --www=. --www-prefix=/healthz "$@" 1>&2 &
for retry in $(seq 1 3); do
PROXY_PORT=$(kube::util::get_random_port)
kube::log::status "On try ${retry}, use proxy port ${PROXY_PORT} if it's free"
if kube::util::test_host_port_free "127.0.0.1" "${PROXY_PORT}"; then
# the --www and --www-prefix are just to make something definitely show up for
# wait_for_url to see.
kubectl proxy -p ${PROXY_PORT} --www=. --www-prefix=/healthz "$@" 1>&2 & break
fi
sleep 1;
done
PROXY_PID=$!
kube::util::wait_for_url "http://127.0.0.1:${PROXY_PORT}/healthz" "kubectl proxy $@"
}
@ -86,7 +96,6 @@ API_HOST=${API_HOST:-127.0.0.1}
KUBELET_PORT=${KUBELET_PORT:-10250}
KUBELET_HEALTHZ_PORT=${KUBELET_HEALTHZ_PORT:-10248}
CTLRMGR_PORT=${CTLRMGR_PORT:-10252}
PROXY_PORT=${PROXY_PORT:-8001}
PROXY_HOST=127.0.0.1 # kubectl only serves on localhost.
# ensure ~/.kube/config isn't loaded by tests