diff --git a/IM/connectors/Kubernetes.py b/IM/connectors/Kubernetes.py index 95c88cf4..a007c334 100644 --- a/IM/connectors/Kubernetes.py +++ b/IM/connectors/Kubernetes.py @@ -18,6 +18,7 @@ import json import requests import time +import re from netaddr import IPNetwork, IPAddress try: from urlparse import urlparse @@ -282,9 +283,13 @@ def _generate_service_data(self, namespace, name, outports): @staticmethod def _get_env_variables(radl_system): env_vars = [] - for elem in radl_system.getValue("environment.variables", []): - parts = elem.split(":") - env_vars.append({'name': parts[0], 'value': ":".join(parts[1:])}) + if radl_system.getValue('environment.variables'): + keypairs = radl_system.getValue('environment.variables').split(",") + for keypair in keypairs: + parts = keypair.split("=") + key = parts[0].strip() + value = parts[1].strip() + env_vars.append({'name': key, 'value': value}) return env_vars def _generate_pod_data(self, namespace, name, outports, system, volumes, tags): @@ -312,7 +317,8 @@ def _generate_pod_data(self, namespace, name, outports, system, volumes, tags): # Add instance tags if tags: for k, v in tags.items(): - pod_data['metadata']['labels'][k] = v + # Remove special characters + pod_data['metadata']['labels'][k] = re.sub('[!"#$%&\'()*+,/:;<=>?@[\\]^`{|}~]', '', v).lstrip("_-") containers = [{ 'name': name, diff --git a/IM/tosca/Tosca.py b/IM/tosca/Tosca.py index 11d24d12..0c55889a 100644 --- a/IM/tosca/Tosca.py +++ b/IM/tosca/Tosca.py @@ -767,7 +767,7 @@ def _get_artifact_full_uri(self, node, artifact_name): repo = artifact_def['repository'] repo_url = self._get_repository_url(repo) if repo_url: - res = repo_url + "/" + artifact_def['file'] + res = repo_url + "/" + self._final_function_result(artifact_def['file'], node) else: res = artifact_def @@ -2156,7 +2156,7 @@ def _gen_k8s_system(self, node, nodetemplates): raise Exception("Only one artifact is supported for K8s container.") artifact = list(artifacts.values())[0] - image = artifact.get("file", None) + image = self._final_function_result(artifact.get("file", None), node) if not image: raise Exception("No image specified for K8s container.") if "tosca.artifacts.Deployment.Image.Container.Docker" != artifact.get("type", None): @@ -2175,10 +2175,12 @@ def _gen_k8s_system(self, node, nodetemplates): value = self._final_function_result(prop.value, node) if value not in [None, [], {}]: if prop.name == "environment": - env = [] + variables = "" for k, v in value.items(): - env.append("%s:%s" % (k, v)) - res.setValue('environment.variables', env) + if variables != "": + variables += "," + variables += "%s=%s" % (k, v) + res.setValue("environment.variables", variables) runtime = self._find_host_node(node, nodetemplates, base_root_type="tosca.nodes.SoftwareComponent") diff --git a/test/files/tosca_k8s.yml b/test/files/tosca_k8s.yml index b7d191f0..48f57227 100644 --- a/test/files/tosca_k8s.yml +++ b/test/files/tosca_k8s.yml @@ -11,6 +11,13 @@ repositories: topology_template: + inputs: + + image: + type: string + description: The image to be used in the container + default: "mysql:5.7" + node_templates: # The MYSQL container based on official MySQL image in Docker hub @@ -23,7 +30,7 @@ topology_template: - host: mysql_runtime artifacts: my_image: - file: "mysql:5.7" + file: { get_input: image } type: tosca.artifacts.Deployment.Image.Container.Docker repository: docker_hub diff --git a/test/unit/Tosca.py b/test/unit/Tosca.py index 7a8df35b..3160362f 100755 --- a/test/unit/Tosca.py +++ b/test/unit/Tosca.py @@ -440,7 +440,7 @@ def test_tosca_k8s(self): self.assertEqual(node.getValue("memory.size"), 2000000000) self.assertEqual(node.getValue("disk.1.size"), 10000000000) self.assertEqual(node.getValue("disk.1.mount_path"), '/some/path') - self.assertEqual(node.getValue("environment.variables"), ['MYSQL_ROOT_PASSWORD:my-secret']) + self.assertEqual(node.getValue("environment.variables"), 'MYSQL_ROOT_PASSWORD=my-secret') net = radl.get_network_by_id('mysql_container_pub') self.assertEqual(net.getValue("outports"), '33306/tcp-3306/tcp') self.assertEqual(net.getValue("outbound"), 'yes') diff --git a/test/unit/connectors/Kubernetes.py b/test/unit/connectors/Kubernetes.py index 9799c2f7..a6d402a9 100755 --- a/test/unit/connectors/Kubernetes.py +++ b/test/unit/connectors/Kubernetes.py @@ -123,7 +123,8 @@ def test_20_launch(self, save_data, requests): memory.size>=512m and net_interface.0.connection = 'net' and net_interface.0.dns_name = 'test' and - environment.variables = ['var:some_val'] and + environment.variables = 'var=some_val' and + instance_tags = 'key=_inva:lid_' and disk.0.os.name = 'linux' and disk.0.image.url = 'docker://someimage' and disk.1.size = 10G and @@ -163,7 +164,7 @@ def test_20_launch(self, save_data, requests): "metadata": { "name": "test", "namespace": "infid", - "labels": {"name": "test", "IM_INFRA_ID": "infid"}, + "labels": {"name": "test", "IM_INFRA_ID": "infid", "key": "invalid_"}, }, "spec": { "containers": [