Skip to content

Commit

Permalink
add CodecFactoryOptions for codecfactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaunceyctx authored and c00664376 committed Jan 6, 2025
1 parent a9b7c2d commit 5749d97
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
16 changes: 11 additions & 5 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ type Options struct {
// Scheme is the scheme to use for mapping objects to GroupVersionKinds
Scheme *runtime.Scheme

// CodecFactoryStrict/CodecFactoryPretty are used to indicate whether enable Strict/Pretty mode of CodecFactory
CodecFactoryStrict bool
CodecFactoryPretty bool

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

Expand Down Expand Up @@ -419,11 +423,13 @@ 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,
CodecFactoryStrict: opts.CodecFactoryStrict,
CodecFactoryPretty: opts.CodecFactoryPretty,
Mapper: opts.Mapper,
ResyncPeriod: *opts.SyncPeriod,
Namespace: namespace,
Selector: internal.Selector{
Label: config.LabelSelector,
Field: config.FieldSelector,
Expand Down
11 changes: 10 additions & 1 deletion pkg/cache/internal/informers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import (
type InformersOpts struct {
HTTPClient *http.Client
Scheme *runtime.Scheme
CodecFactoryStrict bool
CodecFactoryPretty bool
Mapper meta.RESTMapper
ResyncPeriod time.Duration
Namespace string
Expand All @@ -61,6 +63,13 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
if options.NewInformer != nil {
newInformer = *options.NewInformer
}
var mutators []serializer.CodecFactoryOptionsMutator
if options.CodecFactoryStrict {
mutators = append(mutators, serializer.EnableStrict)
}
if options.CodecFactoryPretty {
mutators = append(mutators, serializer.EnablePretty)
}
return &Informers{
config: config,
httpClient: options.HTTPClient,
Expand All @@ -71,7 +80,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, mutators...),
paramCodec: runtime.NewParameterCodec(options.Scheme),
resync: options.ResyncPeriod,
startWait: make(chan struct{}),
Expand Down
14 changes: 13 additions & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type Options struct {
// Scheme, if provided, will be used to map go structs to GroupVersionKinds
Scheme *runtime.Scheme

// CodecFactoryStrict/CodecFactoryPretty, if provided, will be used to indicate whether enable Strict/Pretty mode of CodecFactory
CodecFactoryStrict bool
CodecFactoryPretty bool

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

Expand Down Expand Up @@ -145,12 +149,20 @@ func newClient(config *rest.Config, options Options) (*client, error) {
}
}

var mutators []serializer.CodecFactoryOptionsMutator
if options.CodecFactoryStrict {
mutators = append(mutators, serializer.EnableStrict)
}
if options.CodecFactoryPretty {
mutators = append(mutators, serializer.EnablePretty)
}

resources := &clientRestResources{
httpClient: options.HTTPClient,
config: config,
scheme: options.Scheme,
mapper: options.Mapper,
codecs: serializer.NewCodecFactory(options.Scheme),
codecs: serializer.NewCodecFactory(options.Scheme, mutators...),

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

0 comments on commit 5749d97

Please sign in to comment.