Skip to content

Commit 59f98f8

Browse files
committed
Add KCP helpers for creating new managers/clients/caches
1 parent 3f3188c commit 59f98f8

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

pkg/kcp/wrappers.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright 2022 The KCP Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package kcp
18+
19+
import (
20+
"net/http"
21+
22+
"k8s.io/client-go/rest"
23+
24+
kcpcache "github.com/kcp-dev/apimachinery/pkg/cache"
25+
kcpclient "github.com/kcp-dev/apimachinery/pkg/client"
26+
27+
ctrl "sigs.k8s.io/controller-runtime"
28+
"sigs.k8s.io/controller-runtime/pkg/cache"
29+
"sigs.k8s.io/controller-runtime/pkg/client"
30+
"sigs.k8s.io/controller-runtime/pkg/cluster"
31+
"sigs.k8s.io/controller-runtime/pkg/manager"
32+
)
33+
34+
// NewClusterAwareManager returns a kcp-aware manager with appropriate defaults for cache and
35+
// client creation.
36+
func NewClusterAwareManager(cfg *rest.Config, options ctrl.Options) (manager.Manager, error) {
37+
if options.NewCache == nil {
38+
options.NewCache = NewClusterAwareCache
39+
}
40+
if options.NewClient == nil {
41+
options.NewClient = NewClusterAwareClient
42+
}
43+
44+
return ctrl.NewManager(cfg, options)
45+
}
46+
47+
// NewClusterAwareCache returns a cache.Cache that handles multi-cluster watches.
48+
func NewClusterAwareCache(config *rest.Config, opts cache.Options) (cache.Cache, error) {
49+
c := rest.CopyConfig(config)
50+
c.Host += "/clusters/*"
51+
opts.KeyFunction = kcpcache.ClusterAwareKeyFunc
52+
return cache.New(c, opts)
53+
}
54+
55+
// NewClusterAwareClient returns a client.Client that is configured to use the context
56+
// to scope requests to the proper cluster. To scope requests, pass the request context with the cluster set.
57+
// Example:
58+
// import (
59+
// "context"
60+
// kcpclient "github.com/kcp-dev/apimachinery/pkg/client"
61+
// ctrl "sigs.k8s.io/controller-runtime"
62+
// )
63+
// func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
64+
// ctx = kcpclient.WithCluster(ctx, req.ObjectKey.Cluster)
65+
// // from here on pass this context to all client calls
66+
// ...
67+
// }
68+
func NewClusterAwareClient(cache cache.Cache, config *rest.Config, opts client.Options, uncachedObjects ...client.Object) (client.Client, error) {
69+
httpClient, err := ClusterAwareHTTPClient(config)
70+
if err != nil {
71+
return nil, err
72+
}
73+
opts.HTTPClient = httpClient
74+
return cluster.DefaultNewClient(cache, config, opts, uncachedObjects...)
75+
}
76+
77+
// ClusterAwareHTTPClient returns an http.Client with a cluster aware round tripper.
78+
func ClusterAwareHTTPClient(config *rest.Config) (*http.Client, error) {
79+
httpClient, err := rest.HTTPClientFor(config)
80+
if err != nil {
81+
return nil, err
82+
}
83+
84+
httpClient.Transport = kcpclient.NewClusterRoundTripper(httpClient.Transport)
85+
return httpClient, nil
86+
}

0 commit comments

Comments
 (0)