diff --git a/IM/tosca/Tosca.py b/IM/tosca/Tosca.py index e1ba9e74..b5adbc26 100644 --- a/IM/tosca/Tosca.py +++ b/IM/tosca/Tosca.py @@ -35,7 +35,7 @@ class Tosca: """ ARTIFACTS_PATH = os.path.dirname(os.path.realpath(__file__)) + "/tosca-types/artifacts" - ARTIFACTS_REMOTE_REPO = "https://raw.githubusercontent.com/indigo-dc/tosca-types/master/artifacts/" + ARTIFACTS_REMOTE_REPO = "https://raw.githubusercontent.com/grycap/tosca/main/artifacts/" GET_TIMEOUT = 20 logger = logging.getLogger('InfrastructureManager') @@ -2189,9 +2189,9 @@ def _get_oscar_service_json(self, node): return res - def _gen_k8s_volumes(self, node, nodetemplates, value): + def _gen_k8s_volumes(self, node, nodetemplates, value, cont=1): + """Get the volumes attached to an K8s container.""" volumes = [] - cont = 1 # volume format should be "volume_name:mount_path" for vol in value: size = None @@ -2217,18 +2217,44 @@ def _gen_k8s_volumes(self, node, nodetemplates, value): cont += 1 return volumes + def _gen_k8s_configmaps(self, res, cms): + """Get the configmaps attached to an K8s container.""" + cont = 1 + for cm in cms: + mount_path = cm.get("deploy_path") + cm_file = cm.get("file") + content = cm.get("properties", {}).get("content", "") + if content: + res.setValue('disk.%d.content' % cont, content) + # if content is not empty file is ignored + if cm_file and not content: + resp = self.cache_session.get(cm_file, timeout=self.GET_TIMEOUT) + if resp.status_code != 200: + raise Exception("Error downloading file %s: %s\n%s" % (cm_file, resp.reason, resp.text)) + res.setValue('disk.%d.content' % cont, resp.text) + if content or cm_file: + res.setValue('disk.%d.mount_path' % cont, mount_path) + cont += 1 + + return cont + def _gen_k8s_system(self, node, nodetemplates): - """Get the volumes attached to an K8s container.""" + """Generate the system for a K8s app.""" res = system(node.name) nets = [] + cms = [] for artifact in list(self._get_node_artifacts(node).values()): - if artifact.get("type", None) == "tosca.artifacts.Deployment.Image.Container.Docker": - image = self._final_function_result(artifact.get("file", None), node) + if artifact.get("type") == "tosca.artifacts.Deployment.Image.Container.Docker": + image = self._final_function_result(artifact.get("file"), node) + elif artifact.get("type") == "tosca.artifacts.File" and artifact.get("deploy_path"): + cms.append(artifact) if not image: raise Exception("No image specified for K8s container.") + cont = self._gen_k8s_configmaps(res, cms) + repo = artifact.get("repository", None) if repo: repo_url = self._get_repository_url(repo) @@ -2265,7 +2291,7 @@ def _gen_k8s_system(self, node, nodetemplates): value = int(ScalarUnit_Size(value).get_num_from_scalar_unit('B')) res.setValue("memory.size", value, 'B') elif prop.name == 'volumes': - for num, size, mount_path, volume_id in self._gen_k8s_volumes(node, nodetemplates, value): + for num, size, mount_path, volume_id in self._gen_k8s_volumes(node, nodetemplates, value, cont): if volume_id: res.setValue('disk.%d.image.url' % num, volume_id) if size: diff --git a/test/files/tosca_k8s.yml b/test/files/tosca_k8s.yml index 8b588040..eec042db 100644 --- a/test/files/tosca_k8s.yml +++ b/test/files/tosca_k8s.yml @@ -47,6 +47,16 @@ topology_template: my_image: file: grycap/im type: tosca.artifacts.Deployment.Image.Container.Docker + my_config_map: + deploy_path: /etc/im/im.cfg + file: https://raw.githubusercontent.com/grycap/im/master/etc/im.cfg + type: tosca.artifacts.File + properties: + # when the content is not provided, the file is downloaded from the URL + # otherwise, the file is ignored + content: | + [im] + REST_API = True # The properties of the runtime to host the container im_runtime: diff --git a/test/unit/Tosca.py b/test/unit/Tosca.py index 9248c9f3..9c383a36 100755 --- a/test/unit/Tosca.py +++ b/test/unit/Tosca.py @@ -455,6 +455,8 @@ def test_tosca_k8s(self): net = radl.get_network_by_id('im_container_pub') self.assertEqual(net.getValue("outports"), '30880/tcp-8800/tcp') self.assertEqual(net.getValue("outbound"), 'yes') + self.assertEqual(node.getValue("disk.1.content"), '[im]\nREST_API = True\n') + self.assertEqual(node.getValue("disk.1.mount_path"), '/etc/im/im.cfg') self.assertEqual(node.getValue("environment.variables"), 'IM_DATA_DB=mysql://root:my-secret@mysql-container:3306/im-db') self.assertEqual(node.getValue("net_interface.0.connection"), 'im_container_pub')