kubernetes/kops · error

no serializer for %q

Error message

no serializer for %q

What it means

kopscodecs.ToMediaTypeWithVersion looks up a serializer matching the requested mediaType (e.g. "application/yaml", "application/json") among Codecs.SupportedMediaTypes(). If no registered serializer supports that media type string, it fails before encoding. The mediaType string must exactly match a supported entry.

Source

Thrown at pkg/kopscodecs/codecs.go:55

	Codecs         = serializer.NewCodecFactory(Scheme)
	ParameterCodec = runtime.NewParameterCodec(Scheme)
)

func init() {
	metav1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
	install.Install(Scheme)
}

// ToVersionedYaml encodes the object to YAML
func ToVersionedYaml(obj runtime.Object) ([]byte, error) {
	return ToVersionedYamlWithVersion(obj, v1alpha2.SchemeGroupVersion)
}

// ToMediaTypeWithVersion encodes the object to the specified mediaType, in a specified API version
func ToMediaTypeWithVersion(obj runtime.Object, mediaType string, gv runtime.GroupVersioner) ([]byte, error) {
	e, ok := runtime.SerializerInfoForMediaType(Codecs.SupportedMediaTypes(), mediaType)
	if !ok {
		return nil, fmt.Errorf("no serializer for %q", mediaType)
	}

	_, isUnstructured := obj.(*unstructured.Unstructured)
	var w bytes.Buffer
	if isUnstructured {
		err := e.Serializer.Encode(obj, &w)
		if err != nil {
			return nil, fmt.Errorf("error encoding %T with unstructured encoder: %w", obj, err)
		}
	} else {
		encoder := Codecs.EncoderForVersion(e.Serializer, gv)
		if err := encoder.Encode(obj, &w); err != nil {
			return nil, fmt.Errorf("error encoding %T with structured encoder: %w", obj, err)
		}
	}
	return w.Bytes(), nil
}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Use exactly "application/yaml" or "application/json" as the mediaType.
  2. Iterate Codecs.SupportedMediaTypes() to pick a valid media type instead of hardcoding.
  3. Strip parameters (charset etc.) from any media type taken from headers/config before calling.

Example fix

// before
b, err := kopscodecs.ToMediaTypeWithVersion(obj, "text/yaml", gv)
// after
b, err := kopscodecs.ToMediaTypeWithVersion(obj, "application/yaml", gv)
Defensive patterns

Strategy: validation

Validate before calling

func supportedMediaType(mt string) bool {
	for _, si := range kopscodecs.Codecs.SupportedMediaTypes() {
		if si.MediaType == mt {
			return true
		}
	}
	return false
}
// call before: if !supportedMediaType(mediaType) { normalize to "application/yaml" or "application/json" }

Try / catch

b, err := kopscodecs.ToMediaTypeWithVersion(obj, mediaType, gv)
if err != nil {
	return fmt.Errorf("unsupported media type %q: %w", mediaType, err)
}

Prevention

When it happens

Trigger: Calling ToMediaTypeWithVersion (directly or via ToVersionedYamlWithVersion / ToVersionedJSONWithVersion) with a mediaType string like "text/yaml", "application/yaml; charset=utf-8", or a typo rather than an exactly-supported media type.

Common situations: Passing a media type from an HTTP Content-Type header including parameters; using a MIME type from config that differs from the codecs' registered names; kops API version changes that alter supported media types.

Related errors


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/fb2dbb5aea2e68c8. Report an issue: GitHub.