From a713f5488c9132c3406c84c9ee393f7cfce4a6f9 Mon Sep 17 00:00:00 2001 From: Emin Aktas Date: Thu, 17 Oct 2024 21:45:55 +0300 Subject: [PATCH] fix(namespaces): unify namespace settings and improve naming readability - Standardized namespace configurations across all workloads for consistency. - Resolved issue where some workloads did not allow setting a namespace. - Updated the naming template to include hyphens (`-`) for better readability. These changes enhance deployment consistency, prevent namespace misconfigurations, and improve clarity in resource names. Signed-off-by: Emin Aktas --- docs/konfig.md | 2 +- models/kube/backend/job_backend.k | 4 +++- models/kube/backend/server_backend.k | 5 ++++- models/kube/frontend/job.k | 7 +++++++ models/kube/frontend/server.k | 8 ++++++++ models/kube/metadata/metadata.k | 1 + models/kube/mixins/configmap_mixin.k | 3 ++- models/kube/mixins/ingress_mixin.k | 3 ++- models/kube/mixins/namespace_mixin.k | 2 +- models/kube/mixins/secret_mixin.k | 3 ++- models/kube/mixins/service_mixin.k | 3 ++- models/kube/mixins/serviceaccount_mixin.k | 1 + models/kube/utils/metadata_builder.k | 2 +- 13 files changed, 35 insertions(+), 9 deletions(-) diff --git a/docs/konfig.md b/docs/konfig.md index 6e81ddf..b3b4d95 100644 --- a/docs/konfig.md +++ b/docs/konfig.md @@ -160,7 +160,7 @@ ServerBackend converts the user-written front-end model `Server` into a collecti |**provider**|[]||| |**sidecarContainers**|[{str:}]||| |**workloadAttributes** `required`|{str:}||{
metadata = utils.MetadataBuilder(config) \| {
name = workloadName
}
spec = {
replicas = config.replicas
if config.useBuiltInSelector:
selector: {matchLabels: app.selector \| config.selector \| _applicationLabel}
else:
selector: {matchLabels: config.selector}
template = {
metadata = {
if config.useBuiltInLabels:
labels = app.labels \| _applicationLabel

**config.podMetadata
}
spec = {
containers = [mainContainer] + (sidecarContainers or [])
initContainers = initContainers
if config.volumes:
volumes = [utils.to_kube_volume(v) for v in config.volumes if v.volumeSource]

if config.serviceAccount:
serviceAccountName = config.serviceAccount.name

}
}
}
}| -|**workloadName** `required`|str||config.name or "{}{}".format(metadata.__META_APP_NAME, metadata.__META_ENV_TYPE_NAME).lower()| +|**workloadName** `required`|str||config.name or "{}-{}".format(metadata.__META_APP_NAME, metadata.__META_ENV_TYPE_NAME).lower()| ### Job Job is the common user interface for one-time jobs, which is defined by Kubernetes Job. Job supports reliable parallel execution of Pods. diff --git a/models/kube/backend/job_backend.k b/models/kube/backend/job_backend.k index 9480092..313d50b 100644 --- a/models/kube/backend/job_backend.k +++ b/models/kube/backend/job_backend.k @@ -17,7 +17,8 @@ schema JobBackend[inputConfig: frontend.Job]: config: frontend.Job = inputConfig # variables - jobName: str = "{}-{}".format(metadata.__META_APP_NAME, metadata.__META_ENV_TYPE_NAME).lower() + jobName: str = config.name or "{}-{}".format(metadata.__META_APP_NAME, metadata.__META_ENV_TYPE_NAME).lower() + jobNamespace: str = config.namespace or "{}-{}".format(metadata.__META_APP_NAMESPACE, metadata.__META_ENV_TYPE_NAME).lower() app: utils.ApplicationBuilder = utils.ApplicationBuilder {} mainContainerDict: {str:} mainContainer: {str:} @@ -48,6 +49,7 @@ schema JobBackend[inputConfig: frontend.Job]: jobAttrs: {str:} = { metadata = utils.MetadataBuilder(config) | { name = jobName + namespace = jobNamespace } spec = { activeDeadlineSeconds = config.activeDeadlineSeconds diff --git a/models/kube/backend/server_backend.k b/models/kube/backend/server_backend.k index 7676dcb..2f374e6 100644 --- a/models/kube/backend/server_backend.k +++ b/models/kube/backend/server_backend.k @@ -24,7 +24,9 @@ schema ServerBackend[inputConfig: server.Server]: # Store the input config parameter, ensure it can be seen in protocol and mixin. config: server.Server = inputConfig # Workload name. - workloadName: str = config.name or "{}{}".format(metadata.__META_APP_NAME, metadata.__META_ENV_TYPE_NAME).lower() + workloadName: str = config.name or "{}-{}".format(metadata.__META_APP_NAME, metadata.__META_ENV_TYPE_NAME).lower() + # Workload namespace + workloadNamespace: str = config.namespace or "{}-{}".format(metadata.__META_APP_NAMESPACE, metadata.__META_ENV_TYPE_NAME).lower() # App variable contains labels, selector and environments. app: utils.ApplicationBuilder = utils.ApplicationBuilder {} # Main containers and sidecar contrainers. @@ -58,6 +60,7 @@ schema ServerBackend[inputConfig: server.Server]: workloadAttributes: {str:} = { metadata = utils.MetadataBuilder(config) | { name = workloadName + namespace = workloadNamespace } spec = { replicas = config.replicas diff --git a/models/kube/frontend/job.k b/models/kube/frontend/job.k index e214b84..c19af52 100644 --- a/models/kube/frontend/job.k +++ b/models/kube/frontend/job.k @@ -18,6 +18,11 @@ schema Job: If not defined, a generated name ("{__META_APP_NAME}-{__META_ENV_TYPE_NAME}") will be used. The value of metadata.__META_APP_NAME will be extracted from the value of the "name" defined through option("app"), and the value of __META_ENV_TYPE_NAME will be extracted from the value of the "name" defined through option("env"). + namespace: str, default is Undefined, optional. + The namespace of the workload and service. + If not defined, a generated name ("{__META_APP_NAMESPACE}-{__META_ENV_TYPE_NAME}") will be used. + The value of metadata.__META_APP_NAMESPACE will be extracted from the value of the "name" defined through option("appns"), + and the value of __META_ENV_TYPE_NAME will be extracted from the value of the "name" defined through option("env"). activeDeadlineSeconds: int, default is Undefined, optional. Specifies the duration in seconds relative to the startTime that the job may be active before the system tries to terminate it; value must be positive integer @@ -87,6 +92,8 @@ schema Job: # job name name?: str + # job namespace + namespace?: str # subset of batchv1.JobSpec activeDeadlineSeconds?: int backoffLimit?: int = 6 diff --git a/models/kube/frontend/server.k b/models/kube/frontend/server.k index 4865d3d..21fe469 100644 --- a/models/kube/frontend/server.k +++ b/models/kube/frontend/server.k @@ -20,6 +20,11 @@ schema Server: If not defined, a generated name ("{__META_APP_NAME}-{__META_ENV_TYPE_NAME}") will be used. The value of metadata.__META_APP_NAME will be extracted from the value of the "name" defined through option("app"), and the value of __META_ENV_TYPE_NAME will be extracted from the value of the "name" defined through option("env"). + namespace: str, default is Undefined, optional. + The namespace of the workload and service. + If not defined, a generated name ("{__META_APP_NAMESPACE}-{__META_ENV_TYPE_NAME}") will be used. + The value of metadata.__META_APP_NAMESPACE will be extracted from the value of the "name" defined through option("appns"), + and the value of __META_ENV_TYPE_NAME will be extracted from the value of the "name" defined through option("env"). workloadType: "Deployment" | "StatefulSet", default is "Deployment", required. Application workload type, default to 'Deployment' renderType: "Server" | "KubeVelaApplication", default is "Server", optional. @@ -97,6 +102,9 @@ schema Server: # workload name name?: str + # workload namespace + namespace?: str + # Application workload type, default to 'Deployment' workloadType: "Deployment" | "StatefulSet" = "Deployment" diff --git a/models/kube/metadata/metadata.k b/models/kube/metadata/metadata.k index 742f5d7..c15bbeb 100644 --- a/models/kube/metadata/metadata.k +++ b/models/kube/metadata/metadata.k @@ -1,3 +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 diff --git a/models/kube/mixins/configmap_mixin.k b/models/kube/mixins/configmap_mixin.k index eb9e6e3..aad5403 100644 --- a/models/kube/mixins/configmap_mixin.k +++ b/models/kube/mixins/configmap_mixin.k @@ -9,7 +9,8 @@ mixin ConfigMapMixin for protocol.ServerProtocol: kubernetes: resource.ResourceMapping { ConfigMap = [v1.ConfigMap { metadata: utils.MetadataBuilder(_c) | { - name = _c?.name or "{}{}{}".format(metadata.__META_APP_NAME, metadata.__META_ENV_TYPE_NAME, _i) + name = _c?.name or "{}-{}-{}".format(metadata.__META_APP_NAME, metadata.__META_ENV_TYPE_NAME, _i) + namespace = _c?.namespace or "{}-{}".format(metadata.__META_APP_NAMESPACE, metadata.__META_ENV_TYPE_NAME) } data = _c?.data binaryData = _c?.binaryData diff --git a/models/kube/mixins/ingress_mixin.k b/models/kube/mixins/ingress_mixin.k index 59cd55c..db9ef0b 100644 --- a/models/kube/mixins/ingress_mixin.k +++ b/models/kube/mixins/ingress_mixin.k @@ -9,7 +9,8 @@ mixin IngressMixin for protocol.ServerProtocol: kubernetes: resource.ResourceMapping { Ingress = [networking_v1.Ingress { metadata: utils.MetadataBuilder(_s) | { - name = _s?.name or "{}{}{}".format(metadata.__META_APP_NAME, metadata.__META_ENV_TYPE_NAME, _i) + name = _s?.name or "{}-{}-{}".format(metadata.__META_APP_NAME, metadata.__META_ENV_TYPE_NAME, _i) + namespace = _s?.namespace or "{}-{}".format(metadata.__META_APP_NAMESPACE, metadata.__META_ENV_TYPE_NAME) } spec.rules = _s?.rules spec.tls = _s?.tls diff --git a/models/kube/mixins/namespace_mixin.k b/models/kube/mixins/namespace_mixin.k index 5cc5eca..6b09b78 100644 --- a/models/kube/mixins/namespace_mixin.k +++ b/models/kube/mixins/namespace_mixin.k @@ -25,7 +25,7 @@ mixin NamespaceMixin for protocol.ServerProtocol: kubernetes: resource.ResourceMapping { Namespace = [ v1.Namespace { - metadata.name = metadata.__META_APP_NAME + metadata.name = config?.namespace or "{}-{}".format(metadata.__META_APP_NAMESPACE, metadata.__META_ENV_TYPE_NAME) } ] } diff --git a/models/kube/mixins/secret_mixin.k b/models/kube/mixins/secret_mixin.k index 14a0a7e..c548cd6 100644 --- a/models/kube/mixins/secret_mixin.k +++ b/models/kube/mixins/secret_mixin.k @@ -9,7 +9,8 @@ mixin SecretMixin for protocol.ServerProtocol: kubernetes: resource.ResourceMapping { Secret = [v1.Secret { metadata: utils.MetadataBuilder(_s) | { - name = _s?.name or "{}{}{}".format(metadata.__META_APP_NAME, metadata.__META_ENV_TYPE_NAME, _i) + name = _s?.name or "{}-{}-{}".format(metadata.__META_APP_NAME, metadata.__META_ENV_TYPE_NAME, _i) + namespace = _s?.namespace or "{}-{}".format(metadata.__META_APP_NAMESPACE, metadata.__META_ENV_TYPE_NAME) } data = _s?.data stringData = _s?.stringData diff --git a/models/kube/mixins/service_mixin.k b/models/kube/mixins/service_mixin.k index 3627176..bef23ac 100644 --- a/models/kube/mixins/service_mixin.k +++ b/models/kube/mixins/service_mixin.k @@ -11,7 +11,8 @@ mixin ServiceMixin for protocol.ServerProtocol: kubernetes: resource.ResourceMapping { Service = [v1.Service { metadata: utils.MetadataBuilder(_s) | { - name = _s?.name or (workloadName if len(config.services) == 1 else "{}{}{}".format(metadata.__META_APP_NAME, metadata.__META_ENV_TYPE_NAME, _i)) + name = _s?.name or (workloadName if len(config.services) == 1 else "{}-{}-{}".format(metadata.__META_APP_NAME, metadata.__META_ENV_TYPE_NAME, _i)) + namespace = _s?.namespace or (workloadNamespace if len(config.services) == 1 else "{}-{}".format(metadata.__META_APP_NAMESPACE, metadata.__META_ENV_TYPE_NAME)) } spec: { clusterIP = _s?.clusterIP if _s?.type in ["ClusterIP", "NodePort", "LoadBalancer", None, Undefined] else Undefined diff --git a/models/kube/mixins/serviceaccount_mixin.k b/models/kube/mixins/serviceaccount_mixin.k index 49d7719..99ff4c4 100644 --- a/models/kube/mixins/serviceaccount_mixin.k +++ b/models/kube/mixins/serviceaccount_mixin.k @@ -9,6 +9,7 @@ mixin ServiceAccountMixin for protocol.ServerProtocol: ServiceAccount = [v1.ServiceAccount { metadata: utils.MetadataBuilder(config.serviceAccount) | { name = config.serviceAccount.name + namespace = config.serviceAccount.namespace } imagePullSecrets = config.serviceAccount.imagePullSecrets secrets = config.serviceAccount.secrets diff --git a/models/kube/utils/metadata_builder.k b/models/kube/utils/metadata_builder.k index 1d1a7db..83f7946 100644 --- a/models/kube/utils/metadata_builder.k +++ b/models/kube/utils/metadata_builder.k @@ -3,7 +3,7 @@ import models.kube.metadata MetadataBuilder = lambda config -> {str:} { { name: config?.name or metadata.__META_APP_NAME - namespace: config?.name or metadata.__META_APP_NAME + namespace: config?.namespace or metadata.__META_APP_NAMESPACE labels: config?.labels annotations: config?.annotations }