diff --git a/controllers/machinehealthcheck_controller.go b/controllers/machinehealthcheck_controller.go index 9b9f36f34079..7f418092125c 100644 --- a/controllers/machinehealthcheck_controller.go +++ b/controllers/machinehealthcheck_controller.go @@ -145,7 +145,7 @@ func (r *MachineHealthCheckReconciler) Reconcile(ctx context.Context, req ctrl.R defer func() { // Always attempt to patch the object and status after each reconciliation. // Patch ObservedGeneration only if the reconciliation completed successfully - patchOpts := []patch.Option{} + var patchOpts []patch.Option if reterr == nil { patchOpts = append(patchOpts, patch.WithStatusObservedGeneration{}) } @@ -263,7 +263,7 @@ func (r *MachineHealthCheckReconciler) reconcile(ctx context.Context, logger log EventRemediationRestricted, message, ) - errList := []error{} + var errList []error for _, t := range append(healthy, unhealthy...) { if err := t.patchHelper.Patch(ctx, t.Machine); err != nil { errList = append(errList, errors.Wrapf(err, "failed to patch machine status for machine: %s/%s", t.Machine.Namespace, t.Machine.Name)) @@ -287,8 +287,8 @@ func (r *MachineHealthCheckReconciler) reconcile(ctx context.Context, logger log m.Status.RemediationsAllowed = remediationCount conditions.MarkTrue(m, clusterv1.RemediationAllowedCondition) - errList := r.PatchUnhealthyTargets(ctx, logger, unhealthy, cluster, m) - errList = append(errList, r.PatchHealthyTargets(ctx, logger, healthy, cluster, m)...) + errList := r.patchUnhealthyTargets(ctx, logger, unhealthy, cluster, m) + errList = append(errList, r.patchHealthyTargets(ctx, logger, healthy, m)...) // handle update errors if len(errList) > 0 { @@ -306,9 +306,9 @@ func (r *MachineHealthCheckReconciler) reconcile(ctx context.Context, logger log return ctrl.Result{}, nil } -// PatchHealthyTargets patches healthy machines with MachineHealthCheckSuccededCondition. -func (r *MachineHealthCheckReconciler) PatchHealthyTargets(ctx context.Context, logger logr.Logger, healthy []healthCheckTarget, cluster *clusterv1.Cluster, m *clusterv1.MachineHealthCheck) []error { - errList := []error{} +// patchHealthyTargets patches healthy machines with MachineHealthCheckSucceededCondition. +func (r *MachineHealthCheckReconciler) patchHealthyTargets(ctx context.Context, logger logr.Logger, healthy []healthCheckTarget, m *clusterv1.MachineHealthCheck) []error { + var errList []error for _, t := range healthy { if m.Spec.RemediationTemplate != nil { // Get remediation request object @@ -337,10 +337,10 @@ func (r *MachineHealthCheckReconciler) PatchHealthyTargets(ctx context.Context, return errList } -// PatchUnhealthyTargets patches machines with MachineOwnerRemediatedCondition for remediation. -func (r *MachineHealthCheckReconciler) PatchUnhealthyTargets(ctx context.Context, logger logr.Logger, unhealthy []healthCheckTarget, cluster *clusterv1.Cluster, m *clusterv1.MachineHealthCheck) []error { +// patchUnhealthyTargets patches machines with MachineOwnerRemediatedCondition for remediation. +func (r *MachineHealthCheckReconciler) patchUnhealthyTargets(ctx context.Context, logger logr.Logger, unhealthy []healthCheckTarget, cluster *clusterv1.Cluster, m *clusterv1.MachineHealthCheck) []error { // mark for remediation - errList := []error{} + var errList []error for _, t := range unhealthy { condition := conditions.Get(t.Machine, clusterv1.MachineHealthCheckSuccededCondition) @@ -440,7 +440,7 @@ func (r *MachineHealthCheckReconciler) clusterToMachineHealthCheck(o client.Obje } // This list should only contain MachineHealthChecks which belong to the given Cluster - requests := []reconcile.Request{} + var requests []reconcile.Request for _, mhc := range mhcList.Items { key := types.NamespacedName{Namespace: mhc.Namespace, Name: mhc.Name} requests = append(requests, reconcile.Request{NamespacedName: key}) @@ -502,7 +502,7 @@ func (r *MachineHealthCheckReconciler) getMachineFromNode(ctx context.Context, n } // TODO(vincepri): Remove this loop once controller runtime fake client supports // adding indexes on objects. - items := []*clusterv1.Machine{} + var items []*clusterv1.Machine for i := range machineList.Items { machine := &machineList.Items[i] if machine.Status.NodeRef != nil && machine.Status.NodeRef.Name == nodeName { diff --git a/controllers/machinehealthcheck_controller_test.go b/controllers/machinehealthcheck_controller_test.go index 06f35e23afe4..920aef54ac55 100644 --- a/controllers/machinehealthcheck_controller_test.go +++ b/controllers/machinehealthcheck_controller_test.go @@ -2617,10 +2617,10 @@ func TestPatchTargets(t *testing.T) { } // Target with wrong patch helper will fail but the other one will be patched. - g.Expect(len(r.PatchUnhealthyTargets(context.TODO(), log.NullLogger{}, []healthCheckTarget{target1, target3}, defaultCluster, mhc))).To(BeNumerically(">", 0)) + g.Expect(len(r.patchUnhealthyTargets(context.TODO(), log.NullLogger{}, []healthCheckTarget{target1, target3}, defaultCluster, mhc))).To(BeNumerically(">", 0)) g.Expect(cl.Get(ctx, client.ObjectKey{Name: machine2.Name, Namespace: machine2.Namespace}, machine2)).NotTo(HaveOccurred()) g.Expect(conditions.Get(machine2, clusterv1.MachineOwnerRemediatedCondition).Status).To(Equal(corev1.ConditionFalse)) // Target with wrong patch helper will fail but the other one will be patched. - g.Expect(len(r.PatchHealthyTargets(context.TODO(), log.NullLogger{}, []healthCheckTarget{target1, target3}, defaultCluster, mhc))).To(BeNumerically(">", 0)) + g.Expect(len(r.patchHealthyTargets(context.TODO(), log.NullLogger{}, []healthCheckTarget{target1, target3}, mhc))).To(BeNumerically(">", 0)) }