abiosoft/colima · error

error encoding kubernetes config to json: %w

Error message

error encoding kubernetes config to json: %w

What it means

Returned by kubernetesRuntime.setConfig when json.Marshal of the config.Kubernetes struct (version, type, k3s args etc.) fails before persisting it to the guest's config store via c.guest.Set(ConfigKey, ...). In practice this error is nearly unreachable: config.Kubernetes is a plain serializable struct of strings/bools, so the Go encoding/json package will not fail on it in shipped code.

Source

Thrown at environment/container/kubernetes/kubernetes.go:86

	return c.guest.RunQuiet("sudo", "service", "k3s", "status") == nil
}

func (c kubernetesRuntime) runtime() string {
	return c.guest.Get(environment.ContainerRuntimeKey)
}

func (c kubernetesRuntime) config() config.Kubernetes {
	conf := config.Kubernetes{Version: DefaultVersion}
	if b := c.guest.Get(ConfigKey); b != "" {
		_ = json.Unmarshal([]byte(b), &conf)
	}
	return conf
}

func (c kubernetesRuntime) setConfig(conf config.Kubernetes) error {
	b, err := json.Marshal(conf)
	if err != nil {
		return fmt.Errorf("error encoding kubernetes config to json: %w", err)
	}

	return c.guest.Set(ConfigKey, string(b))
}

func (c *kubernetesRuntime) Provision(ctx context.Context) error {
	log := c.Logger(ctx)
	a := c.Init(ctx)
	if c.Running(ctx) {
		return nil
	}

	appConf, ok := ctx.Value(config.CtxKey()).(config.Config)
	runtime := appConf.Runtime
	conf := appConf.Kubernetes

	if !ok {
		// this should be a restart/start while vm is active

View on GitHub (pinned to c3a5f9184d)

Solutions

  1. If embedding/extending colima: audit any custom fields you added to config.Kubernetes for channels, funcs, or cyclic MarshalJSON.
  2. Upgrade/repair your colima build — a stock binary cannot produce this from user input.
  3. As a workaround, reset stored settings so a fresh config is written: 'colima delete --data kubernetes' (or 'colima delete -f' then 'colima start --kubernetes').
  4. If you maintain a fork, add a unit test asserting json.Marshal(config.Kubernetes{}) never errors.
Defensive patterns

Strategy: validation

Validate before calling

// if extending config.Kubernetes in a fork, assert serializability at startup
if _, err := json.Marshal(config.Kubernetes{}); err != nil {
    return fmt.Errorf("config.Kubernetes is not json-serializable: %w", err)
}

Try / catch

b, err := json.Marshal(conf)
if err != nil {
    // unreachable in practice: fall back to defaults rather than failing the start
    log.Warnln(fmt.Errorf("error encoding kubernetes config to json: %w", err))
    b, _ = json.Marshal(config.Kubernetes{Version: DefaultVersion})
}

Prevention

When it happens

Trigger: Only achievable if config.Kubernetes were extended with a json-incompatible field (chan, func, complex, or a MarshalJSON that errors), or if a value of a custom type errors at runtime; today it is defensive programming around json.Marshal. The realistic call path is any 'colima start --kubernetes --kubernetes-version ...' that writes runtime settings.

Common situations: Virtually none on released builds — if seen, it indicates a fork or custom build of colima with a broken Kubernetes settings struct, or memory corruption; treat as an internal invariant violation rather than an operational issue.

Related errors


AI-assisted analysis of abiosoft/colima@c3a5f9184d (2026-08-15). Data as JSON: /api/errors/9f16f552264264db. Report an issue: GitHub.