kubernetes/kops · error
error marshaling json: %v
Error message
error marshaling json: %v
What it means
After assembling the ExecCredential (cached or freshly issued), the helper serializes it with json.MarshalIndent for output to kubectl. A marshaling failure returns this wrapped error. In practice this is nearly impossible with the fixed ExecCredential struct (only string, time.Time fields), so it almost always indicates corrupt in-memory data such as a cache object with a non-serializable time value.
Source
Thrown at pkg/commands/helpers/kubectl_auth.go:138
klog.Infof("cached credential had wrong api version")
cached = nil
}
isCached := false
if cached != nil {
execCredential = cached
isCached = true
} else {
status, err := buildCredentials(ctx, f, options)
if err != nil {
return err
}
execCredential.Status = *status
}
b, err := json.MarshalIndent(execCredential, "", " ")
if err != nil {
return fmt.Errorf("error marshaling json: %v", err)
}
_, err = out.Write(b)
if err != nil {
return fmt.Errorf("error writing to stdout: %v", err)
}
if !isCached {
if err := os.MkdirAll(filepath.Dir(cacheFilePath), 0o755); err != nil {
klog.Warningf("failed to make cache directory for %q: %v", cacheFilePath, err)
}
if err := os.WriteFile(cacheFilePath, b, 0o600); err != nil {
klog.Warningf("failed to write cache file %q: %v", cacheFilePath, err)
}
}
return nil
}
View on GitHub (pinned to 4c8573c808)
Solutions
- Delete the stale cache file under ~/.kube/cache/kops-authentication/ and re-run so credentials are rebuilt.
- Check klog output for 'cached credential ... was not valid' and remove the referenced file.
- If reproducible in a custom build, inspect the ExecCredential content with a debugger.
Defensive patterns
Strategy: try-catch
Try / catch
err := helpers.RunKubectlAuthHelper(ctx, f, out, options)
if err != nil && strings.Contains(err.Error(), "error marshaling json") {
// clear cache and retry once
os.RemoveAll(filepath.Join(homedir.HomeDir(), ".kube", "cache", "kops-authentication"))
err = helpers.RunKubectlAuthHelper(ctx, f, out, options)
} Prevention
- Do not hand-edit files under ~/.kube/cache/kops-authentication/.
- Treat cache-load warnings in klog as signals to clear the cache.
- Use official kOps builds.
When it happens
Trigger: json.MarshalIndent(execCredential, ...) returns an error — e.g. a cached ExecCredential unmarshaled into the struct carries an invalid/unsupported field state, or an environment where encoding/json is restricted/patched.
Common situations: Hand-edited cache files in ~/.kube/cache/kops-authentication feeding pathological values; running a heavily modified/custom build; extremely rare Go runtime issues.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
- error building annotation patch: %v
- building node patch: %w
- error building node patch: %v
- marshalling nodeupConfig: %w
- error marshaling json: %v
AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05).
Data as JSON: /api/errors/af679ec37a78e9a4.
Report an issue: GitHub.