Skip to content

Commit bf04d69

Browse files
tchinmai7eljohnson92
authored andcommitted
chore: refactor
1 parent 58404f5 commit bf04d69

File tree

2 files changed

+32
-46
lines changed

2 files changed

+32
-46
lines changed

internal/controller/linodemachine_controller.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,6 @@ func (r *LinodeMachineReconciler) reconcilePreflightReady(ctx context.Context, i
717717
return ctrl.Result{}, nil
718718
}
719719

720-
//nolint:cyclop // making it simpler complicates things more
721720
func (r *LinodeMachineReconciler) reconcileUpdate(ctx context.Context, logger logr.Logger, machineScope *scope.MachineScope) (ctrl.Result, error) {
722721
logger.Info("updating machine")
723722
instanceID, err := util.GetInstanceID(machineScope.LinodeMachine.Spec.ProviderID)

internal/controller/linodemachinetemplate_controller.go

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -126,36 +126,23 @@ func (lmtr *LinodeMachineTemplateReconciler) reconcile(ctx context.Context, lmtS
126126
}
127127

128128
machinesFoundForTemplate = true
129-
130-
// Define reconciliation tasks
131-
reconciliationTasks := []struct {
132-
shouldReconcile bool
133-
fieldType string
134-
failureReason string
135-
logMessage string
136-
}{
137-
{
138-
shouldReconcile: !slices.Equal(lmtScope.LinodeMachineTemplate.Spec.Template.Spec.Tags, lmtScope.LinodeMachineTemplate.Status.Tags),
139-
fieldType: "tags",
140-
failureReason: "FailedToPatchLinodeMachineWithTags",
141-
logMessage: "Failed to update tags on LinodeMachine",
142-
},
143-
{
144-
shouldReconcile: lmtScope.LinodeMachineTemplate.Spec.Template.Spec.FirewallID != lmtScope.LinodeMachineTemplate.Status.FirewallID,
145-
fieldType: "firewallID",
146-
failureReason: "FailedToPatchLinodeMachineWithFirewallID",
147-
logMessage: "Failed to update firewall ID on LinodeMachine",
148-
},
129+
if !slices.Equal(lmtScope.LinodeMachineTemplate.Spec.Template.Spec.Tags, lmtScope.LinodeMachineTemplate.Status.Tags) {
130+
err := lmtr.reconcileTags(ctx, lmtScope.LinodeMachineTemplate, &machine)
131+
if err != nil {
132+
lmtr.Logger.Error(err, "Failed to update tags on LinodeMachine", "template", lmtScope.LinodeMachineTemplate.Name, "machine", machine.Name)
133+
outErr = errors.Join(outErr, err)
134+
failureReason = "FailedToPatchLinodeMachine"
135+
return ctrl.Result{}, outErr
136+
}
149137
}
150138

151-
// Execute reconciliation tasks
152-
for _, task := range reconciliationTasks {
153-
if task.shouldReconcile {
154-
if err := lmtr.reconcileField(ctx, lmtScope.LinodeMachineTemplate, &machine, task.fieldType); err != nil {
155-
lmtr.Logger.Error(err, task.logMessage, "template", lmtScope.LinodeMachineTemplate.Name, "machine", machine.Name)
156-
outErr = errors.Join(outErr, err)
157-
failureReason = task.failureReason
158-
}
139+
if lmtScope.LinodeMachineTemplate.Spec.Template.Spec.FirewallID != lmtScope.LinodeMachineTemplate.Status.FirewallID {
140+
err := lmtr.reconcileFirewallID(ctx, lmtScope.LinodeMachineTemplate, &machine)
141+
if err != nil {
142+
lmtr.Logger.Error(err, "Failed to update FirewallID on LinodeMachine", "template", lmtScope.LinodeMachineTemplate.Name, "machine", machine.Name)
143+
outErr = errors.Join(outErr, err)
144+
failureReason = "FailedToPatchLinodeMachine"
145+
return ctrl.Result{}, outErr
159146
}
160147
}
161148
}
@@ -176,30 +163,30 @@ func (lmtr *LinodeMachineTemplateReconciler) reconcile(ctx context.Context, lmtS
176163
return ctrl.Result{}, outErr
177164
}
178165

179-
// reconcileField updates a specific field on a LinodeMachine based on the field type
180-
func (lmtr *LinodeMachineTemplateReconciler) reconcileField(ctx context.Context, lmt *infrav1alpha2.LinodeMachineTemplate, machine *infrav1alpha2.LinodeMachine, fieldType string) error {
166+
func (lmtr *LinodeMachineTemplateReconciler) reconcileTags(ctx context.Context, lmt *infrav1alpha2.LinodeMachineTemplate, machine *infrav1alpha2.LinodeMachine) error {
181167
helper, err := patch.NewHelper(machine, lmtr.Client)
182168
if err != nil {
183169
return fmt.Errorf("failed to init patch helper: %w", err)
184170
}
185171

186-
switch fieldType {
187-
case "tags":
188-
machine.Spec.Tags = lmt.Spec.Template.Spec.Tags
189-
if err := helper.Patch(ctx, machine); err != nil {
190-
return fmt.Errorf("failed to patch LinodeMachine %s with new %s: %w", machine.Name, fieldType, err)
191-
}
192-
lmtr.Logger.Info("Patched LinodeMachine with new tags", "machine", machine.Name, "tags", lmt.Spec.Template.Spec.Tags)
193-
case "firewallID":
194-
machine.Spec.FirewallID = lmt.Spec.Template.Spec.FirewallID
195-
if err := helper.Patch(ctx, machine); err != nil {
196-
return fmt.Errorf("failed to patch LinodeMachine %s with new %s: %w", machine.Name, fieldType, err)
197-
}
198-
lmtr.Logger.Info("Patched LinodeMachine with new firewall ID", "machine", machine.Name, "firewallID", lmt.Spec.Template.Spec.FirewallID)
199-
default:
200-
return fmt.Errorf("unsupported field type: %s", fieldType)
172+
machine.Spec.Tags = lmt.Spec.Template.Spec.Tags
173+
if err := helper.Patch(ctx, machine); err != nil {
174+
return fmt.Errorf("failed to patch LinodeMachine %s with new tags: %w", machine.Name, err)
201175
}
176+
lmtr.Logger.Info("Patched LinodeMachine with new tags", "machine", machine.Name, "tags", lmt.Spec.Template.Spec.Tags)
177+
178+
return nil
179+
}
202180

181+
func (lmtr *LinodeMachineTemplateReconciler) reconcileFirewallID(ctx context.Context, lmt *infrav1alpha2.LinodeMachineTemplate, machine *infrav1alpha2.LinodeMachine) error {
182+
helper, err := patch.NewHelper(machine, lmtr.Client)
183+
if err != nil {
184+
return fmt.Errorf("failed to init patch helper: %w", err)
185+
}
186+
machine.Spec.FirewallID = lmt.Spec.Template.Spec.FirewallID
187+
if err := helper.Patch(ctx, machine); err != nil {
188+
return fmt.Errorf("failed to patch LinodeMachine %s with new firewallID: %w", machine.Name, err)
189+
}
203190
return nil
204191
}
205192

0 commit comments

Comments
 (0)