Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

kubectl apply rejects a manifest with strict decoding error: unknown field

Your error

Error from server (BadRequest): error when creating "deploy.yaml": Deployment in version "v1" cannot be handled as a Deployment: strict decoding error: unknown field "spec.template.metadata.containers"2 more lines it can show

Fix it fast

Most common cause, 1 of 3

A key sits under the wrong parent, or is spelled wrong

Move the block to the parent that kubectl explain shows for it, or correct the spelling, then run the server dry run again.

Fix
spec:
  template:
    metadata:
      labels:
        app: web
    spec:            # containers belong here, not under metadata
      containers:
        - name: web
          image: nginx:1.29
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

strict decoding error: unknown field means the API server met a key that its schema for that object lacks, and kubectl's default strict validation rejects the request. The key is misplaced or misspelled, newer than your cluster, or missing from an old CustomResourceDefinition. Fix whatever the quoted path points to. Applies to Kubernetes 1.27 to 1.37.

  • Applies to Kubernetes 1.37 (kubectl 1.37.2), Kubernetes 1.27 and later API servers, Helm 4.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Send the manifest to the server without saving it, so every unknown path is listed in one go, and note the server's version. The quoted path plus the version tell the causes apart.

Run this first
kubectl apply --dry-run=server -f deploy.yaml
kubectl version

Then find the line you see in this list and open its fix:

  1. 1A key sits under the wrong parent, or is spelled wrongThe path joins real field names in an impossible place, such as spec.template.metadata.containers or spec.limits, or the last word has a typo
  2. 2The field is newer than the cluster's API serverThe YAML matches current Kubernetes docs, the path ends in a recently added field such as initContainers[0].restartPolicy, and kubectl version shows an older Server Version
  3. 3A custom resource uses a field the installed CRD does not define yetThe kind is a custom resource (cert-manager, Prometheus Operator and similar), the operator or chart was upgraded recently, and the unknown path is a new option from its changelog

What does the error look like?

  • Error from server (BadRequest): error when creating "deploy.yaml": Deployment in version "v1" cannot be handled as a Deployment: strict decoding error: unknown field "spec.template.metadata.containers"
  • Job in version "v1" cannot be handled as a Job: strict decoding error: unknown field "spec.template.spec.initContainers[0].restartPolicy"
  • strict decoding error: unknown field

Cause 1 of 3

A key sits under the wrong parent, or is spelled wrong

You seeThe path joins real field names in an impossible place, such as spec.template.metadata.containers or spec.limits, or the last word has a typo

Check

Most unknown field errors come from indentation. In the dotnet-docker sample that produced unknown field spec.template.metadata.containers, containers and volumes had been placed under template.metadata, where Kubernetes has no such keys, instead of under template.spec. A spelling slip such as imagePullPolicyy fails the same way.

Check
kubectl explain deployment.spec.template.spec.containers

Fix

Move the block to the parent that kubectl explain shows for it, or correct the spelling, then run the server dry run again. Each path it still prints is another key to move.

Fix
spec:
  template:
    metadata:
      labels:
        app: web
    spec:            # containers belong here, not under metadata
      containers:
        - name: web
          image: nginx:1.29

Verify

The dry run completes and no BadRequest line is printed.

Verify
kubectl apply --dry-run=server -f deploy.yaml

Confirmed by github.com

Did this fix it?

Cause 2 of 3

The field is newer than the cluster's API server

You seeThe YAML matches current Kubernetes docs, the path ends in a recently added field such as initContainers[0].restartPolicy, and kubectl version shows an older Server Version

Check

An API server only knows the fields of its own release. The Kubernetes website issue for the sidecar examples shows exactly this: a Job using restartPolicy on an init container failed on a 1.27.3 cluster, because native sidecar containers first appeared in 1.28 and have been stable only since 1.33. Docs for the newest release describe fields an older cluster rejects.

Check
kubectl version
kubectl explain pod.spec.initContainers.restartPolicy

Fix

Upgrade the cluster to a release that has the field, or write the manifest for the version you run, for example an ordinary sidecar in containers instead of a restartable init container. Use the docs version selector to read the pages for your cluster's minor version.

Fix
kubectl version            # Server Version: v1.27.x
# either upgrade the control plane to 1.33 or later,
# or remove restartPolicy: Always from initContainers and run the helper in containers

Verify

kubectl explain now documents the field (after an upgrade), or the reworked manifest passes the server dry run.

Verify
kubectl apply --dry-run=server -f job.yaml

Confirmed by github.com

Did this fix it?

Cause 3 of 3

A custom resource uses a field the installed CRD does not define yet

You seeThe kind is a custom resource (cert-manager, Prometheus Operator and similar), the operator or chart was upgraded recently, and the unknown path is a new option from its changelog

Check

Strict validation also applies to custom resources, whose schema comes from the CustomResourceDefinition stored in the cluster. Helm installs a chart's CRDs once and skips them afterwards; its docs state that upgrading or deleting CRDs is not supported. After a chart upgrade the CRD can stay old while your manifests already use the new field.

Check
kubectl explain <kind>.spec --recursive

Fix

Apply the CRDs that ship with the chart version you now run, then apply your custom resource again. Read the operator's upgrade notes first, since some releases need a migration step.

Fix
helm show crds <repo>/<chart> --version <chart-version> | kubectl apply --server-side -f -
kubectl apply -f my-resource.yaml

Verify

kubectl explain on the custom kind lists the new field and the resource is created.

Verify
kubectl explain <kind>.spec

Confirmed by helm.sh

Did this fix it?

How do I stop it coming back?

  • Never switch to --validate=ignore to get past this: the server then drops the unknown key without a word, and the setting you wrote silently does nothing.
  • Pin the Kubernetes docs version you copy examples from to your cluster's minor version.
  • Make CRD upgrades an explicit step in the runbook for every Helm chart that ships CRDs.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Dispatch Pro.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.