From b1a8556a7638bd70c41bb49a1a75be2123efac5b Mon Sep 17 00:00:00 2001 From: CrystalChun Date: Wed, 6 Sep 2023 12:47:51 -0700 Subject: [PATCH] MGMT-14838: Fix code after upgrade https://issues.redhat.com/browse/MGMT-14838 There were a few changes to controller-runtime such as the Cache BuilderWithOptions function and Source.Kind being removed. --- controllers/agentmachine_controller.go | 13 ++++++------- main.go | 8 ++++---- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/controllers/agentmachine_controller.go b/controllers/agentmachine_controller.go index 55037e0f..f2b473f5 100644 --- a/controllers/agentmachine_controller.go +++ b/controllers/agentmachine_controller.go @@ -49,7 +49,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "sigs.k8s.io/controller-runtime/pkg/source" ) const ( @@ -634,7 +633,7 @@ func getAddresses(foundAgent *aiv1beta1.Agent) []clusterv1.MachineAddress { return machineAddresses } -func (r *AgentMachineReconciler) mapMachineToAgentMachine(machine client.Object) []reconcile.Request { +func (r *AgentMachineReconciler) mapMachineToAgentMachine(ctx context.Context, machine client.Object) []reconcile.Request { log := r.Log.WithFields( logrus.Fields{ "machine": machine.GetName(), @@ -646,7 +645,7 @@ func (r *AgentMachineReconciler) mapMachineToAgentMachine(machine client.Object) opts := &client.ListOptions{ Namespace: machine.GetNamespace(), } - if err := r.List(context.Background(), amList, opts); err != nil { + if err := r.List(ctx, amList, opts); err != nil { log.Debugf("failed to list agent machines") return []reconcile.Request{} } @@ -671,7 +670,7 @@ func (r *AgentMachineReconciler) mapMachineToAgentMachine(machine client.Object) // SetupWithManager sets up the controller with the Manager. func (r *AgentMachineReconciler) SetupWithManager(mgr ctrl.Manager, agentNamespace string) error { - mapAgentToAgentMachine := func(agent client.Object) []reconcile.Request { + mapAgentToAgentMachine := func(ctx context.Context, agent client.Object) []reconcile.Request { log := r.Log.WithFields( logrus.Fields{ "agent": agent.GetName(), @@ -688,7 +687,7 @@ func (r *AgentMachineReconciler) SetupWithManager(mgr ctrl.Manager, agentNamespa Namespace: namespace, } - if err := r.List(context.Background(), amList, opts); err != nil { + if err := r.List(ctx, amList, opts); err != nil { log.WithError(err).Error("failed to list agent machines") return []reconcile.Request{} } @@ -710,7 +709,7 @@ func (r *AgentMachineReconciler) SetupWithManager(mgr ctrl.Manager, agentNamespa return ctrl.NewControllerManagedBy(mgr). For(&capiproviderv1alpha1.AgentMachine{}). - Watches(&source.Kind{Type: &aiv1beta1.Agent{}}, handler.EnqueueRequestsFromMapFunc(mapAgentToAgentMachine)). - Watches(&source.Kind{Type: &clusterv1.Machine{}}, handler.EnqueueRequestsFromMapFunc(r.mapMachineToAgentMachine)). + Watches(&aiv1beta1.Agent{}, handler.EnqueueRequestsFromMapFunc(mapAgentToAgentMachine)). + Watches(&clusterv1.Machine{}, handler.EnqueueRequestsFromMapFunc(r.mapMachineToAgentMachine)). Complete(r) } diff --git a/main.go b/main.go index 68b87a35..a286eb3a 100644 --- a/main.go +++ b/main.go @@ -84,12 +84,12 @@ func main() { HealthProbeBindAddress: probeAddr, LeaderElection: enableLeaderElection, LeaderElectionID: "7605f49b.agent-install.openshift.io", - NewCache: cache.BuilderWithOptions(cache.Options{ - DefaultSelector: cache.ObjectSelector{Field: fields.OneTermEqualSelector("metadata.namespace", watchNamespace)}, - SelectorsByObject: cache.SelectorsByObject{ + Cache: cache.Options{ + DefaultFieldSelector: fields.OneTermEqualSelector("metadata.namespace", watchNamespace), + ByObject: map[client.Object]cache.ByObject{ &aiv1beta1.Agent{}: {Field: fields.OneTermEqualSelector("metadata.namespace", agentsNamespace)}, }, - }), + }, }) if err != nil { setupLog.Error(err, "unable to start manager")