2 ENV=$(kubectl get nodes --all-namespaces | wc -l)
3 if [[ $ENV -gt 2 ]]; then
4 COMPUTE_NODE=$(kubectl get nodes --all-namespaces | grep -v master | awk 'NR==2{print $1}')
6 COMPUTE_NODE=$(kubectl get nodes --all-namespaces | grep master | awk 'NR==1{print $1}')
8 cases=("exclusive ${COMPUTE_NODE} 1" "shared ${COMPUTE_NODE} -1")
17 function wait_for_pod_up {
19 while [[ $status_phase != "Running" ]]; do
20 new_phase=$(kubectl get pods "$@" | awk 'NR==2{print $3}')
21 if [[ $new_phase != $status_phase ]]; then
22 echo "$(date +%H:%M:%S) - $@ : $new_phase"
23 status_phase=$new_phase
25 if [[ $new_phase == "Running" ]]; then
26 echo "Pod $@ is up and running.."
28 if [[ $new_phase == "Err"* ]]; then
35 function start_nginx_pod {
36 kubectl delete deployment -n default nginx --ignore-not-found=true
37 kubectl create deployment nginx --image=nginx
39 nginx_pod=$(kubectl get pods --all-namespaces| grep nginx | awk 'NR==1{print $2}')
40 wait_for_pod_up $nginx_pod
41 kubectl delete deployment -n default nginx --ignore-not-found=true
43 until [[ $pod_status == "" ]]; do
44 pod_status=$(kubectl get pod $nginx_pod --ignore-not-found=true | awk 'NR==2{print $3}')
48 rm -f $DIR/$pod_name.yaml
49 kubectl delete pod $pod_name --ignore-not-found=true --now --wait
53 for ((i=0;i<$num;i++)); do
54 inner_case=(${cases[$i]})
55 num_inner=${#inner_case[*]}
56 for ((j=0;j<$num_inner;j++)); do
57 case[$j]=${inner_case[$j]}
59 echo "##################################"
60 if [ "${case[$POOL]}" == "exclusive" ]; then
61 echo "TC: to allocate ${case[$CORE]} CPU(s) from pool of ${case[$POOL]} on node of ${case[$NODE]}"
62 TOTAL=$(kubectl get cmk-nodereport ${case[$NODE]} -o json | jq .spec.report.description.pools.${case[$POOL]} | jq .cpuLists | awk -F '{' '{print $(NF)}' | awk -F '}' '{print $(NF)}' | awk -F ',' '{print $(NF)}' | grep "\"tasks\": \[" | wc -l)
63 echo "ready to generate yaml"
64 cat << EOF > $DIR/$pod_name.yaml
72 nodeName: ${case[$NODE]}
75 - "/opt/bin/cmk isolate --conf-dir=/etc/cmk --pool=exclusive sleep -- 3900"
83 value: "${case[$CORE]}"
85 imagePullPolicy: "IfNotPresent"
88 - mountPath: "/host/proc"
90 - mountPath: "/opt/bin"
92 - mountPath: "/etc/cmk"
107 echo "ready to create pod"
108 kubectl create -f $DIR/$pod_name.yaml --validate=false
110 echo "waiting for pod up"
111 for pod in $pod_name; do
114 echo "waiting for CPU allocation finished ..."
116 until [[ $TOTAL -gt $rest ]]; do
117 rest=$(kubectl get cmk-nodereport ${case[$NODE]} -o json | jq .spec.report.description.pools.exclusive | jq .cpuLists | awk -F '{' '{print $(NF)}' | awk -F '}' '{print $(NF)}' | awk -F ',' '{print $(NF)}' | grep "\"tasks\": \[\]" | wc -l)
119 let allocated=`expr $TOTAL - $rest`
120 echo "The allocated CPU amount is:" $allocated
121 echo "deploy a nginx pod"
123 if [[ $allocated == ${case[$CORE]} ]]; then
124 echo "CPU was allocated as expected, TC passed !!"
126 echo "failed to allocate CPU, TC failed !!"
128 rm -f $DIR/$pod_name.yaml
129 echo "ready to delete pod"
130 kubectl delete pod $pod_name --ignore-not-found=true --now --wait
131 echo "Pod was deleted"
132 echo "##################################"
136 echo "TC: to allocate CPU(s) from pool of ${case[$POOL]} on node of ${case[$NODE]}"
137 echo "ready to generate yaml"
138 cat << EOF > $DIR/$pod_name.yaml
146 nodeName: ${case[$NODE]}
150 - "/opt/bin/cmk isolate --conf-dir=/etc/cmk --pool=shared sleep -- 3900"
157 - name: CMK_NUM_CORES
160 imagePullPolicy: "IfNotPresent"
162 - mountPath: "/host/proc"
164 - mountPath: "/opt/bin"
165 name: cmk-install-dir
166 - mountPath: "/etc/cmk"
170 - "/opt/bin/cmk isolate --conf-dir=/etc/cmk --pool=shared sleep -- 3300"
177 - name: CMK_NUM_CORES
180 imagePullPolicy: "IfNotPresent"
182 - mountPath: "/host/proc"
184 - mountPath: "/opt/bin"
185 name: cmk-install-dir
186 - mountPath: "/etc/cmk"
191 name: cmk-install-dir
200 echo "ready to create pod"
201 kubectl create -f $DIR/$pod_name.yaml --validate=false
203 echo "waiting for pod up"
204 for pod in $pod_name; do
207 echo "waiting for CPU allocation finished ..."
210 until [ $rest == 2 -o $timeout == 180 ]; do
211 rest=$(kubectl get cmk-nodereport ${case[$NODE]} -o json | jq .spec.report.description.pools.shared | jq .cpuLists | awk -F '{' '{print $(NF)}' | awk -F '}' '{print $(NF)}' | grep -v "cpus" | grep " "| grep -v "tasks"| grep -v "\]" | wc -l)
215 echo "The CPU allocated in shared pool for 2 tasks"
216 echo "deploy a nginx pod"
218 if [[ $rest == 2 ]]; then
219 echo "CPU was allocated as expected, TC passed !!"
221 echo "failed to allocate CPU, TC failed !!"
223 rm -f $DIR/$pod_name.yaml
224 echo "ready to delete pod"
225 kubectl delete pod $pod_name --ignore-not-found=true --now --wait
226 echo "Pod was deleted"
227 echo "##################################"