Skip to content

Commit

Permalink
clean up conditions usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Mendoza committed Apr 10, 2024
1 parent b455edf commit f542bc4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
10 changes: 5 additions & 5 deletions controller/linodemachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (r *LinodeMachineReconciler) reconcileCreate(
return ctrl.Result{}, err
}

if !reconciler.OneOfConditionsTrue(machineScope.LinodeMachine, ConditionPreflightConfigured) {
if !conditions.IsTrue(machineScope.LinodeMachine, ConditionPreflightConfigured) {

Check failure on line 325 in controller/linodemachine_controller.go

View workflow job for this annotation

GitHub Actions / go-analyse

`if !conditions.IsTrue(machineScope.LinodeMachine, ConditionPreflightConfigured)` has complex nested blocks (complexity: 9) (nestif)
if createOpts == nil {
createOpts, err = r.newCreateConfig(ctx, machineScope, tags, logger)
if err != nil {
Expand Down Expand Up @@ -353,7 +353,7 @@ func (r *LinodeMachineReconciler) reconcileCreate(
conditions.MarkTrue(machineScope.LinodeMachine, ConditionPreflightConfigured)
}

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

Expand All @@ -369,7 +369,7 @@ func (r *LinodeMachineReconciler) reconcileCreate(
conditions.MarkTrue(machineScope.LinodeMachine, ConditionPreflightBootTriggered)
}

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

Expand Down Expand Up @@ -446,7 +446,7 @@ func (r *LinodeMachineReconciler) createRootDisk(
linodeInstanceID int,
createOpts linodego.InstanceCreateOptions,
) (*linodego.InstanceDisk, error) {
if reconciler.OneOfConditionsTrue(machineScope.LinodeMachine, ConditionPreflightRootDiskCreated) {
if conditions.IsTrue(machineScope.LinodeMachine, ConditionPreflightRootDiskCreated) {
listFilter := util.Filter{
Label: "root",

Check warning on line 451 in controller/linodemachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controller/linodemachine_controller.go#L450-L451

Added lines #L450 - L451 were not covered by tests
}
Expand Down Expand Up @@ -510,7 +510,7 @@ func (r *LinodeMachineReconciler) createEtcdDisk(
machineScope *scope.MachineScope,
linodeInstanceID int,
) (*linodego.InstanceDisk, error) {
if reconciler.OneOfConditionsTrue(machineScope.LinodeMachine, ConditionPreflightEtcdDiskCreated) {
if conditions.IsTrue(machineScope.LinodeMachine, ConditionPreflightEtcdDiskCreated) {
listFilter := util.Filter{
Label: "etcd-data",

Check warning on line 515 in controller/linodemachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controller/linodemachine_controller.go#L514-L515

Added lines #L514 - L515 were not covered by tests
}
Expand Down
30 changes: 15 additions & 15 deletions controller/linodemachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ var _ = Describe("create", Label("machine", "create"), func() {
_, err := reconciler.reconcileCreate(ctx, logger, &mScope)
Expect(err).NotTo(HaveOccurred())

Expect(rutil.OneOfConditionsTrue(&linodeMachine, ConditionPreflightCreated)).To(BeTrue())
Expect(rutil.OneOfConditionsTrue(&linodeMachine, ConditionPreflightConfigured)).To(BeTrue())
Expect(rutil.OneOfConditionsTrue(&linodeMachine, ConditionPreflightBootTriggered)).To(BeTrue())
Expect(rutil.OneOfConditionsTrue(&linodeMachine, ConditionPreflightReady)).To(BeTrue())
Expect(conditions.IsTrue(&linodeMachine, ConditionPreflightCreated)).To(BeTrue())
Expect(conditions.IsTrue(&linodeMachine, ConditionPreflightConfigured)).To(BeTrue())
Expect(conditions.IsTrue(&linodeMachine, ConditionPreflightBootTriggered)).To(BeTrue())
Expect(conditions.IsTrue(&linodeMachine, ConditionPreflightReady)).To(BeTrue())

Expect(*linodeMachine.Status.InstanceState).To(Equal(linodego.InstanceOffline))
Expect(*linodeMachine.Spec.InstanceID).To(Equal(123))
Expand Down Expand Up @@ -225,7 +225,7 @@ var _ = Describe("create", Label("machine", "create"), func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("time is up"))

Expect(rutil.OneOfConditionsTrue(&linodeMachine, ConditionPreflightCreated)).To(BeFalse())
Expect(conditions.IsTrue(&linodeMachine, ConditionPreflightCreated)).To(BeFalse())
Expect(conditions.Get(&linodeMachine, ConditionPreflightCreated).Severity).To(Equal(clusterv1.ConditionSeverityError))
Expect(conditions.Get(&linodeMachine, ConditionPreflightCreated).Message).To(ContainSubstring("time is up"))
})
Expand Down Expand Up @@ -304,10 +304,10 @@ var _ = Describe("create", Label("machine", "create"), func() {
_, err := reconciler.reconcileCreate(ctx, logger, &mScope)
Expect(err).NotTo(HaveOccurred())

Expect(rutil.OneOfConditionsTrue(&linodeMachine, ConditionPreflightCreated)).To(BeTrue())
Expect(rutil.OneOfConditionsTrue(&linodeMachine, ConditionPreflightConfigured)).To(BeTrue())
Expect(rutil.OneOfConditionsTrue(&linodeMachine, ConditionPreflightBootTriggered)).To(BeTrue())
Expect(rutil.OneOfConditionsTrue(&linodeMachine, ConditionPreflightReady)).To(BeTrue())
Expect(conditions.IsTrue(&linodeMachine, ConditionPreflightCreated)).To(BeTrue())
Expect(conditions.IsTrue(&linodeMachine, ConditionPreflightConfigured)).To(BeTrue())
Expect(conditions.IsTrue(&linodeMachine, ConditionPreflightBootTriggered)).To(BeTrue())
Expect(conditions.IsTrue(&linodeMachine, ConditionPreflightReady)).To(BeTrue())

Expect(*linodeMachine.Status.InstanceState).To(Equal(linodego.InstanceOffline))
Expect(*linodeMachine.Spec.InstanceID).To(Equal(123))
Expand Down Expand Up @@ -374,8 +374,8 @@ var _ = Describe("create", Label("machine", "create"), func() {
Expect(res.RequeueAfter).To(Equal(rutil.DefaultMachineControllerWaitForRunningDelay))
Expect(err).ToNot(HaveOccurred())

Expect(rutil.OneOfConditionsTrue(&linodeMachine, ConditionPreflightCreated)).To(BeTrue())
Expect(rutil.OneOfConditionsTrue(&linodeMachine, ConditionPreflightConfigured)).To(BeFalse())
Expect(conditions.IsTrue(&linodeMachine, ConditionPreflightCreated)).To(BeTrue())
Expect(conditions.IsTrue(&linodeMachine, ConditionPreflightConfigured)).To(BeFalse())

listInst = mockLinodeClient.EXPECT().
ListInstances(ctx, gomock.Any()).
Expand Down Expand Up @@ -432,10 +432,10 @@ var _ = Describe("create", Label("machine", "create"), func() {
_, err = reconciler.reconcileCreate(ctx, logger, &mScope)
Expect(err).NotTo(HaveOccurred())

Expect(rutil.OneOfConditionsTrue(&linodeMachine, ConditionPreflightCreated)).To(BeTrue())
Expect(rutil.OneOfConditionsTrue(&linodeMachine, ConditionPreflightConfigured)).To(BeTrue())
Expect(rutil.OneOfConditionsTrue(&linodeMachine, ConditionPreflightBootTriggered)).To(BeTrue())
Expect(rutil.OneOfConditionsTrue(&linodeMachine, ConditionPreflightReady)).To(BeTrue())
Expect(conditions.IsTrue(&linodeMachine, ConditionPreflightCreated)).To(BeTrue())
Expect(conditions.IsTrue(&linodeMachine, ConditionPreflightConfigured)).To(BeTrue())
Expect(conditions.IsTrue(&linodeMachine, ConditionPreflightBootTriggered)).To(BeTrue())
Expect(conditions.IsTrue(&linodeMachine, ConditionPreflightReady)).To(BeTrue())

Expect(*linodeMachine.Status.InstanceState).To(Equal(linodego.InstanceOffline))
Expect(*linodeMachine.Spec.InstanceID).To(Equal(123))
Expand Down
10 changes: 0 additions & 10 deletions util/reconciler/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ import (
"sigs.k8s.io/cluster-api/util/conditions"
)

func OneOfConditionsTrue(from conditions.Getter, typs ...clusterv1.ConditionType) bool {
for _, typ := range typs {
if conditions.IsTrue(from, typ) {
return true
}
}

return false
}

func RecordDecayingCondition(to conditions.Setter, typ clusterv1.ConditionType, reason, message string, timeout time.Duration) bool {
conditions.MarkFalse(to, typ, reason, clusterv1.ConditionSeverityWarning, message)

Expand Down

0 comments on commit f542bc4

Please sign in to comment.