Determinisitically get kubectl proxy port

This commit is contained in:
Daniel Smith 2016-01-27 17:09:52 -08:00
parent 2aff6ccf06
commit 22b43d8f82

View File

@ -28,9 +28,12 @@ source "${KUBE_ROOT}/hack/lib/test.sh"
# Stops the running kubectl proxy, if there is one. # Stops the running kubectl proxy, if there is one.
function stop-proxy() function stop-proxy()
{ {
[[ -n "${PROXY_PORT-}" ]] && kube::log::status "Stopping proxy on port ${PROXY_PORT}"
[[ -n "${PROXY_PID-}" ]] && kill "${PROXY_PID}" 1>&2 2>/dev/null [[ -n "${PROXY_PID-}" ]] && kill "${PROXY_PID}" 1>&2 2>/dev/null
[[ -n "${PROXY_PORT_FILE-}" ]] && rm -f ${PROXY_PORT_FILE}
PROXY_PID= PROXY_PID=
PROXY_PORT= PROXY_PORT=
PROXY_PORT_FILE=
} }
# Starts "kubect proxy" to test the client proxy. $1: api_prefix # Starts "kubect proxy" to test the client proxy. $1: api_prefix
@ -38,22 +41,32 @@ function start-proxy()
{ {
stop-proxy stop-proxy
kube::log::status "Starting kubectl proxy" PROXY_PORT_FILE=$(mktemp proxy-port.out.XXXXX)
kube::log::status "Starting kubectl proxy on random port; output file in ${PROXY_PORT_FILE}; args: ${1-}"
for retry in $(seq 1 3); do
PROXY_PORT=$(kube::util::get_random_port) if [ $# -eq 0 ]; then
kube::log::status "On try ${retry}, use proxy port ${PROXY_PORT} if it's free" kubectl proxy --port=0 --www=. 1>${PROXY_PORT_FILE} 2>&1 &
if kube::util::test_host_port_free "127.0.0.1" "${PROXY_PORT}"; then else
if [ $# -eq 0 ]; then kubectl proxy --port=0 --www=. --api-prefix="$1" 1>${PROXY_PORT_FILE} 2>&1 &
kubectl proxy -p ${PROXY_PORT} --www=. 1>&2 & break fi
else PROXY_PID=$!
kubectl proxy -p ${PROXY_PORT} --www=. --api-prefix="$1" 1>&2 & break PROXY_PORT=
fi
local attempts=0
while [[ -z ${PROXY_PORT} ]]; do
if (( ${attempts} > 9 )); then
kill "${PROXY_PID}"
kube::log::error_exit "Couldn't start proxy. Failed to read port after ${attempts} tries. Got: $(cat ${PROXY_PORT_FILE})"
fi fi
sleep 1; sleep .5
kube::log::status "Attempt ${attempts} to read ${PROXY_PORT_FILE}..."
PROXY_PORT=$(sed 's/.*Starting to serve on 127.0.0.1:\([0-9]*\)$/\1/'< ${PROXY_PORT_FILE})
attempts=$((attempts+1))
done done
PROXY_PID=$! kube::log::status "kubectl proxy running on port ${PROXY_PORT}"
# We try checking kubectl proxy 30 times with 1s delays to avoid occasional # We try checking kubectl proxy 30 times with 1s delays to avoid occasional
# failures. # failures.
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then