Skip to content

Commit 4a90459

Browse files
ncdcfabianvf
andcommitted
UPSTREAM: <carry>: add cluster-aware manager, client, etc
Signed-off-by: Andy Goldstein <[email protected]> Co-authored-by: Fabian von Feilitzsch <[email protected]>
1 parent 8c6f4b4 commit 4a90459

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

pkg/kcp/wrappers.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
"strings"
22+
23+
"k8s.io/apimachinery/pkg/api/meta"
24+
"k8s.io/client-go/rest"
25+
k8scache "k8s.io/client-go/tools/cache"
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/client/apiutil"
31+
"sigs.k8s.io/controller-runtime/pkg/cluster"
32+
"sigs.k8s.io/controller-runtime/pkg/manager"
33+
34+
kcpcache "github.com/kcp-dev/apimachinery/pkg/cache"
35+
kcpclient "github.com/kcp-dev/apimachinery/pkg/client"
36+
"github.com/kcp-dev/apimachinery/third_party/informers"
37+
)
38+
39+
// NewClusterAwareManager returns a kcp-aware manager with appropriate defaults for cache and
40+
// client creation.
41+
func NewClusterAwareManager(cfg *rest.Config, options ctrl.Options) (manager.Manager, error) {
42+
if options.NewCache == nil {
43+
options.NewCache = NewClusterAwareCache
44+
}
45+
46+
if options.NewClient == nil {
47+
options.NewClient = NewClusterAwareClient
48+
}
49+
50+
if options.MapperProvider == nil {
51+
options.MapperProvider = NewClusterAwareMapperProvider
52+
}
53+
54+
return ctrl.NewManager(cfg, options)
55+
}
56+
57+
// NewClusterAwareCache returns a cache.Cache that handles multi-cluster watches.
58+
func NewClusterAwareCache(config *rest.Config, opts cache.Options) (cache.Cache, error) {
59+
c := rest.CopyConfig(config)
60+
c.Host += "/clusters/*"
61+
opts.NewInformerFunc = informers.NewSharedIndexInformer
62+
63+
opts.Indexers = k8scache.Indexers{
64+
kcpcache.ClusterIndexName: kcpcache.ClusterIndexFunc,
65+
kcpcache.ClusterAndNamespaceIndexName: kcpcache.ClusterAndNamespaceIndexFunc,
66+
}
67+
return cache.New(c, opts)
68+
}
69+
70+
// NewClusterAwareClient returns a client.Client that is configured to use the context
71+
// to scope requests to the proper cluster. To scope requests, pass the request context with the cluster set.
72+
// Example:
73+
// import (
74+
// "context"
75+
// kcpclient "github.com/kcp-dev/apimachinery/pkg/client"
76+
// ctrl "sigs.k8s.io/controller-runtime"
77+
// )
78+
// func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
79+
// ctx = kcpclient.WithCluster(ctx, req.ObjectKey.Cluster)
80+
// // from here on pass this context to all client calls
81+
// ...
82+
// }
83+
func NewClusterAwareClient(cache cache.Cache, config *rest.Config, opts client.Options, uncachedObjects ...client.Object) (client.Client, error) {
84+
httpClient, err := ClusterAwareHTTPClient(config)
85+
if err != nil {
86+
return nil, err
87+
}
88+
opts.HTTPClient = httpClient
89+
return cluster.DefaultNewClient(cache, config, opts, uncachedObjects...)
90+
}
91+
92+
// ClusterAwareHTTPClient returns an http.Client with a cluster aware round tripper.
93+
func ClusterAwareHTTPClient(config *rest.Config) (*http.Client, error) {
94+
httpClient, err := rest.HTTPClientFor(config)
95+
if err != nil {
96+
return nil, err
97+
}
98+
99+
httpClient.Transport = kcpclient.NewClusterRoundTripper(httpClient.Transport)
100+
return httpClient, nil
101+
}
102+
103+
// NewClusterAwareMapperProvider is a MapperProvider that returns a logical cluster aware meta.RESTMapper.
104+
func NewClusterAwareMapperProvider(c *rest.Config) (meta.RESTMapper, error) {
105+
mapperCfg := rest.CopyConfig(c)
106+
if !strings.HasSuffix(mapperCfg.Host, "/clusters/*") {
107+
mapperCfg.Host += "/clusters/*"
108+
}
109+
110+
return apiutil.NewDynamicRESTMapper(mapperCfg)
111+
}

0 commit comments

Comments
 (0)