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 Aug 28, 2023
1 parent babebd9 commit bb43298
Show file tree
Hide file tree
Showing 3 changed files with 33 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 @@ -46,7 +46,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 @@ -159,7 +160,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 @@ -172,7 +173,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 := rancherClusterNameFromCAPICluster(capiCluster.Name)

rancherCluster, err := rancherClusterHandler.Get(client.ObjectKey{Namespace: capiCluster.Namespace, Name: rancherClusterName})
Expand Down Expand Up @@ -331,7 +332,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 @@ -42,6 +42,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
47 changes: 27 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,45 @@ 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).
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 bb43298

Please sign in to comment.