8ddff3bfc36bd2b7fab8094e602d9ed61a679bee
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / rapid / rapid_k8s_pod.py
1 ##
2 ## Copyright (c) 2019 Intel Corporation
3 ##
4 ## Licensed under the Apache License, Version 2.0 (the "License");
5 ## you may not use this file except in compliance with the License.
6 ## You may obtain a copy of the License at
7 ##
8 ##     http://www.apache.org/licenses/LICENSE-2.0
9 ##
10 ## Unless required by applicable law or agreed to in writing, software
11 ## distributed under the License is distributed on an "AS IS" BASIS,
12 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ## See the License for the specific language governing permissions and
14 ## limitations under the License.
15 ##
16
17 from os import path
18 import time, yaml
19 import logging
20 from kubernetes import client, config
21
22 from rapid_sshclient import SSHClient
23
24 class Pod:
25     """Class which represents test pods.
26     For example with traffic gen, forward/swap applications, etc
27     """
28     k8s_CoreV1Api = None
29
30     _log = None
31
32     _name = "pod"
33     _namespace = "default"
34     _nodeSelector_hostname = None
35     _spec_filename = None
36     _last_status = None
37     _id = None
38     _admin_ip = None
39     _dp_ip = None
40     _dp_subnet = None
41
42     _ssh_client = None
43
44     _sriov_vf = None
45     _sriov_vf_mac = None
46
47     def __init__(self, name, namespace = "default", logger_name = "k8srapid"):
48         self._log = logging.getLogger(logger_name)
49
50         self._name = name
51         self._namespace = namespace
52         self._ssh_client = SSHClient(logger_name = logger_name)
53
54     def __del__(self):
55         """Destroy POD. Do a cleanup.
56         """
57         if self._ssh_client is not None:
58             self._ssh_client.disconnect()
59
60     def create_from_yaml(self):
61         """Load POD description from yaml file.
62         """
63         with open(path.join(path.dirname(__file__),
64             self._spec_filename)) as yaml_file:
65             self.body = yaml.safe_load(yaml_file)
66
67             self.body["metadata"]["name"] = self._name
68
69             if (self._nodeSelector_hostname is not None):
70                 if ("nodeSelector" not in self.body["spec"]):
71                     self.body["spec"]["nodeSelector"] = {}
72                 self.body["spec"]["nodeSelector"]["kubernetes.io/hostname"] = \
73                         self._nodeSelector_hostname
74             self._log.debug("Creating POD, body:\n%s" % self.body)
75
76             try:
77                 self.k8s_CoreV1Api.create_namespaced_pod(body = self.body,
78                                                 namespace = self._namespace)
79             except client.rest.ApiException as e:
80                 self._log.error("Couldn't create POD %s!\n%s\n" % (self._name,
81                     e))
82
83     def terminate(self):
84         """Terminate POD. Close SSH connection.
85         """
86         if self._ssh_client is not None:
87             self._ssh_client.disconnect()
88
89         try:
90             self.k8s_CoreV1Api.delete_namespaced_pod(name = self._name,
91                                                      namespace = self._namespace)
92         except client.rest.ApiException as e:
93             if e.reason != "Not Found":
94                 self._log.error("Couldn't delete POD %s!\n%s\n" % (self._name, e.reason))
95
96     def update_admin_ip(self):
97         """Check for admin IP address assigned by k8s.
98         """
99         try:
100             pod = self.k8s_CoreV1Api.read_namespaced_pod_status(name = self._name, namespace = self._namespace)
101             self._admin_ip = pod.status.pod_ip
102         except client.rest.ApiException as e:
103             self._log.error("Couldn't update POD %s admin IP!\n%s\n" % (self._name, e))
104
105     def wait_for_start(self):
106         """Wait for POD to start.
107         """
108         self._log.info("Waiting for POD %s to start..." % self._name)
109         while True:
110             self.get_status()
111             if (self._last_status == "Running" or self._last_status == "Failed"
112                 or self._last_status == "Unknown"):
113                 break
114             else:
115                 time.sleep(3)
116
117         self.update_admin_ip()
118
119         return self._last_status
120
121     def ssh_run_cmd(self, cmd):
122         """Execute command for POD via SSH connection.
123         SSH credentials should be configured before use of this function.
124         """
125         self._ssh_client.run_cmd(cmd)
126
127     def get_name(self):
128         return self._name
129
130     def get_admin_ip(self):
131         return self._admin_ip
132
133     def get_dp_ip(self):
134         return self._dp_ip
135
136     def get_dp_subnet(self):
137         return self._dp_subnet
138
139     def get_dp_mac(self):
140         return self._sriov_vf_mac
141
142     def get_dp_pci_dev(self):
143         return self._sriov_vf
144
145     def get_id(self):
146         return self._id
147
148     def get_status(self):
149         """Get current status fro the pod.
150         """
151         try:
152             pod = self.k8s_CoreV1Api.read_namespaced_pod_status(name = self._name,
153                                                                 namespace = self._namespace)
154         except client.rest.ApiException as e:
155             self._log.error("Couldn't read POD %s status!\n%s\n" % (self._name, e))
156
157         self._last_status = pod.status.phase
158         return self._last_status
159
160     def get_sriov_dev_mac(self):
161         """Get assigned by k8s SRIOV network device plugin SRIOV VF devices.
162         Return 0 in case of sucessfull configuration.
163         Otherwise return -1.
164         """
165         self._log.info("Checking assigned SRIOV VF for POD %s" % self._name)
166         ret = self._ssh_client.run_cmd("cat /opt/rapid/k8s_sriov_device_plugin_envs")
167         if ret != 0:
168             self._log.error("Failed to check assigned SRIOV VF!"
169                             "Error %s" % self._ssh_client.get_error())
170             return -1
171
172         cmd_output = self._ssh_client.get_output().decode("utf-8").rstrip()
173         self._log.debug("Environment variable %s" % cmd_output)
174
175         # Parse environment variable
176         cmd_output = cmd_output.split("=")[1]
177         self._sriov_vf = cmd_output.split(",")[0]
178         self._log.debug("Using first SRIOV VF %s" % self._sriov_vf)
179
180         # find DPDK version
181         self._log.info("Checking DPDK version for POD %s" % self._name)
182         ret = self._ssh_client.run_cmd("cat /opt/rapid/dpdk_version")
183         if ret != 0:
184             self._log.error("Failed to check DPDK version"
185                             "Error %s" % self._ssh_client.get_error())
186             return -1
187         dpdk_version = self._ssh_client.get_output().decode("utf-8").rstrip()
188         self._log.debug("DPDK version %s" % dpdk_version)
189         if (dpdk_version >= '20.11.0'):
190             allow_parameter = 'allow'
191         else:
192             allow_parameter = 'pci-whitelist'
193
194         self._log.info("Getting MAC address for assigned SRIOV VF %s" % \
195                 self._sriov_vf)
196         self._ssh_client.run_cmd("sudo /opt/rapid/port_info_app -n 4 \
197                 --{} {}".format(allow_parameter, self._sriov_vf))
198         if ret != 0:
199             self._log.error("Failed to get MAC address!"
200                             "Error %s" % self._ssh_client.get_error())
201             return -1
202
203         # Parse MAC address
204         cmd_output = self._ssh_client.get_output().decode("utf-8").rstrip()
205         self._log.debug(cmd_output)
206         cmd_output = cmd_output.splitlines()
207         for line in cmd_output:
208             if line.startswith("Port 0 MAC: "):
209                 self._sriov_vf_mac = line[12:]
210
211         self._log.debug("MAC %s" % self._sriov_vf_mac)
212
213     def set_dp_ip(self, dp_ip):
214         self._dp_ip = dp_ip
215
216     def set_dp_subnet(self, dp_subnet):
217         self._dp_subnet = dp_subnet
218
219     def set_id(self, pod_id):
220         self._id = pod_id
221
222     def set_nodeselector(self, hostname):
223         """Set hostname on which POD will be executed.
224         """
225         self._nodeSelector_hostname = hostname
226
227     def set_spec_file_name(self, file_name):
228         """Set pod spec filename.
229         """
230         self._spec_filename = file_name
231
232     def set_ssh_credentials(self, user, rsa_private_key):
233         """Set SSH credentials for the SSH connection to the POD.
234         """
235         self.update_admin_ip()
236         self._ssh_client.set_credentials(ip = self._admin_ip,
237                                          user = user,
238                                          rsa_private_key = rsa_private_key)