Skip to content

Commit 824b15a

Browse files
Merge pull request #24 from astefanutti/pr-apireader
UPSTREAM: <carry>: Support cluster-aware APIReader client
2 parents 4b60fd6 + 4691cdd commit 824b15a

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

pkg/cluster/cluster.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ type Options struct {
109109
// by the manager. If not set this will use the default new cache function.
110110
NewCache cache.NewCacheFunc
111111

112+
// NewAPIReaderFunc is the function that creates the APIReader client to be
113+
// used by the manager. If not set this will use the default new APIReader
114+
// function.
115+
NewAPIReader NewAPIReaderFunc
116+
112117
// NewClient is the func that creates the client to be used by the manager.
113118
// If not set this will create the default DelegatingClient that will
114119
// use the cache for reads and the client for writes.
@@ -169,7 +174,7 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
169174

170175
clientOptions := client.Options{Scheme: options.Scheme, Mapper: mapper}
171176

172-
apiReader, err := client.New(config, clientOptions)
177+
apiReader, err := options.NewAPIReader(config, clientOptions)
173178
if err != nil {
174179
return nil, err
175180
}
@@ -217,6 +222,10 @@ func setOptionsDefaults(options Options) Options {
217222
}
218223
}
219224

225+
if options.NewAPIReader == nil {
226+
options.NewAPIReader = DefaultNewAPIReader
227+
}
228+
220229
// Allow users to define how to create a new client
221230
if options.NewClient == nil {
222231
options.NewClient = DefaultNewClient
@@ -268,3 +277,11 @@ func DefaultNewClient(cache cache.Cache, config *rest.Config, options client.Opt
268277
UncachedObjects: uncachedObjects,
269278
})
270279
}
280+
281+
// NewAPIReaderFunc allows a user to define how to create an API server client.
282+
type NewAPIReaderFunc func(config *rest.Config, options client.Options) (client.Reader, error)
283+
284+
// DefaultNewAPIReader creates the default API server client.
285+
func DefaultNewAPIReader(config *rest.Config, options client.Options) (client.Reader, error) {
286+
return client.New(config, options)
287+
}

pkg/kcp/wrappers.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func NewClusterAwareManager(cfg *rest.Config, options ctrl.Options) (manager.Man
4343
options.NewCache = NewClusterAwareCache
4444
}
4545

46+
if options.NewAPIReader == nil {
47+
options.NewAPIReader = NewClusterAwareAPIReader
48+
}
49+
4650
if options.NewClient == nil {
4751
options.NewClient = NewClusterAwareClient
4852
}
@@ -67,6 +71,29 @@ func NewClusterAwareCache(config *rest.Config, opts cache.Options) (cache.Cache,
6771
return cache.New(c, opts)
6872
}
6973

74+
// NewClusterAwareAPIReader returns a client.Reader that provides read-only access to the API server,
75+
// and is configured to use the context to scope requests to the proper cluster. To scope requests,
76+
// pass the request context with the cluster set.
77+
// Example:
78+
// import (
79+
// "context"
80+
// kcpclient "github.com/kcp-dev/apimachinery/pkg/client"
81+
// ctrl "sigs.k8s.io/controller-runtime"
82+
// )
83+
// func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
84+
// ctx = kcpclient.WithCluster(ctx, req.ObjectKey.Cluster)
85+
// // from here on pass this context to all client calls
86+
// ...
87+
// }
88+
func NewClusterAwareAPIReader(config *rest.Config, opts client.Options) (client.Reader, error) {
89+
httpClient, err := ClusterAwareHTTPClient(config)
90+
if err != nil {
91+
return nil, err
92+
}
93+
opts.HTTPClient = httpClient
94+
return cluster.DefaultNewAPIReader(config, opts)
95+
}
96+
7097
// NewClusterAwareClient returns a client.Client that is configured to use the context
7198
// to scope requests to the proper cluster. To scope requests, pass the request context with the cluster set.
7299
// Example:

pkg/manager/manager.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ type Options struct {
234234
// by the manager. If not set this will use the default new cache function.
235235
NewCache cache.NewCacheFunc
236236

237+
// NewAPIReaderFunc is the function that creates the APIReader client to be
238+
// used by the manager. If not set this will use the default new APIReader
239+
// function.
240+
NewAPIReader cluster.NewAPIReaderFunc
241+
237242
// NewClient is the func that creates the client to be used by the manager.
238243
// If not set this will create the default DelegatingClient that will
239244
// use the cache for reads and the client for writes.
@@ -326,6 +331,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
326331
clusterOptions.SyncPeriod = options.SyncPeriod
327332
clusterOptions.Namespace = options.Namespace
328333
clusterOptions.NewCache = options.NewCache
334+
clusterOptions.NewAPIReader = options.NewAPIReader
329335
clusterOptions.NewClient = options.NewClient
330336
clusterOptions.ClientDisableCacheFor = options.ClientDisableCacheFor
331337
clusterOptions.DryRunClient = options.DryRunClient

0 commit comments

Comments
 (0)