lima-vm/lima · error

failed to unmarshal service object: %w (line=%#q)

Error message

failed to unmarshal service object: %w (line=%#q)

What it means

After the outer watch-event envelope unmarshals, the `object` field must decode into the internal `service` struct (metadata.namespace, metadata.name, spec ports, etc.). If the object payload does not match that shape, this error is thrown including the decode error and raw line. It means the JSON structure deviates from the expected Kubernetes Service schema the watcher models.

Source

Thrown at pkg/guestagent/kubernetesservice/kubernetesservice.go:173

	for scanner.Scan() {
		line := scanner.Bytes()
		line = bytes.TrimSpace(line)
		if len(line) == 0 {
			continue
		}

		var ev struct {
			Type   eventType       `json:"type"`
			Object json.RawMessage `json:"object"`
		}
		if err := json.Unmarshal(line, &ev); err != nil {
			return fmt.Errorf("failed to unmarshal line %#q: %w", string(line), err)
		}

		var svc service
		if err := json.Unmarshal(ev.Object, &svc); err != nil {
			return fmt.Errorf("failed to unmarshal service object: %w (line=%#q)", err, line)
		}

		key := svc.Metadata.Namespace + "/" + svc.Metadata.Name
		s.rwMutex.Lock()
		switch ev.Type {
		case added, modified:
			s.serviceSpecs[key] = &svc.Spec
		case deleted:
			delete(s.serviceSpecs, key)
		default:
			// NOP
		}
		s.rwMutex.Unlock()
	}

	if err := scanner.Err(); err != nil {
		return fmt.Errorf("failed to scan kubectl event stream: %w", err)
	}

View on GitHub (pinned to dd909d0973)

Solutions

  1. Check the embedded decode error and raw line to see which field mismatches.
  2. Confirm the kubectl command watches only Service objects (correct resource and `-o json` flags).
  3. Compare the object's apiVersion/kind against the expected Service schema.
  4. Pin or adjust the watcher's service struct fields to match the kubectl/API server version's schema.

Example fix

// before: struct lacks a field whose type changed
var svc service
if err := json.Unmarshal(ev.Object, &svc); err != nil { ... }
// after: tolerant decoding of metadata
var svc service
type envelopeMeta struct {
	Metadata struct {
		Name      string `json:"name"`
		Namespace string `json:"namespace"`
	} `json:"metadata"`
}
if err := json.Unmarshal(ev.Object, &svc); err != nil {
	return fmt.Errorf("failed to unmarshal service object: %w (line=%#q)", err, line)
}
Defensive patterns

Strategy: validation

Validate before calling

// verify the event object kind before decoding into service
var probe struct {
	Kind string `json:"kind"`
}
if err := json.Unmarshal(ev.Object, &probe); err != nil || probe.Kind != "Service" {
	return nil // ignore non-Service objects
}

Try / catch

if err := json.Unmarshal(ev.Object, &svc); err != nil {
	log.WithError(err).Warnf("unrecognized service object, skipping line %s", line)
	continue
}

Prevention

When it happens

Trigger: readKubectlStream receives a watch event whose `object` fails to unmarshal into `service` (e.g. a DeletedEvent/NonResource payload, an object with unexpected types like null metadata where strings are expected).

Common situations: kubectl watch emitting non-Service objects due to wrong resource/selector flags; custom or aggregated API objects with divergent schema; newer kubectl output shape changes.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of lima-vm/lima@dd909d0973 (2026-09-01). Data as JSON: /api/errors/f5c46b6cb939b7e0. Report an issue: GitHub.