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_COUNTis set) might fail in CI (where it is not set). - Conversely, an incorrectly typed default (
replicas: "3"instead ofreplicas: 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.
Expansion Rules
Section titled “Expansion Rules”${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 → trueAfter 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 → falseMixed-Text Substitution
Section titled “Mixed-Text Substitution”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"Schema Inference
Section titled “Schema Inference”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-locationorspec.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)
Practical Guidance
Section titled “Practical Guidance”Refresh Stale Schemas
Section titled “Refresh Stale Schemas”If a valid new field is rejected because an older schema is cached, fetch current schemas and validate again:
ksail workload validate ./k8s --refresh-schemasRefresh 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:
ksail workload validate ./k8s --refresh-schemas \ --schema-location './schemas/{{.ResourceKind}}_{{.ResourceAPIVersion}}.json'Validate Cached or Local Schemas
Section titled “Validate Cached or Local Schemas”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:
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.
Avoid False Validation Failures
Section titled “Avoid False Validation Failures”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 validationspec: replicas: ${REPLICA_COUNT}
# After — default 2 is correctly typed as an integerspec: replicas: ${REPLICA_COUNT:-2}Verify Your Defaults Are Well-Typed
Section titled “Verify Your Defaults Are Well-Typed”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 integerspec: replicas: ${REPLICA_COUNT:-"2"}
# Correct: unquoted numeric defaultspec: replicas: ${REPLICA_COUNT:-2}Skip Validation for Specific Kinds
Section titled “Skip Validation for Specific Kinds”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:
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: - CorootThe flag and the configured kinds are merged. Skip only the kinds whose schema is genuinely unavailable — skipped resources are not validated at all.
Use workload validate Before Pushing
Section titled “Use workload validate Before Pushing”Combine workload validate with workload push in your workflow to catch schema errors before they enter the GitOps pipeline:
# Validate first, then push if cleanksail workload validate && ksail workload pushThis 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.
Multi-Environment Validation
Section titled “Multi-Environment Validation”Use --config to validate against the settings of a specific environment:
# Validate against staging schema/constraintsksail --config ksail.staging.yaml workload validate
# Validate against productionksail --config ksail.prod.yaml workload validateSee Multi-Environment Workflows for how to structure per-environment config files.
Supported Substitution Syntax
Section titled “Supported Substitution Syntax”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.