{"record":{"id":"84cfc3c2c6703670","repo":"cilium/cilium","slug":"failed-to-unmarshal-json-w-84cfc3","errorCode":null,"errorMessage":"failed to unmarshal json: %w","messagePattern":"failed to unmarshal json: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cilium-cli/features/status.go","lineNumber":367,"sourceCode":"}\n\nfunc ciliumOperatorBinary(pod corev1.Pod) string {\n\tfor _, container := range pod.Spec.Containers {\n\t\tif container.Name == defaults.OperatorContainerName {\n\t\t\tfor _, cmd := range container.Command {\n\t\t\t\tif strings.Contains(cmd, \"cilium-operator\") {\n\t\t\t\t\treturn cmd\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn \"\"\n}\n\nfunc nodeStatusFromOutput(output string) ([]*models.Metric, error) {\n\tvar encStatus []*models.Metric\n\tif err := json.Unmarshal([]byte(output), &encStatus); err != nil {\n\t\treturn []*models.Metric{}, fmt.Errorf(\"failed to unmarshal json: %w\", err)\n\t}\n\treturn encStatus, nil\n}\n\ntype statusPrinter interface {\n\tprintHeader(sorted []string) error\n\tprintNode(metricName, labels string, isBinary bool, values map[float64]struct{}, key string, nodesSorted []string, metricsPerNode map[string]map[string]float64)\n\tend() error\n}\n\n// parseNameAndLabels splits the key into name and labels based on the first \";\" separator\nfunc parseNameAndLabels(key string) (string, string) {\n\tif before, after, found := strings.Cut(key, \";\"); found {\n\t\treturn before, after\n\t}\n\treturn key, \"\"\n}\n","sourceCodeStart":349,"sourceCodeEnd":385,"githubUrl":"https://github.com/cilium/cilium/blob/ac7b90affa4baf0642e6685319d56907b3a73a6d/cilium-cli/features/status.go#L349-L385","documentation":" Shared JSON parser for feature-metrics output: `json.Unmarshal([]byte(output), &encStatus)` into []*models.Metric failed. Any caller (agent or operator fetch) surfaces this wrapped error when the command output is not the expected JSON metric array.","triggerScenarios":" Output of the in-pod command contains non-JSON content — command-not-found usage text, log lines, truncated output, or empty string — so Unmarshal fails.","commonSituations":" Wrong container command produced help/usage text; pod output buffer truncated; Cilium version emits a different JSON schema than models.Metric.","solutions":["Run the same command manually with `kubectl exec` and confirm it emits a JSON array","Fix the container/binary selection so the real cilium binary runs","Align cilium-cli and Cilium versions so the JSON schema matches models.Metric","Callers should catch this and fall back to skipping metrics collection for that node"],"exampleFix":"null","handlingStrategy":"try-catch","validationCode":"if strings.TrimSpace(output) == \"\" {\n\treturn errors.New(\"empty metrics output; cannot unmarshal\")\n}","typeGuard":"func parsesAsMetrics(output string) ([]*models.Metric, bool) {\n\tvar m []*models.Metric\n\terr := json.Unmarshal([]byte(output), &m)\n\treturn m, err == nil\n}","tryCatchPattern":"enc, err := nodeStatusFromOutput(out)\nif err != nil {\n\tvar uerr *json.UnmarshalTypeError\n\tif errors.As(err, &uerr) {\n\t\tlog.Printf(\"schema mismatch at offset %d: %v\", uerr.Offset, uerr)\n\t}\n\treturn degradeGracefully()\n}","preventionTips":["Verify in-pod command output with kubectl exec before integrating","Keep models.Metric schema aligned with the Cilium version's JSON output","Check for command-usage text (wrong binary) as the root cause","Log raw output on unmarshal failure for debugging"],"tags":["json","unmarshal","parsing"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"ac7b90affa4baf0642e6685319d56907b3a73a6d","analyzedAt":"2026-08-31T18:27:15.868Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}