Make generated client codecs support CBOR based on feature gate.

Kubernetes-commit: 57e337dbbf9e437bcb350d6551689669bb79d579
This commit is contained in:
Ben Luddy 2024-10-24 16:38:45 -04:00 committed by Kubernetes Publisher
parent 3dc7fd5f4c
commit d8fc5bb524

View File

@ -32,6 +32,9 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/runtime/serializer/cbor"
"k8s.io/client-go/features"
"k8s.io/client-go/pkg/version"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/transport"
@ -672,3 +675,31 @@ func CopyConfig(config *Config) *Config {
}
return c
}
// CodecFactoryForGeneratedClient returns the provided CodecFactory if there are no enabled client
// feature gates affecting serialization. Otherwise, it constructs and returns a new CodecFactory
// from the provided Scheme.
//
// This is supported ONLY for use by clients generated with client-gen. The caller is responsible
// for ensuring that the CodecFactory argument was constructed using the Scheme argument.
func CodecFactoryForGeneratedClient(scheme *runtime.Scheme, codecs serializer.CodecFactory) serializer.CodecFactory {
if !features.TestOnlyFeatureGates.Enabled(features.TestOnlyClientAllowsCBOR) {
// NOTE: This assumes client-gen will not generate CBOR-enabled Codecs as long as
// the feature gate exists.
return codecs
}
return serializer.NewCodecFactory(scheme, serializer.WithSerializer(func(creater runtime.ObjectCreater, typer runtime.ObjectTyper) runtime.SerializerInfo {
return runtime.SerializerInfo{
MediaType: "application/cbor",
MediaTypeType: "application",
MediaTypeSubType: "cbor",
Serializer: cbor.NewSerializer(creater, typer),
StrictSerializer: cbor.NewSerializer(creater, typer, cbor.Strict(true)),
StreamSerializer: &runtime.StreamSerializerInfo{
Framer: cbor.NewFramer(),
Serializer: cbor.NewSerializer(creater, typer, cbor.Transcode(false)),
},
}
}))
}