Fix values parsing in KSU workflows 00/14600/6
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Wed, 25 Sep 2024 10:15:40 +0000 (12:15 +0200)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Thu, 3 Oct 2024 08:56:46 +0000 (10:56 +0200)
Change-Id: I074f7e9e4e37d664f70aa3771316756314df95a7
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
osm_lcm/odu_libs/ksu.py
osm_lcm/odu_libs/templates/launcher-create-ksu-hr.j2
osm_lcm/odu_libs/templates/launcher-update-ksu-hr.j2

index 1f43649..0326264 100644 (file)
 import yaml
 
 
+MAP_PROFILE = {
+    "infra_controller_profiles": "infra-controllers",
+    "infra_config_profiles": "infra-controllers",
+    "resource_profiles": "managed_resources",
+    "app_profiles": "apps",
+}
+
+
 async def create_ksus(self, op_id, op_params_list, content_list):
     self.logger.info("Create KSU workflow Enter")
     self.logger.info(
@@ -34,7 +42,13 @@ async def create_ksus(self, op_id, op_params_list, content_list):
         raise Exception(
             "There is no ODU workflow yet able to manage multiple OKAs for a KSU"
         )
-    oka_path = oka_list[0]["sw_catalog_path"]
+    oka_item = oka_list[0]
+    if "sw_catalog_path" in oka_item:
+        oka_path = oka_item["sw_catalog_path"]
+    else:
+        oka_type = "infra-controllers"
+        oka_name = oka_item["git_name"]
+        oka_path = f"{oka_type}/{oka_name}/templates"
 
     workflow_template = "launcher-create-ksu-hr.j2"
     workflow_name = f"create-ksus-{op_id}"
@@ -44,8 +58,9 @@ async def create_ksus(self, op_id, op_params_list, content_list):
     osm_project_name = "osm_admin"  # TODO: get project name from db_ksu
     kustomization_name = ksu_name
     helmrelease_name = ksu_name
-    target_ns = ksu_params.get("namespace")
+    target_ns = ksu_params.get("namespace", "default")
     profile_type = ksu_params.get("profile", {}).get("profile_type")
+    profile_type = MAP_PROFILE[profile_type]
     profile_name = ksu_params.get("profile", {}).get("name")
     age_public_key = ksu_params.get("profile", {}).get("age_pubkey")
     substitute_environment = ksu_params.get("substitute_environment", "false")
@@ -53,8 +68,10 @@ async def create_ksus(self, op_id, op_params_list, content_list):
     custom_env_vars = ksu_params.get("custom_env_vars", "")
     if custom_env_vars:
         custom_env_vars = "|\n" + "\n".join(
-            [" " * 12 + f"{k}={v}" for k, v in custom_env_vars.items()]
+            [" " * 10 + f"{k}={v}" for k, v in custom_env_vars.items()]
         )
+    else:
+        custom_env_vars = '""'
     inline_values = ksu_params.get("inline_values", "")
     if inline_values:
         yaml_string = yaml.safe_dump(
@@ -63,16 +80,23 @@ async def create_ksus(self, op_id, op_params_list, content_list):
         inline_values = "|\n" + "\n".join(
             [" " * 8 + line for line in yaml_string.splitlines()]
         )
+    else:
+        inline_values = '""'
     is_preexisting_cm = "false"
-    values_configmap_name = f"cm-{ksu_name}"
     cm_values = ksu_params.get("configmap_values", "")
     if cm_values:
         yaml_string = yaml.safe_dump(
             cm_values, sort_keys=False, default_flow_style=False
         )
-        custom_env_vars = "|\n" + "\n".join(
+        cm_values = "|\n" + "\n".join(
             [" " * 8 + line for line in yaml_string.splitlines()]
         )
+        values_configmap_name = f"cm-{ksu_name}"
+        cm_key = "values.yaml"
+    else:
+        values_configmap_name = ""
+        cm_key = ""
+        cm_values = '""'
     is_preexisting_secret = "false"
     secret_values = ksu_params.get("secret_values", "")
     if secret_values:
@@ -84,8 +108,8 @@ async def create_ksus(self, op_id, op_params_list, content_list):
         )
     else:
         values_secret_name = ""
-        reference_secret_for_values = ""
-        reference_key_for_values = ""
+        reference_secret_for_values = "this-secret-does-not-exist"
+        reference_key_for_values = "this-key-does-not-exist"
     sync = "true"
 
     if secret_values:
@@ -120,6 +144,7 @@ async def create_ksus(self, op_id, op_params_list, content_list):
         reference_key_for_values=reference_key_for_values,
         is_preexisting_cm=is_preexisting_cm,
         values_configmap_name=values_configmap_name,
+        cm_key=cm_key,
         cm_values=cm_values,
         ksu_name=ksu_name,
         profile_name=profile_name,
@@ -167,8 +192,9 @@ async def update_ksus(self, op_id, op_params_list, content_list):
     osm_project_name = "osm_admin"  # TODO: get project name from db_ksu
     kustomization_name = ksu_name
     helmrelease_name = ksu_name
-    target_ns = ksu_params.get("namespace")
+    target_ns = ksu_params.get("namespace", "default")
     profile_type = ksu_params.get("profile", {}).get("profile_type")
+    profile_type = MAP_PROFILE[profile_type]
     profile_name = ksu_params.get("profile", {}).get("name")
     age_public_key = ksu_params.get("profile", {}).get("age_pubkey")
     substitute_environment = ksu_params.get("substitute_environment", "false")
@@ -176,8 +202,10 @@ async def update_ksus(self, op_id, op_params_list, content_list):
     custom_env_vars = ksu_params.get("custom_env_vars", "")
     if custom_env_vars:
         custom_env_vars = "|\n" + "\n".join(
-            [" " * 12 + f"{k}={v}" for k, v in custom_env_vars.items()]
+            [" " * 10 + f"{k}={v}" for k, v in custom_env_vars.items()]
         )
+    else:
+        custom_env_vars = '""'
     inline_values = ksu_params.get("inline_values", "")
     if inline_values:
         yaml_string = yaml.safe_dump(
@@ -186,16 +214,23 @@ async def update_ksus(self, op_id, op_params_list, content_list):
         inline_values = "|\n" + "\n".join(
             [" " * 8 + line for line in yaml_string.splitlines()]
         )
+    else:
+        inline_values = '""'
     is_preexisting_cm = "false"
-    values_configmap_name = f"cm-{ksu_name}"
     cm_values = ksu_params.get("configmap_values", "")
     if cm_values:
         yaml_string = yaml.safe_dump(
             cm_values, sort_keys=False, default_flow_style=False
         )
-        custom_env_vars = "|\n" + "\n".join(
+        cm_values = "|\n" + "\n".join(
             [" " * 8 + line for line in yaml_string.splitlines()]
         )
+        values_configmap_name = f"cm-{ksu_name}"
+        cm_key = "values.yaml"
+    else:
+        values_configmap_name = ""
+        cm_key = ""
+        cm_values = '""'
     is_preexisting_secret = "false"
     secret_values = ksu_params.get("secret_values", "")
     if secret_values:
@@ -207,8 +242,8 @@ async def update_ksus(self, op_id, op_params_list, content_list):
         )
     else:
         values_secret_name = ""
-        reference_secret_for_values = ""
-        reference_key_for_values = ""
+        reference_secret_for_values = "this-secret-does-not-exist"
+        reference_key_for_values = "this-key-does-not-exist"
 
     if secret_values:
         secret_namespace = "osm-workflows"
@@ -242,6 +277,7 @@ async def update_ksus(self, op_id, op_params_list, content_list):
         reference_key_for_values=reference_key_for_values,
         is_preexisting_cm=is_preexisting_cm,
         values_configmap_name=values_configmap_name,
+        cm_key=cm_key,
         cm_values=cm_values,
         ksu_name=ksu_name,
         profile_name=profile_name,
@@ -282,6 +318,7 @@ async def delete_ksus(self, op_id, op_params_list, content_list):
     osm_project_name = "osm_admin"  # TODO: get project name from db_ksu
     profile_name = ksu_params.get("profile", {}).get("name")
     profile_type = ksu_params.get("profile", {}).get("profile_type")
+    profile_type = MAP_PROFILE[profile_type]
 
     # Render workflow
     manifest = self.render_jinja_template(
index 09a56b8..207dfef 100644 (file)
@@ -47,7 +47,7 @@ spec:
       value: "{{ substitution_filter }}"
     ## Custom environment variables (formatted as .env), to be used for template parametrization
     - name: custom_env_vars
-      value: "{{ custom_env_vars }}"
+      value: {{ custom_env_vars }}
       # value: |
       #     KEY1=value1
       #     KEY2=value2
@@ -86,11 +86,11 @@ spec:
       value: "{{ reference_key_for_values }}"
     # Specific parameters - Configmap generation
     - name: is_preexisting_cm
-      value: "{is_preexisting_cm}"
+      value: "{{ is_preexisting_cm }}"
     - name: values_cm_name
       value: "{{ values_configmap_name }}"
     - name: cm_key
-      value: "values.yaml"
+      value: "{{ cm_key }}"
     - name: cm_values
       value: {{ cm_values }}
       # value: |
index bbd05ff..2accba7 100644 (file)
@@ -47,7 +47,7 @@ spec:
       value: "{{ substitution_filter }}"
     ## Custom environment variables (formatted as .env), to be used for template parametrization
     - name: custom_env_vars
-      value: "{custom_env_vars}"
+      value: {{ custom_env_vars }}
     # Specific parameters - Patch HelmRelease in KSU with inline values
     - name: kustomization_name
       value: "{{ kustomization_name }}"
@@ -72,13 +72,13 @@ spec:
       value: "{{ reference_key_for_values }}"
     # Specific parameters - Configmap generation
     - name: is_preexisting_cm
-      value: "false"
+      value: "{{ is_preexisting_cm }}"
     - name: values_cm_name
       value: "{{ values_configmap_name }}"
     - name: cm_key
-      value: "values.yaml"
+      value: "{{ cm_key }}"
     - name: cm_values
-      value: "{{ cm_values }}"
+      value: {{ cm_values }}
     # Specific parameters - KSU rendering
     - name: ksu_name
       value: "{{ ksu_name }}"