@@ -36,23 +36,26 @@ 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
+ 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
56
59
}
57
60
58
61
// NewInformers creates a new InformersMap that can create informers under the hood.
@@ -61,6 +64,23 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
61
64
if options .NewInformer != nil {
62
65
newInformer = * options .NewInformer
63
66
}
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
+
64
84
return & Informers {
65
85
config : config ,
66
86
httpClient : options .HTTPClient ,
@@ -71,7 +91,8 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
71
91
Unstructured : make (map [schema.GroupVersionKind ]* Cache ),
72
92
Metadata : make (map [schema.GroupVersionKind ]* Cache ),
73
93
},
74
- codecs : serializer .NewCodecFactory (options .Scheme ),
94
+ defaultCodecs : serializer .NewCodecFactory (options .Scheme ),
95
+ codecsByObject : codecFactories ,
75
96
paramCodec : runtime .NewParameterCodec (options .Scheme ),
76
97
resync : options .ResyncPeriod ,
77
98
startWait : make (chan struct {}),
@@ -139,8 +160,11 @@ type Informers struct {
139
160
// tracker tracks informers keyed by their type and groupVersionKind
140
161
tracker tracker
141
162
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
144
168
145
169
// paramCodec is used by list and watch
146
170
paramCodec runtime.ParameterCodec
@@ -512,7 +536,12 @@ func (ip *Informers) makeListWatcher(gvk schema.GroupVersionKind, obj runtime.Ob
512
536
// Structured.
513
537
//
514
538
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 )
516
545
if err != nil {
517
546
return nil , err
518
547
}
0 commit comments