From 5749d97ab2e7c42c8f4445c5a9580003a69632d3 Mon Sep 17 00:00:00 2001 From: Chaunceyctx Date: Sat, 28 Dec 2024 15:28:21 +0800 Subject: [PATCH] add CodecFactoryOptions for codecfactory --- pkg/cache/cache.go | 16 +++++++++++----- pkg/cache/internal/informers.go | 11 ++++++++++- pkg/client/client.go | 14 +++++++++++++- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 57dab7fbbb..d21051f310 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -140,6 +140,10 @@ type Options struct { // Scheme is the scheme to use for mapping objects to GroupVersionKinds Scheme *runtime.Scheme + // CodecFactoryStrict/CodecFactoryPretty are used to indicate whether enable Strict/Pretty mode of CodecFactory + CodecFactoryStrict bool + CodecFactoryPretty bool + // Mapper is the RESTMapper to use for mapping GroupVersionKinds to Resources Mapper meta.RESTMapper @@ -419,11 +423,13 @@ func newCache(restConfig *rest.Config, opts Options) newCacheFunc { return &informerCache{ scheme: opts.Scheme, Informers: internal.NewInformers(restConfig, &internal.InformersOpts{ - HTTPClient: opts.HTTPClient, - Scheme: opts.Scheme, - Mapper: opts.Mapper, - ResyncPeriod: *opts.SyncPeriod, - Namespace: namespace, + HTTPClient: opts.HTTPClient, + Scheme: opts.Scheme, + CodecFactoryStrict: opts.CodecFactoryStrict, + CodecFactoryPretty: opts.CodecFactoryPretty, + Mapper: opts.Mapper, + ResyncPeriod: *opts.SyncPeriod, + Namespace: namespace, Selector: internal.Selector{ Label: config.LabelSelector, Field: config.FieldSelector, diff --git a/pkg/cache/internal/informers.go b/pkg/cache/internal/informers.go index a40382d6f3..6cc492229a 100644 --- a/pkg/cache/internal/informers.go +++ b/pkg/cache/internal/informers.go @@ -44,6 +44,8 @@ import ( type InformersOpts struct { HTTPClient *http.Client Scheme *runtime.Scheme + CodecFactoryStrict bool + CodecFactoryPretty bool Mapper meta.RESTMapper ResyncPeriod time.Duration Namespace string @@ -61,6 +63,13 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers { if options.NewInformer != nil { newInformer = *options.NewInformer } + var mutators []serializer.CodecFactoryOptionsMutator + if options.CodecFactoryStrict { + mutators = append(mutators, serializer.EnableStrict) + } + if options.CodecFactoryPretty { + mutators = append(mutators, serializer.EnablePretty) + } return &Informers{ config: config, httpClient: options.HTTPClient, @@ -71,7 +80,7 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers { Unstructured: make(map[schema.GroupVersionKind]*Cache), Metadata: make(map[schema.GroupVersionKind]*Cache), }, - codecs: serializer.NewCodecFactory(options.Scheme), + codecs: serializer.NewCodecFactory(options.Scheme, mutators...), paramCodec: runtime.NewParameterCodec(options.Scheme), resync: options.ResyncPeriod, startWait: make(chan struct{}), diff --git a/pkg/client/client.go b/pkg/client/client.go index 6d87440174..ef7a99fb72 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -44,6 +44,10 @@ type Options struct { // Scheme, if provided, will be used to map go structs to GroupVersionKinds Scheme *runtime.Scheme + // CodecFactoryStrict/CodecFactoryPretty, if provided, will be used to indicate whether enable Strict/Pretty mode of CodecFactory + CodecFactoryStrict bool + CodecFactoryPretty bool + // Mapper, if provided, will be used to map GroupVersionKinds to Resources Mapper meta.RESTMapper @@ -145,12 +149,20 @@ func newClient(config *rest.Config, options Options) (*client, error) { } } + var mutators []serializer.CodecFactoryOptionsMutator + if options.CodecFactoryStrict { + mutators = append(mutators, serializer.EnableStrict) + } + if options.CodecFactoryPretty { + mutators = append(mutators, serializer.EnablePretty) + } + resources := &clientRestResources{ httpClient: options.HTTPClient, config: config, scheme: options.Scheme, mapper: options.Mapper, - codecs: serializer.NewCodecFactory(options.Scheme), + codecs: serializer.NewCodecFactory(options.Scheme, mutators...), structuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta), unstructuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta),