Skip to content

Commit

Permalink
add CodecFactoryOptionsMutators for codecfactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaunceyctx committed Dec 28, 2024
1 parent a9b7c2d commit 6d063ae
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
15 changes: 10 additions & 5 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
toolscache "k8s.io/client-go/tools/cache"
Expand Down Expand Up @@ -140,6 +141,9 @@ type Options struct {
// Scheme is the scheme to use for mapping objects to GroupVersionKinds
Scheme *runtime.Scheme

// CodecFactoryOptionsMutators is used to indicate whether enable Strict/Pretty mode of CodecFactory
CodecFactoryOptionsMutators []serializer.CodecFactoryOptionsMutator

// Mapper is the RESTMapper to use for mapping GroupVersionKinds to Resources
Mapper meta.RESTMapper

Expand Down Expand Up @@ -419,11 +423,12 @@ func newCache(restConfig *rest.Config, opts Options) newCacheFunc {
return &informerCache{
scheme: opts.Scheme,
Informers: internal.NewInformers(restConfig, &internal.InformersOpts{
HTTPClient: opts.HTTPClient,
Scheme: opts.Scheme,
Mapper: opts.Mapper,
ResyncPeriod: *opts.SyncPeriod,
Namespace: namespace,
HTTPClient: opts.HTTPClient,
Scheme: opts.Scheme,
CodecFactoryOptionsMutators: opts.CodecFactoryOptionsMutators,
Mapper: opts.Mapper,
ResyncPeriod: *opts.SyncPeriod,
Namespace: namespace,
Selector: internal.Selector{
Label: config.LabelSelector,
Field: config.FieldSelector,
Expand Down
25 changes: 13 additions & 12 deletions pkg/cache/internal/informers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@ import (

// InformersOpts configures an InformerMap.
type InformersOpts struct {
HTTPClient *http.Client
Scheme *runtime.Scheme
Mapper meta.RESTMapper
ResyncPeriod time.Duration
Namespace string
NewInformer *func(cache.ListerWatcher, runtime.Object, time.Duration, cache.Indexers) cache.SharedIndexInformer
Selector Selector
Transform cache.TransformFunc
UnsafeDisableDeepCopy bool
EnableWatchBookmarks bool
WatchErrorHandler cache.WatchErrorHandler
HTTPClient *http.Client
Scheme *runtime.Scheme
CodecFactoryOptionsMutators []serializer.CodecFactoryOptionsMutator
Mapper meta.RESTMapper
ResyncPeriod time.Duration
Namespace string
NewInformer *func(cache.ListerWatcher, runtime.Object, time.Duration, cache.Indexers) cache.SharedIndexInformer
Selector Selector
Transform cache.TransformFunc
UnsafeDisableDeepCopy bool
EnableWatchBookmarks bool
WatchErrorHandler cache.WatchErrorHandler
}

// NewInformers creates a new InformersMap that can create informers under the hood.
Expand All @@ -71,7 +72,7 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
Unstructured: make(map[schema.GroupVersionKind]*Cache),
Metadata: make(map[schema.GroupVersionKind]*Cache),
},
codecs: serializer.NewCodecFactory(options.Scheme),
codecs: serializer.NewCodecFactory(options.Scheme, options.CodecFactoryOptionsMutators...),
paramCodec: runtime.NewParameterCodec(options.Scheme),
resync: options.ResyncPeriod,
startWait: make(chan struct{}),
Expand Down
5 changes: 4 additions & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type Options struct {
// Scheme, if provided, will be used to map go structs to GroupVersionKinds
Scheme *runtime.Scheme

// CodecFactoryOptionsMutators, if provided, will be used to indicate whether enable Strict/Pretty mode of CodecFactory
CodecFactoryOptionsMutators []serializer.CodecFactoryOptionsMutator

// Mapper, if provided, will be used to map GroupVersionKinds to Resources
Mapper meta.RESTMapper

Expand Down Expand Up @@ -150,7 +153,7 @@ func newClient(config *rest.Config, options Options) (*client, error) {
config: config,
scheme: options.Scheme,
mapper: options.Mapper,
codecs: serializer.NewCodecFactory(options.Scheme),
codecs: serializer.NewCodecFactory(options.Scheme, options.CodecFactoryOptionsMutators...),

structuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta),
unstructuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta),
Expand Down

0 comments on commit 6d063ae

Please sign in to comment.