diff --git a/controllers/machinehealthcheck_controller.go b/controllers/machinehealthcheck_controller.go index 0017dda415a0..4adba9a4cb1f 100644 --- a/controllers/machinehealthcheck_controller.go +++ b/controllers/machinehealthcheck_controller.go @@ -195,7 +195,7 @@ func (r *MachineHealthCheckReconciler) reconcile(ctx context.Context, logger log // fetch all targets logger.V(3).Info("Finding targets") - targets, err := r.getTargetsFromMHC(ctx, logger, remoteClient, m) + targets, err := r.getTargetsFromMHC(ctx, logger, remoteClient, cluster, m) if err != nil { logger.Error(err, "Failed to fetch targets from MachineHealthCheck") return ctrl.Result{}, err diff --git a/controllers/machinehealthcheck_targets.go b/controllers/machinehealthcheck_targets.go index c7cf43d7c8f7..17fd2cdd9ea7 100644 --- a/controllers/machinehealthcheck_targets.go +++ b/controllers/machinehealthcheck_targets.go @@ -176,7 +176,7 @@ func (t *healthCheckTarget) needsRemediation(logger logr.Logger, timeoutForMachi // getTargetsFromMHC uses the MachineHealthCheck's selector to fetch machines // and their nodes targeted by the health check, ready for health checking. -func (r *MachineHealthCheckReconciler) getTargetsFromMHC(ctx context.Context, logger logr.Logger, clusterClient client.Reader, mhc *clusterv1.MachineHealthCheck) ([]healthCheckTarget, error) { +func (r *MachineHealthCheckReconciler) getTargetsFromMHC(ctx context.Context, logger logr.Logger, clusterClient client.Reader, cluster *clusterv1.Cluster, mhc *clusterv1.MachineHealthCheck) ([]healthCheckTarget, error) { machines, err := r.getMachinesFromMHC(ctx, mhc) if err != nil { return nil, errors.Wrap(err, "error getting machines from MachineHealthCheck") @@ -185,11 +185,6 @@ func (r *MachineHealthCheckReconciler) getTargetsFromMHC(ctx context.Context, lo return nil, nil } - var cluster clusterv1.Cluster - if err := clusterClient.Get(ctx, client.ObjectKey{Namespace: mhc.Namespace, Name: mhc.Spec.ClusterName}, &cluster); err != nil { - return nil, errors.Wrapf(err, "error getting Cluster %s/%s for MachineHealthCheck %s", mhc.Namespace, mhc.Spec.ClusterName, mhc.Name) - } - targets := []healthCheckTarget{} for k := range machines { skip, reason := shouldSkipRemediation(&machines[k]) @@ -203,7 +198,7 @@ func (r *MachineHealthCheckReconciler) getTargetsFromMHC(ctx context.Context, lo return nil, errors.Wrap(err, "unable to initialize patch helper") } target := healthCheckTarget{ - Cluster: &cluster, + Cluster: cluster, MHC: mhc, Machine: &machines[k], patchHelper: patchHelper, diff --git a/controllers/machinehealthcheck_targets_test.go b/controllers/machinehealthcheck_targets_test.go index caab2448f59d..9860689fc0e0 100644 --- a/controllers/machinehealthcheck_targets_test.go +++ b/controllers/machinehealthcheck_targets_test.go @@ -170,7 +170,7 @@ func TestGetTargetsFromMHC(t *testing.T) { t.patchHelper = patchHelper } - targets, err := reconciler.getTargetsFromMHC(ctx, ctrl.LoggerFrom(ctx), k8sClient, testMHC) + targets, err := reconciler.getTargetsFromMHC(ctx, ctrl.LoggerFrom(ctx), k8sClient, cluster, testMHC) gs.Expect(err).ToNot(HaveOccurred()) gs.Expect(len(targets)).To(Equal(len(tc.expectedTargets)))