Skip to content

UPSTREAM: <carry>: Support cluster-aware APIReader client #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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