From 5d08dcf8377e76f2ce303dc79404f511ebef82e3 Mon Sep 17 00:00:00 2001 From: Janet Kuo Date: Tue, 29 Sep 2015 17:02:28 -0700 Subject: [PATCH] Use random ports when doing kubectl proxy in testing --- hack/lib/util.sh | 26 ++++++++++++++++++++++++++ hack/test-cmd.sh | 17 +++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/hack/lib/util.sh b/hack/lib/util.sh index 7154e1e0082..22a81315a2c 100755 --- a/hack/lib/util.sh +++ b/hack/lib/util.sh @@ -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() { diff --git a/hack/test-cmd.sh b/hack/test-cmd.sh index 040a3f44efa..add0823ae5f 100755 --- a/hack/test-cmd.sh +++ b/hack/test-cmd.sh @@ -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