Skip to content

Commit

Permalink
Merge pull request #25 from eminaktas/fix/namespace
Browse files Browse the repository at this point in the history
fix(namespaces): unify namespace settings and improve naming readability
  • Loading branch information
Peefy authored Oct 18, 2024
2 parents 065b228 + a713f54 commit 09bfec6
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/konfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ ServerBackend converts the user-written front-end model `Server` into a collecti
|**provider**|[]|||
|**sidecarContainers**|[{str:}]|||
|**workloadAttributes** `required`|{str:}||{<br /> metadata = utils.MetadataBuilder(config) \| {<br /> name = workloadName<br /> }<br /> spec = {<br /> replicas = config.replicas<br /> if config.useBuiltInSelector:<br /> selector: {matchLabels: app.selector \| config.selector \| _applicationLabel}<br /> else:<br /> selector: {matchLabels: config.selector}<br /> template = {<br /> metadata = {<br /> if config.useBuiltInLabels:<br /> labels = app.labels \| _applicationLabel<br /> <br /> **config.podMetadata<br /> }<br /> spec = {<br /> containers = [mainContainer] + (sidecarContainers or [])<br /> initContainers = initContainers<br /> if config.volumes:<br /> volumes = [utils.to_kube_volume(v) for v in config.volumes if v.volumeSource]<br /> <br /> if config.serviceAccount:<br /> serviceAccountName = config.serviceAccount.name<br /> <br /> }<br /> }<br /> }<br />}|
|**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.
Expand Down
4 changes: 3 additions & 1 deletion models/kube/backend/job_backend.k
Original file line number Diff line number Diff line change
Expand Up @@ -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:}
Expand Down Expand Up @@ -48,6 +49,7 @@ schema JobBackend[inputConfig: frontend.Job]:
jobAttrs: {str:} = {
metadata = utils.MetadataBuilder(config) | {
name = jobName
namespace = jobNamespace
}
spec = {
activeDeadlineSeconds = config.activeDeadlineSeconds
Expand Down
5 changes: 4 additions & 1 deletion models/kube/backend/server_backend.k
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -58,6 +60,7 @@ schema ServerBackend[inputConfig: server.Server]:
workloadAttributes: {str:} = {
metadata = utils.MetadataBuilder(config) | {
name = workloadName
namespace = workloadNamespace
}
spec = {
replicas = config.replicas
Expand Down
7 changes: 7 additions & 0 deletions models/kube/frontend/job.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -87,6 +92,8 @@ schema Job:

# job name
name?: str
# job namespace
namespace?: str
# subset of batchv1.JobSpec
activeDeadlineSeconds?: int
backoffLimit?: int = 6
Expand Down
8 changes: 8 additions & 0 deletions models/kube/frontend/server.k
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -97,6 +102,9 @@ schema Server:
# workload name
name?: str

# workload namespace
namespace?: str

# Application workload type, default to 'Deployment'
workloadType: "Deployment" | "StatefulSet" = "Deployment"

Expand Down
1 change: 1 addition & 0 deletions models/kube/metadata/metadata.k
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion models/kube/mixins/configmap_mixin.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion models/kube/mixins/ingress_mixin.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion models/kube/mixins/namespace_mixin.k
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
]
}
3 changes: 2 additions & 1 deletion models/kube/mixins/secret_mixin.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion models/kube/mixins/service_mixin.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions models/kube/mixins/serviceaccount_mixin.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion models/kube/utils/metadata_builder.k
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 09bfec6

Please sign in to comment.