2 ## Copyright (c) 2019-2020 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.
18 from kubernetes import client, config
23 import ConfigParser as configparser
25 from logging import handlers
27 from rapid_k8s_pod import Pod
30 """Deployment class to create containers for test execution in Kubernetes
33 LOG_FILE_NAME = "createrapidk8s.log"
34 SSH_PRIVATE_KEY = "./rapid_rsa_key"
37 POD_YAML_TEMPLATE_FILE_NAME = "pod-rapid.yaml"
41 _runtime_config = None
42 _total_number_of_pods = 0
47 self._log = logging.getLogger("k8srapid")
48 self._log.setLevel(logging.DEBUG)
50 console_formatter = logging.Formatter("%(message)s")
51 console_handler = logging.StreamHandler(sys.stdout)
52 console_handler.setLevel(logging.DEBUG)
53 console_handler.setFormatter(console_formatter)
55 file_formatter = logging.Formatter("%(asctime)s - "
58 file_handler = logging.handlers.RotatingFileHandler(self.LOG_FILE_NAME,
60 file_handler.setLevel(logging.DEBUG)
61 file_handler.setFormatter(file_formatter)
63 self._log.addHandler(file_handler)
64 self._log.addHandler(console_handler)
66 # Initialize k8s plugin
67 config.load_kube_config()
68 Pod.k8s_CoreV1Api = client.CoreV1Api()
70 def load_create_config(self, config_file_name):
71 """Read and parse configuration file for the test environment.
73 self._log.info("Loading configuration file %s", config_file_name)
74 self._create_config = configparser.RawConfigParser()
76 self._create_config.read(config_file_name)
77 except Exception as e:
78 self._log.error("Failed to read config file!\n%s\n" % e)
81 # Now parse config file content
82 # Parse [DEFAULT] section
83 if self._create_config.has_option("DEFAULT", "total_number_of_pods"):
84 self._total_number_of_pods = self._create_config.getint(
85 "DEFAULT", "total_number_of_pods")
87 self._log.error("No option total_number_of_pods in DEFAULT section")
90 self._log.debug("Total number of pods %d" % self._total_number_of_pods)
92 # Parse [PODx] sections
93 for i in range(1, int(self._total_number_of_pods) + 1):
95 if self._create_config.has_option("POD%d" % i,
97 pod_name = self._create_config.get(
100 pod_name = "pod-rapid-%d" % i
102 # Search for POD hostname
103 if self._create_config.has_option("POD%d" % i,
104 "nodeSelector_hostname"):
105 pod_nodeselector_hostname = self._create_config.get(
106 "POD%d" % i, "nodeSelector_hostname")
108 pod_nodeselector_hostname = None
110 # Search for POD dataplane static IP
111 if self._create_config.has_option("POD%d" % i,
113 pod_dp_ip = self._create_config.get(
114 "POD%d" % i, "dp_ip")
119 pod.set_nodeselector(pod_nodeselector_hostname)
120 pod.set_dp_ip(pod_dp_ip)
123 # Add POD to the list of PODs which need to be created
124 self._pods.append(pod)
128 def create_pods(self):
129 """ Create test PODs and wait for them to start.
130 Collect information for tests to run.
132 self._log.info("Creating PODs...")
134 # Create PODs using template from yaml file
135 for pod in self._pods:
136 self._log.info("Creating POD %s...", pod.get_name())
137 pod.create_from_yaml(K8sDeployment.POD_YAML_TEMPLATE_FILE_NAME)
139 # Wait for PODs to start
140 for pod in self._pods:
143 # Collect information from started PODs for test execution
144 for pod in self._pods:
145 pod.set_ssh_credentials(K8sDeployment.SSH_USER, K8sDeployment.SSH_PRIVATE_KEY)
146 pod.get_sriov_dev_mac()
148 def save_runtime_config(self, config_file_name):
149 self._log.info("Saving config %s for runrapid script...",
151 self._runtime_config = configparser.RawConfigParser()
154 # self._runtime_config.set("DEFAULT",
155 # "total_number_of_test_machines",
156 # self._total_number_of_pods)
159 self._runtime_config.add_section("ssh")
160 self._runtime_config.set("ssh",
162 K8sDeployment.SSH_PRIVATE_KEY)
163 self._runtime_config.set("ssh",
165 K8sDeployment.SSH_USER)
168 self._runtime_config.add_section("rapid")
169 self._runtime_config.set("rapid",
170 "total_number_of_machines",
171 self._total_number_of_pods)
173 # Export information about each pod
175 for pod in self._pods:
176 self._runtime_config.add_section("M%d" % pod.get_id())
177 self._runtime_config.set("M%d" % pod.get_id(),
178 "admin_ip", pod.get_admin_ip())
179 self._runtime_config.set("M%d" % pod.get_id(),
180 "dp_mac1", pod.get_dp_mac())
181 self._runtime_config.set("M%d" % pod.get_id(),
182 "dp_pci_dev", pod.get_dp_pci_dev())
183 self._runtime_config.set("M%d" % pod.get_id(),
184 "dp_ip1", pod.get_dp_ip())
187 self._runtime_config.add_section("Varia")
188 self._runtime_config.set("Varia",
192 # Write runtime config file
193 with open(config_file_name, "w") as file:
194 self._runtime_config.write(file)
196 def delete_pods(self):
197 for pod in self._pods: