Skip to content

Substitution Expansion in `workload validate`

ksail workload validate runs kubeconform against your Kubernetes manifests to catch schema errors before they reach the cluster. When your manifests use Flux postBuild substitutions (${VAR}, ${VAR:-default}, ${VAR:=default}), the validator needs concrete values to produce valid YAML for schema checking.

This guide explains how the expansion works, why environment variables are not read, and how to write substitutions that validate cleanly.

Why Local Environment Variables Are Not Used

Section titled “Why Local Environment Variables Are Not Used”

workload validate is designed to be deterministic — the same manifests should produce the same validation result regardless of the machine, user, or environment they are run on. Reading from local environment variables would break this:

  • A manifest that validates on a developer’s machine (because $REPLICA_COUNT is set) might fail in CI (where it is not set).
  • Conversely, an incorrectly typed default (replicas: "3" instead of replicas: 3) might slip through validation on a machine where the env var overrides the bad default.

Instead, KSail uses typed schema-aware placeholders derived from the Kubernetes JSON schema. Validation catches type mismatches at schema-check time, not at deploy time.

${VAR} — Bare Variable Reference (No Default)

Section titled “${VAR} — Bare Variable Reference (No Default)”

A bare ${VAR} is replaced with a typed placeholder based on the JSON schema type of the field it occupies.

Schema type Placeholder value
string "placeholder"
integer 0
number 0.0
boolean true
unknown / no schema "placeholder"

Example:

spec:
replicas: ${REPLICA_COUNT} # integer field → 0
image: ${IMAGE_REF} # string field → "placeholder"
enableFeature: ${FEATURE_ON} # boolean field → true

After expansion:

spec:
replicas: 0
image: placeholder
enableFeature: true

${VAR:-default} and ${VAR:=default} — Variable With Default

Section titled “${VAR:-default} and ${VAR:=default} — Variable With Default”

When a default is provided (either :- or := syntax), the default value is used as-is and coerced to the schema type.

spec:
replicas: ${REPLICA_COUNT:-3} # integer field → 3 (not "3")
name: ${APP_NAME:-my-app} # string field → "my-app"
debug: ${DEBUG_MODE:-false} # boolean field → false

When a variable reference is embedded inside a larger string, the result is always a string:

spec:
hostname: ${ENV}-app.example.com # → "placeholder-app.example.com"
image: myrepo/${IMAGE_NAME:-nginx}:latest # → "myrepo/nginx:latest"

KSail configures the same ordered schema locations and strictness for typed substitution and validation. In the default additional mode, built-in Kubernetes schemas take precedence, followed by supplied locations and then the CRD catalogue. In only mode, both passes receive the supplied locations without implicit registries.

Placeholder inference reads remote schemas only from kubeconform’s disk cache; supplied local directories and path templates are read directly. If an earlier remote schema is uncached, inference can fall through to a later local schema while validation downloads and uses the earlier remote schema. This can cause misleading type errors. Use --refresh-schemas to fetch the selected schemas before inference when both passes must use the same schema precedence.

Schema files are looked up from the OS user cache directory (for example ~/.cache/ksail/kubeconform/), or from ${TMPDIR}/ksail/kubeconform when the user cache directory is unavailable (such as in some CI environments).

  • Core Kubernetes resources (Deployment, Service, ConfigMap, …): typed via kubernetes-json-schema when the relevant schema is present in the kubeconform disk cache
  • CRDs (FluxCD, cert-manager, …): typed via the CRDs-catalog when their schemas are present in the kubeconform disk cache
  • Supplied schemas (--schema-location or spec.workload.validation.schemaLocations): local files are read directly; remote templates and registry directories use the same cache keys as kubeconform
  • Resources without an available schema: all bare ${VAR} expand to "placeholder" (string)

If a valid new field is rejected because an older schema is cached, fetch current schemas and validate again:

Terminal window
ksail workload validate ./k8s --refresh-schemas

Refresh uses a temporary cache for the selected resources, fetches their schemas before typing Flux placeholders, and still performs full validation afterward. The persistent cache receives the downloaded entries only after all validation stages succeed. Unrelated cached schemas remain; normal validation reuses the refreshed entries without automatic expiry. If downloading or validation fails, the existing cache is preserved. The temporary cache is removed on both success and failure.

Registry locations are tried in order. An unreachable earlier location can stop ordinary validation before a later cached CRD schema is considered. Use explicit location selection when the default registries are not part of the intended schema set, as described below.

Remote locations must be reachable during refresh. An unavailable selected schema fails refresh even when --ignore-missing-schemas would normally permit it; explicit kind exclusions still apply. Invalid enum values and incorrectly typed defaults continue to fail. A private schema absent from the catalogue can be supplied at the same time:

Terminal window
ksail workload validate ./k8s --refresh-schemas \
--schema-location './schemas/{{.ResourceKind}}_{{.ResourceAPIVersion}}.json'

Select only to omit implicit built-in and catalogue locations. Supplied CLI locations come first, followed by configured locations and any derived CRD schemas. At least one nonblank effective location is required. By default, resources without an applicable schema are skipped because --ignore-missing-schemas is true. Set --ignore-missing-schemas=false when missing schemas must fail. Selecting only a CRD catalogue does not supply schemas for core Kubernetes kinds.

For an unrendered HelmRelease, run this command while online to populate its native cache, then repeat it without network access:

Terminal window
ksail workload validate ./k8s/helmrelease.yaml \
--strict --skip-helm-render --skip-secrets=false --ignore-missing-schemas=false \
--schema-location-mode only \
--schema-location 'https://raw.githubusercontent.com/datreeio/CRDs-catalog/main/{{.Group}}/{{.ResourceKind}}_{{.ResourceAPIVersion}}.json'

Add --refresh-schemas while online if placeholders require schemas before validation or the cached response is stale. Offline reuse requires every selected schema and remote reference to be cached already, or supplied as a complete local reference graph. only chooses locations; it does not disable transport. Missing cache entries may still attempt a download and fail. TLS verification remains enabled, and invalid values or missing required schemas still fail.

For a local schema tree, replace the location with './schemas/{{.ResourceKind}}_{{.ResourceAPIVersion}}.json'. Omit --schema-location-mode only or choose additional to retain the ordinary registry order.

If workload validate reports a type error for a field that uses ${VAR} without a default, add a meaningful default of the correct type:

# Before — bare ${VAR} expands to "placeholder" (string), fails integer validation
spec:
replicas: ${REPLICA_COUNT}
# After — default 2 is correctly typed as an integer
spec:
replicas: ${REPLICA_COUNT:-2}

When using ${VAR:-default}, ensure the default value matches the field’s expected type. Incorrect defaults will cause validation failures even if the env var is always set at runtime:

# This will fail validation: "2" is a string, but replicas expects an integer
spec:
replicas: ${REPLICA_COUNT:-"2"}
# Correct: unquoted numeric default
spec:
replicas: ${REPLICA_COUNT:-2}

workload validate checks each resource against its JSON schema from the CRDs-catalog. When a CRD’s catalog schema is stale or missing, kubeconform can reject otherwise-valid manifests — for example flagging a newer field as additional properties ... not allowed. (Kubernetes Secrets are already skipped by default to avoid SOPS-related failures.)

Refresh a stale cached schema first, or supply the current schema with --schema-location. Skip only kinds whose schemas are genuinely unavailable, using --skip-kinds:

Terminal window
ksail workload validate --skip-kinds Coroot,SealedSecret

…or, so it also applies in CI (the ksail-cluster action with validate: "true"), via ksail.yaml:

spec:
workload:
validation:
skipKinds:
- Coroot

The flag and the configured kinds are merged. Skip only the kinds whose schema is genuinely unavailable — skipped resources are not validated at all.

Combine workload validate with workload push in your workflow to catch schema errors before they enter the GitOps pipeline:

Terminal window
# Validate first, then push if clean
ksail workload validate && ksail workload push

This is the same pattern the ksail-cluster composite GitHub Action uses when validate: "true" is set. See PR Preview Clusters for the full CI/CD workflow.

Use --config to validate against the settings of a specific environment:

Terminal window
# Validate against staging schema/constraints
ksail --config ksail.staging.yaml workload validate
# Validate against production
ksail --config ksail.prod.yaml workload validate

See Multi-Environment Workflows for how to structure per-environment config files.

KSail supports the standard Flux postBuild variable syntax:

Syntax Behavior during validation
${VAR} Typed placeholder (schema-derived)
${VAR:-default} default value, coerced to schema type
${VAR:=default} default value, coerced to schema type
${VAR} in mixed text Replaced with "placeholder" (string)

Other Flux substitution forms (e.g., shell-style $VAR without braces, $(VAR) command substitution) are not expanded by KSail and are left as-is.