Skip to content

Generate jsonschemas from helm charts.

License

Notifications You must be signed in to change notification settings

dadav/helm-schema

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
Aug 18, 2023
Mar 6, 2024
Aug 18, 2023
Mar 6, 2024
Aug 18, 2023
Aug 30, 2023
Nov 14, 2023
Sep 8, 2023
Aug 18, 2023
Mar 6, 2024
Sep 8, 2023
Sep 8, 2023

Repository files navigation

helm-schema


Latest Release Go Docs Build Status MIT LICENSE pre-commit Go Report

This tool tries to help you to easily create some nice JSON schema for your helm chart.

By default it will traverse the current directory and look for Chart.yaml files. For every file, helm-schema will try to find one of the given value filenames. The first files found will be read and a jsonschema will be created. For every dependency defined in the Chart.yaml file, a reference to the dependencies JSON schema will be created.

ℹ️ The tool uses jsonschema Draft 7, because the library helm uses only supports that version.

Installation

Via go install:

go install github.com/dadav/helm-schema/cmd/helm-schema@latest

From aur:

paru -S helm-schema

Usage

Pre-commit hook

If you want to automatically generate a new values.schema.json if you change the values.yaml file, you can do the following:

  1. Install pre-commit
  2. Copy the .pre-commit-config.yaml to your helm chart repository.
  3. Then run these commands:
pre-commit install
pre-commit install-hooks

Running the binary directly

You can also just run the binary yourself:

helm-schema

Options

The binary has the following options:

Flags:
  -c, --chart-search-root string      "directory to search recursively within for charts (default ".")"
  -x, --dont-strip-helm-docs-prefix   "Disable the removal of the helm-docs prefix (--)"
  -d, --dry-run                       "don't actually create files just print to stdout passed"
  -h, --help                          "help for helm-schema"
  -s, --keep-full-comment             "Keep the whole leading comment (default: cut at empty line)"
  -l, --log-level string              "Level of logs that should printed, one of (panic, fatal, error, warning, info, debug, trace) (default "info")"
  -n, --no-dependencies               "don't analyze dependencies"
  -o, --output-file string            "jsonschema file path relative to each chart directory to which jsonschema will be written (default 'values.schema.json')"
  -f, --value-files strings           "filenames to check for chart values (default [values.yaml])"
  -k, --skip-auto-generation strings  "skip the auto generation for these fields (default [])"
  -v, --version                       "version for helm-schema"

Annotations

The jsonschema must be between two entries of : # @schema, example below :

# @schema
# my: annotation
# @schema

⚠️ It must be written in front of the key you want to annotate.

ℹ️ If you don't use the properties option on hashes or don't use items on arrays, it will be parsed from the values and their annotations instead.

Available annotations

Key Description Values
type Defines the jsonschema-type of the object. Multiple values are suported (e.g. [string, integer]) as a shortcut to anyOf object, array, string, number, integer, boolean or null
title Defines the title field of the object Defaults to the key itself
description Defines the description field of the object. Defaults to the comment which has no # @schema prefix
default Sets the default value and will be displayed first on the users IDE
properties Contains a map with keys as property names and values as schema Takes an object
pattern Regex pattern to test the value Takes an string
format The format keyword allows for basic semantic identification of certain kinds of string values
required Adds the key to the required items true or false
deprecated Marks the option as deprecated true or false
items Contains the schema that describes the possible array items
enum Multiple allowed values
const Single allowed value
examples Some examples you can provide for the end user Takes an array
minimum Minimum value. Can't be used with exclusiveMinimum Must be smaller than maximum or exclusiveMaximum (if used)
exclusiveMinimum Exclusive minimum. Can't be used with minimum Must be smaller than maximum or exclusiveMaximum (if used)
maximum Maximum value. Can't be used with exclusiveMaximum Must be bigger than minimum or exclusiveMinimum (if used)
exclusiveMaximum Exclusive maximum value. Can't be used with maximum Must be bigger than minimum or exclusiveMinimum (if used)
multipleOf The yaml-value must be a multiple of. For example: If you set this to 10, allowed values would be 0, 10, 20, 30... Takes an int
additionalProperties Allow additional keys in maps. Useful if you want to use for example additionalAnnotations, which will be filled with keys that the jsonschema can't know Defaults to false if the map is not an empty map. Takes a schema or boolean value
patternProperties Contains a map which maps schemas to pattern. If properties match the patterns, the given schema is applied Takes an object
anyOf Accepts an array of schemas. None or one must apply Takes an array
oneOf Accepts an array of schemas. One or more must apply Takes an array
allOf Accepts an array of schemas. All must apply Takes an array
if/then/else if the given schema applies, then also apply the given schema or else the other schema

Validation & completion

To take advantage of the generated values.schema.json, you can use it within your IDE through a plugin supporting the yaml-language-server annotation (e.g. VSCode - YAML)

You'll have to place this line at the top of your values.yaml ($schema=<path-or-url-to-your-schema>) :

# yaml-language-server: $schema=values.schema.json

foo: bar

Example

Some schema examples you may want to use:

---
# vim: set ft=yaml:
# yaml-language-server: $schema=values.schema.json

# This text will be used as description.
# @schema
# type: integer
# minimum: 0
# @schema
foo: 1

# You can define multiple types as an array.
# @schema
# type: [string, integer]
# minimum: 0
# @schema
fool: 1

# Same as above but more verbose.
# @schema
# anyOf:
#   - type: string
#   - type: integer
# minimum: 0
# @schema
foot: 1

# A pattern is also possible.
# In this case null or some string starting with foo.
# @schema
# anyOf:
#   - type: "null"
#   - pattern: ^foo
# @schema
bar:

# If you don't use `type`, the current value type will be used.
# @schema
# title: Some title
# description: Some description
# @schema
baz: foo

# By default every property is a required property,
# you can disable this with `required=false`
# @schema
# required: false
# @schema
bax: foo

# You can also use conditional settings with if/then/else
# @schema
# anyOf:
#   - type: "null"
#   - type: string
# if:
#   type: "null"
# then:
#   description: It's a null value
# else:
#   description: It's a string
# @schema
bay:

# If you want to specify a schema for possible array values without using a default value,
# do it like this:
# @schema
# type: array
# items:
#   type: object
#   properties:
#     foo:
#       type: integer
#     bar:
#       type: string
# @schema
bazi: []

helm-docs

If you're using helm-docs, then you can combine both annotations and use both pre-commit hooks to automatically generate your documentation (e.g. README.md) alongside your values.schema.json. Here's how:

# @schema
# type: array
# @schema
# -- helm-docs description here
foo: []

💡 Make sure to place the @schema annotations before the actual field description to avoid having it in your helm-docs generated table

Dependencies

Per default, helm-schema will try to also create the schemas for the dependencies in the charts directory. These schemas will be added as properties in the main schema, but the requiredProperties field will be nullified. Otherwise you would have to always overwrite all the required fields.

If you don't want to generate jsonschemas for dependencies, you can use the -n option.

Limitations

You can't change the jsonschema for dependencies by using @schema annotations on dependency config values. For example:

# foo is a dependency chart
foo:
  # You can't change the schema here, this has no effect.
  # @schema
  # type: number
  # @schema
  bar: 1

License

MIT