Skip to content

Commit

Permalink
Make generated client codecs support CBOR based on feature gate.
Browse files Browse the repository at this point in the history
Kubernetes-commit: 57e337dbbf9e437bcb350d6551689669bb79d579
  • Loading branch information
benluddy authored and k8s-publishing-bot committed Oct 24, 2024
1 parent 3dc7fd5 commit d8fc5bb
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions rest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)),
},
}
}))
}

0 comments on commit d8fc5bb

Please sign in to comment.