Skip to content

About

Zero config, accurate JSON schema generator for helm charts via static analysis

Topics

Resources

Stars

5 stars

Watchers

1 watching

Forks

Latest commit

 

History

1,949 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

helm-schema

A values.schema.json for any Helm chart, from its templates.

Install · Example · Usage · Configuration · Documentation

build status test status dependency status docs.rs crates.io

helm-schema reads a Helm chart's templates, follows every .Values path into the Kubernetes field it renders, and writes the JSON Schema Helm checks values against. A misspelled key or a wrong type then fails helm install with its path, instead of rendering a broken manifest.

Good for:

  • charts you maintain and want to ship with a values.schema.json;
  • keeping that schema in sync with the templates in CI;
  • charts with custom resources, typed from a CRD catalog.

Not for:

  • a schema written from annotations in values.yaml, which generators that read those comments produce;
  • a complete contract for every chart: values read through computed keys, or built at render time, stay loosely typed.

Install

Pick one:

# With Homebrew
brew install --cask romnn/tap/helm-schema

# With mise
mise use github:romnn/helm-schema

# From source, which compiles it and takes a few minutes
cargo install --locked helm-schema-cli

It needs neither Helm nor a cluster. The first run downloads the Kubernetes and CRD schemas the chart needs and caches them, so later runs can work offline.

Example

  1. A chart whose template puts replicaCount into a Deployment:

    # mychart/templates/deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: web
    spec:
      replicas: {{ .Values.replicaCount }}
  2. Generate the schema beside values.yaml, where Helm looks for it:

    helm-schema ./mychart --output mychart/values.schema.json

    replicaCount gets the type and description of spec.replicas, though values.yaml only says replicaCount: 1. An excerpt of the schema:

    {
      "properties": {
        "replicaCount": {
          "anyOf": [
            {
              "description": "Number of desired pods. This is a pointer to distinguish between explicit zero and not specified. Defaults to 1.",
              "format": "int32",
              "type": "integer"
            },
            {
              "pattern": "^(([+-]_*)?(0|[1-9][0-9_]{0,17}|0[xX][0-9a-fA-F]{1,15}|0[bB][01]{1,62}|0[oO][0-7]{1,20}|0[0-7]{1,20})|[+-]_*0[0-7]{0,8}[89][0-9]{0,8})$",
              "type": "string"
            }
          ]
        }
      }
    }

    The second member accepts a quoted number such as "2", which Helm renders the same way.

  3. Helm now checks every install, upgrade, lint and template against it, and names a misspelled key:

    helm template ./mychart --set replicaCont=3
    # Error: values don't meet the specifications of the schema(s) in the following chart(s):
    # mychart:
    # - at '': additional properties 'replicaCont' not allowed

Getting started walks through a larger chart and how to read its schema.

Usage

# Write the schema to a file; without --output it goes to standard output
helm-schema ./mychart --output mychart/values.schema.json

# A packaged chart works too
helm-schema ./mychart-0.1.0.tgz --output values.schema.json

# Read Kubernetes schemas for another version than the default v1.35.0
helm-schema ./mychart --k8s-version v1.34.0

# Also find APIs that recent Kubernetes removed, such as policy/v1beta1
helm-schema ./mychart --k8s-version-fallback auto

# Without network, from a cache filled by an earlier run
helm-schema ./mychart --offline --k8s-schema-cache-dir ./k8s-schema-cache

# Only what the templates imply, without Kubernetes types
helm-schema ./mychart --no-k8s-schemas

# Merge hand-written schemas over the generated one, in order
helm-schema ./mychart --override-schema ./schema-override.json

A schema over Helm's 5 MiB limit for a chart file is written as compact JSON with short definition names, with a note on standard error. To keep a readable schema and still run Helm, run Helm through helm-schema, which hands it a shortened copy:

helm-schema lint ./mychart -f my-values.yaml
helm-schema template ./mychart --name-template my-release

Warnings, such as a resource whose schema was not found, go to standard error; --diag-format json writes them as one JSON object per line. The documentation has every flag:

Configuration

Most charts need no configuration. For a chart whose full schema is too large or too slow for Helm, the lean profile drops conditional checks, so the schema accepts more but never less. Pass it as --profile lean, or let the chart carry it in helm-schema.yaml at its root:

# The format's version; the only required key.
version: 1

# The preset: full, the default, or lean.
profile: lean

# Each control is on or off, and overrides the profile.
emission:
  # Conditional checks placed at the values root.
  root-anchored-conditionals: off

  # Conditional checks placed inside the object whose values they test.
  local-conditionals: on

  # Rejections of values with which rendering fails.
  terminal-clauses: off

  # Checks that depend on which Kubernetes kind a value selects.
  kind-partitions: off

Every key but version has a flag of the same name, and flags win over the file. --print-effective-config shows each setting and where it came from; Configuration has the details.

Status

helm-schema is alpha. It works for many charts, but Helm templates have many edge cases, so expect some charts to need a schema override and an occasional wrong or missing type.

License

MIT

About

Zero config, accurate JSON schema generator for helm charts via static analysis

Topics

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Contributors

Languages