@@ -18,7 +18,6 @@ import (
18
18
"context"
19
19
"flag"
20
20
"fmt"
21
- "os"
22
21
23
22
kptoci "github.com/GoogleContainerTools/kpt/pkg/oci"
24
23
api "github.com/GoogleContainerTools/kpt/porch/controllers/remoterootsyncsets/api/v1alpha1"
@@ -28,11 +27,7 @@ import (
28
27
"github.com/GoogleContainerTools/kpt/porch/pkg/oci"
29
28
"k8s.io/apimachinery/pkg/api/meta"
30
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31
- "k8s.io/client-go/discovery"
32
- memory "k8s.io/client-go/discovery/cached"
33
- "k8s.io/client-go/dynamic"
34
30
"k8s.io/client-go/rest"
35
- "k8s.io/client-go/restmapper"
36
31
"k8s.io/klog/v2"
37
32
ctrl "sigs.k8s.io/controller-runtime"
38
33
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -59,6 +54,8 @@ func (o *Options) BindFlags(prefix string, flags *flag.FlagSet) {
59
54
type RemoteRootSyncSetReconciler struct {
60
55
Options
61
56
57
+ remoteclient.RemoteClientGetter
58
+
62
59
client.Client
63
60
64
61
ociStorage * kptoci.Storage
@@ -229,38 +226,21 @@ func updateAggregateStatus(subject *api.RemoteRootSyncSet) bool {
229
226
}
230
227
231
228
func (r * RemoteRootSyncSetReconciler ) applyToClusterRef (ctx context.Context , subject * api.RemoteRootSyncSet , clusterRef * api.ClusterRef ) (* applyset.ApplyResults , error ) {
232
- var restConfig * rest.Config
233
-
234
- if os .Getenv ("HACK_ENABLE_LOOPBACK" ) != "" {
235
- if clusterRef .Name == "loopback!" {
236
- restConfig = r .localRESTConfig
237
- klog .Warningf ("HACK: using loopback! configuration" )
238
- }
239
- }
240
-
241
- if restConfig == nil {
242
- rc , err := remoteclient .GetRemoteClient (ctx , r .Client , clusterRef , subject .Namespace )
243
- if err != nil {
244
- return nil , err
245
- }
246
- restConfig = rc
229
+ remoteClient , err := r .GetRemoteClient (ctx , clusterRef , subject .Namespace )
230
+ if err != nil {
231
+ return nil , err
247
232
}
248
233
249
- client , err := dynamic . NewForConfig ( restConfig )
234
+ restMapper , err := remoteClient . RESTMapper ( )
250
235
if err != nil {
251
- return nil , fmt . Errorf ( "failed to create a new dynamic client: %w" , err )
236
+ return nil , err
252
237
}
253
238
254
- // TODO: Use a better discovery client
255
- discovery , err := discovery .NewDiscoveryClientForConfig (restConfig )
239
+ dynamicClient , err := remoteClient .DynamicClient ()
256
240
if err != nil {
257
- return nil , fmt . Errorf ( "error building discovery client: %w" , err )
241
+ return nil , err
258
242
}
259
243
260
- cached := memory .NewMemCacheClient (discovery )
261
-
262
- restMapper := restmapper .NewDeferredDiscoveryRESTMapper (cached )
263
-
264
244
objects , err := r .BuildObjectsToApply (ctx , subject )
265
245
if err != nil {
266
246
return nil , err
@@ -278,7 +258,7 @@ func (r *RemoteRootSyncSetReconciler) applyToClusterRef(ctx context.Context, sub
278
258
279
259
applyset , err := applyset .New (applyset.Options {
280
260
RESTMapper : restMapper ,
281
- Client : client ,
261
+ Client : dynamicClient ,
282
262
PatchOptions : patchOptions ,
283
263
})
284
264
if err != nil {
@@ -339,6 +319,10 @@ func (r *RemoteRootSyncSetReconciler) SetupWithManager(mgr ctrl.Manager) error {
339
319
return err
340
320
}
341
321
322
+ if err := r .RemoteClientGetter .Init (mgr ); err != nil {
323
+ return err
324
+ }
325
+
342
326
r .Client = mgr .GetClient ()
343
327
344
328
if err := ctrl .NewControllerManagedBy (mgr ).
0 commit comments