Skip to content

Instantly share code, notes, and snippets.

@hemanthnakkina-zz
Created November 26, 2020 09:51
Show Gist options
  • Save hemanthnakkina-zz/d13f9ddbb7711248dc5b326a05e726d0 to your computer and use it in GitHub Desktop.
Save hemanthnakkina-zz/d13f9ddbb7711248dc5b326a05e726d0 to your computer and use it in GitHub Desktop.
Send parallel API requests to kube-api server
#!/bin/bash -eux
# This file is based on https://code.launchpad.net/~freyes/+git/experiment
# Modified to use xargs instead of parallel
# source: https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/
PARALLEL_REQS=20
SLEEP_SECS=0.1
TEMPLOG=$(mktemp).log
K8S_ENDPOINT='/api'
# Select name of cluster you want to interact with from above output:
export CLUSTER_NAME="juju-cluster"
# Point to the API server referring the cluster name
APISERVER=$(kubectl config view -o jsonpath="{.clusters[?(@.name==\"$CLUSTER_NAME\")].cluster.server}")
# Gets the token value
TOKEN=$(kubectl get secrets -o jsonpath="{.items[?(@.metadata.annotations['kubernetes\.io/service-account\.name']=='default')].data.token}"|base64 --decode)
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
function ctrl_c() {
local DEST=query-kubeapiserver.$(date '+%Y%m%d_%H%M').log
mv $TEMPLOG $DEST
echo "output at $DEST"
exit 0
}
set +x
while true; do
seq $PARALLEL_REQS | xargs -I {} -n1 -P$PARALLEL_REQS curl -s -X GET $APISERVER$K8S_ENDPOINT --header "Authorization: Bearer $TOKEN" --insecure 2>&1 >> $TEMPLOG
/bin/sleep $SLEEP_SECS
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment