Support KSUs based on kubernetes manifests and env var substitution 81/14881/5
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Fri, 17 Jan 2025 00:29:37 +0000 (01:29 +0100)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Mon, 20 Jan 2025 09:25:23 +0000 (10:25 +0100)
Change-Id: I03be2333fee7e4d0973130db504d8f3687763c51
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
osm_lcm/odu_libs/ksu.py

index 767e30b..453b43f 100644 (file)
@@ -64,15 +64,22 @@ async def create_ksus(self, op_id, op_params_list, content_list):
     profile_name = ksu_params.get("profile", {}).get("name")
     age_public_key = ksu_params.get("profile", {}).get("age_pubkey")
     target_ns = oka_params.get("namespace", "default")
-    substitute_environment = oka_params.get("substitute_environment", "false")
-    substitution_filter = oka_params.get("substitution_filter", "")
-    custom_env_vars = oka_params.get("custom_env_vars", "")
-    if custom_env_vars:
-        custom_env_vars = "|\n" + "\n".join(
-            [" " * 10 + f"{k}={v}" for k, v in custom_env_vars.items()]
-        )
-    else:
-        custom_env_vars = '""'
+    substitute_environment = oka_params.get("substitute_environment", "true").lower()
+    custom_env_vars = oka_params.get("custom_env_vars", {})
+    if "APPNAME" not in custom_env_vars:
+        custom_env_vars["APPNAME"] = ksu_name
+    if "TARGET_NS" not in custom_env_vars:
+        custom_env_vars["TARGET_NS"] = target_ns
+    custom_env_vars_str = "|\n"
+    substitution_filter_list = []
+    for k, v in custom_env_vars.items():
+        custom_env_vars_str += " " * 10 + f"{k}={v}\n"
+        substitution_filter_list.append(f"${k}")
+    substitution_filter = ",".join(substitution_filter_list)
+    # TODO: add additional substitution filters
+    # substitution_filter = (
+    #     f"{substitution_filter},{oka_params.get('substitution_filter', '')}".strip(",")
+    # )
     inline_values = oka_params.get("inline_values", "")
     if inline_values:
         yaml_string = yaml.safe_dump(
@@ -133,7 +140,7 @@ async def create_ksus(self, op_id, op_params_list, content_list):
         templates_path=oka_path,
         substitute_environment=substitute_environment,
         substitution_filter=substitution_filter,
-        custom_env_vars=custom_env_vars,
+        custom_env_vars=custom_env_vars_str,
         kustomization_name=kustomization_name,
         helmrelease_name=helmrelease_name,
         inline_values=inline_values,
@@ -205,15 +212,22 @@ async def update_ksus(self, op_id, op_params_list, content_list):
     profile_name = ksu_params.get("profile", {}).get("name")
     age_public_key = ksu_params.get("profile", {}).get("age_pubkey")
     target_ns = oka_params.get("namespace", "default")
-    substitute_environment = oka_params.get("substitute_environment", "false")
-    substitution_filter = oka_params.get("substitution_filter", "")
-    custom_env_vars = oka_params.get("custom_env_vars", "")
-    if custom_env_vars:
-        custom_env_vars = "|\n" + "\n".join(
-            [" " * 10 + f"{k}={v}" for k, v in custom_env_vars.items()]
-        )
-    else:
-        custom_env_vars = '""'
+    substitute_environment = oka_params.get("substitute_environment", "true").lower()
+    custom_env_vars = oka_params.get("custom_env_vars", {})
+    if "APPNAME" not in custom_env_vars:
+        custom_env_vars["APPNAME"] = ksu_name
+    if "TARGET_NS" not in custom_env_vars:
+        custom_env_vars["TARGET_NS"] = target_ns
+    custom_env_vars_str = "|\n"
+    substitution_filter_list = []
+    for k, v in custom_env_vars.items():
+        custom_env_vars_str += " " * 10 + f"{k}={v}\n"
+        substitution_filter_list.append(f"${k}")
+    substitution_filter = ",".join(substitution_filter_list)
+    # TODO: add additional substitution filters
+    # substitution_filter = (
+    #     f"{substitution_filter},{oka_params.get('substitution_filter', '')}".strip(",")
+    # )
     inline_values = oka_params.get("inline_values", "")
     if inline_values:
         yaml_string = yaml.safe_dump(
@@ -273,7 +287,7 @@ async def update_ksus(self, op_id, op_params_list, content_list):
         templates_path=oka_path,
         substitute_environment=substitute_environment,
         substitution_filter=substitution_filter,
-        custom_env_vars=custom_env_vars,
+        custom_env_vars=custom_env_vars_str,
         kustomization_name=kustomization_name,
         helmrelease_name=helmrelease_name,
         inline_values=inline_values,