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
46 def __init__(self, name, namespace = "default", logger_name = "k8srapid"):
47 self._log = logging.getLogger(logger_name)
50 self._namespace = namespace
51 self._ssh_client = SSHClient(logger_name = logger_name)
54 """Destroy POD. Do a cleanup.
56 if self._ssh_client is not None:
57 self._ssh_client.disconnect()
59 def create_from_yaml(self, file_name):
60 """Load POD description from yaml file.
62 with open(path.join(path.dirname(__file__), file_name)) as yaml_file:
63 self.body = yaml.safe_load(yaml_file)
65 self.body["metadata"]["name"] = self._name
67 if (self._nodeSelector_hostname is not None):
68 if ("nodeSelector" not in self.body["spec"]):
69 self.body["spec"]["nodeSelector"] = {}
70 self.body["spec"]["nodeSelector"]["kubernetes.io/hostname"] = self._nodeSelector_hostname
71 self._log.debug("Creating POD, body:\n%s" % self.body)
74 self.k8s_CoreV1Api.create_namespaced_pod(body = self.body,
75 namespace = self._namespace)
76 except client.rest.ApiException as e:
77 self._log.error("Couldn't create POD %s!\n%s\n" % (self._name, e))
80 """Terminate POD. Close SSH connection.
82 if self._ssh_client is not None:
83 self._ssh_client.disconnect()
86 self.k8s_CoreV1Api.delete_namespaced_pod(name = self._name,
87 namespace = self._namespace)
88 except client.rest.ApiException as e:
89 if e.reason != "Not Found":
90 self._log.error("Couldn't delete POD %s!\n%s\n" % (self._name, e.reason))
92 def update_admin_ip(self):
93 """Check for admin IP address assigned by k8s.
96 pod = self.k8s_CoreV1Api.read_namespaced_pod_status(name = self._name, namespace = self._namespace)
97 self._admin_ip = pod.status.pod_ip
98 except client.rest.ApiException as e:
99 self._log.error("Couldn't update POD %s admin IP!\n%s\n" % (self._name, e))
101 def wait_for_start(self):
102 """Wait for POD to start.
104 self._log.info("Waiting for POD %s to start..." % self._name)
107 if (self._last_status == "Running" or self._last_status == "Failed"
108 or self._last_status == "Unknown"):
113 self.update_admin_ip()
115 return self._last_status
117 def ssh_run_cmd(self, cmd):
118 """Execute command for POD via SSH connection.
119 SSH credentials should be configured before use of this function.
121 self._ssh_client.run_cmd(cmd)
126 def get_admin_ip(self):
127 return self._admin_ip
132 def get_dp_subnet(self):
133 return self._dp_subnet
135 def get_dp_mac(self):
136 return self._sriov_vf_mac
138 def get_dp_pci_dev(self):
139 return self._sriov_vf
144 def get_status(self):
145 """Get current status fro the pod.
148 pod = self.k8s_CoreV1Api.read_namespaced_pod_status(name = self._name,
149 namespace = self._namespace)
150 except client.rest.ApiException as e:
151 self._log.error("Couldn't read POD %s status!\n%s\n" % (self._name, e))
153 self._last_status = pod.status.phase
154 return self._last_status
156 def get_sriov_dev_mac(self):
157 """Get assigned by k8s SRIOV network device plugin SRIOV VF devices.
158 Return 0 in case of sucessfull configuration.
161 self._log.info("Checking assigned SRIOV VF for POD %s" % self._name)
162 ret = self._ssh_client.run_cmd("cat /opt/rapid/k8s_sriov_device_plugin_envs")
164 self._log.error("Failed to check assigned SRIOV VF!"
165 "Error %s" % self._ssh_client.get_error())
168 cmd_output = self._ssh_client.get_output().decode("utf-8").rstrip()
169 self._log.debug("Environment variable %s" % cmd_output)
171 # Parse environment variable
172 cmd_output = cmd_output.split("=")[1]
173 self._sriov_vf = cmd_output.split(",")[0]
174 self._log.debug("Using first SRIOV VF %s" % self._sriov_vf)
176 self._log.info("Getting MAC address for assigned SRIOV VF %s" % self._sriov_vf)
177 self._ssh_client.run_cmd("sudo /opt/rapid/port_info_app -n 4 -w %s" % self._sriov_vf)
179 self._log.error("Failed to get MAC address!"
180 "Error %s" % self._ssh_client.get_error())
184 cmd_output = self._ssh_client.get_output().decode("utf-8").rstrip()
185 self._log.debug(cmd_output)
186 cmd_output = cmd_output.splitlines()
187 for line in cmd_output:
188 if line.startswith("Port 0 MAC: "):
189 self._sriov_vf_mac = line[12:]
191 self._log.debug("MAC %s" % self._sriov_vf_mac)
193 def set_dp_ip(self, dp_ip):
196 def set_dp_subnet(self, dp_subnet):
197 self._dp_subnet = dp_subnet
199 def set_id(self, pod_id):
202 def set_nodeselector(self, hostname):
203 """Set hostname on which POD will be executed.
205 self._nodeSelector_hostname = hostname
207 def set_ssh_credentials(self, user, rsa_private_key):
208 """Set SSH credentials for the SSH connection to the POD.
210 self.update_admin_ip()
211 self._ssh_client.set_credentials(ip = self._admin_ip,
213 rsa_private_key = rsa_private_key)