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)
55 """Destroy POD. Do a cleanup.
57 if self._ssh_client is not None:
58 self._ssh_client.disconnect()
60 def create_from_yaml(self):
61 """Load POD description from yaml file.
63 with open(path.join(path.dirname(__file__),
64 self._spec_filename)) as yaml_file:
65 self.body = yaml.safe_load(yaml_file)
67 self.body["metadata"]["name"] = self._name
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)
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,
84 """Terminate POD. Close SSH connection.
86 if self._ssh_client is not None:
87 self._ssh_client.disconnect()
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))
96 def update_admin_ip(self):
97 """Check for admin IP address assigned by k8s.
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))
105 def wait_for_start(self):
106 """Wait for POD to start.
108 self._log.info("Waiting for POD %s to start..." % self._name)
111 if (self._last_status == "Running" or self._last_status == "Failed"
112 or self._last_status == "Unknown"):
117 self.update_admin_ip()
119 return self._last_status
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.
125 self._ssh_client.run_cmd(cmd)
130 def get_admin_ip(self):
131 return self._admin_ip
136 def get_dp_subnet(self):
137 return self._dp_subnet
139 def get_dp_mac(self):
140 return self._sriov_vf_mac
142 def get_dp_pci_dev(self):
143 return self._sriov_vf
148 def get_status(self):
149 """Get current status fro the pod.
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))
157 self._last_status = pod.status.phase
158 return self._last_status
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.
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")
168 self._log.error("Failed to check assigned SRIOV VF!"
169 "Error %s" % self._ssh_client.get_error())
172 cmd_output = self._ssh_client.get_output().decode("utf-8").rstrip()
173 self._log.debug("Environment variable %s" % cmd_output)
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)
181 self._log.info("Checking DPDK version for POD %s" % self._name)
182 ret = self._ssh_client.run_cmd("cat /opt/rapid/dpdk_version")
184 self._log.error("Failed to check DPDK version"
185 "Error %s" % self._ssh_client.get_error())
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'
192 allow_parameter = 'pci-whitelist'
194 self._log.info("Getting MAC address for assigned SRIOV VF %s" % \
196 self._ssh_client.run_cmd("sudo /opt/rapid/port_info_app -n 4 \
197 --{} {}".format(allow_parameter, self._sriov_vf))
199 self._log.error("Failed to get MAC address!"
200 "Error %s" % self._ssh_client.get_error())
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:]
211 self._log.debug("MAC %s" % self._sriov_vf_mac)
213 def set_dp_ip(self, dp_ip):
216 def set_dp_subnet(self, dp_subnet):
217 self._dp_subnet = dp_subnet
219 def set_id(self, pod_id):
222 def set_nodeselector(self, hostname):
223 """Set hostname on which POD will be executed.
225 self._nodeSelector_hostname = hostname
227 def set_spec_file_name(self, file_name):
228 """Set pod spec filename.
230 self._spec_filename = file_name
232 def set_ssh_credentials(self, user, rsa_private_key):
233 """Set SSH credentials for the SSH connection to the POD.
235 self.update_admin_ip()
236 self._ssh_client.set_credentials(ip = self._admin_ip,
238 rsa_private_key = rsa_private_key)