@@ -36,23 +36,25 @@ 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+ Mapper meta.RESTMapper
50+ ResyncPeriod time.Duration
51+ Namespace string
52+ NewInformer func (cache.ListerWatcher , runtime.Object , time.Duration , cache.Indexers ) cache.SharedIndexInformer
53+ Selector Selector
54+ Transform cache.TransformFunc
55+ UnsafeDisableDeepCopy bool
56+ EnableWatchBookmarks bool
57+ WatchErrorHandler cache.WatchErrorHandler
5658}
5759
5860// NewInformers creates a new InformersMap that can create informers under the hood.
@@ -61,6 +63,23 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
6163 if options .NewInformer != nil {
6264 newInformer = options .NewInformer
6365 }
66+
67+ codecFactories := make (map [schema.GroupVersionKind ]serializer.CodecFactory )
68+ for obj , codecFactoryOptions := range options .CodecFactoryOptionsByObject {
69+ gvk , err := apiutil .GVKForObject (obj , options .Scheme )
70+ if err != nil {
71+ continue
72+ }
73+ var mutators []serializer.CodecFactoryOptionsMutator
74+ if codecFactoryOptions .Strict {
75+ mutators = append (mutators , serializer .EnableStrict )
76+ }
77+ if codecFactoryOptions .Pretty {
78+ mutators = append (mutators , serializer .EnablePretty )
79+ }
80+ codecFactories [gvk ] = serializer .NewCodecFactory (options .Scheme , mutators ... )
81+ }
82+
6483 return & Informers {
6584 config : config ,
6685 httpClient : options .HTTPClient ,
@@ -71,7 +90,8 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
7190 Unstructured : make (map [schema.GroupVersionKind ]* Cache ),
7291 Metadata : make (map [schema.GroupVersionKind ]* Cache ),
7392 },
74- codecs : serializer .NewCodecFactory (options .Scheme ),
93+ defaultCodecs : serializer .NewCodecFactory (options .Scheme ),
94+ codecsByObject : codecFactories ,
7595 paramCodec : runtime .NewParameterCodec (options .Scheme ),
7696 resync : options .ResyncPeriod ,
7797 startWait : make (chan struct {}),
@@ -139,8 +159,11 @@ type Informers struct {
139159 // tracker tracks informers keyed by their type and groupVersionKind
140160 tracker tracker
141161
142- // codecs is used to create a new REST client
143- codecs serializer.CodecFactory
162+ // codecsByObject is used to override defaultCodecs for specific GroupVersionKind(object)
163+ codecsByObject map [schema.GroupVersionKind ]serializer.CodecFactory
164+
165+ // defaultCodecs is used to create a new REST client
166+ defaultCodecs serializer.CodecFactory
144167
145168 // paramCodec is used by list and watch
146169 paramCodec runtime.ParameterCodec
@@ -512,7 +535,12 @@ func (ip *Informers) makeListWatcher(gvk schema.GroupVersionKind, obj runtime.Ob
512535 // Structured.
513536 //
514537 default :
515- client , err := apiutil .RESTClientForGVK (gvk , false , ip .config , ip .codecs , ip .httpClient )
538+ codecFactory := ip .defaultCodecs
539+ if override , ok := ip .codecsByObject [gvk ]; ok {
540+ codecFactory = override
541+ }
542+
543+ client , err := apiutil .RESTClientForGVK (gvk , false , ip .config , codecFactory , ip .httpClient )
516544 if err != nil {
517545 return nil , err
518546 }
0 commit comments