Maaz Zindani

Loading the index…

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

kubectl apply rejects a manifest with cannot unmarshal into Go struct field

Your error

Service in version "v1" cannot be handled as a Service: json: cannot unmarshal object into Go struct field ServiceSpec.spec.ports of type []v1.ServicePort2 more lines it can show

Fix it fast

Most common cause, 1 of 3

A list field was written as a map

Put a dash in front of each item so the field becomes a list of objects.

Fix
spec:
  selector:
    app: web
  ports:
    - name: http
      port: 80
      targetPort: 8080
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

The API server decoded your YAML and one field had the wrong shape. cannot unmarshal object into a []v1.ServicePort field means a list written without dashes; number or bool into type string means an unquoted value like 8080 or true; array into a map means dashes where a map belongs. Fix that field. Applies to Kubernetes 1.37.

  • Applies to Kubernetes 1.37, kubectl 1.37
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

The message names three things: the JSON kind it found (object, array, number, bool), the field path, and the Go type it expected. Ask kubectl what that field should look like, using the path from the message.

Run this first
kubectl explain service.spec.ports

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

  1. 1A list field was written as a mapThe error says cannot unmarshal object and the expected type starts with []
  2. 2A string field got an unquoted number or booleanThe error says cannot unmarshal number or cannot unmarshal bool and ends with of type string
  3. 3A map field was written as a listThe error says cannot unmarshal array and the expected type is map[string]string

What does the error look like?

  • Service in version "v1" cannot be handled as a Service: json: cannot unmarshal object into Go struct field ServiceSpec.spec.ports of type []v1.ServicePort
  • json: cannot unmarshal number into Go struct field EnvVar.spec.template.spec.containers.env.value of type string
  • json: cannot unmarshal array into Go struct field ObjectMeta.metadata.labels of type map[string]string

Cause 1 of 3

A list field was written as a map

You seeThe error says cannot unmarshal object and the expected type starts with []

Check

When Kubernetes reports cannot unmarshal object into a type that begins with [], the field takes a list, but the YAML gave it key-value pairs. Service ports, container lists and env are all arrays in the API reference, so each entry needs a leading dash.

Check
kubectl apply --dry-run=server -f service.yaml

Fix

Put a dash in front of each item so the field becomes a list of objects.

Fix
spec:
  selector:
    app: web
  ports:
    - name: http
      port: 80
      targetPort: 8080

Verify

The server-side dry run reports the Service as created or configured, and no unmarshal error appears.

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

Confirmed by kubernetes.io

Did this fix it?

Cause 2 of 3

A string field got an unquoted number or boolean

You seeThe error says cannot unmarshal number or cannot unmarshal bool and ends with of type string

Check

YAML reads 8080, 1.0 and true as a number or boolean, not text. EnvVar value, label values and annotation values are strings in the API, so an unquoted number or flag there fails decoding with this message.

Check
grep -nE "value: [0-9]|value: (true|false)" deployment.yaml

Fix

Wrap the value in quotes. Kubernetes guidance is to quote anything that could be read as a boolean, and the same rule keeps numbers as text.

Fix
env:
  - name: PORT
    value: "8080"
  - name: FEATURE_FLAG
    value: "true"

Verify

The dry run passes and the running pod shows the variable as text.

Verify
kubectl exec deploy/web -- printenv PORT

Confirmed by kubernetes.io

Did this fix it?

Cause 3 of 3

A map field was written as a list

You seeThe error says cannot unmarshal array and the expected type is map[string]string

Check

Labels, annotations, selectors and matchLabels are maps of string keys to string values. Writing them with dashes turns them into a list, which the decoder cannot place into a map.

Check
kubectl explain deployment.spec.selector.matchLabels

Fix

Remove the dashes so each label is a plain key: value line under the map.

Fix
metadata:
  labels:
    app: web
    tier: frontend

Verify

The object applies and the labels are visible on it.

Verify
kubectl get deploy web --show-labels

Confirmed by kubernetes.io

Did this fix it?

How do I stop it coming back?

  • Run kubectl apply --dry-run=server on every changed manifest in CI before merging.
  • Quote every env, label and annotation value in templates, even when it looks like a number.
  • Check field shapes with kubectl explain instead of copying fragments from older manifests.

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.