Skip to content

Commit

Permalink
🐛 awsmachine: only register machine to LB when it's running
Browse files Browse the repository at this point in the history
The AWS docs [1] state that to register an instance with the Load
Balancer target groups, the instance must be running. Currently, CAPA
tries to register it while it's still pending, causing the following
error:

```
0314 22:24:18.512176  701419 awsmachine_controller.go:605] "failed to reconcile LB attachment" err=<
    [could not register machine to load balancer: could not register control plane instance "i-039bb22b1e8df7c99" with load balancer: failed to register instance with target group 'mrb-capa-67-d2pfx-int-22623': InvalidTarget: The following targets are not in a running state and cannot be registered: 'i-039bb22b1e8df7c99'
        status code: 400, request id: 17514354-77ac-42b1-a882-489760563bbd, could not register machine to load balancer: could not register control plane instance "i-039bb22b1e8df7c99" with load balancer: failed to register instance with target group 'mrb-capa-67-d2pfx-ext-6443': InvalidTarget: The following targets are not in a running state and cannot be registered: 'i-039bb22b1e8df7c99'
        status code: 400, request id: 84e1849c-abb7-4af9-9220-8791fdc1a3fb]
 >
I0314 22:24:18.512325  701419 recorder.go:104] "events: Failed to register control plane instance \"i-039bb22b1e8df7c99\" with load balancer: failed to register instance with target group 'mrb-capa-67-d2pfx-ext-6443': InvalidTarget: The following targets are not in a running state and cannot be registered: 'i-039bb22b1e8df7c99'\n\tstatus code: 400, request id: 84e1849c-abb7-4af9-9220-8791fdc1a3fb" type="Warning" object={"kind":"AWSMachine","namespace":"openshift-cluster-api-guests","name":"mrb-capa-67-d2pfx-bootstrap","uid":"58af1162-380b-4f3e-93fe-c0e81401070e","apiVersion":"infrastructure.cluster.x-k8s.io/v1beta2","resourceVersion":"562"} reason="FailedAttachControlPlaneELB"
```

Even though this doesn't stop the install from succeeding, let's wait
for the instance state to be "running" and with that avoid unnecessary
AWS API calls.

[1] https://docs.aws.amazon.com/elasticloadbalancing/latest/application/target-group-register-targets.html#register-instances
  • Loading branch information
r4f4 committed Jun 28, 2024
1 parent 1313226 commit d967ea4
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions controllers/awsmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,15 @@ func (r *AWSMachineReconciler) reconcileLBAttachment(machineScope *scope.Machine
continue
}

// See https://docs.aws.amazon.com/elasticloadbalancing/latest/application/target-group-register-targets.html#register-instances
if ptr.Deref(machineScope.GetInstanceState(), infrav1.InstanceStatePending) != infrav1.InstanceStateRunning {
const msg = "instance is not running"
r.Recorder.Eventf(machineScope.AWSMachine, corev1.EventTypeWarning, "FailedAttachControlPlaneELB",
"Cannot register control plane instance %q with load balancer: %s", i.ID, msg)
conditions.MarkFalse(machineScope.AWSMachine, infrav1.ELBAttachedCondition, infrav1.ELBAttachFailedReason, clusterv1.ConditionSeverityInfo, msg)
continue
}

if err := r.registerInstanceToLBs(machineScope, elbsvc, i, lbSpec); err != nil {
errs = append(errs, errors.Wrapf(err, "could not register machine to load balancer"))
}
Expand Down

0 comments on commit d967ea4

Please sign in to comment.