{"record":{"id":"3fe1ec2a5d35c145","repo":"kubernetes/kops","slug":"marshaling-karpenter-resource-for-q-w","errorCode":null,"errorMessage":"marshaling Karpenter resource for %q: %w","messagePattern":"marshaling Karpenter resource for %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"upup/pkg/fi/cloudup/template_functions_karpenter.go","lineNumber":209,"sourceCode":"\tnodeClass, err := tf.buildKarpenterEC2NodeClass(ig)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\treturn marshalKarpenterResource(ig, nodeClass)\n}\n\nfunc (tf *TemplateFunctions) KarpenterNodePool(ig *kops.InstanceGroup) (string, error) {\n\tnodePool, err := tf.buildKarpenterNodePool(ig)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\treturn marshalKarpenterResource(ig, nodePool)\n}\n\nfunc marshalKarpenterResource(ig *kops.InstanceGroup, object interface{}) (string, error) {\n\tdata, err := yaml.Marshal(object)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"marshaling Karpenter resource for %q: %w\", ig.Name, err)\n\t}\n\treturn strings.TrimSpace(string(data)), nil\n}\n\nfunc (tf *TemplateFunctions) buildKarpenterEC2NodeClass(ig *kops.InstanceGroup) (*karpenterEC2NodeClass, error) {\n\tamiSelectorTerms, err := buildKarpenterAMITerms(ig.Spec.Image)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"building amiSelectorTerms for %q: %w\", ig.Name, err)\n\t}\n\n\tinstanceProfile, err := tf.LinkToIAMInstanceProfile(ig)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"building instance profile for %q: %w\", ig.Name, err)\n\t}\n\n\ttags, err := tf.CloudTagsForInstanceGroup(ig)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"building tags for %q: %w\", ig.Name, err)","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/upup/pkg/fi/cloudup/template_functions_karpenter.go#L191-L227","documentation":"Returned by marshalKarpenterResource when yaml.Marshal fails to serialize a Karpenter resource (EC2NodeClass or NodePool) for the given instance group. This indicates the in-memory Karpenter object contains a value the YAML encoder cannot represent (e.g. cyclic references, unsupported types, or a custom marshaler error). The instance group name is included for identification.","triggerScenarios":"Calling KarpenterEC2NodeClass or KarpenterNodePool for an instance group where the built karpenterEC2NodeClass/nodePool struct contains unmarshalable data passed to yaml.Marshal.","commonSituations":"A newly added field on the Karpenter structs has a type go-yaml cannot encode; a custom MarshalYAML returns an error; non-primitive runtime values leaked into a field.","solutions":["Read the wrapped yaml.Marshal error to find the failing field","Inspect karpenterEC2NodeClass/nodePool fields populated for the named instance group","Ensure new fields are YAML-serializable types (strings, ints, maps, slices)","Add or fix custom MarshalYAML implementations if needed"],"exampleFix":"// before: field with unsupported type\nstatus any // holds arbitrary runtime values\n// after\nstatus string // or a struct of YAML-friendly typed fields","handlingStrategy":"try-catch","validationCode":"if _, err := yaml.Marshal(object); err != nil {\n\treturn fmt.Errorf(\"karpenter resource for %q not YAML-serializable: %w\", ig.Name, err)\n}","typeGuard":"func isYAMLEncodable(v interface{}) bool { _, err := yaml.Marshal(v); return err == nil }","tryCatchPattern":"out, err := karpenterFn(ig)\nif err != nil {\n\treturn fmt.Errorf(\"karpenter resource for %q: %w\", ig.Name, err)\n}","preventionTips":["Keep karpenterEC2NodeClass/NodePool fields YAML-friendly","Test marshalKarpenterResource for each instance group type in CI","Avoid embedding arbitrary runtime values in resource fields","Fix custom MarshalYAML methods to handle nil receivers"],"tags":["yaml","serialization","karpenter","kops"],"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"}