How-to guide / Platform capabilities
Use Kix-managed secrets
These docs describe Kix's main branch, which can include changes not yet released. Read the docs for the latest release.
Use scope.mkSecret when a package should create and own a Kubernetes Secret.
The returned resource includes helpers for environment variables, envFrom,
and volumes, with key names checked during evaluation.
This example uses non-sensitive development values. It assumes you already have a local Kix package with a workload.
Create the Secret
Section titled “Create the Secret”Add the Secret to the package’s returned parts:
15 credentials = scope.mkSecret {16 name = "${scope.instanceName}-credentials";17 stringData = {18 username = "demo";19 password = "development-only";20 };21 };When keys is omitted, Kix derives the declared key list from stringData.
A declared key needs a value in stringData unless source names a SOPS
file that holds it; the build refuses one with neither.
Set type when the workload needs a Kubernetes Secret type other than
Opaque.
Always use scope.mkSecret for a Secret managed by Kix. Its out helpers
carry dependency information and validate key names.
Add the values to a workload
Section titled “Add the values to a workload”Use out.mkEnv to map environment-variable names to Secret keys:
25 deployment = scope.mkDeployment {26 name = scope.instanceName;27 spec = {28 replicas = 1;29 selector.matchLabels = scope.selectorLabels;30 template.spec.containers = [31 {32 name = "app";33 image = "busybox:1.36";34 command = [ "sh" "-c" "sleep 3600" ];35 env = self.credentials.out.mkEnv {36 APP_USERNAME = "username";37 APP_PASSWORD = "password";38 };39 }40 ];41 };42 };The generated Deployment refers to managed-example-credentials through
secretKeyRef. It also depends on the Secret, so Kix applies the Secret before
the workload.
For other consumption patterns, use:
secret.out.keyRef "key"for onevalueFromentry.secret.out.envFromto expose every key throughenvFrom.secret.out.volume "credentials"to create a Secret volume source.
Check the package
Section titled “Check the package”Evaluate the example cluster:
❱ kix check how-to-platform-secrets
TOOL RESULT DETAILS
eval pass 11 manifests evaluated
scorecard pass 0 errors, 13 warnings, 2 info
governance.hasOwner (warning), 2:
secret-examples/external-example: package 'external-example' has no meta.owner
secret-examples/managed-example: package 'managed-example' has no meta.owner
reliability.boundedResources (warning), 2:
secret-examples/external-example/external-example: container 'app' is missing resource settings: requests.cpu, requests.memory, limits.cpu, limits.memory. Set CPU and memory requests and limits. Without a memory limit, a workload can exhaust the node's memory.
secret-examples/managed-example/managed-example: container 'app' is missing resource settings: requests.cpu, requests.memory, limits.cpu, limits.memory. Set CPU and memory requests and limits. Without a memory limit, a workload can exhaust the node's memory.
reliability.hasProbes (warning), 4:
secret-examples/external-example/external-example: container 'app' has no livenessProbe
secret-examples/external-example/external-example: container 'app' has no readinessProbe
secret-examples/managed-example/managed-example: container 'app' has no livenessProbe
secret-examples/managed-example/managed-example: container 'app' has no readinessProbe
security.hasNetworkPolicy (warning), 1:
secret-examples: namespace 'secret-examples' has workloads but no network policy
security.nonRoot (warning), 2:
secret-examples/external-example/external-example: container 'app' may run as root: neither it nor the pod sets securityContext.runAsNonRoot, and no non-zero runAsUser resolves for it
secret-examples/managed-example/managed-example: container 'app' may run as root: neither it nor the pod sets securityContext.runAsNonRoot, and no non-zero runAsUser resolves for it
security.readOnlyRootfs (warning), 2:
secret-examples/external-example/external-example: container 'app' does not set readOnlyRootFilesystem
secret-examples/managed-example/managed-example: container 'app' does not set readOnlyRootFilesystem
2 info finding(s) not listed; run with -v to list them. If the workload requests a key not declared by the Secret, evaluation fails and lists the available keys. Fix the key name in the workload or add it to the Secret before deploying.