@@ -36,23 +36,25 @@ import (
36
36
"k8s.io/client-go/metadata"
37
37
"k8s.io/client-go/rest"
38
38
"k8s.io/client-go/tools/cache"
39
+ "sigs.k8s.io/controller-runtime/pkg/client"
39
40
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
40
41
"sigs.k8s.io/controller-runtime/pkg/internal/syncs"
41
42
)
42
43
43
44
// InformersOpts configures an InformerMap.
44
45
type 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
56
58
}
57
59
58
60
// NewInformers creates a new InformersMap that can create informers under the hood.
@@ -61,6 +63,23 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
61
63
if options .NewInformer != nil {
62
64
newInformer = options .NewInformer
63
65
}
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
+
64
83
return & Informers {
65
84
config : config ,
66
85
httpClient : options .HTTPClient ,
@@ -71,7 +90,8 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
71
90
Unstructured : make (map [schema.GroupVersionKind ]* Cache ),
72
91
Metadata : make (map [schema.GroupVersionKind ]* Cache ),
73
92
},
74
- codecs : serializer .NewCodecFactory (options .Scheme ),
93
+ defaultCodecs : serializer .NewCodecFactory (options .Scheme ),
94
+ codecsByObject : codecFactories ,
75
95
paramCodec : runtime .NewParameterCodec (options .Scheme ),
76
96
resync : options .ResyncPeriod ,
77
97
startWait : make (chan struct {}),
@@ -139,8 +159,11 @@ type Informers struct {
139
159
// tracker tracks informers keyed by their type and groupVersionKind
140
160
tracker tracker
141
161
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
144
167
145
168
// paramCodec is used by list and watch
146
169
paramCodec runtime.ParameterCodec
@@ -512,7 +535,12 @@ func (ip *Informers) makeListWatcher(gvk schema.GroupVersionKind, obj runtime.Ob
512
535
// Structured.
513
536
//
514
537
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 )
516
544
if err != nil {
517
545
return nil , err
518
546
}
0 commit comments