github.com/kata-containers/tests@v0.0.0-20240307153542-772105b56064/metrics/network/latency_kubernetes/latency-network.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright (c) 2022 Intel Corporation
     4  #
     5  # SPDX-License-Identifier: Apache-2.0
     6  
     7  set -e
     8  
     9  SCRIPT_PATH=$(dirname "$(readlink -f "$0")")
    10  
    11  source "${SCRIPT_PATH}/../../../.ci/lib.sh"
    12  source "${SCRIPT_PATH}/../../lib/common.bash"
    13  latency_file=$(mktemp latencyresults.XXXXXXXXXX)
    14  TEST_NAME="${TEST_NAME:-latency}"
    15  CI_JOB="${CI_JOB:-}"
    16  
    17  function remove_tmp_file() {
    18  	rm -rf "${latency_file}"
    19  }
    20  
    21  trap remove_tmp_file EXIT
    22  
    23  function main() {
    24  	init_env
    25  	cmds=("bc" "jq")
    26  	check_cmds "${cmds[@]}"
    27  
    28  	# Check no processes are left behind
    29  	check_processes
    30  
    31  	if [ -z "${CI_JOB}" ]; then
    32  		# Start kubernetes
    33  		start_kubernetes
    34  	fi
    35  
    36  	export KUBECONFIG="$HOME/.kube/config"
    37  
    38  	wait_time=20
    39  	sleep_time=2
    40  
    41  	# Create server
    42  	kubectl create -f "${SCRIPT_PATH}/runtimeclass_workloads/latency-server.yaml"
    43  
    44  	# Get the names of the server pod
    45  	export server_pod_name="latency-server"
    46  
    47  	# Verify the server pod is working
    48  	local cmd="kubectl get pod $server_pod_name -o yaml | grep 'phase: Running'"
    49  	waitForProcess "$wait_time" "$sleep_time" "$cmd"
    50  
    51  	# Create client
    52  	kubectl create -f "${SCRIPT_PATH}/runtimeclass_workloads/latency-client.yaml"
    53  
    54  	# Get the names of the client pod
    55  	export client_pod_name="latency-client"
    56  
    57  	# Verify the client pod is working
    58  	local cmd="kubectl get pod $client_pod_name -o yaml | grep 'phase: Running'"
    59  	waitForProcess "$wait_time" "$sleep_time" "$cmd"
    60  
    61  	# Get the ip address of the server pod
    62  	export server_ip_add=$(kubectl get pod "$server_pod_name" -o jsonpath='{.status.podIP}')
    63  
    64  	# Number of packets (sent)
    65  	local number="${number:-30}"
    66  
    67  	local client_command="ping -c ${number} ${server_ip_add}"
    68  
    69  	kubectl exec "$client_pod_name" -- sh -c "$client_command" > "$latency_file"
    70  
    71  	metrics_json_init
    72  
    73  	local latency=$(cat $latency_file | grep avg | cut -f2 -d'=' | sed 's/[[:blank:]]//g' | cut -f2 -d'/')
    74  
    75  	metrics_json_start_array
    76  
    77  	local json="$(cat << EOF
    78  	{
    79  		"latency": {
    80  			"Result" : $latency,
    81  			"Units" : "ms"
    82  		}
    83  	}
    84  EOF
    85  )"
    86  
    87  	metrics_json_add_array_element "$json"
    88  	metrics_json_end_array "Results"
    89  	metrics_json_save
    90  
    91  	kubectl delete pod "$client_pod_name" "$server_pod_name"
    92  	if [ -z "${CI_JOB}" ]; then
    93  		end_kubernetes
    94  		check_processes
    95  	fi
    96  }
    97  main "$@"