{"record":{"id":"ad668ac19cc1da2f","repo":"kubernetes/kops","slug":"error-marshaling-s-to-yaml-v","errorCode":null,"errorMessage":"error marshaling %s to yaml: %v","messagePattern":"error marshaling (.+?) to yaml: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/kubemanifest/manifest.go","lineNumber":240,"sourceCode":"\thumanFields := strings.Join(fields, \".\")\n\n\tcurrent := m.data\n\tfor _, field := range fields {\n\t\tv, found := current[field]\n\t\tif !found {\n\t\t\treturn fmt.Errorf(\"field %q in %s not found\", field, humanFields)\n\t\t}\n\n\t\tm, ok := v.(map[string]interface{})\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"field %q in %s was not an object, was %T\", field, humanFields, v)\n\t\t}\n\t\tcurrent = m\n\t}\n\n\tb, err := yaml.Marshal(current)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error marshaling %s to yaml: %v\", humanFields, err)\n\t}\n\n\tif err := yaml.Unmarshal(b, obj); err != nil {\n\t\treturn fmt.Errorf(\"error unmarshaling subobject %s: %v\", humanFields, err)\n\t}\n\n\treturn nil\n}\n\n// Set mutates a subfield to the newValue\nfunc (m *Object) Set(newValue interface{}, fieldPath ...string) error {\n\thumanFields := strings.Join(fieldPath, \".\")\n\n\tcurrent := m.data\n\tif len(fieldPath) >= 2 {\n\t\tfor _, field := range fieldPath[:len(fieldPath)-1] {\n\t\t\tv, found := current[field]\n\t\t\tif !found {","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/kubemanifest/manifest.go#L222-L258","documentation":"kOps' pkg/kubemanifest Object.Reparse extracts a nested subobject of a YAML manifest and round-trips it (yaml.Marshal then yaml.Unmarshal) into a typed Go struct. This error fires when yaml.Marshal fails on the subobject's generic map data. Marshal of map[string]interface{} data rarely fails, so this usually indicates non-serializable content (e.g. values of unsupported Go types injected programmatically).","triggerScenarios":"Calling Object.Reparse(obj, fields...) where the subobject at the given field path contains data yaml.Marshal cannot serialize, such as channels, funcs, or other non-YAML-representable Go values placed into the manifest map in code.","commonSituations":"Custom mutation code that inserts raw Go values (e.g. a struct with private fields or a non-marshalable type) into a kubemanifest Object before reparsing into a typed pod spec; corrupted manifest data built programmatically rather than parsed from YAML.","solutions":["Inspect the value at the field path and ensure only YAML-safe types (string, bool, float64, map[string]interface{}, []interface{}) are stored in the manifest map","Marshal the subobject yourself in a test to reproduce and identify the offending key/value","If the value came from a parsed YAML file, re-validate the source manifest file for corruption or exotic YAML tags"],"exampleFix":"// before: injecting a raw Go struct into the manifest\nmanifest.Set(myCustomStruct{}, \"spec\", \"template\")\n// after: convert to a YAML-safe map first\nb, _ := yaml.Marshal(myCustomStruct)\nm := map[string]interface{}{}\nyaml.Unmarshal(b, &m)\nmanifest.Set(m, \"spec\", \"template\")","handlingStrategy":"validation","validationCode":"if _, err := yaml.Marshal(subObj); err != nil {\n    // do not call Reparse; sanitize the map first\n}","typeGuard":"func isYAMLSafe(v interface{}) bool {\n    switch v.(type) {\n    case nil, string, bool, float64, int, map[string]interface{}, []interface{}:\n        return true\n    }\n    return false\n}","tryCatchPattern":"if err := obj.Reparse(target, \"spec\", \"template\"); err != nil {\n    return fmt.Errorf(\"reparse spec.template failed: %w\", err)\n}","preventionTips":["Store only YAML-decoded generic types in kubemanifest objects","Never inject raw Go structs with private fields into manifest maps","Round-trip test manifest mutations with yaml.Marshal in unit tests"],"tags":["go","yaml","serialization","kubemanifest"],"backgroundTag":"yaml-marshal-failed","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}