From 731145842539b9d3c6445b8793794f8c7275b102 Mon Sep 17 00:00:00 2001 From: Emin Aktas Date: Thu, 14 Nov 2024 20:07:04 +0300 Subject: [PATCH] feat: update ApplicationBuilder for improved metadata, parameter compatibility, and env handling - added a new option parameter to set application metadata, enabling metadata specification within the KCLRun file. This addresses a limitation when building with the kcl kubeclt plugin. - renamed `env` to `appenv` and `cluster` to `appcluster` to avoid conflicts with system environment variables, especially when using `env` as a key in options with KCLRun. - modified `ApplicationBuilder` to use `EnvMap` instead of a list of dictionaries for envs, ensuring compatibility with the container env parameter structure. Signed-off-by: Emin Aktas --- models/kube/backend/job_backend.k | 2 +- models/kube/backend/server_backend.k | 2 +- models/kube/metadata/metadata.k | 8 ++++---- models/kube/utils/application_builder.k | 25 ++++++++++++------------- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/models/kube/backend/job_backend.k b/models/kube/backend/job_backend.k index 313d50b..d724644 100644 --- a/models/kube/backend/job_backend.k +++ b/models/kube/backend/job_backend.k @@ -32,7 +32,7 @@ schema JobBackend[inputConfig: frontend.Job]: mainContainerDict = { **config.mainContainer if config.mainContainer.useBuiltInEnv: - env += app.envs + env = config.mainContainer.env | app.envs name = config.mainContainer.name or "main" image = config.image resource = config?.schedulingStrategy?.resource diff --git a/models/kube/backend/server_backend.k b/models/kube/backend/server_backend.k index 0e88988..196cf7e 100644 --- a/models/kube/backend/server_backend.k +++ b/models/kube/backend/server_backend.k @@ -41,7 +41,7 @@ schema ServerBackend[inputConfig: server.Server]: mainContainer = utils.volume_patch(config.volumes, [utils.ContainerFrontend2Kube({ **config.mainContainer if config.mainContainer.useBuiltInEnv: - env += app.envs + env = config.mainContainer.env | app.envs name = config.mainContainer.name or "main" image = config.image resource = config?.schedulingStrategy?.resource diff --git a/models/kube/metadata/metadata.k b/models/kube/metadata/metadata.k index c15bbeb..3227187 100644 --- a/models/kube/metadata/metadata.k +++ b/models/kube/metadata/metadata.k @@ -1,4 +1,4 @@ -__META_APP_NAME = option("app") or "sampleapp" -__META_APP_NAMESPACE = option("appns") or "sampleappns" -__META_ENV_TYPE_NAME = option("env") or "prod" -__META_CLUSTER_NAME = option("cluster") or Undefined +__META_APP_NAME = option("app") or option("params")?.app or "sampleapp" +__META_APP_NAMESPACE = option("appns") or option("params")?.appns or "sampleappns" +__META_ENV_TYPE_NAME = option("appenv") or option("params")?.appenv or "prod" +__META_CLUSTER_NAME = option("appcluster") or option("params")?.appcluster or Undefined diff --git a/models/kube/utils/application_builder.k b/models/kube/utils/application_builder.k index e74d838..3723355 100644 --- a/models/kube/utils/application_builder.k +++ b/models/kube/utils/application_builder.k @@ -1,4 +1,5 @@ import models.kube.metadata +import models.kube.frontend.container.env as e schema ApplicationBuilder: """ApplicationBuilder contains the workload labels, selector and environments about the application @@ -9,26 +10,24 @@ schema ApplicationBuilder: "app.kubernetes.io/name": metadata.__META_APP_NAME "app.kubernetes.io/env": metadata.__META_ENV_TYPE_NAME "app.kubernetes.io/instance": "{}-{}".format(metadata.__META_APP_NAME, metadata.__META_ENV_TYPE_NAME).lower() - "cluster.x-k8s.io/cluster-name": metadata.__META_CLUSTER_NAME + if metadata.__META_CLUSTER_NAME: + "cluster.x-k8s.io/cluster-name": metadata.__META_CLUSTER_NAME } selector: {str:str} = labels - envs: [{str:}] = [ - { - name: "APP_NAME" + envs: e.EnvMap = { + APP_NAME: { value: metadata.__META_APP_NAME } - { - name: "ENVIRONMENT" + ENVIRONMENT: { value: metadata.__META_ENV_TYPE_NAME } - { - name: "INSTANCE" + INSTANCE: { value: "{}-{}".format(metadata.__META_APP_NAME, metadata.__META_ENV_TYPE_NAME).lower() } - { - name: "CLUSTER" - value: metadata.__META_CLUSTER_NAME - } - ] + if metadata.__META_CLUSTER_NAME: + CLUSTER: { + value: metadata.__META_CLUSTER_NAME + } + }