@@ -36,23 +36,26 @@ import (
3636 "k8s.io/client-go/metadata"
3737 "k8s.io/client-go/rest"
3838 "k8s.io/client-go/tools/cache"
39+ "sigs.k8s.io/controller-runtime/pkg/client"
3940 "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
4041 "sigs.k8s.io/controller-runtime/pkg/internal/syncs"
4142)
4243
4344// InformersOpts configures an InformerMap.
4445type InformersOpts struct {
45- HTTPClient * http.Client
46- Scheme * runtime.Scheme
47- Mapper meta.RESTMapper
48- ResyncPeriod time.Duration
49- Namespace string
50- NewInformer * func (cache.ListerWatcher , runtime.Object , time.Duration , cache.Indexers ) cache.SharedIndexInformer
51- Selector Selector
52- Transform cache.TransformFunc
53- UnsafeDisableDeepCopy bool
54- EnableWatchBookmarks bool
55- WatchErrorHandler cache.WatchErrorHandler
46+ HTTPClient * http.Client
47+ Scheme * runtime.Scheme
48+ CodecFactoryOptionsByObject map [client.Object ]client.CodecFactoryOptions
49+ Test map [bool ]map [bool ]string
50+ Mapper meta.RESTMapper
51+ ResyncPeriod time.Duration
52+ Namespace string
53+ NewInformer * func (cache.ListerWatcher , runtime.Object , time.Duration , cache.Indexers ) cache.SharedIndexInformer
54+ Selector Selector
55+ Transform cache.TransformFunc
56+ UnsafeDisableDeepCopy bool
57+ EnableWatchBookmarks bool
58+ WatchErrorHandler cache.WatchErrorHandler
5659}
5760
5861// NewInformers creates a new InformersMap that can create informers under the hood.
@@ -61,6 +64,23 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
6164 if options .NewInformer != nil {
6265 newInformer = * options .NewInformer
6366 }
67+
68+ codecFactories := make (map [schema.GroupVersionKind ]serializer.CodecFactory )
69+ for obj , codecFactoryOptions := range options .CodecFactoryOptionsByObject {
70+ gvk , err := apiutil .GVKForObject (obj , options .Scheme )
71+ if err != nil {
72+ continue
73+ }
74+ var mutators []serializer.CodecFactoryOptionsMutator
75+ if codecFactoryOptions .Strict {
76+ mutators = append (mutators , serializer .EnableStrict )
77+ }
78+ if codecFactoryOptions .Pretty {
79+ mutators = append (mutators , serializer .EnablePretty )
80+ }
81+ codecFactories [gvk ] = serializer .NewCodecFactory (options .Scheme , mutators ... )
82+ }
83+
6484 return & Informers {
6585 config : config ,
6686 httpClient : options .HTTPClient ,
@@ -71,7 +91,8 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
7191 Unstructured : make (map [schema.GroupVersionKind ]* Cache ),
7292 Metadata : make (map [schema.GroupVersionKind ]* Cache ),
7393 },
74- codecs : serializer .NewCodecFactory (options .Scheme ),
94+ defaultCodecs : serializer .NewCodecFactory (options .Scheme ),
95+ codecsByObject : codecFactories ,
7596 paramCodec : runtime .NewParameterCodec (options .Scheme ),
7697 resync : options .ResyncPeriod ,
7798 startWait : make (chan struct {}),
@@ -139,8 +160,11 @@ type Informers struct {
139160 // tracker tracks informers keyed by their type and groupVersionKind
140161 tracker tracker
141162
142- // codecs is used to create a new REST client
143- codecs serializer.CodecFactory
163+ // codecsByObject is used to override defaultCodecs for specific GroupVersionKind(object)
164+ codecsByObject map [schema.GroupVersionKind ]serializer.CodecFactory
165+
166+ // defaultCodecs is used to create a new REST client
167+ defaultCodecs serializer.CodecFactory
144168
145169 // paramCodec is used by list and watch
146170 paramCodec runtime.ParameterCodec
@@ -512,7 +536,12 @@ func (ip *Informers) makeListWatcher(gvk schema.GroupVersionKind, obj runtime.Ob
512536 // Structured.
513537 //
514538 default :
515- client , err := apiutil .RESTClientForGVK (gvk , false , ip .config , ip .codecs , ip .httpClient )
539+ codecFactory := ip .defaultCodecs
540+ if override , ok := ip .codecsByObject [gvk ]; ok {
541+ codecFactory = override
542+ }
543+
544+ client , err := apiutil .RESTClientForGVK (gvk , false , ip .config , codecFactory , ip .httpClient )
516545 if err != nil {
517546 return nil , err
518547 }
0 commit comments