Merge "Move topology-manager configuration to kubespray"
[multicloud/k8s.git] / kud / tests / topology-manager.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2020
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 set -o errexit
12 set -o nounset
13 set -o pipefail
14
15 source _common.sh
16 source _functions.sh
17
18 sriov_capable_nodes=$(kubectl get nodes -o json | jq -r '.items[] | select((.status.capacity."intel.com/intel_sriov_700"!=null) and ((.status.capacity."intel.com/intel_sriov_700"|tonumber)>=2)) | .metadata.name')
19 if [ -z "$sriov_capable_nodes" ]; then
20     echo "Ethernet adaptor version is not set. Topology manager test case cannot run on this machine"
21     exit 0
22 else
23     echo "NIC card specs match. Topology manager option avaiable for this version."
24 fi
25
26 pod_name=pod-topology-manager
27 csar_id=bd55cccc-bf34-11ea-b3de-0242ac130004
28
29 function create_pod_yaml {
30     local csar_id=$1
31     _checks_args $csar_id
32     pushd ${CSAR_DIR}/${csar_id}
33
34     cat << POD > $pod_name.yaml
35 kind: Pod
36 apiVersion: v1
37 metadata:
38   name: $pod_name
39   annotations:
40     k8s.v1.cni.cncf.io/networks: sriov-eno2
41 spec:
42   containers:
43   - name: $pod_name
44     image: docker.io/centos/tools:latest
45     command:
46     - /sbin/init
47     resources:
48       limits:
49         cpu: "1"
50         memory: "500Mi"
51         intel.com/intel_sriov_700: '1'
52       requests:
53         cpu: "1"
54         memory: "500Mi"
55         intel.com/intel_sriov_700: '1'
56 POD
57     popd
58 }
59
60 create_pod_yaml ${csar_id}
61 kubectl delete pod $pod_name --ignore-not-found=true --now --wait
62 kubectl create -f ${CSAR_DIR}/${csar_id}/$pod_name.yaml --validate=false
63
64 status_phase=""
65 while [[ $status_phase != "Running" ]]; do
66     new_phase=$(kubectl get pods $pod_name | awk 'NR==2{print $3}')
67     if [[ $new_phase != $status_phase ]]; then
68         echo "$(date +%H:%M:%S) - $pod_name : $new_phase"
69         status_phase=$new_phase
70     fi
71     if [[ $new_phase == "Running" ]]; then
72         echo "Pod is up and running.."
73     fi
74     if [[ $new_phase == "Err"* ]]; then
75         exit 1
76     fi
77 done
78
79 uid=$(kubectl get pod pod-topology-manager -o jsonpath='{.metadata.uid}')
80 node_name=$(kubectl get pod $pod_name -o jsonpath='{.spec.nodeName}')
81 node_ip=$(kubectl get node $node_name -o jsonpath='{.status.addresses[].address}')
82
83 apt-get install -y jq
84 cpu_core=$(ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $node_ip -- cat /var/lib/kubelet/cpu_manager_state | jq -r --arg UID "${uid}" --arg POD_NAME "${pod_name}" '.entries[$UID][$POD_NAME]')
85 numa_node_number=$(ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $node_ip -- lscpu | grep "NUMA node(s)" | awk -F ':' '{print $2}')
86 for (( node=0; node<$numa_node_number; node++ )); do
87     ranges=$(ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $node_ip -- lscpu | grep "NUMA node"$node | awk -F ':' '{print $2}')
88     ranges=(${ranges//,/ })
89     for range in ${ranges[@]}; do
90         min=$(echo $range | awk -F '-' '{print $1}')
91         max=$(echo $range | awk -F '-' '{print $2}')
92         if [ $cpu_core -ge $min ] && [ $cpu_core -le $max ]; then
93             cpu_numa_node=$node
94         fi
95     done
96 done
97
98 vf_pci=$(kubectl exec -it $pod_name -- env | grep PCIDEVICE_INTEL_COM_INTEL_SRIOV_700 | awk -F '=' '{print $2}' | sed 's/\r//g')
99 vf_numa_node=$(ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $node_ip -- cat /sys/bus/pci/devices/$vf_pci/numa_node)
100
101 echo "The allocated cpu core is:" $cpu_core
102 echo "The numa node of the allocated cpu core is:" $cpu_numa_node
103 echo "The PCI address of the allocated vf is:" $vf_pci
104 echo "The numa node of the allocated vf is:" $vf_numa_node
105 if [ $cpu_numa_node == $vf_numa_node ]; then
106     echo "The allocated cpu core and vf are on the same numa node"
107 else
108     echo "The allocated cpu core and vf are on different numa nodes"
109 fi
110
111 kubectl delete pod $pod_name --now
112 echo "Test complete."