Skip to content

Instantly share code, notes, and snippets.

@williamcaban
Created September 17, 2021 12:34
Show Gist options
  • Save williamcaban/d37b9a89481c63e728793c56039b1816 to your computer and use it in GitHub Desktop.
Save williamcaban/d37b9a89481c63e728793c56039b1816 to your computer and use it in GitHub Desktop.
Script to test latency and speed towards OpenShift Kubernetes API Server
# If $USERNAME and $PASSWORD are not defined
# in the environment prompt for them
if [ -z "${USERNAME+x}" ]; then
read -p 'Username: ' USERNAME
fi
if [ -z "${PASSWORD+x}" ]; then
read -sp 'Password: ' PASSWORD
fi
echo -e "\nAttempting to find the K8s API Server and token"
# K8s server API
K8S_API=$( oc whoami --show-server )
# Try to get token from
TOKEN=$( oc whoami --show-token )
# If using kubeadmin default cert authentication we need differnet way to obtain token
if [ $TOKEN == ""]; then
echo "Obtaining token by authentication instead"
# Identify API endpoint for the OAUTH authorization server
AUTH_ENDPOINT=$( oc get --raw '/.well-known/oauth-authorization-server' | jq -r .authorization_endpoint )
# Do user/pass authentication to obtain token
URL_WTOKEN=$( curl -ks -u $USERNAME:$PASSWORD -o /dev/null -w "%{redirect_url}\n" "$AUTH_ENDPOINT?client_id=openshift-challenging-client&response_type=token" )
# extract token from URL
TOKEN=$( echo $URL_WTOKEN | sed "s/^.*access_token=//" | sed "s/&.*//" )
fi
for a in {1..100}; do
# endpoint to test -- Update as needed
TEST_ENDPOINT="$K8S_API/api/v1/nodes"
#
curl -k -H "Authorization: Bearer $TOKEN" -so /dev/null \
-w "HTTP:%{http_code} DNS:%{time_namelookup}s CONNECT:%{time_connect}s START:%{time_starttransfer}s TOTAL:%{time_total}s SPEED:%{speed_download}Bps\n" \
"$TEST_ENDPOINT";
sleep 1 ; done
@jrosal06
Copy link

Amazing! Thanks a lot for you answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment