Merge "Add sriov-network addon helm chart"
[multicloud/k8s.git] / kud / tests / sriov.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2018
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 pipefail
12
13 sriov_capable_nodes=$(kubectl get nodes -o json | jq -r '.items[] | select((.status.capacity."intel.com/intel_sriov_700"|tonumber)>=2) | .metadata.name')
14 if [ -z "$sriov_capable_nodes" ]; then
15     echo "SRIOV test case cannot run on the cluster."
16     exit 0
17 else
18     echo "SRIOV option avaiable in the cluster."
19 fi
20
21 pod_name=pod-case-01
22
23 function create_pod_yaml_with_single_VF {
24
25 cat << POD > $HOME/$pod_name-single.yaml
26 apiVersion: v1
27 kind: Pod
28 metadata:
29   name: pod-case-01
30   annotations:
31     k8s.v1.cni.cncf.io/networks: sriov-eno2
32 spec:
33   containers:
34   - name: test-pod
35     image: docker.io/centos/tools:latest
36     command:
37     - /sbin/init
38     resources:
39       requests:
40         intel.com/intel_sriov_700: '1'
41       limits:
42         intel.com/intel_sriov_700: '1'
43 POD
44 }
45
46 function create_pod_yaml_with_multiple_VF {
47
48 cat << POD > $HOME/$pod_name-multiple.yaml
49 apiVersion: v1
50 kind: Pod
51 metadata:
52   name: pod-case-01
53   annotations:
54     k8s.v1.cni.cncf.io/networks: sriov-eno2, sriov-eno2
55 spec:
56   containers:
57   - name: test-pod
58     image: docker.io/centos/tools:latest
59     command:
60     - /sbin/init
61     resources:
62       requests:
63         intel.com/intel_sriov_700: '2'
64       limits:
65         intel.com/intel_sriov_700: '2'
66 POD
67 }
68 create_pod_yaml_with_single_VF
69 create_pod_yaml_with_multiple_VF
70
71 for podType in ${POD_TYPE:-single multiple}; do
72
73     kubectl delete pod $pod_name --ignore-not-found=true --now --wait
74     allocated_node_resource=$(kubectl describe node | grep "intel.com/intel_sriov_700" | tail -n1 |awk '{print $(NF)}')
75
76     echo "The allocated resource of the node is: " $allocated_node_resource
77
78     kubectl create -f $HOME/$pod_name-$podType.yaml --validate=false
79
80         for pod in $pod_name; do
81             status_phase=""
82             while [[ $status_phase != "Running" ]]; do
83                 new_phase=$(kubectl get pods $pod | awk 'NR==2{print $3}')
84                 if [[ $new_phase != $status_phase ]]; then
85                     echo "$(date +%H:%M:%S) - $pod-$podType : $new_phase"
86                     status_phase=$new_phase
87                 fi
88                 if [[ $new_phase == "Running" ]]; then
89                     echo "Pod is up and running.."
90                 fi
91                 if [[ $new_phase == "Err"* ]]; then
92                     exit 1
93                 fi
94             done
95         done
96     allocated_node_resource=$(kubectl describe node | grep "intel.com/intel_sriov_700" | tail -n1 |awk '{print $(NF)}')
97
98     echo " The current resource allocation after the pod creation is: " $allocated_node_resource
99     kubectl delete pod $pod_name --now
100     echo "Test complete."
101
102 done