Add env vars and mounts
Kix provides helpers for the repetitive parts of container environment and volume configuration. Use them to keep the source resource attached to the workload dependency graph.
This guide uses the package created in Create a local package.
Add environment variables
Section titled “Add environment variables”kix.mkEnvVars converts an attribute set into the list Kubernetes expects:
env = kix.mkEnvVars { APP_ENV = config.environment; };The rendered container contains:
env: - name: APP_ENV value: productionEntries whose value is null are omitted. Use kix.mkEnv when you need to
combine simple values with raw Kubernetes entries such as secretKeyRef or
fieldRef:
env = kix.mkEnv { APP_ENV = config.environment; OPTIONAL_VALUE = config.optionalValue; } [ { name = "POD_NAME"; valueFrom.fieldRef.fieldPath = "metadata.name"; } ];Create the resource to mount
Section titled “Create the resource to mount”This package stores its page content in a ConfigMap:
content = scope.mkResource { apiVersion = "v1"; kind = "ConfigMap"; inherit name; data."index.html" = "${config.message}\n"; };Create a mount from the wrapped resource:
contentMount = kix.mount.configMap self.content { name = "content"; mountPath = "/usr/share/nginx/html"; readOnly = true; };Passing self.content is significant. The mount retains the dependency on the
ConfigMap, so Kix can order the resources and include changes to the content in
the workload identity.
The other mount helpers follow the same shape:
kix.mount.secretmounts a Secret.kix.mount.pvcmounts a PersistentVolumeClaim.kix.mount.emptyDircreates an ephemeral volume.kix.mount.hostPathmounts a node path.
Apply mounts to the workload
Section titled “Apply mounts to the workload”Pass the mount list through kix.withMounts before wrapping the Deployment:
|> kix.withMounts [ contentMount ]With one container, Kix adds the volumes and volume mounts automatically. For
a Pod template with several containers, use kix.withMountsOn "container-name"
to select the recipient.
Null entries in the mount list are ignored, which makes optional mounts easy
to express with an if expression:
|> kix.withMounts [ contentMount (if config.cache.enable then cacheMount else null)]Render the cluster and inspect the Deployment’s env, volumeMounts, and
volumes fields. This excerpt shows the container wiring the helpers
produced:
❱ kix build how-to-application --output json Show output
{
"containers": [
{
"name": "nginx",
"env": [
{
"name": "APP_ENV",
"value": "production"
}
],
"volumeMounts": [
{
"mountPath": "/usr/share/nginx/html",
"name": "content",
"readOnly": true
}
]
}
],
"volumes": [
{
"configMap": {
"name": "production"
},
"name": "content"
}
]
} The environment variables come from the helper calls above. The mount pairs
the volumeMounts entry on the container with the volumes entry that names
the ConfigMap.