Skip to content

Commit

Permalink
Merge pull request #1528 from grycap/new_k8s
Browse files Browse the repository at this point in the history
Fix label values
  • Loading branch information
micafer authored Feb 19, 2024
2 parents 4253219 + 3ef7f0a commit a15b8c5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
14 changes: 10 additions & 4 deletions IM/connectors/Kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import json
import requests
import time
import re
from netaddr import IPNetwork, IPAddress
try:
from urlparse import urlparse
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 7 additions & 5 deletions IM/tosca/Tosca.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -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")

Expand Down
9 changes: 8 additions & 1 deletion test/files/tosca_k8s.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/unit/Tosca.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
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 @@ -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
Expand Down Expand Up @@ -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": [
Expand Down

0 comments on commit a15b8c5

Please sign in to comment.