From 19524bf8f2b3c0c84b7acde731e79bdc4697ed4a Mon Sep 17 00:00:00 2001 From: Simon Beck Date: Wed, 4 Dec 2024 11:44:41 +0100 Subject: [PATCH] Working reconcile across clusters --- cmd/sliexporter.go | 16 ++++++++-------- .../maintenance_controller/controller.go | 6 +++++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/cmd/sliexporter.go b/cmd/sliexporter.go index 32499c790..5df5771a2 100644 --- a/cmd/sliexporter.go +++ b/cmd/sliexporter.go @@ -156,7 +156,7 @@ func (s *sliProber) executeSLIProber(cmd *cobra.Command, _ []string) error { if utils.IsKindAvailable(managedupgradev1beta1.GroupVersion, "UpgradeJob", config) { log.Info("Enable OC maintenance observer") - serviceCluster, err := getServiceCluster() + serviceCluster, err := getServiceCluster(config) if err != nil { return err } @@ -164,6 +164,10 @@ func (s *sliProber) executeSLIProber(cmd *cobra.Command, _ []string) error { log.Error(err, "unable to create controller", "controller", "Maintenance Observer") return err } + err = mgr.Add(*serviceCluster) + if err != nil { + return err + } } if utils.IsKindAvailable(vshnv1.GroupVersion, "XVSHNMariaDB", ctrl.GetConfigOrDie()) { log.Info("Enabling VSHNMariaDB controller") @@ -198,19 +202,15 @@ func (s *sliProber) executeSLIProber(cmd *cobra.Command, _ []string) error { return nil } -// serviceCluster will create an incluster config by default. -// If serviceKubeConfig is set, it will use that instead. -func getServiceCluster() (*cluster.Cluster, error) { - config, err := getServiceClusterConfig() - if err != nil { - return nil, err - } +func getServiceCluster(config *rest.Config) (*cluster.Cluster, error) { serviceCluster, err := cluster.New(config, func(o *cluster.Options) { o.Scheme = pkg.SetupScheme() }) return &serviceCluster, err } +// getServiceClusterConfig will create an incluster config by default. +// If serviceKubeConfig is set, it will use that instead. func getServiceClusterConfig() (*rest.Config, error) { if s.serviceKubeConfig != "" { diff --git a/pkg/sliexporter/maintenance_controller/controller.go b/pkg/sliexporter/maintenance_controller/controller.go index ec66afd8f..9911aa4ee 100644 --- a/pkg/sliexporter/maintenance_controller/controller.go +++ b/pkg/sliexporter/maintenance_controller/controller.go @@ -96,8 +96,12 @@ func (r *MaintenanceReconciler) jobState(job managedupgradev1beta1.UpgradeJob) s func (r *MaintenanceReconciler) SetupWithManager(mgr ctrl.Manager, serviceCluster *cluster.Cluster) error { sc := *serviceCluster + // For external reconcile triggers we can't register a kind via `For()` + // instead we have to name the reconciler something + // usually the name is the lowercase of the kind + // so that's what I've used here. return ctrl.NewControllerManagedBy(mgr). - For(&managedupgradev1beta1.UpgradeJob{}). + Named("upgradejob"). // This is the magic sauce, it makes the reconciler reconcile on events happening on the serviceCluster WatchesRawSource(source.Kind(sc.GetCache(), &managedupgradev1beta1.UpgradeJob{}, &handler.TypedEnqueueRequestForObject[*managedupgradev1beta1.UpgradeJob]{})). Complete(r)