# Getting started

This page takes a small chart from no schema to Helm rejecting a wrong value. `helm-schema` reads
the templates itself and needs no cluster; Helm is only needed to see the result in step 3.

## 1. Install helm-schema

Pick one:

**Homebrew**

```sh
brew install --cask romnn/tap/helm-schema
```

**mise**

```sh
mise use github:romnn/helm-schema
```

**Cargo**

Builds from source, which takes a few minutes. The crate is `helm-schema-cli`, and the binary it
installs is `helm-schema`.

```sh
cargo install --locked helm-schema-cli
```

**Binary**

Every [release](https://github.com/romnn/helm-schema/releases) has archives for:

- **Linux:** `x86_64` and `aarch64`.
- **macOS:** Intel and Apple Silicon.
- **Windows:** `x86_64` and `aarch64`.

Extract `helm-schema` from the archive for your platform and put it on your `PATH`.

## 2. Generate a schema

Take this small chart, `quickstart`, with a Deployment and a Service:

**values.yaml**

```yaml {title="values.yaml"}
replicaCount: 2

image:
  repository: nginx
  tag: "1.27"

service:
  port: 80

resources:
  limits:
    cpu: 500m
    memory: 256Mi
```

**deployment.yaml**

```helm {title="templates/deployment.yaml"}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: quickstart
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    spec:
      containers:
        - name: app
          image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
          resources: {{ toYaml .Values.resources | nindent 12 }}
```

**service.yaml**

```helm {title="templates/service.yaml"}
apiVersion: v1
kind: Service
metadata:
  name: quickstart
spec:
  ports:
    - port: {{ .Values.service.port }}
```

Point `helm-schema` at the chart directory, or at a packaged `.tgz`, and write the schema where Helm
looks for it, beside `values.yaml`:

```sh
helm-schema ./quickstart --output quickstart/values.schema.json
```

Without `--output`, the schema goes to standard output, and warnings always go to standard error.
The first run downloads the Kubernetes schema of each resource the chart renders and caches it;
[Caching](https://romnn.github.io/helm-schema/docs/reference/caching/) says where, and how to run without network.

## 3. Let Helm check values

With the schema beside `values.yaml`, Helm checks the values on every `install`, `upgrade`, `lint`
and `template`. A values file with a misspelled key and an image written as one string:

```yaml {title="my-values.yaml"}
replicaCont: 3
image: nginx:1.27
```

With the schema, Helm stops before rendering and names both mistakes:

```console
$ helm template ./quickstart -f my-values.yaml
Error: values don't meet the specifications of the schema(s) in the following chart(s):
quickstart:
- at '/image': got string, want object
- at '': additional properties 'replicaCont' not allowed
```

Before step 2, without the schema, the same command got further: Helm rendered until the template
failed, and never reported the misspelled key:

```console
$ helm template ./quickstart -f my-values.yaml
Error: quickstart/templates/deployment.yaml:11:27
  executing "quickstart/templates/deployment.yaml" at <.Values.image.repository>:
    can't evaluate field repository in type interface {}
```

## 4. Read the schema

Each value's type is in the schema's `properties`. This is that part, printed with
`jq .properties quickstart/values.schema.json`:

```json {collapse=24}
{
  "global": {},
  "image": {
    "additionalProperties": {},
    "properties": {
      "repository": {},
      "tag": {}
    },
    "type": "object"
  },
  "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"
      }
    ]
  },
  "resources": {
    "additionalProperties": {},
    "description": "ResourceRequirements describes the compute resource requirements.",
    "properties": {
      "claims": {
        "description": "Claims lists the names of resources, defined in spec.resourceClaims, that are used by this container.\n\nThis field depends on the DynamicResourceAllocation feature gate.\n\nThis field is immutable. It can only be set for containers.",
        "items": {
          "description": "ResourceClaim references one entry in PodSpec.ResourceClaims.",
          "properties": {
            "name": {
              "description": "Name must match the name of one entry in pod.spec.resourceClaims of the Pod where this field is used. It makes that resource available inside a container.",
              "type": "string"
            },
            "request": {
              "description": "Request is the name chosen for a request in the referenced claim. If empty, everything from the claim is made available, otherwise only the result of this request.",
              "type": "string"
            }
          },
          "required": [
            "name"
          ],
          "type": "object"
        },
        "type": "array",
        "x-kubernetes-list-map-keys": [
          "name"
        ],
        "x-kubernetes-list-type": "map"
      },
      "limits": {
        "additionalProperties": {
          "oneOf": [
            {
              "type": "string"
            },
            {
              "type": "number"
            }
          ]
        },
        "description": "Limits describes the maximum amount of compute resources allowed. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/",
        "properties": {
          "cpu": {
            "type": "string"
          },
          "memory": {
            "type": "string"
          }
        },
        "type": "object"
      },
      "requests": {
        "additionalProperties": {
          "oneOf": [
            {
              "type": "string"
            },
            {
              "type": "number"
            }
          ]
        },
        "description": "Requests describes the minimum amount of compute resources required. If Requests is omitted for a container, it defaults to Limits if that is explicitly specified, otherwise to an implementation-defined value. Requests cannot exceed Limits. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/",
        "type": "object"
      }
    },
    "type": "object"
  },
  "service": {
    "additionalProperties": {},
    "properties": {
      "port": {
        "anyOf": [
          {
            "description": "The port that will be exposed by this service.",
            "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"
          }
        ]
      }
    },
    "required": [
      "port"
    ],
    "type": "object"
  }
}
```

To keep it readable, this schema was generated with `--no-minimize`, which writes each shared
definition out in place. The conditions between values are in the schema's `allOf`, beside
`properties`.

Nothing in the chart states a type, yet every value has one:

- **`replicaCount`** goes into `spec.replicas`, so it is an `int32`; the string member accepts a
  quoted number such as `"2"`.
- **`resources`** goes into a container's `resources`, so it gets the whole
  `ResourceRequirements` shape, though `values.yaml` only sets two limits.
- **The root** has `additionalProperties: false`, which reports a misspelled key, and `required`
  lists the values the templates always render.

## 5. Keep it current

The schema changes whenever the templates do. The same chart and options always give the same
bytes, so commit `values.schema.json` and let CI regenerate it and fail on a difference:
[Continuous integration](https://romnn.github.io/helm-schema/docs/ci/) has the workflow.

Next:

- **[How it works](https://romnn.github.io/helm-schema/docs/how-it-works/):** what the schema is built from, and what it
  never guesses.
- **[Template analysis](https://romnn.github.io/helm-schema/docs/guide/template-analysis/):** how `if`, `with` and `range`
  shape the schema.
- **[Configuration](https://romnn.github.io/helm-schema/docs/reference/configuration/):** `helm-schema.yaml`, for a chart
  whose schema is too large or slow for Helm.
