From 09319ab5320d49c9d4f716bbcfca1c51dc2eb34a Mon Sep 17 00:00:00 2001 From: suyiiyii Date: Fri, 14 Mar 2025 11:26:43 +0800 Subject: [PATCH] Add environment injection to deployment and enhance app rendering --- base/deploy.k | 1 + base/env.k | 16 ++++++++++++++++ main.k | 6 ++++-- tmp/build-errors.log | 2 +- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/base/deploy.k b/base/deploy.k index a8f58e3..928c913 100644 --- a/base/deploy.k +++ b/base/deploy.k @@ -9,6 +9,7 @@ schema App: port: int appRender = lambda a: App { + """Render app to Deployment and Service""" deployment = appsv1.Deployment { apiVersion = "apps/v1" kind = "Deployment" diff --git a/base/env.k b/base/env.k index a143a0f..37da848 100644 --- a/base/env.k +++ b/base/env.k @@ -1,3 +1,4 @@ +import k8s.api.apps.v1 as appsv1 import k8s.api.core.v1 as corev1 schema Envs: @@ -15,3 +16,18 @@ envSecretRender = lambda e: Envs { } secret } + +envInjector = lambda envs: Envs, deploy: appsv1.Deployment { + """Inject envs to Deployment""" + _result: [appsv1.Deployment] = [] + if deploy.kind != "Deployment": + _result = [deploy] + else: + secretEnv = [{ + name = k + secret = v + } for k, v in envs.envs] + deploy.spec.template.spec.containers[0].env = secretEnv + _result = [deploy] + _result +} diff --git a/main.k b/main.k index 8153025..d980463 100644 --- a/main.k +++ b/main.k @@ -15,15 +15,17 @@ base.App { port = 8080 } -base.Envs { +envs = base.Envs { envs = { "key1": "value1" "key2": "value2" } } -apps_manifests = [base.appRender(a) for a in base.App.instances()] +apps_manifests = [base.envInjector(envs,base.appRender(a)) for a in base.App.instances()] env_manifests = [base.envSecretRender(a) for a in base.Envs.instances()] + + # base.appRender(a) manifests.yaml_stream([apps_manifests, env_manifests]) diff --git a/tmp/build-errors.log b/tmp/build-errors.log index e412141..707aa92 100644 --- a/tmp/build-errors.log +++ b/tmp/build-errors.log @@ -1 +1 @@ -exit status 1exit status 2exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1 \ No newline at end of file +exit status 1exit status 2exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1 \ No newline at end of file