diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 57dab7fbbb..75fff83574 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -32,6 +32,7 @@ import ( "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apimachinery/pkg/runtime/serializer" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" toolscache "k8s.io/client-go/tools/cache" @@ -140,6 +141,9 @@ type Options struct { // Scheme is the scheme to use for mapping objects to GroupVersionKinds Scheme *runtime.Scheme + // CodecFactoryOptionsMutators is used to indicate whether enable Strict/Pretty mode of CodecFactory + CodecFactoryOptionsMutators []serializer.CodecFactoryOptionsMutator + // Mapper is the RESTMapper to use for mapping GroupVersionKinds to Resources Mapper meta.RESTMapper @@ -419,11 +423,12 @@ 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, + CodecFactoryOptionsMutators: opts.CodecFactoryOptionsMutators, + 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..e14b9bd007 100644 --- a/pkg/cache/internal/informers.go +++ b/pkg/cache/internal/informers.go @@ -42,17 +42,18 @@ import ( // InformersOpts configures an InformerMap. type InformersOpts struct { - HTTPClient *http.Client - Scheme *runtime.Scheme - Mapper meta.RESTMapper - ResyncPeriod time.Duration - Namespace string - NewInformer *func(cache.ListerWatcher, runtime.Object, time.Duration, cache.Indexers) cache.SharedIndexInformer - Selector Selector - Transform cache.TransformFunc - UnsafeDisableDeepCopy bool - EnableWatchBookmarks bool - WatchErrorHandler cache.WatchErrorHandler + HTTPClient *http.Client + Scheme *runtime.Scheme + CodecFactoryOptionsMutators []serializer.CodecFactoryOptionsMutator + Mapper meta.RESTMapper + ResyncPeriod time.Duration + Namespace string + NewInformer *func(cache.ListerWatcher, runtime.Object, time.Duration, cache.Indexers) cache.SharedIndexInformer + Selector Selector + Transform cache.TransformFunc + UnsafeDisableDeepCopy bool + EnableWatchBookmarks bool + WatchErrorHandler cache.WatchErrorHandler } // NewInformers creates a new InformersMap that can create informers under the hood. @@ -71,7 +72,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, options.CodecFactoryOptionsMutators...), 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..53cfdafd45 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -44,6 +44,9 @@ type Options struct { // Scheme, if provided, will be used to map go structs to GroupVersionKinds Scheme *runtime.Scheme + // CodecFactoryOptionsMutators, if provided, will be used to indicate whether enable Strict/Pretty mode of CodecFactory + CodecFactoryOptionsMutators []serializer.CodecFactoryOptionsMutator + // Mapper, if provided, will be used to map GroupVersionKinds to Resources Mapper meta.RESTMapper @@ -150,7 +153,7 @@ func newClient(config *rest.Config, options Options) (*client, error) { config: config, scheme: options.Scheme, mapper: options.Mapper, - codecs: serializer.NewCodecFactory(options.Scheme), + codecs: serializer.NewCodecFactory(options.Scheme, options.CodecFactoryOptionsMutators...), structuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta), unstructuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta),