Skip to content

Commit 9b05920

Browse files
authored
Refactor controller logic for getting RESTConfig to a remote cluster (#3712)
We had two copies, rationalize and take the best of each. Also remove the HACK_ENABLE_LOOPBACK hack now that we can target remote clusters.
1 parent e78ca44 commit 9b05920

File tree

8 files changed

+285
-322
lines changed

8 files changed

+285
-322
lines changed

porch/controllers/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ build-image:
2929

3030
.PHONY: run-local
3131
run-local:
32-
GCP_PROJECT_ID=${GCP_PROJECT_ID} HACK_ENABLE_LOOPBACK=1 go run .
32+
GCP_PROJECT_ID=${GCP_PROJECT_ID} go run .

porch/controllers/remoterootsyncsets/api/v1alpha1/remoterootsyncset_types.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,26 @@ type ClusterRef struct {
7474
Namespace string `json:"namespace,omitempty"`
7575
}
7676

77+
func (r *ClusterRef) GetKind() string {
78+
return r.Kind
79+
}
80+
81+
func (r *ClusterRef) GetName() string {
82+
return r.Name
83+
}
84+
85+
func (r *ClusterRef) GetNamespace() string {
86+
return r.Namespace
87+
}
88+
89+
func (r *ClusterRef) GetAPIVersion() string {
90+
return r.ApiVersion
91+
}
92+
93+
type PackageRef struct {
94+
Name string `json:"name,omitempty"`
95+
}
96+
7797
type RootSyncTemplate struct {
7898
SourceFormat string `json:"sourceFormat,omitempty"`
7999
// Git *GitInfo `json:"git,omitempty"`

porch/controllers/remoterootsyncsets/pkg/controllers/remoterootsyncset/remoterootsync_controller.go

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"context"
1919
"flag"
2020
"fmt"
21-
"os"
2221

2322
kptoci "github.com/GoogleContainerTools/kpt/pkg/oci"
2423
api "github.com/GoogleContainerTools/kpt/porch/controllers/remoterootsyncsets/api/v1alpha1"
@@ -28,11 +27,7 @@ import (
2827
"github.com/GoogleContainerTools/kpt/porch/pkg/oci"
2928
"k8s.io/apimachinery/pkg/api/meta"
3029
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"
3430
"k8s.io/client-go/rest"
35-
"k8s.io/client-go/restmapper"
3631
"k8s.io/klog/v2"
3732
ctrl "sigs.k8s.io/controller-runtime"
3833
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -59,6 +54,8 @@ func (o *Options) BindFlags(prefix string, flags *flag.FlagSet) {
5954
type RemoteRootSyncSetReconciler struct {
6055
Options
6156

57+
remoteclient.RemoteClientGetter
58+
6259
client.Client
6360

6461
ociStorage *kptoci.Storage
@@ -229,38 +226,21 @@ func updateAggregateStatus(subject *api.RemoteRootSyncSet) bool {
229226
}
230227

231228
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
247232
}
248233

249-
client, err := dynamic.NewForConfig(restConfig)
234+
restMapper, err := remoteClient.RESTMapper()
250235
if err != nil {
251-
return nil, fmt.Errorf("failed to create a new dynamic client: %w", err)
236+
return nil, err
252237
}
253238

254-
// TODO: Use a better discovery client
255-
discovery, err := discovery.NewDiscoveryClientForConfig(restConfig)
239+
dynamicClient, err := remoteClient.DynamicClient()
256240
if err != nil {
257-
return nil, fmt.Errorf("error building discovery client: %w", err)
241+
return nil, err
258242
}
259243

260-
cached := memory.NewMemCacheClient(discovery)
261-
262-
restMapper := restmapper.NewDeferredDiscoveryRESTMapper(cached)
263-
264244
objects, err := r.BuildObjectsToApply(ctx, subject)
265245
if err != nil {
266246
return nil, err
@@ -278,7 +258,7 @@ func (r *RemoteRootSyncSetReconciler) applyToClusterRef(ctx context.Context, sub
278258

279259
applyset, err := applyset.New(applyset.Options{
280260
RESTMapper: restMapper,
281-
Client: client,
261+
Client: dynamicClient,
282262
PatchOptions: patchOptions,
283263
})
284264
if err != nil {
@@ -339,6 +319,10 @@ func (r *RemoteRootSyncSetReconciler) SetupWithManager(mgr ctrl.Manager) error {
339319
return err
340320
}
341321

322+
if err := r.RemoteClientGetter.Init(mgr); err != nil {
323+
return err
324+
}
325+
342326
r.Client = mgr.GetClient()
343327

344328
if err := ctrl.NewControllerManagedBy(mgr).

0 commit comments

Comments
 (0)