Skip to content

Commit 08062e5

Browse files
Chaunceyctxc00664376
authored and
c00664376
committed
add CodecFactoryOptions for codecfactory
1 parent a9b7c2d commit 08062e5

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

pkg/cache/cache.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"k8s.io/apimachinery/pkg/labels"
3333
"k8s.io/apimachinery/pkg/runtime"
3434
"k8s.io/apimachinery/pkg/runtime/schema"
35+
"k8s.io/apimachinery/pkg/runtime/serializer"
3536
"k8s.io/client-go/kubernetes/scheme"
3637
"k8s.io/client-go/rest"
3738
toolscache "k8s.io/client-go/tools/cache"
@@ -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+
// CodecFactoryOptions is used to indicate whether enable Strict/Pretty mode of CodecFactory
145+
CodecFactoryOptions serializer.CodecFactoryOptions
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+
CodecFactoryOptions: opts.CodecFactoryOptions,
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

+9-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
type InformersOpts struct {
4545
HTTPClient *http.Client
4646
Scheme *runtime.Scheme
47+
CodecFactoryOptions serializer.CodecFactoryOptions
4748
Mapper meta.RESTMapper
4849
ResyncPeriod time.Duration
4950
Namespace string
@@ -61,6 +62,13 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
6162
if options.NewInformer != nil {
6263
newInformer = *options.NewInformer
6364
}
65+
var mutators []serializer.CodecFactoryOptionsMutator
66+
if options.CodecFactoryOptions.Strict {
67+
mutators = append(mutators, serializer.EnableStrict)
68+
}
69+
if options.CodecFactoryOptions.Pretty {
70+
mutators = append(mutators, serializer.EnablePretty)
71+
}
6472
return &Informers{
6573
config: config,
6674
httpClient: options.HTTPClient,
@@ -71,7 +79,7 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
7179
Unstructured: make(map[schema.GroupVersionKind]*Cache),
7280
Metadata: make(map[schema.GroupVersionKind]*Cache),
7381
},
74-
codecs: serializer.NewCodecFactory(options.Scheme),
82+
codecs: serializer.NewCodecFactory(options.Scheme, mutators...),
7583
paramCodec: runtime.NewParameterCodec(options.Scheme),
7684
resync: options.ResyncPeriod,
7785
startWait: make(chan struct{}),

pkg/client/client.go

+12-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+
// CodecFactoryOptions, if provided, will be used to indicate whether enable Strict/Pretty mode of CodecFactory
48+
CodecFactoryOptions serializer.CodecFactoryOptions
49+
4750
// Mapper, if provided, will be used to map GroupVersionKinds to Resources
4851
Mapper meta.RESTMapper
4952

@@ -145,12 +148,20 @@ func newClient(config *rest.Config, options Options) (*client, error) {
145148
}
146149
}
147150

151+
var mutators []serializer.CodecFactoryOptionsMutator
152+
if options.CodecFactoryOptions.Strict {
153+
mutators = append(mutators, serializer.EnableStrict)
154+
}
155+
if options.CodecFactoryOptions.Pretty {
156+
mutators = append(mutators, serializer.EnablePretty)
157+
}
158+
148159
resources := &clientRestResources{
149160
httpClient: options.HTTPClient,
150161
config: config,
151162
scheme: options.Scheme,
152163
mapper: options.Mapper,
153-
codecs: serializer.NewCodecFactory(options.Scheme),
164+
codecs: serializer.NewCodecFactory(options.Scheme, mutators...),
154165

155166
structuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta),
156167
unstructuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta),

0 commit comments

Comments
 (0)