Skip to content

Commit

Permalink
Improve k8s with configmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Feb 23, 2024
1 parent 5ed2eea commit 42e2eb2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
40 changes: 33 additions & 7 deletions IM/tosca/Tosca.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions test/files/tosca_k8s.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions test/unit/Tosca.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 42e2eb2

Please sign in to comment.