Skip to content

Commit

Permalink
Merge pull request #19 from kcl-lang/refactor-move-kube-volume-func-t…
Browse files Browse the repository at this point in the history
…o-utils

refactor: move kube volume function to utils
  • Loading branch information
Peefy authored Jul 30, 2024
2 parents 2dce09b + 19ef635 commit 848fdbf
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 46 deletions.
1 change: 0 additions & 1 deletion examples/server/app_sidecar/prod/kcl.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ konfig = { path = "../../../../../konfig" }
k8s = "1.28"
[profile]
entries = ["../base/base.k", "main.k", "${konfig:KCL_MOD}/models/kube/render/render.k"]

1 change: 0 additions & 1 deletion examples/server/app_sidecar/prod/main.k
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import konfig.models.kube.frontend
import konfig.models.kube.frontend.sidecar as s
import konfig.models.kube.frontend.container.env as e
import konfig.models.kube.frontend.volume as v
import konfig.models.kube.templates.resource as res_tpl

Expand Down
21 changes: 5 additions & 16 deletions models/kube/backend/job_backend.k
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ schema JobBackend[inputConfig: frontend.Job]:
image = config.image
resource = config?.schedulingStrategy?.resource
}
mainContainer = utils.VolumePatch(config.volumes, [utils.ContainerFrontend2Kube(mainContainerDict)])?[0]
mainContainer = utils.volume_patch(config.volumes, [utils.ContainerFrontend2Kube(mainContainerDict)])?[0]

if config.sidecarContainers:
sidecarContainers = utils.VolumePatch(config.volumes, [utils.ContainerFrontend2Kube(_s) for _s in config.sidecarContainers])
sidecarContainers = utils.volume_patch(config.volumes, [utils.ContainerFrontend2Kube(_s) for _s in config.sidecarContainers])

if config.initContainers:
initContainers = utils.VolumePatch(config.volumes, [utils.ContainerFrontend2Kube(_s) for _s in config.initContainers])
initContainers = utils.volume_patch(config.volumes, [utils.ContainerFrontend2Kube(_s) for _s in config.initContainers])

# construct job attributes
jobAttrs: {str:} = {
Expand Down Expand Up @@ -72,20 +72,9 @@ schema JobBackend[inputConfig: frontend.Job]:
]
initContainers = initContainers
restartPolicy = config.restartPolicy
# volume
# volumes
if config.volumes: volumes = [
(lambda volume {
volumeType = typeof(volume.volumeSource)
assert volumeType in VOLUME_SOURCE_TYPE_MAPPING, "Invalid frontend volume type, please check VOLUME_SOURCE_TYPE_MAPPING"
kubeVolumeType = VOLUME_SOURCE_TYPE_MAPPING[volumeType]
{
name = volume.name
if typeof(volume.volumeSource) == "EmptyDir" and volume.volumeSource.medium == "":
"${kubeVolumeType}" = {}
else:
"${kubeVolumeType}" = volume.volumeSource
}
})(volume) for volume in config.volumes if volume.volumeSource
utils.to_kube_volume(v) for v in config.volumes if v.volumeSource
]
# service account
if config.serviceAccount:
Expand Down
30 changes: 4 additions & 26 deletions models/kube/backend/server_backend.k
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ import models.kube.metadata
import models.kube.utils
import models.kube.resource

# Frontend volume to k8s volume mapping.
VOLUME_SOURCE_TYPE_MAPPING = {
EmptyDir = "emptyDir"
Secret = "secret"
ConfigMap = "configMap"
DownwardAPI = "downwardAPI"
CSI = "csi"
HostPath = "hostPath"
}

schema ServerBackend[inputConfig: server.Server]:
"""ServerBackend converts the user-written front-end model `Server` into a
collection of k8s resources and places the resource collection into
Expand Down Expand Up @@ -46,7 +36,7 @@ schema ServerBackend[inputConfig: server.Server]:
if config.mainContainer:
assert config.image, "config.image must be specified and can't be empty or None or Undefined"
# Construct input of converter using the volumes.
mainContainer = utils.VolumePatch(config.volumes, [utils.ContainerFrontend2Kube({
mainContainer = utils.volume_patch(config.volumes, [utils.ContainerFrontend2Kube({
**config.mainContainer
if config.mainContainer.useBuiltInEnv:
env += app.envs
Expand All @@ -56,10 +46,10 @@ schema ServerBackend[inputConfig: server.Server]:
})])?[0]

if config.sidecarContainers:
sidecarContainers = utils.VolumePatch(config.volumes, [utils.ContainerFrontend2Kube(_s) for _s in config.sidecarContainers])
sidecarContainers = utils.volume_patch(config.volumes, [utils.ContainerFrontend2Kube(_s) for _s in config.sidecarContainers])

if config.initContainers:
initContainers = utils.VolumePatch(config.volumes, [utils.ContainerFrontend2Kube(_s) for _s in config.initContainers])
initContainers = utils.volume_patch(config.volumes, [utils.ContainerFrontend2Kube(_s) for _s in config.initContainers])

_applicationLabel: {str: str} = {
"app.k8s.io/component": workloadName
Expand All @@ -85,19 +75,7 @@ schema ServerBackend[inputConfig: server.Server]:
containers = [mainContainer] + (sidecarContainers or [])
initContainers = initContainers
if config.volumes: volumes = [
(lambda volume {
"""Convert frontend volume to k8s Volume."""
volumeType = typeof(volume.volumeSource)
assert volumeType in VOLUME_SOURCE_TYPE_MAPPING, "Invalid frontend volume type, please check VOLUME_SOURCE_TYPE_MAPPING"
kubeVolumeType = VOLUME_SOURCE_TYPE_MAPPING[volumeType]
{
name = volume.name
if typeof(volume.volumeSource) == "EmptyDir" and volume.volumeSource.medium == "":
"${kubeVolumeType}" = {}
else:
"${kubeVolumeType}" = volume.volumeSource
}
})(volume) for volume in config.volumes if volume.volumeSource
utils.to_kube_volume(v) for v in config.volumes if v.volumeSource
]
if config.serviceAccount:
serviceAccountName = config.serviceAccount.name
Expand Down
10 changes: 10 additions & 0 deletions models/kube/templates/volumes.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

# Frontend volume to k8s volume mapping.
VOLUME_SOURCE_TYPE_MAPPING = {
EmptyDir = "emptyDir"
Secret = "secret"
ConfigMap = "configMap"
DownwardAPI = "downwardAPI"
CSI = "csi"
HostPath = "hostPath"
}
20 changes: 18 additions & 2 deletions models/kube/utils/volume_patch.k
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import models.kube.frontend.volume
import models.kube.templates

VolumePatch = lambda volumes: [volume.Volume], containers: [{str:}] -> [{str:}] {
"""VolumePatch patches volumes into _containers and returns the patched _container"""

volume_patch = lambda volumes: [volume.Volume], containers: [{str:}] -> [{str:}] {
"""volume_patch patches volumes into _containers and returns the patched _container"""
[
_container | {
volumeMounts = sum([[
Expand All @@ -15,3 +17,17 @@ VolumePatch = lambda volumes: [volume.Volume], containers: [{str:}] -> [{str:}]
} for _container in containers
] if containers else Undefined
}

to_kube_volume = lambda v: volume.Volume {
"""Convert frontend volume to k8s Volume."""
volumeType = typeof(v.volumeSource)
assert volumeType in templates.VOLUME_SOURCE_TYPE_MAPPING, "Invalid frontend volume type, please check VOLUME_SOURCE_TYPE_MAPPING"
kubeVolumeType = templates.VOLUME_SOURCE_TYPE_MAPPING[volumeType]
{
name = v.name
if typeof(v.volumeSource) == "EmptyDir" and v.volumeSource.medium == "":
"${kubeVolumeType}" = {}
else:
"${kubeVolumeType}" = v.volumeSource
}
}

0 comments on commit 848fdbf

Please sign in to comment.