1 #!/usr/bin/env python2.7
4 ## Copyright (c) 2019-2020 Intel Corporation
6 ## Licensed under the Apache License, Version 2.0 (the "License");
7 ## you may not use this file except in compliance with the License.
8 ## You may obtain a copy of the License at
10 ## http://www.apache.org/licenses/LICENSE-2.0
12 ## Unless required by applicable law or agreed to in writing, software
13 ## distributed under the License is distributed on an "AS IS" BASIS,
14 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ## See the License for the specific language governing permissions and
16 ## limitations under the License.
20 from kubernetes import client, config
23 from logging import handlers
28 """Deployment class to create containers for test execution in Kubernetes
31 LOG_FILE_NAME = "createrapidk8s.log"
32 SSH_PRIVATE_KEY = "./rapid_rsa_key"
35 POD_YAML_TEMPLATE_FILE_NAME = "pod-rapid.yaml"
39 _runtime_config = None
40 _total_number_of_pods = 0
45 self._log = logging.getLogger("k8srapid")
46 self._log.setLevel(logging.DEBUG)
48 console_formatter = logging.Formatter("%(message)s")
49 console_handler = logging.StreamHandler(sys.stdout)
50 console_handler.setLevel(logging.DEBUG)
51 console_handler.setFormatter(console_formatter)
53 file_formatter = logging.Formatter("%(asctime)s - "
56 file_handler = logging.handlers.RotatingFileHandler(self.LOG_FILE_NAME,
58 file_handler.setLevel(logging.DEBUG)
59 file_handler.setFormatter(file_formatter)
61 self._log.addHandler(file_handler)
62 self._log.addHandler(console_handler)
64 # Initialize k8s plugin
65 config.load_kube_config()
66 Pod.k8s_CoreV1Api = client.CoreV1Api()
68 def load_create_config(self, config_file_name):
69 """Read and parse configuration file for the test environment.
71 self._log.info("Loading configuration file %s", config_file_name)
72 self._create_config = ConfigParser.RawConfigParser()
74 self._create_config.read(config_file_name)
75 except Exception as e:
76 self._log.error("Failed to read config file!\n%s\n" % e)
79 # Now parse config file content
80 # Parse [DEFAULT] section
81 if self._create_config.has_option("DEFAULT", "total_number_of_pods"):
82 self._total_number_of_pods = self._create_config.getint(
83 "DEFAULT", "total_number_of_pods")
85 self._log.error("No option total_number_of_pods in DEFAULT section")
88 self._log.debug("Total number of pods %d" % self._total_number_of_pods)
90 # Parse [PODx] sections
91 for i in range(1, int(self._total_number_of_pods) + 1):
93 if self._create_config.has_option("POD%d" % i,
95 pod_name = self._create_config.get(
98 pod_name = "pod-rapid-%d" % i
100 # Search for POD hostname
101 if self._create_config.has_option("POD%d" % i,
102 "nodeSelector_hostname"):
103 pod_nodeselector_hostname = self._create_config.get(
104 "POD%d" % i, "nodeSelector_hostname")
106 pod_nodeselector_hostname = None
108 # Search for POD dataplane static IP
109 if self._create_config.has_option("POD%d" % i,
111 pod_dp_ip = self._create_config.get(
112 "POD%d" % i, "dp_ip")
117 pod.set_nodeselector(pod_nodeselector_hostname)
118 pod.set_dp_ip(pod_dp_ip)
121 # Add POD to the list of PODs which need to be created
122 self._pods.append(pod)
126 def create_pods(self):
127 """ Create test PODs and wait for them to start.
128 Collect information for tests to run.
130 self._log.info("Creating PODs...")
132 # Create PODs using template from yaml file
133 for pod in self._pods:
134 self._log.info("Creating POD %s...", pod.get_name())
135 pod.create_from_yaml(K8sDeployment.POD_YAML_TEMPLATE_FILE_NAME)
137 # Wait for PODs to start
138 for pod in self._pods:
141 # Collect information from started PODs for test execution
142 for pod in self._pods:
143 pod.set_ssh_credentials(K8sDeployment.SSH_USER, K8sDeployment.SSH_PRIVATE_KEY)
144 pod.get_sriov_dev_mac()
146 def save_runtime_config(self, config_file_name):
147 self._log.info("Saving config %s for runrapid script...",
149 self._runtime_config = ConfigParser.RawConfigParser()
152 # self._runtime_config.set("DEFAULT",
153 # "total_number_of_test_machines",
154 # self._total_number_of_pods)
157 self._runtime_config.add_section("ssh")
158 self._runtime_config.set("ssh",
160 K8sDeployment.SSH_PRIVATE_KEY)
161 self._runtime_config.set("ssh",
163 K8sDeployment.SSH_USER)
166 self._runtime_config.add_section("rapid")
167 self._runtime_config.set("rapid",
168 "total_number_of_machines",
169 self._total_number_of_pods)
171 # Export information about each pod
173 for pod in self._pods:
174 self._runtime_config.add_section("M%d" % pod.get_id())
175 self._runtime_config.set("M%d" % pod.get_id(),
176 "admin_ip", pod.get_admin_ip())
177 self._runtime_config.set("M%d" % pod.get_id(),
178 "dp_mac1", pod.get_dp_mac())
179 self._runtime_config.set("M%d" % pod.get_id(),
180 "dp_pci_dev", pod.get_dp_pci_dev())
181 self._runtime_config.set("M%d" % pod.get_id(),
182 "dp_ip1", pod.get_dp_ip())
185 self._runtime_config.add_section("Varia")
186 self._runtime_config.set("Varia",
190 # Write runtime config file
191 with open(config_file_name, "w") as file:
192 self._runtime_config.write(file)
194 def delete_pods(self):
195 for pod in self._pods: