diff --git a/metrics/lib/common.bash b/metrics/lib/common.bash index adcee14..9ca0cd1 100755 --- a/metrics/lib/common.bash +++ b/metrics/lib/common.bash @@ -10,6 +10,7 @@ RESULT_DIR="${LIB_DIR}/../results" source ${LIB_DIR}/kata-common.bash source ${LIB_DIR}/json.bash +source ${LIB_DIR}/k8s-api.bash source /etc/os-release || source /usr/lib/os-release # Set variables to reasonable defaults if unset or empty diff --git a/metrics/lib/k8s-api.bash b/metrics/lib/k8s-api.bash new file mode 100644 index 0000000..dc50796 --- /dev/null +++ b/metrics/lib/k8s-api.bash @@ -0,0 +1,46 @@ +#!/bin/bash +# +# Copyright (c) 2019 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 + +# Helper routines for talking to k8s over its API, via http/curl + +API_ADDRESS="http://127.0.0.1" +API_PORT="8090" +PROXY_CMD="kubectl proxy --port=${API_PORT}" +clean_up_proxy=false + +k8s_api_init() { + # check if kubectl proxy is already running. If it is use the port, + # specified, otherwise start kubectl proxy command. + if [ $use_api != "no" ]; then + # assuming command was called "kubectl proxy --port=####" + # FIXME make this more flexible for --port parameter being in a different order + local port + port=$(ps -ef | awk '$8 == "kubectl" && $9 == "proxy" {print $10}' | cut -b1-7 --complement) + if [ -z $port ]; then + echo "starting kubectl proxy" + clean_up_proxy=true + ${PROXY_CMD} & + else + echo "found proxy port: ${port}" + API_PORT=$port + fi + fi +} + +k8s_api_shutdown() { + # if this script launched the proxy, clean it up to prevent hang on exit + if [ "$clean_up_proxy" = "true" ]; then + echo "cleaning up kubectl proxy" + kill $(pgrep -f "${PROXY_CMD}") + fi +} + +# get the number of nodes in the "Ready" state +get_num_nodes() { + n=$(kubectl get nodes --no-headers=true | awk '$2 == "Ready" {print $1}' | wc -l) + echo "$n" +} + diff --git a/metrics/scaling/k8s_scale.sh b/metrics/scaling/k8s_scale.sh index 8bdb064..32bb253 100755 --- a/metrics/scaling/k8s_scale.sh +++ b/metrics/scaling/k8s_scale.sh @@ -34,17 +34,6 @@ use_api=${use_api:-yes} TEST_ARGS="runtime=${RUNTIME}" TEST_NAME="k8s scaling" -API_ADDRESS="http://127.0.0.1" -API_PORT="8090" -PROXY_CMD="kubectl proxy --port=${API_PORT}" -clean_up_proxy=false - -# get the number of nodes in the "Ready" state -get_num_nodes() { - n=$(kubectl get nodes --no-headers=true | awk '$2 == "Ready" {print $1}' | wc -l) - echo "$n" -} - # $1 is the launch time in seconds this pod/container took to start up. # $2 is the number of pod/containers under test grab_stats() { @@ -104,22 +93,7 @@ init() { # FIXME - check the node(s) can run enough pods - check 'max-pods' in the # kubelet config - from 'kubectl describe node -o json' ? - # check if kubectl proxy is already running. If it is use the port, - # specified, otherwise start kubectl proxy command. - if [ $use_api != "no" ]; then - # assuming command was called "kubectl proxy --port=####" - # FIXME make this more flexible for --port parameter being in a different order - local port - port=$(ps -ef | awk '$8 == "kubectl" && $9 == "proxy" {print $10}' | cut -b1-7 --complement) - if [ -z $port ]; then - echo "starting kubectl proxy" - clean_up_proxy=true - ${PROXY_CMD} & - else - echo "found proxy port: ${port}" - API_PORT=$port - fi - fi + k8s_api_init # Launch our stats gathering pod kubectl apply -f ${SCRIPT_PATH}/${stats_pod}.yaml @@ -245,11 +219,7 @@ EOF metrics_json_add_fragment "$json" metrics_json_save - # if this script launched the proxy, clean it up to prevent hang on exit - if [ "$clean_up_proxy" = "true" ]; then - echo "cleaning up kubectl proxy" - kill $(pgrep -f "${PROXY_CMD}") - fi + k8s_api_shutdown } show_vars()