Allow a different pod spec for each pod 07/73807/2
authorLuc Provoost <luc.provoost@gmail.com>
Tue, 17 Jan 2023 11:23:58 +0000 (12:23 +0100)
committerLuc Provoost <luc.provoost@gmail.com>
Tue, 17 Jan 2023 17:45:13 +0000 (17:45 +0000)
A new parameter is now introduced in the [PODx] sections of the
rapid.pods file. This parameter specifies the pod spec yaml file that
will be used to create this specif pod. When this parameter is not
available, the default pod-rapid.yaml file will be used. The parameter
name is spec_file_name.
Also cleaned some lines of code exceeding 80 characters.

Signed-off-by: Luc Provoost <luc.provoost@gmail.com>
Change-Id: Id26881517d45baf4bf6be2e82b4f9e049f2b2547

VNFs/DPPD-PROX/helper-scripts/rapid/rapid_k8s_deployment.py
VNFs/DPPD-PROX/helper-scripts/rapid/rapid_k8s_pod.py
VNFs/DPPD-PROX/helper-scripts/rapid/rapid_machine.py

index bfb8161..9ef7ef6 100644 (file)
@@ -121,6 +121,14 @@ class K8sDeployment:
             else:
                 pod_nodeselector_hostname = None
 
+            # Search for POD spec
+            if self._create_config.has_option("POD%d" % i,
+                                              "spec_file_name"):
+                pod_spec_file_name = self._create_config.get(
+                    "POD%d" % i, "spec_file_name")
+            else:
+                pod_spec_file_name = K8sDeployment.POD_YAML_TEMPLATE_FILE_NAME
+
             # Search for POD dataplane static IP
             if self._create_config.has_option("POD%d" % i,
                                               "dp_ip"):
@@ -139,6 +147,7 @@ class K8sDeployment:
 
             pod = Pod(pod_name, self._namespace)
             pod.set_nodeselector(pod_nodeselector_hostname)
+            pod.set_spec_file_name(pod_spec_file_name)
             pod.set_dp_ip(pod_dp_ip)
             pod.set_dp_subnet(pod_dp_subnet)
             pod.set_id(i)
@@ -157,7 +166,7 @@ class K8sDeployment:
         # Create PODs using template from yaml file
         for pod in self._pods:
             self._log.info("Creating POD %s...", pod.get_name())
-            pod.create_from_yaml(K8sDeployment.POD_YAML_TEMPLATE_FILE_NAME)
+            pod.create_from_yaml()
 
         # Wait for PODs to start
         for pod in self._pods:
index d15fe7f..3a90dcb 100644 (file)
@@ -32,6 +32,7 @@ class Pod:
     _name = "pod"
     _namespace = "default"
     _nodeSelector_hostname = None
+    _spec_filename = None
     _last_status = None
     _id = None
     _admin_ip = None
@@ -56,10 +57,11 @@ class Pod:
         if self._ssh_client is not None:
             self._ssh_client.disconnect()
 
-    def create_from_yaml(self, file_name):
+    def create_from_yaml(self):
         """Load POD description from yaml file.
         """
-        with open(path.join(path.dirname(__file__), file_name)) as yaml_file:
+        with open(path.join(path.dirname(__file__),
+            self._spec_filename)) as yaml_file:
             self.body = yaml.safe_load(yaml_file)
 
             self.body["metadata"]["name"] = self._name
@@ -67,14 +69,16 @@ class Pod:
             if (self._nodeSelector_hostname is not None):
                 if ("nodeSelector" not in self.body["spec"]):
                     self.body["spec"]["nodeSelector"] = {}
-                self.body["spec"]["nodeSelector"]["kubernetes.io/hostname"] = self._nodeSelector_hostname
+                self.body["spec"]["nodeSelector"]["kubernetes.io/hostname"] = \
+                        self._nodeSelector_hostname
             self._log.debug("Creating POD, body:\n%s" % self.body)
 
             try:
                 self.k8s_CoreV1Api.create_namespaced_pod(body = self.body,
                                                 namespace = self._namespace)
             except client.rest.ApiException as e:
-                self._log.error("Couldn't create POD %s!\n%s\n" % (self._name, e))
+                self._log.error("Couldn't create POD %s!\n%s\n" % (self._name,
+                    e))
 
     def terminate(self):
         """Terminate POD. Close SSH connection.
@@ -204,6 +208,11 @@ class Pod:
         """
         self._nodeSelector_hostname = hostname
 
+    def set_spec_file_name(self, file_name):
+        """Set pod spec filename.
+        """
+        self._spec_filename = file_name
+
     def set_ssh_credentials(self, user, rsa_private_key):
         """Set SSH credentials for the SSH connection to the POD.
         """
index 352561d..f00f916 100644 (file)
@@ -43,8 +43,7 @@ class RapidMachine(object):
             mac_key = 'dp_mac{}'.format(index)
             if ip_key in machine_params.keys():
                 if mac_key in machine_params.keys():
-                    dp_port = {'ip': machine_params[ip_key],
-                            'mac' : machine_params[mac_key]}
+                    dp_port = {'ip': machine_params[ip_key], 'mac' : machine_params[mac_key]}
                 else:
                     dp_port = {'ip': machine_params[ip_key], 'mac' : None}
                 self.dp_ports.append(dict(dp_port))
@@ -59,8 +58,7 @@ class RapidMachine(object):
             PROXConfigfile =  open (self.machine_params['config_file'], 'r')
             PROXConfig = PROXConfigfile.read()
             PROXConfigfile.close()
-            self.all_tasks_for_this_cfg = set(re.findall("task\s*=\s*(\d+)",
-                PROXConfig))
+            self.all_tasks_for_this_cfg = set(re.findall("task\s*=\s*(\d+)",PROXConfig))
 
     def get_cores(self):
         return (self.machine_params['cores'])
@@ -94,19 +92,16 @@ class RapidMachine(object):
             else:
                 RapidLog.critical('{Cannot determine cpuset')
         cpuset_cpus = self._client.run_cmd(cmd).decode().rstrip()
-        RapidLog.debug('{} ({}): Allocated cpuset: {}'.format(self.name,
-            self.ip, cpuset_cpus))
+        RapidLog.debug('{} ({}): Allocated cpuset: {}'.format(self.name, self.ip, cpuset_cpus))
         self.cpu_mapping = self.expand_list_format(cpuset_cpus)
-        RapidLog.debug('{} ({}): Expanded cpuset: {}'.format(self.name,
-            self.ip, self.cpu_mapping))
+        RapidLog.debug('{} ({}): Expanded cpuset: {}'.format(self.name, self.ip, self.cpu_mapping))
 
         # Log CPU core mapping for user information
         cpu_mapping_str = ''
         for i in range(len(self.cpu_mapping)):
             cpu_mapping_str = cpu_mapping_str + '[' + str(i) + '->' + str(self.cpu_mapping[i]) + '], '
         cpu_mapping_str = cpu_mapping_str[:-2]
-        RapidLog.debug('{} ({}): CPU mapping: {}'.format(self.name, self.ip,
-            cpu_mapping_str))
+        RapidLog.debug('{} ({}): CPU mapping: {}'.format(self.name, self.ip, cpu_mapping_str))
 
     def remap_cpus(self, cpus):
         """Convert relative cpu ids provided as function parameter to match
@@ -126,14 +121,12 @@ class RapidMachine(object):
 
         if 'mcore' in self.machine_params.keys():
             cpus_remapped = self.remap_cpus(self.machine_params['mcore'])
-            RapidLog.debug('{} ({}): mcore {} remapped to {}'.format(self.name,
-                self.ip, self.machine_params['mcore'], cpus_remapped))
+            RapidLog.debug('{} ({}): mcore {} remapped to {}'.format(self.name, self.ip, self.machine_params['mcore'], cpus_remapped))
             self.machine_params['mcore'] = cpus_remapped
 
         if 'cores' in self.machine_params.keys():
             cpus_remapped = self.remap_cpus(self.machine_params['cores'])
-            RapidLog.debug('{} ({}): cores {} remapped to {}'.format(self.name,
-                self.ip, self.machine_params['cores'], cpus_remapped))
+            RapidLog.debug('{} ({}): cores {} remapped to {}'.format(self.name, self.ip, self.machine_params['cores'], cpus_remapped))
             self.machine_params['cores'] = cpus_remapped
 
     def devbind(self):
@@ -170,17 +163,20 @@ class RapidMachine(object):
             else:
                 LuaFile.write("eal=\"\"\n")
             if 'mcore' in self.machine_params.keys():
-                LuaFile.write('mcore="%s"\n'% ','.join(map(str, self.machine_params['mcore'])))
+                LuaFile.write('mcore="%s"\n'% ','.join(map(str,
+                    self.machine_params['mcore'])))
             if 'cores' in self.machine_params.keys():
-                LuaFile.write('cores="%s"\n'% ','.join(map(str, self.machine_params['cores'])))
+                LuaFile.write('cores="%s"\n'% ','.join(map(str,
+                    self.machine_params['cores'])))
             if 'ports' in self.machine_params.keys():
-                LuaFile.write('ports="%s"\n'% ','.join(map(str, self.machine_params['ports'])))
+                LuaFile.write('ports="%s"\n'% ','.join(map(str,
+                    self.machine_params['ports'])))
             if 'dest_ports' in self.machine_params.keys():
                 for index, dest_port in enumerate(self.machine_params['dest_ports'], start = 1):
                     LuaFile.write('dest_ip{}="{}"\n'.format(index, dest_port['ip']))
                     LuaFile.write('dest_hex_ip{}=convertIPToHex(dest_ip{})\n'.format(index, index))
                     if dest_port['mac']:
-                        LuaFile.write('dest_hex_mac{}="{}"\n'.format(index,
+                        LuaFile.write('dest_hex_mac{}="{}"\n'.format(index ,
                             dest_port['mac'].replace(':',' ')))
             if 'gw_vm' in self.machine_params.keys():
                 for index, gw_ip in enumerate(self.machine_params['gw_ips'],