diff --git a/examples/server/app_sidecar/prod/kcl.mod b/examples/server/app_sidecar/prod/kcl.mod index 8a22093..90a60be 100644 --- a/examples/server/app_sidecar/prod/kcl.mod +++ b/examples/server/app_sidecar/prod/kcl.mod @@ -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"] - diff --git a/examples/server/app_sidecar/prod/main.k b/examples/server/app_sidecar/prod/main.k index 0a2bca4..00af1b2 100644 --- a/examples/server/app_sidecar/prod/main.k +++ b/examples/server/app_sidecar/prod/main.k @@ -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 diff --git a/models/kube/backend/job_backend.k b/models/kube/backend/job_backend.k index 5f78fef..9480092 100644 --- a/models/kube/backend/job_backend.k +++ b/models/kube/backend/job_backend.k @@ -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:} = { @@ -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: diff --git a/models/kube/backend/server_backend.k b/models/kube/backend/server_backend.k index b7038f2..7676dcb 100644 --- a/models/kube/backend/server_backend.k +++ b/models/kube/backend/server_backend.k @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/models/kube/templates/volumes.k b/models/kube/templates/volumes.k new file mode 100644 index 0000000..87350a0 --- /dev/null +++ b/models/kube/templates/volumes.k @@ -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" +} diff --git a/models/kube/utils/volume_patch.k b/models/kube/utils/volume_patch.k index 5e00838..0a1a31a 100644 --- a/models/kube/utils/volume_patch.k +++ b/models/kube/utils/volume_patch.k @@ -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([[ @@ -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 + } +}