Skip to content

Commit 4fff6e3

Browse files
committed
add CodecFactoryOptionsMutators for codecfactory
1 parent a9b7c2d commit 4fff6e3

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

pkg/cache/cache.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package cache
1919
import (
2020
"context"
2121
"fmt"
22+
"k8s.io/apimachinery/pkg/runtime/serializer"
2223
"maps"
2324
"net/http"
2425
"slices"
@@ -140,6 +141,9 @@ type Options struct {
140141
// Scheme is the scheme to use for mapping objects to GroupVersionKinds
141142
Scheme *runtime.Scheme
142143

144+
// CodecFactoryOptionsMutators is used to indicate whether enable Strict/Pretty mode of CodecFactory
145+
CodecFactoryOptionsMutators []serializer.CodecFactoryOptionsMutator
146+
143147
// Mapper is the RESTMapper to use for mapping GroupVersionKinds to Resources
144148
Mapper meta.RESTMapper
145149

@@ -419,11 +423,12 @@ func newCache(restConfig *rest.Config, opts Options) newCacheFunc {
419423
return &informerCache{
420424
scheme: opts.Scheme,
421425
Informers: internal.NewInformers(restConfig, &internal.InformersOpts{
422-
HTTPClient: opts.HTTPClient,
423-
Scheme: opts.Scheme,
424-
Mapper: opts.Mapper,
425-
ResyncPeriod: *opts.SyncPeriod,
426-
Namespace: namespace,
426+
HTTPClient: opts.HTTPClient,
427+
Scheme: opts.Scheme,
428+
CodecFactoryOptionsMutators: opts.CodecFactoryOptionsMutators,
429+
Mapper: opts.Mapper,
430+
ResyncPeriod: *opts.SyncPeriod,
431+
Namespace: namespace,
427432
Selector: internal.Selector{
428433
Label: config.LabelSelector,
429434
Field: config.FieldSelector,

pkg/cache/internal/informers.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,18 @@ import (
4242

4343
// InformersOpts configures an InformerMap.
4444
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
45+
HTTPClient *http.Client
46+
Scheme *runtime.Scheme
47+
CodecFactoryOptionsMutators []serializer.CodecFactoryOptionsMutator
48+
Mapper meta.RESTMapper
49+
ResyncPeriod time.Duration
50+
Namespace string
51+
NewInformer *func(cache.ListerWatcher, runtime.Object, time.Duration, cache.Indexers) cache.SharedIndexInformer
52+
Selector Selector
53+
Transform cache.TransformFunc
54+
UnsafeDisableDeepCopy bool
55+
EnableWatchBookmarks bool
56+
WatchErrorHandler cache.WatchErrorHandler
5657
}
5758

5859
// NewInformers creates a new InformersMap that can create informers under the hood.
@@ -71,7 +72,7 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
7172
Unstructured: make(map[schema.GroupVersionKind]*Cache),
7273
Metadata: make(map[schema.GroupVersionKind]*Cache),
7374
},
74-
codecs: serializer.NewCodecFactory(options.Scheme),
75+
codecs: serializer.NewCodecFactory(options.Scheme, options.CodecFactoryOptionsMutators...),
7576
paramCodec: runtime.NewParameterCodec(options.Scheme),
7677
resync: options.ResyncPeriod,
7778
startWait: make(chan struct{}),

pkg/client/client.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ type Options struct {
4444
// Scheme, if provided, will be used to map go structs to GroupVersionKinds
4545
Scheme *runtime.Scheme
4646

47+
// CodecFactoryOptionsMutators, if provided, will be used to indicate whether enable Strict/Pretty mode of CodecFactory
48+
CodecFactoryOptionsMutators []serializer.CodecFactoryOptionsMutator
49+
4750
// Mapper, if provided, will be used to map GroupVersionKinds to Resources
4851
Mapper meta.RESTMapper
4952

@@ -150,7 +153,7 @@ func newClient(config *rest.Config, options Options) (*client, error) {
150153
config: config,
151154
scheme: options.Scheme,
152155
mapper: options.Mapper,
153-
codecs: serializer.NewCodecFactory(options.Scheme),
156+
codecs: serializer.NewCodecFactory(options.Scheme, options.CodecFactoryOptionsMutators...),
154157

155158
structuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta),
156159
unstructuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta),

0 commit comments

Comments
 (0)