From 0c71128f4340b014bd67fc7b1f9653a0ca651d50 Mon Sep 17 00:00:00 2001 From: CrystalChun Date: Tue, 19 Sep 2023 09:19:07 -0700 Subject: [PATCH] MGMT-14838: Required code changes for upgraded dependencies https://issues.redhat.com/browse/MGMT-14838 These code changes are needed after upgrading some dependencies in the previous commit. --- controllers/agentmachine_controller.go | 13 ++++----- controllers/agentmachine_controller_test.go | 6 ++-- controllers/suite_test.go | 5 +--- main.go | 31 +++++++++++++-------- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/controllers/agentmachine_controller.go b/controllers/agentmachine_controller.go index e334dabf..8cfd00a9 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 ( @@ -640,7 +639,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(), @@ -652,7 +651,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{} } @@ -677,7 +676,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(), @@ -694,7 +693,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{} } @@ -716,7 +715,7 @@ func (r *AgentMachineReconciler) SetupWithManager(mgr ctrl.Manager, agentNamespa return ctrl.NewControllerManagedBy(mgr). For(&capiproviderv1.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 af731204..d9f7e871 100644 --- a/controllers/agentmachine_controller_test.go +++ b/controllers/agentmachine_controller_test.go @@ -134,7 +134,7 @@ func newAgentMachine(name, namespace string, spec capiproviderv1.AgentMachineSpe }, 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, capiproviderv1.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/controllers/suite_test.go b/controllers/suite_test.go index 4892cfd9..fa6991f0 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -26,7 +26,6 @@ import ( "k8s.io/client-go/kubernetes/scheme" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/envtest" - "sigs.k8s.io/controller-runtime/pkg/envtest/printer" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" ) @@ -40,9 +39,7 @@ var testEnv *envtest.Environment func TestAPIs(t *testing.T) { RegisterFailHandler(Fail) - RunSpecsWithDefaultAndCustomReporters(t, - "Controller Suite", - []Reporter{printer.NewlineReporter{}}) + RunSpecs(t, "Controller Suite") } var _ = BeforeSuite(func() { diff --git a/main.go b/main.go index fb3b0a0f..43d4145a 100644 --- a/main.go +++ b/main.go @@ -35,6 +35,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/log/zap" + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" ) var ( @@ -78,19 +79,21 @@ func main() { ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) scheme = controllers.GetKubeClientSchemes(scheme) mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ + Metrics: metricsserver.Options{ + BindAddress: metricsAddr, + }, Scheme: scheme, - MetricsBindAddress: metricsAddr, - Port: 9443, 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") os.Exit(1) @@ -101,10 +104,16 @@ func main() { if agentsNamespace != "" { setupLog.Info("Watching Agents objects only in namespace for reconciliation", "agent-namespace", agentsNamespace) agentMgr, err2 := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ - MetricsBindAddress: "0", - Scheme: scheme, - LeaderElection: false, - Namespace: agentsNamespace, + Metrics: metricsserver.Options{ + BindAddress: "0", + }, + Scheme: scheme, + LeaderElection: false, + Cache: cache.Options{ + ByObject: map[client.Object]cache.ByObject{ + &aiv1beta1.Agent{}: {Field: fields.OneTermEqualSelector("metadata.namespace", agentsNamespace)}, + }, + }, }) if err2 != nil { setupLog.Error(err, "unable to start Agent manager")