Skip to content

Commit

Permalink
Merge pull request #1618 from grycap/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
micafer authored Oct 18, 2024
2 parents c066a9f + e2de8a1 commit f377d91
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
17 changes: 10 additions & 7 deletions IM/connectors/Kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,9 @@ def _generate_ingress_data(self, namespace, name, dns, port, apps_dns):
if dns_url[1]:
host = dns_url[1]
if apps_dns and not host.endswith(apps_dns):
host += "." + apps_dns
if not host.endswith(".") and not apps_dns.startswith("."):
host += "."
host += apps_dns
if dns_url[2]:
path = dns_url[2]

Expand Down Expand Up @@ -422,12 +424,13 @@ def _gen_basic_k8s_elem(namespace, name, kind, version="v1"):
def _get_env_variables(radl_system):
env_vars = []
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})
# Parse the environment variables
# The pattern is: key="value" or key=value
# in case of value with commas it should be enclosed in double quotes
pattern = r'([^,=]+)=(".*?(?<!\\)"|[^,]*)'
keypairs = re.findall(pattern, radl_system.getValue('environment.variables'))
for key, value in keypairs:
env_vars.append({'name': key.strip(), 'value': value.strip(' "')})
return env_vars

def _generate_pod_data(self, namespace, name, outports, system, volumes, configmaps, tags):
Expand Down
8 changes: 5 additions & 3 deletions IM/tosca/Tosca.py
Original file line number Diff line number Diff line change
Expand Up @@ -2150,13 +2150,13 @@ def _gen_k8s_volumes(self, node, nodetemplates, value, cont=1):
cont += 1
return volumes

def _gen_k8s_configmaps(self, res, cms):
def _gen_k8s_configmaps(self, res, cms, node):
"""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", "")
content = self._final_function_result(cm.get("properties", {}).get("content", ""), node)
if content:
res.setValue('disk.%d.content' % cont, content)
# if content is not empty file is ignored
Expand Down Expand Up @@ -2186,7 +2186,7 @@ def _gen_k8s_system(self, node, nodetemplates):
if not image:
raise Exception("No image specified for K8s container.")

cont = self._gen_k8s_configmaps(res, cms)
cont = self._gen_k8s_configmaps(res, cms, node)

repo = artifact.get("repository", None)
if repo:
Expand All @@ -2206,6 +2206,8 @@ def _gen_k8s_system(self, node, nodetemplates):
for k, v in value.items():
if variables != "":
variables += ","
if ',' in v:
v = '"%s"' % v
variables += "%s=%s" % (k, v)
res.setValue("environment.variables", variables)
elif prop.name == "command":
Expand Down
3 changes: 3 additions & 0 deletions doc/source/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ The available keys are:

* ``namespace`` indicates a namespace name to be associated to the Kubernetes credential (from version 1.7.1).

* ``apps_dns`` indicates a DNS domain used by the Kubernetes provider to expose application URLs.
(from version 1.7.1).

Vault Credentials support
^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
10 changes: 7 additions & 3 deletions test/files/tosca_k8s.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ topology_template:
# when the content is not provided, the file is downloaded from the URL
# otherwise, the file is ignored
# If the content is base64 encoded, it is assumed to be a K8s Secret
content: |
[im]
REST_API = True
content:
concat:
- |-
[im]
REST_API =
- "True"
my_secret:
deploy_path: /etc/secret
type: tosca.artifacts.File
Expand Down Expand Up @@ -86,6 +89,7 @@ topology_template:
environment:
MYSQL_ROOT_PASSWORD: { get_input: mysql_root_password }
MYSQL_DATABASE: "im-db"
TEST: "some,value"
requirements:
- host: mysql_runtime
artifacts:
Expand Down
5 changes: 3 additions & 2 deletions test/unit/Tosca.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ def test_tosca_k8s(self):
self.assertEqual(node.getValue("memory.size"), 1000000000)
self.assertEqual(node.getValue("disk.1.size"), 10000000000)
self.assertEqual(node.getValue("disk.1.mount_path"), '/var/lib/mysql')
self.assertEqual(node.getValue("environment.variables"), 'MYSQL_ROOT_PASSWORD=my-secret,MYSQL_DATABASE=im-db')
self.assertEqual(node.getValue("environment.variables"),
'MYSQL_ROOT_PASSWORD=my-secret,MYSQL_DATABASE=im-db,TEST="some,value"')
self.assertEqual(node.getValue("net_interface.0.connection"), 'mysql_container_priv')
self.assertIsNone(node.getValue("net_interface.1.connection"))
net = radl.get_network_by_id('mysql_container_priv')
Expand All @@ -457,7 +458,7 @@ 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.content"), '[im]\nREST_API = True')
self.assertEqual(node.getValue("disk.1.mount_path"), '/etc/im/im.cfg')
self.assertEqual(node.getValue("disk.2.content"), 'c29tZSBlbmNvZGVkIGNvbnRlbnQ=')
self.assertEqual(node.getValue("disk.2.mount_path"), '/etc/secret')
Expand Down
5 changes: 3 additions & 2 deletions test/unit/connectors/Kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_20_launch(self, save_data, requests):
memory.size>=512m and
net_interface.0.connection = 'net' and
net_interface.0.dns_name = 'https://ingress.domain.com/path' and
environment.variables = 'var=some_val' and
environment.variables = 'var=some_val,var2="some,val2"' and
instance_tags = 'key=_inva:lid_' and
disk.0.os.name = 'linux' and
disk.0.image.url = 'docker://someimage' and
Expand Down Expand Up @@ -240,7 +240,8 @@ def test_20_launch(self, save_data, requests):
"limits": {"cpu": "1", "memory": "536870912"},
"requests": {"cpu": "1", "memory": "536870912"},
},
"env": [{"name": "var", "value": "some_val"}],
"env": [{"name": "var", "value": "some_val"},
{"name": "var2", "value": "some,val2"}],
"volumeMounts": [{"name": "test-1", "mountPath": "/mnt"},
{'mountPath': '/etc/config', 'name': 'test-cm-2',
'readOnly': True, 'subPath': 'config'},
Expand Down

0 comments on commit f377d91

Please sign in to comment.