From 75c29bb5241a097924d415d4e474608cf42724df Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Fri, 16 Feb 2024 12:35:04 +0100 Subject: [PATCH 1/5] Fix label values --- IM/connectors/Kubernetes.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/IM/connectors/Kubernetes.py b/IM/connectors/Kubernetes.py index 95c88cf4..8e9c7822 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 @@ -312,7 +313,7 @@ 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 + pod_data['metadata']['labels'][k] = re.sub('[!"#$%&\'()*+,/:;<=>?@[\\]^`{|}~]', '', v) containers = [{ 'name': name, From 2c0a28ab58882c87e79de16c56de68c54c7f420b Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Fri, 16 Feb 2024 12:58:19 +0100 Subject: [PATCH 2/5] Fix label values --- IM/connectors/Kubernetes.py | 2 +- test/unit/connectors/Kubernetes.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/IM/connectors/Kubernetes.py b/IM/connectors/Kubernetes.py index 8e9c7822..d5892e6e 100644 --- a/IM/connectors/Kubernetes.py +++ b/IM/connectors/Kubernetes.py @@ -313,7 +313,7 @@ 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] = re.sub('[!"#$%&\'()*+,/:;<=>?@[\\]^`{|}~]', '', v) + pod_data['metadata']['labels'][k] = re.sub('[!"#$%&\'()*+,/:;<=>?@[\\]^`{|}~]', '', v).lstrip("_-") containers = [{ 'name': name, diff --git a/test/unit/connectors/Kubernetes.py b/test/unit/connectors/Kubernetes.py index 9799c2f7..50319fdc 100755 --- a/test/unit/connectors/Kubernetes.py +++ b/test/unit/connectors/Kubernetes.py @@ -124,6 +124,7 @@ def test_20_launch(self, save_data, requests): net_interface.0.connection = 'net' and net_interface.0.dns_name = 'test' 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": [ From 8e36b85c23acec098bc054a04aa3f2148e6579d7 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Fri, 16 Feb 2024 13:15:53 +0100 Subject: [PATCH 3/5] Fix artifacts file functions --- IM/connectors/Kubernetes.py | 1 + IM/tosca/Tosca.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/IM/connectors/Kubernetes.py b/IM/connectors/Kubernetes.py index d5892e6e..162c24bf 100644 --- a/IM/connectors/Kubernetes.py +++ b/IM/connectors/Kubernetes.py @@ -313,6 +313,7 @@ def _generate_pod_data(self, namespace, name, outports, system, volumes, tags): # Add instance tags if tags: for k, v in tags.items(): + # Remove special characters pod_data['metadata']['labels'][k] = re.sub('[!"#$%&\'()*+,/:;<=>?@[\\]^`{|}~]', '', v).lstrip("_-") containers = [{ diff --git a/IM/tosca/Tosca.py b/IM/tosca/Tosca.py index 11d24d12..2ee954eb 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 From fc94e6bb6d60eae0d1984d0dbadbb89493585940 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Fri, 16 Feb 2024 13:42:18 +0100 Subject: [PATCH 4/5] Change k8s env values to string --- IM/connectors/Kubernetes.py | 10 +++++++--- IM/tosca/Tosca.py | 8 +++++--- test/unit/Tosca.py | 2 +- test/unit/connectors/Kubernetes.py | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/IM/connectors/Kubernetes.py b/IM/connectors/Kubernetes.py index 162c24bf..a007c334 100644 --- a/IM/connectors/Kubernetes.py +++ b/IM/connectors/Kubernetes.py @@ -283,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): diff --git a/IM/tosca/Tosca.py b/IM/tosca/Tosca.py index 2ee954eb..139c0f2d 100644 --- a/IM/tosca/Tosca.py +++ b/IM/tosca/Tosca.py @@ -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/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 50319fdc..a6d402a9 100755 --- a/test/unit/connectors/Kubernetes.py +++ b/test/unit/connectors/Kubernetes.py @@ -123,7 +123,7 @@ 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 From 3ef7f0a2862e576e3ff48638f0b9629fa77fc84b Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Fri, 16 Feb 2024 13:53:17 +0100 Subject: [PATCH 5/5] Change k8s env values to string --- IM/tosca/Tosca.py | 2 +- test/files/tosca_k8s.yml | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/IM/tosca/Tosca.py b/IM/tosca/Tosca.py index 139c0f2d..0c55889a 100644 --- a/IM/tosca/Tosca.py +++ b/IM/tosca/Tosca.py @@ -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): 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