# Kubernetes schemas

When a value flows into a field of a built-in Kubernetes resource, `helm-schema` types it from the
Kubernetes JSON schema of that resource. That turns `spec.replicas` into an `int32` with the API's
own description, and a `resources` block into the whole `ResourceRequirements` shape.

The schemas come from
[yannh/kubernetes-json-schema](https://github.com/yannh/kubernetes-json-schema), fetched when first
needed and [cached](https://romnn.github.io/helm-schema/docs/reference/caching/) on disk. A cold and a warm cache give
the same schema.

## Choosing the version

`--k8s-version` picks the Kubernetes version whose schemas are read. The default is `v1.35.0`:

```sh
helm-schema ./mychart --k8s-version v1.34.0
```

The flag repeats, in order of preference: the first version is the primary, and the others are
read, in order, for a resource the primary does not have:

```sh
helm-schema ./mychart --k8s-version v1.34.0 --k8s-version v1.28.0
```

## Automatic version fallback

Charts still ship resources on APIs that recent Kubernetes removed, such as `policy/v1beta1` and
`networking.k8s.io/v1beta1`, both removed in v1.25. `--k8s-version-fallback` adds older minor
versions after a single `--k8s-version`, so their schemas are still found:

```sh
# The default window of 15 older minor versions
helm-schema ./mychart --k8s-version-fallback auto

# Or a window of your own
helm-schema ./mychart --k8s-version-fallback 5
```

The lookup then goes from `v1.35.0` to `v1.34.0`, `v1.33.0` and on, until one has the schema, and
reports [`ResolvedFromFallbackVersion`](https://romnn.github.io/helm-schema/docs/reference/diagnostics/).

- **Only with one `--k8s-version`:** with several, list the versions yourself instead.
- **Not with `--strict-k8s-version`,** which turns fallback off.
- **Only for schema lookup.** Fallback versions never take part in
  [`apiVersion` inference](#apiversion-inference).

## Mirrors

`--k8s-schema-mirror` adds another source of the same schemas, such as an internal copy. It repeats,
and works with and without `--strict-k8s-version`:

```sh
helm-schema ./mychart --k8s-schema-mirror https://schemas.example.com/kubernetes
```

The default source always wins. Each mirror is cached in its own directory, so its files never take
the place of the default's.

## Working offline

Once the cache holds what a chart needs, `--offline` runs without network access:

```sh
helm-schema ./mychart \
  --offline \
  --k8s-schema-cache-dir ./k8s-schema-cache \
  --output values.schema.json
```

- **`--offline`:** never fetches, and only reads the caches. A schema the cache lacks is reported as
  missing, never treated as nonexistent.
- **`--k8s-schema-cache-dir`:** a cache directory you control, filled once online and reused in a
  sealed environment; see [Caching](https://romnn.github.io/helm-schema/docs/reference/caching/).
- **`--no-cache`:** the opposite; skips reading the cache and asks the source again. Answers still
  update the cache, which repairs a stale entry.

To leave Kubernetes types out entirely and keep only what the templates imply:

```sh
helm-schema ./mychart --no-k8s-schemas
```

## `apiVersion` inference

To type a resource's fields, `helm-schema` needs its `apiVersion` and `kind`. It reads them from
the manifest whenever it can, including across `if` and `else` branches and from helpers that
return one of a few literals.

When the `apiVersion` is templated from something unknown, or missing, the lookup fails with
`apiVersion unknown`. `--api-version-guess` turns on a bounded inference:

```sh
helm-schema ./mychart --api-version-guess
```

It takes candidates from, in this order:

1. **your local overrides** in `--crd-override-dir`;
2. **CRDs the chart bundles;**
3. **a built-in list** of Kubernetes and Prometheus Operator kinds that have one obvious
   `apiVersion`, and for a few of them a request to the CRD catalog mirrors.

The first local source that has the kind decides, when it holds exactly one version of it. Otherwise
exactly one candidate across the other sources must remain. A unique answer is reported as
[`InferredApiVersion`](https://romnn.github.io/helm-schema/docs/reference/diagnostics/), and several as
`AmbiguousApiVersion`, in which case the resource stays untyped.

Kinds outside the built-in list, including `Ingress` and `PodDisruptionBudget`, need an
`apiVersion` the template states or a local source; a warm cache never supplies one.
`--strict-api-versions` turns inference off, and cannot be combined with `--api-version-guess`.

## `.Capabilities.APIVersions`

A template that tests `.Capabilities.APIVersions.Has "group/version"` is answered from the
schemas of the primary version, by looking up a resource of that API; a test that names a resource,
such as `"apps/v1/Deployment"`, looks up that kind. Offline, an answer the cache cannot give stays
unknown rather than false.
