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 17, 2024
1 parent 6b286e7 commit 80f49b1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
17 changes: 14 additions & 3 deletions controller/linodemachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,19 @@ 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) {

Check failure on line 324 in controller/linodemachine_controller.go

View workflow job for this annotation

GitHub Actions / go-analyse

unnecessary leading newline (whitespace)

if !reconciler.ConditionTrue(machineScope.LinodeMachine, ConditionPreflightConfigured) {

Check failure on line 326 in controller/linodemachine_controller.go

View workflow job for this annotation

GitHub Actions / go-analyse

unnecessary leading newline (whitespace)
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 @@ -328,7 +339,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 @@ -344,7 +355,7 @@ func (r *LinodeMachineReconciler) reconcileCreate(
}

if !reconciler.ConditionTrue(machineScope.LinodeMachine, ConditionPreflightReady) {

Check failure on line 357 in controller/linodemachine_controller.go

View workflow job for this annotation

GitHub Actions / go-analyse

`if !reconciler.ConditionTrue(machineScope.LinodeMachine, ConditionPreflightReady)` has complex nested blocks (complexity: 6) (nestif)
if err = services.AddNodeToNB(ctx, logger, machineScope); err != nil {
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,
Expand Down
28 changes: 24 additions & 4 deletions controller/linodemachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,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)
waitForInstDisk := mockLinodeClient.EXPECT().
WaitForInstanceDiskStatus(ctx, 123, 100, linodego.DiskReady, defaultResizeWaitSeconds).
Expand Down Expand Up @@ -503,6 +507,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 @@ -519,6 +524,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 @@ -528,6 +534,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 @@ -541,10 +551,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 80f49b1

Please sign in to comment.