From 1b5d830fb0d960fe99589dac170c090faf80b7b2 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 ++++++------- controllers/agentmachine_controller_test.go | 6 +++--- main.go | 8 ++++---- 3 files changed, 13 insertions(+), 14 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/controllers/agentmachine_controller_test.go b/controllers/agentmachine_controller_test.go index be966419..47ea3f68 100644 --- a/controllers/agentmachine_controller_test.go +++ b/controllers/agentmachine_controller_test.go @@ -134,7 +134,7 @@ func newAgentMachine(name, namespace string, spec capiproviderv1alpha1.AgentMach }, Status: clusterv1.MachineStatus{}, } - machine.ObjectMeta.Labels[clusterv1.ClusterLabelName] = cluster.Name + machine.ObjectMeta.Labels[clusterv1.ClusterNameLabel] = cluster.Name Expect(c.Create(ctx, &machine)).To(BeNil()) machineOwnerRef := metav1.OwnerReference{APIVersion: "cluster.x-k8s.io/v1beta1", Kind: "Machine", Name: machine.Name} @@ -616,7 +616,7 @@ var _ = Describe("mapMachineToAgentMachine", func() { agentMachine, machine := newAgentMachine("agentMachine-1", testNamespace, capiproviderv1alpha1.AgentMachineSpec{}, ctx, c) Expect(c.Create(ctx, agentMachine)).To(Succeed()) - requests := amr.mapMachineToAgentMachine(machine) + requests := amr.mapMachineToAgentMachine(ctx, machine) Expect(len(requests)).To(Equal(1)) agentMachineKey := types.NamespacedName{ @@ -637,6 +637,6 @@ var _ = Describe("mapMachineToAgentMachine", func() { machine := clusterv1.Machine{} Expect(c.Get(ctx, key, &machine)).To(Succeed()) - Expect(amr.mapMachineToAgentMachine(&machine)).To(BeEmpty()) + Expect(amr.mapMachineToAgentMachine(ctx, &machine)).To(BeEmpty()) }) }) 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")