Skip to content

Commit

Permalink
reduce cognitive complexity, update tests and use ccm v0.4.3 for upda…
Browse files Browse the repository at this point in the history
…ted helm chart
  • Loading branch information
rahulait committed Apr 24, 2024
1 parent bb49160 commit aab43d7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
25 changes: 19 additions & 6 deletions controller/linodemachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const (
ConditionPreflightAdditionalDisksCreated clusterv1.ConditionType = "PreflightAdditionalDisksCreated"
ConditionPreflightConfigured clusterv1.ConditionType = "PreflightConfigured"
ConditionPreflightBootTriggered clusterv1.ConditionType = "PreflightBootTriggered"
ConditionPreflightNBConfigured clusterv1.ConditionType = "PreflightNBConfigured"
ConditionPreflightReady clusterv1.ConditionType = "PreflightReady"
)

Expand Down Expand Up @@ -248,7 +249,6 @@ func (r *LinodeMachineReconciler) reconcile(
return
}

//nolint:cyclop // keep top-level preflight condition checks in the same function for readability
func (r *LinodeMachineReconciler) reconcileCreate(
ctx context.Context,
logger logr.Logger,
Expand Down Expand Up @@ -312,8 +312,17 @@ func (r *LinodeMachineReconciler) reconcileCreate(
return ctrl.Result{}, err
}

return r.reconcileInstanceCreate(ctx, logger, machineScope, linodeInstance)
}

func (r *LinodeMachineReconciler) reconcileInstanceCreate(
ctx context.Context,
logger logr.Logger,
machineScope *scope.MachineScope,
linodeInstance *linodego.Instance,
) (ctrl.Result, error) {
if !reconciler.ConditionTrue(machineScope.LinodeMachine, ConditionPreflightConfigured) {
if err = r.configureDisks(ctx, logger, machineScope, linodeInstance.ID); err != nil {
if err := r.configureDisks(ctx, logger, machineScope, linodeInstance.ID); err != nil {
if reconciler.RecordDecayingCondition(machineScope.LinodeMachine,
ConditionPreflightConfigured, string(cerrs.CreateMachineError), err.Error(),
reconciler.DefaultMachineControllerPreflightTimeout(r.ReconcileTimeout)) {
Expand All @@ -327,7 +336,7 @@ func (r *LinodeMachineReconciler) reconcileCreate(
}

if !reconciler.ConditionTrue(machineScope.LinodeMachine, ConditionPreflightBootTriggered) {
if err = machineScope.LinodeClient.BootInstance(ctx, linodeInstance.ID, 0); err != nil {
if err := machineScope.LinodeClient.BootInstance(ctx, linodeInstance.ID, 0); err != nil {
logger.Error(err, "Failed to boot instance")

if reconciler.RecordDecayingCondition(machineScope.LinodeMachine,
Expand All @@ -342,19 +351,23 @@ func (r *LinodeMachineReconciler) reconcileCreate(
conditions.MarkTrue(machineScope.LinodeMachine, ConditionPreflightBootTriggered)
}

if !reconciler.ConditionTrue(machineScope.LinodeMachine, ConditionPreflightReady) {
if err = services.AddNodeToNB(ctx, logger, machineScope); err != nil {
if !reconciler.ConditionTrue(machineScope.LinodeMachine, ConditionPreflightNBConfigured) {
if err := services.AddNodeToNB(ctx, logger, machineScope); err != nil {
logger.Error(err, "Failed to add instance to Node Balancer backend")

if reconciler.RecordDecayingCondition(machineScope.LinodeMachine,
ConditionPreflightReady, string(cerrs.CreateMachineError), err.Error(),
ConditionPreflightNBConfigured, string(cerrs.CreateMachineError), err.Error(),
reconciler.DefaultMachineControllerPreflightTimeout(r.ReconcileTimeout)) {
return ctrl.Result{}, err

Check warning on line 361 in controller/linodemachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controller/linodemachine_controller.go#L358-L361

Added lines #L358 - L361 were not covered by tests
}

return ctrl.Result{RequeueAfter: reconciler.DefaultMachineControllerWaitForRunningDelay}, nil

Check warning on line 364 in controller/linodemachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controller/linodemachine_controller.go#L364

Added line #L364 was not covered by tests
}

conditions.MarkTrue(machineScope.LinodeMachine, ConditionPreflightNBConfigured)
}

if !reconciler.ConditionTrue(machineScope.LinodeMachine, ConditionPreflightReady) {
addrs, err := r.buildInstanceAddrs(ctx, machineScope, linodeInstance.ID)
if err != nil {
logger.Error(err, "Failed to get instance ip addresses")

Check warning on line 373 in controller/linodemachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controller/linodemachine_controller.go#L373

Added line #L373 was not covered by tests
Expand Down
24 changes: 20 additions & 4 deletions controller/linodemachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ var _ = Describe("create", Label("machine", "create"), func() {
Return(&linodego.InstanceIPAddressResponse{
IPv4: &linodego.InstanceIPv4Response{
Private: []*linodego.InstanceIP{{Address: "192.168.0.2"}},
Public: []*linodego.InstanceIP{{Address: "172.0.0.2"}},
},
}, nil)
createNB := mockLinodeClient.EXPECT().
Expand All @@ -509,6 +510,7 @@ var _ = Describe("create", Label("machine", "create"), func() {
Return(&linodego.InstanceIPAddressResponse{
IPv4: &linodego.InstanceIPv4Response{
Private: []*linodego.InstanceIP{{Address: "192.168.0.2"}},
Public: []*linodego.InstanceIP{{Address: "172.0.0.2"}},
},
}, nil)
mockLinodeClient.EXPECT().
Expand All @@ -518,6 +520,10 @@ var _ = Describe("create", Label("machine", "create"), func() {
Devices: &linodego.InstanceConfigDeviceMap{
SDA: &linodego.InstanceConfigDevice{DiskID: 100},
},
Interfaces: []linodego.InstanceConfigInterface{{
VPCID: ptr.To(1),
IPv4: &linodego.VPCIPv4{VPC: "10.0.0.2"},
}},
}}, nil)

_, err = reconciler.reconcileCreate(ctx, logger, &mScope)
Expand All @@ -531,10 +537,20 @@ var _ = Describe("create", Label("machine", "create"), func() {
Expect(*linodeMachine.Status.InstanceState).To(Equal(linodego.InstanceOffline))
Expect(*linodeMachine.Spec.InstanceID).To(Equal(123))
Expect(*linodeMachine.Spec.ProviderID).To(Equal("linode://123"))
Expect(linodeMachine.Status.Addresses).To(Equal([]clusterv1.MachineAddress{{
Type: clusterv1.MachineInternalIP,
Address: "192.168.0.2",
}}))
Expect(linodeMachine.Status.Addresses).To(Equal([]clusterv1.MachineAddress{
{
Type: clusterv1.MachineExternalIP,
Address: "172.0.0.2",
},
{
Type: clusterv1.MachineInternalIP,
Address: "10.0.0.2",
},
{
Type: clusterv1.MachineInternalIP,
Address: "192.168.0.2",
},
}))

Expect(testLogs.String()).To(ContainSubstring("creating machine"))
Expect(testLogs.String()).To(ContainSubstring("Linode instance already exists"))
Expand Down
2 changes: 1 addition & 1 deletion templates/addons/provider-linode/linode-ccm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ spec:
repoURL: https://linode.github.io/linode-cloud-controller-manager/
chartName: ccm-linode
namespace: kube-system
version: ${LINODE_CCM_VERSION:=v0.4.1}
version: ${LINODE_CCM_VERSION:=v0.4.3}
options:
waitForJobs: true
wait: true
Expand Down
2 changes: 1 addition & 1 deletion templates/flavors/k3s/secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ stringData:
name: ccm-linode
spec:
targetNamespace: kube-system
version: ${LINODE_CCM_VERSION:=v0.4.1}
version: ${LINODE_CCM_VERSION:=v0.4.3}
chart: ccm-linode
repo: https://linode.github.io/linode-cloud-controller-manager/
bootstrap: true
Expand Down
2 changes: 1 addition & 1 deletion templates/flavors/rke2/secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ stringData:
name: ccm-linode
spec:
targetNamespace: kube-system
version: ${LINODE_CCM_VERSION:=v0.4.1}
version: ${LINODE_CCM_VERSION:=v0.4.3}
chart: ccm-linode
repo: https://linode.github.io/linode-cloud-controller-manager/
bootstrap: true
Expand Down

0 comments on commit aab43d7

Please sign in to comment.