Skip to content

Commit

Permalink
Merge pull request #24 from astefanutti/pr-apireader
Browse files Browse the repository at this point in the history
UPSTREAM: <carry>: Support cluster-aware APIReader client
  • Loading branch information
openshift-merge-robot authored Sep 2, 2022
2 parents 4b60fd6 + 4691cdd commit 824b15a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ type Options struct {
// by the manager. If not set this will use the default new cache function.
NewCache cache.NewCacheFunc

// NewAPIReaderFunc is the function that creates the APIReader client to be
// used by the manager. If not set this will use the default new APIReader
// function.
NewAPIReader NewAPIReaderFunc

// NewClient is the func that creates the client to be used by the manager.
// If not set this will create the default DelegatingClient that will
// use the cache for reads and the client for writes.
Expand Down Expand Up @@ -169,7 +174,7 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {

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

apiReader, err := client.New(config, clientOptions)
apiReader, err := options.NewAPIReader(config, clientOptions)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -217,6 +222,10 @@ func setOptionsDefaults(options Options) Options {
}
}

if options.NewAPIReader == nil {
options.NewAPIReader = DefaultNewAPIReader
}

// Allow users to define how to create a new client
if options.NewClient == nil {
options.NewClient = DefaultNewClient
Expand Down Expand Up @@ -268,3 +277,11 @@ func DefaultNewClient(cache cache.Cache, config *rest.Config, options client.Opt
UncachedObjects: uncachedObjects,
})
}

// NewAPIReaderFunc allows a user to define how to create an API server client.
type NewAPIReaderFunc func(config *rest.Config, options client.Options) (client.Reader, error)

// DefaultNewAPIReader creates the default API server client.
func DefaultNewAPIReader(config *rest.Config, options client.Options) (client.Reader, error) {
return client.New(config, options)
}
27 changes: 27 additions & 0 deletions pkg/kcp/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func NewClusterAwareManager(cfg *rest.Config, options ctrl.Options) (manager.Man
options.NewCache = NewClusterAwareCache
}

if options.NewAPIReader == nil {
options.NewAPIReader = NewClusterAwareAPIReader
}

if options.NewClient == nil {
options.NewClient = NewClusterAwareClient
}
Expand All @@ -67,6 +71,29 @@ func NewClusterAwareCache(config *rest.Config, opts cache.Options) (cache.Cache,
return cache.New(c, opts)
}

// NewClusterAwareAPIReader returns a client.Reader that provides read-only access to the API server,
// and is configured to use the context to scope requests to the proper cluster. To scope requests,
// pass the request context with the cluster set.
// Example:
// import (
// "context"
// kcpclient "github.com/kcp-dev/apimachinery/pkg/client"
// ctrl "sigs.k8s.io/controller-runtime"
// )
// func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
// ctx = kcpclient.WithCluster(ctx, req.ObjectKey.Cluster)
// // from here on pass this context to all client calls
// ...
// }
func NewClusterAwareAPIReader(config *rest.Config, opts client.Options) (client.Reader, error) {
httpClient, err := ClusterAwareHTTPClient(config)
if err != nil {
return nil, err
}
opts.HTTPClient = httpClient
return cluster.DefaultNewAPIReader(config, opts)
}

// NewClusterAwareClient returns a client.Client that is configured to use the context
// to scope requests to the proper cluster. To scope requests, pass the request context with the cluster set.
// Example:
Expand Down
6 changes: 6 additions & 0 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ type Options struct {
// by the manager. If not set this will use the default new cache function.
NewCache cache.NewCacheFunc

// NewAPIReaderFunc is the function that creates the APIReader client to be
// used by the manager. If not set this will use the default new APIReader
// function.
NewAPIReader cluster.NewAPIReaderFunc

// NewClient is the func that creates the client to be used by the manager.
// If not set this will create the default DelegatingClient that will
// use the cache for reads and the client for writes.
Expand Down Expand Up @@ -326,6 +331,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
clusterOptions.SyncPeriod = options.SyncPeriod
clusterOptions.Namespace = options.Namespace
clusterOptions.NewCache = options.NewCache
clusterOptions.NewAPIReader = options.NewAPIReader
clusterOptions.NewClient = options.NewClient
clusterOptions.ClientDisableCacheFor = options.ClientDisableCacheFor
clusterOptions.DryRunClient = options.DryRunClient
Expand Down

0 comments on commit 824b15a

Please sign in to comment.