2 ## Copyright (c) 2019 Intel Corporation
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
8 ## http://www.apache.org/licenses/LICENSE-2.0
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.
20 from kubernetes import client, config
22 from rapid_sshclient import SSHClient
25 """Class which represents test pods.
26 For example with traffic gen, forward/swap applications, etc
33 _namespace = "default"
34 _nodeSelector_hostname = None
47 def __init__(self, name, namespace = "default", logger_name = "k8srapid"):
48 self._log = logging.getLogger(logger_name)
51 self._namespace = namespace
52 self._ssh_client = SSHClient(logger_name = logger_name)
56 """Destroy POD. Do a cleanup.
58 if self._ssh_client is not None:
59 self._ssh_client.disconnect()
61 def create_from_yaml(self):
62 """Load POD description from yaml file.
64 with open(path.join(path.dirname(__file__),
65 self._spec_filename)) as yaml_file:
66 self.body = yaml.safe_load(yaml_file)
68 self.body["metadata"]["name"] = self._name
70 if (self._nodeSelector_hostname is not None):
71 if ("nodeSelector" not in self.body["spec"]):
72 self.body["spec"]["nodeSelector"] = {}
73 self.body["spec"]["nodeSelector"]["kubernetes.io/hostname"] = \
74 self._nodeSelector_hostname
75 self._log.debug("Creating POD, body:\n%s" % self.body)
78 self.k8s_CoreV1Api.create_namespaced_pod(body = self.body,
79 namespace = self._namespace)
80 except client.rest.ApiException as e:
81 self._log.error("Couldn't create POD %s!\n%s\n" % (self._name,
85 """Terminate POD. Close SSH connection.
87 if self._ssh_client is not None:
88 self._ssh_client.disconnect()
91 self.k8s_CoreV1Api.delete_namespaced_pod(name = self._name,
92 namespace = self._namespace)
93 except client.rest.ApiException as e:
94 if e.reason != "Not Found":
95 self._log.error("Couldn't delete POD %s!\n%s\n" % (self._name, e.reason))
97 def update_admin_ip(self):
98 """Check for admin IP address assigned by k8s.
101 pod = self.k8s_CoreV1Api.read_namespaced_pod_status(name = self._name, namespace = self._namespace)
102 self._admin_ip = pod.status.pod_ip
103 except client.rest.ApiException as e:
104 self._log.error("Couldn't update POD %s admin IP!\n%s\n" % (self._name, e))
106 def wait_for_start(self):
107 """Wait for POD to start.
109 self._log.info("Waiting for POD %s to start..." % self._name)
112 if (self._last_status == "Running" or self._last_status == "Failed"
113 or self._last_status == "Unknown"):
118 self.update_admin_ip()
120 return self._last_status
122 def ssh_run_cmd(self, cmd):
123 """Execute command for POD via SSH connection.
124 SSH credentials should be configured before use of this function.
126 self._ssh_client.run_cmd(cmd)
131 def get_admin_ip(self):
132 return self._admin_ip
137 def get_dp_subnet(self):
138 return self._dp_subnet
140 def get_dp_mac(self):
141 return self._sriov_vf_mac
143 def get_dp_pci_dev(self):
144 return self._sriov_vf
146 def get_qat_pci_dev(self):
152 def get_status(self):
153 """Get current status fro the pod.
156 pod = self.k8s_CoreV1Api.read_namespaced_pod_status(name = self._name,
157 namespace = self._namespace)
158 except client.rest.ApiException as e:
159 self._log.error("Couldn't read POD %s status!\n%s\n" % (self._name, e))
161 self._last_status = pod.status.phase
162 return self._last_status
164 def get_qat_dev(self):
165 """Get qat devices if any, assigned by k8s QAT device plugin.
167 self._log.info("Checking assigned QAT VF for POD %s" % self._name)
168 ret = self._ssh_client.run_cmd("cat /opt/rapid/k8s_qat_device_plugin_envs")
170 self._log.error("Failed to check assigned QAT VF!"
171 "Error %s" % self._ssh_client.get_error())
174 cmd_output = self._ssh_client.get_output().decode("utf-8").rstrip()
177 self._log.debug("Before: Using QAT VF %s" % self.qat_vf)
178 self._log.debug("Environment variable %s" % cmd_output)
179 for line in cmd_output.splitlines():
180 self.qat_vf.append(line.split("=")[1])
181 self._log.debug("Using QAT VF %s" % self.qat_vf)
183 self._log.debug("No QAT devices for this pod")
186 def get_sriov_dev_mac(self):
187 """Get assigned by k8s SRIOV network device plugin SRIOV VF devices.
188 Return 0 in case of sucessfull configuration.
191 self._log.info("Checking assigned SRIOV VF for POD %s" % self._name)
192 ret = self._ssh_client.run_cmd("cat /opt/rapid/k8s_sriov_device_plugin_envs")
194 self._log.error("Failed to check assigned SRIOV VF!"
195 "Error %s" % self._ssh_client.get_error())
198 cmd_output = self._ssh_client.get_output().decode("utf-8").rstrip()
199 self._log.debug("Environment variable %s" % cmd_output)
201 # Parse environment variable
202 cmd_output = cmd_output.split("=")[1]
203 self._sriov_vf = cmd_output.split(",")[0]
204 self._log.debug("Using first SRIOV VF %s" % self._sriov_vf)
207 self._log.info("Checking DPDK version for POD %s" % self._name)
208 ret = self._ssh_client.run_cmd("cat /opt/rapid/dpdk_version")
210 self._log.error("Failed to check DPDK version"
211 "Error %s" % self._ssh_client.get_error())
213 dpdk_version = self._ssh_client.get_output().decode("utf-8").rstrip()
214 self._log.debug("DPDK version %s" % dpdk_version)
215 if (dpdk_version >= '20.11.0'):
216 allow_parameter = 'allow'
218 allow_parameter = 'pci-whitelist'
220 self._log.info("Getting MAC address for assigned SRIOV VF %s" % \
222 self._ssh_client.run_cmd("sudo /opt/rapid/port_info_app -n 4 \
223 --{} {}".format(allow_parameter, self._sriov_vf))
225 self._log.error("Failed to get MAC address!"
226 "Error %s" % self._ssh_client.get_error())
230 cmd_output = self._ssh_client.get_output().decode("utf-8").rstrip()
231 self._log.debug(cmd_output)
232 cmd_output = cmd_output.splitlines()
233 for line in cmd_output:
234 if line.startswith("Port 0 MAC: "):
235 self._sriov_vf_mac = line[12:]
237 self._log.debug("MAC %s" % self._sriov_vf_mac)
239 def set_dp_ip(self, dp_ip):
242 def set_dp_subnet(self, dp_subnet):
243 self._dp_subnet = dp_subnet
245 def set_id(self, pod_id):
248 def set_nodeselector(self, hostname):
249 """Set hostname on which POD will be executed.
251 self._nodeSelector_hostname = hostname
253 def set_spec_file_name(self, file_name):
254 """Set pod spec filename.
256 self._spec_filename = file_name
258 def set_ssh_credentials(self, user, rsa_private_key):
259 """Set SSH credentials for the SSH connection to the POD.
261 self.update_admin_ip()
262 self._ssh_client.set_credentials(ip = self._admin_ip,
264 rsa_private_key = rsa_private_key)