Skip to content

Commit

Permalink
feat: use two differentiated clients in CAPIImportReconciler
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Salas <carlos.salas@suse.com>
  • Loading branch information
salasberryfin committed Sep 4, 2023
1 parent ee9d9bf commit 5c7e2d1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
9 changes: 5 additions & 4 deletions internal/controllers/import_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ const (

// CAPIImportReconciler represents a reconciler for importing CAPI clusters in Rancher.
type CAPIImportReconciler struct {
client.Client
Client client.Client
RancherClient client.Client
recorder record.EventRecorder
WatchFilterValue string
Scheme *runtime.Scheme
Expand Down Expand Up @@ -178,7 +179,7 @@ func (r *CAPIImportReconciler) Reconcile(ctx context.Context, req ctrl.Request)
errs = append(errs, fmt.Errorf("error reconciling cluster: %w", err))
}

if err := r.Patch(ctx, capiCluster, patchBase); err != nil {
if err := r.Client.Patch(ctx, capiCluster, patchBase); err != nil {
errs = append(errs, fmt.Errorf("failed to patch cluster: %w", err))
}

Expand All @@ -191,7 +192,7 @@ func (r *CAPIImportReconciler) Reconcile(ctx context.Context, req ctrl.Request)

func (r *CAPIImportReconciler) reconcile(ctx context.Context, capiCluster *clusterv1.Cluster) (ctrl.Result, error) {
// fetch the rancher clusters
rancherClusterHandler := rancher.NewClusterHandler(ctx, r.Client)
rancherClusterHandler := rancher.NewClusterHandler(ctx, r.RancherClient)
rancherClusterName := turtelesnaming.Name(capiCluster.Name).ToRancherName()

rancherCluster, err := rancherClusterHandler.Get(client.ObjectKey{Namespace: capiCluster.Namespace, Name: rancherClusterName})
Expand Down Expand Up @@ -346,7 +347,7 @@ func (r *CAPIImportReconciler) getClusterRegistrationManifest(ctx context.Contex
log := log.FromContext(ctx)

key := client.ObjectKey{Name: clusterRegistrationTokenName, Namespace: clusterName}
tokenHandler := rancher.NewClusterRegistrationTokenHandler(ctx, r.Client)
tokenHandler := rancher.NewClusterRegistrationTokenHandler(ctx, r.RancherClient)

token, err := tokenHandler.Get(key)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/controllers/import_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var _ = Describe("reconcile CAPI Cluster", func() {
BeforeEach(func() {
r = &CAPIImportReconciler{
Client: cl,
RancherClient: cl, // rancher and rancher-turtles deployed in the same cluster
remoteClientGetter: remote.NewClusterClient,
}

Expand Down
48 changes: 28 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func initFlags(fs *pflag.FlagSet) {
"Number of resources to process simultaneously")

fs.StringVar(&rancherKubeconfig, "rancher-kubeconfig", "",
"Path to a kubeconfig file. Only required if out-of-cluster.")
"Path to the Rancher kubeconfig file. Only required if running out-of-cluster.")
}

func main() {
Expand Down Expand Up @@ -161,38 +161,46 @@ func setupChecks(mgr ctrl.Manager) {
}
}

// setupReconcilers can either create a client for an in-cluster installation (rancher and rancher-turtles in the same cluster)
// or create a client for an out-of-cluster installation (rancher and rancher-turtles in different clusters).
func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
var c client.Client
rancherClient, err := setupRancherClient(mgr)
if err != nil {
setupLog.Error(err, "failed to create client")
os.Exit(1)
}

if err := (&controllers.CAPIImportReconciler{
Client: mgr.GetClient(),
RancherClient: rancherClient,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: concurrencyNumber}); err != nil {
setupLog.Error(err, "unable to create capi controller")
os.Exit(1)
}
}

// setupRancherClient can either create a client for an in-cluster installation (rancher and rancher-turtles in the same cluster)
// or create a client for an out-of-cluster installation (rancher and rancher-turtles in different clusters) based on the
// existence of Rancher kubeconfig file.
func setupRancherClient(mgr ctrl.Manager) (client.Client, error) {
if len(rancherKubeconfig) > 0 {
setupLog.Info("out-of-cluster installation of rancher-turtles", "using kubeconfig from path", rancherKubeconfig)

restConfig, err := loadConfigWithContext("", &clientcmd.ClientConfigLoadingRules{ExplicitPath: rancherKubeconfig}, "")
if err != nil {
setupLog.Error(err, "unable to load kubeconfig from file")
os.Exit(1)
return nil, fmt.Errorf("unable to load kubeconfig from file: %w", err)
}

c, err = client.New(restConfig, client.Options{Scheme: mgr.GetClient().Scheme()})

rancherClient, err := client.New(restConfig, client.Options{Scheme: mgr.GetClient().Scheme()})
if err != nil {
setupLog.Error(err, "failed to create client")
os.Exit(1)
return nil, err
}
} else {
setupLog.Info("in-cluster installation of rancher-turtles")
c = mgr.GetClient()
}

if err := (&controllers.CAPIImportReconciler{
Client: c,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: concurrencyNumber}); err != nil {
setupLog.Error(err, "unable to create capi controller")
os.Exit(1)
return rancherClient, nil
}

setupLog.Info("in-cluster installation of rancher-turtles")

return mgr.GetClient(), nil
}

// loadConfigWithContext loads a REST Config from a path using a logic similar to the one used in controller-runtime.
Expand Down

0 comments on commit 5c7e2d1

Please sign in to comment.