{"record":{"id":"a26695e516fa3939","repo":"derailed/k9s","slug":"unable-to-marshal-resource-w","errorCode":null,"errorMessage":"unable to marshal resource %w","messagePattern":"unable to marshal resource %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/dao/generic.go","lineNumber":104,"sourceCode":"\n\treturn dial.Namespace(ns).Get(ctx, n, opts)\n}\n\n// Describe describes a resource.\nfunc (g *Generic) Describe(path string) (string, error) {\n\treturn Describe(g.Client(), g.gvr, path)\n}\n\n// ToYAML returns a resource yaml.\nfunc (g *Generic) ToYAML(path string, showManaged bool) (string, error) {\n\to, err := g.Get(context.Background(), path)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\traw, err := ToYAML(o, showManaged)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"unable to marshal resource %w\", err)\n\t}\n\treturn raw, nil\n}\n\n// Delete deletes a resource.\nfunc (g *Generic) Delete(ctx context.Context, path string, propagation *metav1.DeletionPropagation, grace Grace) error {\n\tns, n := client.Namespaced(path)\n\tauth, err := g.Client().CanI(ns, g.gvr, n, []string{client.DeleteVerb})\n\tif err != nil {\n\t\treturn err\n\t}\n\tif !auth {\n\t\treturn fmt.Errorf(\"user is not authorized to delete %s\", path)\n\t}\n\n\tvar gracePeriod *int64\n\tif grace != DefaultGrace {\n\t\tgracePeriod = (*int64)(&grace)","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/derailed/k9s/blob/2d3ccc6ba2ce98c3781bfc441bb3e884f072774f/internal/dao/generic.go#L86-L122","documentation":"Generic.ToYAML (internal/dao/generic.go:92-106) fetches a resource and serializes it (dao.ToYAML) for the YAML view. If serialization of the fetched object fails, this error wraps the marshal error (note: the format string omits a colon, so the message reads \"unable to marshal resource <err>\").","triggerScenarios":"The unstructured object contains values go-yaml cannot encode: NaN/+Inf/-Inf float values (common in metrics CRDs like ServiceMonitor or PrometheusRule objects and status fields), unsupported map key types, or huge/malformed annotations - data that survived JSON decoding but fails YAML marshaling.","commonSituations":"Viewing YAML of monitoring CRDs whose threshold fields legitimately hold Infinity; resources with exotic annotation payloads written by operators; k9s showManaged toggling which strips/keeps managed fields during marshal.","solutions":["Retry the view with managed fields hidden (toggle y) - stripping managedFields sometimes removes the offending payload","Inspect the resource with kubectl get <res> <name> -o yaml to spot NaN/Infinity values","Report/fix the operator writing non-finite numbers; represent them as strings (\"Infinity\") as Prometheus configs do","As a last resort read the object via kubectl, since kubectl's JSON-path output tolerates these values"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Pre-scan an unstructured object for values yaml cannot encode (NaN/Inf).\nfunc YamlSafe(u *unstructured.Unstructured) error {\n\tvar walk func(v any) error\n\twalk = func(v any) error {\n\t\tswitch t := v.(type) {\n\t\tcase float64:\n\t\t\tif math.IsNaN(t) || math.IsInf(t, 0) { return fmt.Errorf(\"non-finite number %v in %s\", t, u.GetName()) }\n\t\tcase map[string]any:\n\t\t\tfor _, x := range t { if err := walk(x); err != nil { return err } }\n\t\tcase []any:\n\t\t\tfor _, x := range t { if err := walk(x); err != nil { return err } }\n\t\t}\n\t\treturn nil\n\t}\n\treturn walk(u.Object)\n}","typeGuard":null,"tryCatchPattern":"raw, err := g.ToYAML(path, showManaged)\nif err != nil && strings.Contains(err.Error(), \"unable to marshal resource\") {\n    // degrade gracefully: dump JSON instead, which tolerates these values\n    raw, err = json.MarshalIndent(o.Object, \"\", \"  \")\n}\nif err != nil { return \"\", err }","preventionTips":["Store thresholds as strings (\"Infinity\"), never bare .inf/NaN in CRDs","Wrap the YAML view in a JSON fallback so one bad field does not break viewing","Toggle managed-fields off first; it removes payload that sometimes carries the bad value"],"tags":["go","yaml","k9s","marshal","crd","metrics"],"backgroundTag":null,"analyzedSha":"2d3ccc6ba2ce98c3781bfc441bb3e884f072774f","analyzedAt":"2026-08-15T16:09:14.432Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}