{"record":{"id":"707ee0444d03ae77","repo":"kubernetes/kops","slug":"unable-to-convert-t-to-unstructured-w","errorCode":null,"errorMessage":"unable to convert %T to unstructured: %w","messagePattern":"unable to convert %T to unstructured: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/kubemanifest/manifest.go","lineNumber":57,"sourceCode":"func NewObject(data map[string]interface{}) *Object {\n\treturn &Object{data: data}\n}\n\n// ToUnstructured converts the object to an unstructured.Unstructured\nfunc (o *Object) ToUnstructured() *unstructured.Unstructured {\n\treturn &unstructured.Unstructured{Object: o.data}\n}\n\n// GroupVersionKind returns the group/version/kind information for the object\nfunc (o *Object) GroupVersionKind() schema.GroupVersionKind {\n\treturn o.ToUnstructured().GroupVersionKind()\n}\n\n// FromRuntimeObject converts from a runtime.Object.\nfunc FromRuntimeObject(obj runtime.Object) (*Object, error) {\n\tdata, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"unable to convert %T to unstructured: %w\", obj, err)\n\t}\n\treturn NewObject(data), nil\n}\n\n// ObjectList describes a list of objects, allowing us to add bulk-methods\ntype ObjectList []*Object\n\n// LoadObjectsFrom parses multiple objects from a yaml file\nfunc LoadObjectsFrom(contents []byte) (ObjectList, error) {\n\tvar objects []*Object\n\n\tsections := text.SplitContentToSections(contents)\n\n\tfor _, section := range sections {\n\t\t// We need this so we don't error on a section that is empty / commented out\n\t\tif !hasYAMLContent(section) {\n\t\t\tcontinue\n\t\t}","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/kubemanifest/manifest.go#L39-L75","documentation":"FromRuntimeObject converts a typed Kubernetes runtime.Object into an unstructured map via runtime.DefaultUnstructuredConverter.ToUnstructured. If the object cannot be represented as a generic map (unsupported fields, nil pointer, or a type registered without proper JSON tags / conversion reflection issues), this error wraps the underlying failure.","triggerScenarios":"Passing a runtime.Object that is nil, a typed object whose Go struct fields are incompatible with unstructured conversion, or an unregistered/custom type to FromRuntimeObject.","commonSituations":"Programmatically constructing addon/manifest objects from typed structs in kOps tooling; passing a pointer that is nil; using custom CRD Go types without json tags.","solutions":["Check the object is non-nil before calling FromRuntimeObject","Add proper `json:\"...\"` struct tags to custom Go types used as runtime.Objects","Use unstructured.Unstructured directly if the type cannot round-trip through the converter","Inspect the wrapped error message for the specific field causing conversion failure"],"exampleFix":"// before\nvar obj *v1.Pod // nil\no, err := kubemanifest.FromRuntimeObject(obj) // error\n// after\nif obj == nil { return fmt.Errorf(\"object is nil\") }\no, err := kubemanifest.FromRuntimeObject(obj)","handlingStrategy":"validation","validationCode":"func safeFromRuntimeObject(obj runtime.Object) (*kubemanifest.Object, error) {\n\tif obj == nil || reflect.ValueOf(obj).IsNil() {\n\t\treturn nil, fmt.Errorf(\"runtime.Object is nil\")\n\t}\n\treturn kubemanifest.FromRuntimeObject(obj)\n}\n","typeGuard":"func isNilObject(obj runtime.Object) bool {\n\treturn obj == nil || reflect.ValueOf(obj).Kind() == reflect.Ptr && reflect.ValueOf(obj).IsNil()\n}\n","tryCatchPattern":"o, err := kubemanifest.FromRuntimeObject(obj)\nif err != nil {\n\treturn fmt.Errorf(\"cannot serialize %T: %w\", obj, err)\n}\n","preventionTips":["Nil-check objects before conversion","Add json struct tags to all custom runtime.Object types","Prefer unstructured.Unstructured for dynamic objects","Test conversion of every object type you pass through"],"tags":["unstructured","kubernetes-api","go"],"backgroundTag":"unstructured-conversion-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"}