Skip to content

Commit 2c457c6

Browse files
committed
kubernetes/client: add more options for cache client
1 parent 40d14a2 commit 2c457c6

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

kubernetes/client/generic/client.go

+38-10
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,23 @@ import (
1010
"k8s.io/apimachinery/pkg/runtime"
1111
"k8s.io/client-go/kubernetes/scheme"
1212
"k8s.io/client-go/rest"
13+
toolscache "k8s.io/client-go/tools/cache"
1314
"sigs.k8s.io/controller-runtime/pkg/cache"
1415
"sigs.k8s.io/controller-runtime/pkg/client"
1516
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
1617
)
1718

1819
// Options defines options needed to generate a client.
1920
type Options struct {
20-
syncPeriod *time.Duration
21-
scheme *runtime.Scheme
22-
cacheReader bool
23-
ctx context.Context
24-
httpClient *http.Client
25-
mapper meta.RESTMapper
21+
syncPeriod *time.Duration
22+
scheme *runtime.Scheme
23+
cacheReader bool
24+
ctx context.Context
25+
httpClient *http.Client
26+
mapper meta.RESTMapper
27+
defaultNamespaces map[string]cache.Config
28+
defaultTransform toolscache.TransformFunc
29+
byObject map[client.Object]cache.ByObject
2630
}
2731

2832
// WithSyncPeriod sets the SyncPeriod time option.
@@ -71,6 +75,27 @@ func WithMapper(mapper meta.RESTMapper) func(opts *Options) {
7175
}
7276
}
7377

78+
// WithDefaultNamespaces sets the DefaultNamespaces for the cache client.
79+
func WithDefaultNamespaces(defaultNamespaces map[string]cache.Config) func(opts *Options) {
80+
return func(opts *Options) {
81+
opts.defaultNamespaces = defaultNamespaces
82+
}
83+
}
84+
85+
// WithDefaultTransform sets the DefaultTransform for the cache client.
86+
func WithDefaultTransform(defaultTransform toolscache.TransformFunc) func(opts *Options) {
87+
return func(opts *Options) {
88+
opts.defaultTransform = defaultTransform
89+
}
90+
}
91+
92+
// WithByObject sets the ByObject for the cache client.
93+
func WithByObject(byObject map[client.Object]cache.ByObject) func(opts *Options) {
94+
return func(opts *Options) {
95+
opts.byObject = byObject
96+
}
97+
}
98+
7499
// NewCache returns a controller-runtime cache client implementation.
75100
func NewCache(config *rest.Config, options ...func(*Options)) (cache.Cache, error) {
76101
opts := &Options{
@@ -97,10 +122,13 @@ func NewCache(config *rest.Config, options ...func(*Options)) (cache.Cache, erro
97122
}
98123

99124
cacheClient, err := cache.New(config, cache.Options{
100-
HTTPClient: opts.httpClient,
101-
Scheme: opts.scheme,
102-
Mapper: opts.mapper,
103-
SyncPeriod: opts.syncPeriod,
125+
HTTPClient: opts.httpClient,
126+
Scheme: opts.scheme,
127+
Mapper: opts.mapper,
128+
SyncPeriod: opts.syncPeriod,
129+
DefaultNamespaces: opts.defaultNamespaces,
130+
DefaultTransform: opts.defaultTransform,
131+
ByObject: opts.byObject,
104132
})
105133
if err != nil {
106134
return nil, err

0 commit comments

Comments
 (0)