Skip to content

Commit

Permalink
Refactor chanErr consumption in VM controller
Browse files Browse the repository at this point in the history
This patch changes the way the error channel from the async create
workflow is consumed in the VirtualMachine controller.

Previously only a single error was assumed to be returned, but since
this is a channel, the consumer should range over it, accounting for
the possible vim and k8s error.
  • Loading branch information
Bryan Venteicher authored and akutz committed Dec 19, 2024
1 parent eecd7f1 commit 44f994f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,17 @@ func (r *Reconciler) ReconcileNormal(ctx *pkgctx.VirtualMachineContext) (reterr
} else {
// Emit event once goroutine is complete.
go func(obj client.Object) {
err := <-chanErr
r.Recorder.EmitEvent(obj, "Create", err, false)
failed := false
for err := range chanErr {
if err != nil {
failed = true
r.Recorder.EmitEvent(obj, "Create", err, false)
}
}
if !failed {
// If no error the channel is just closed.
r.Recorder.EmitEvent(obj, "Create", nil, false)
}
}(ctx.VM.DeepCopy())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,22 @@ func unitTestsReconcile() {
Expect(reconciler.ReconcileNormal(vmCtx)).ShouldNot(Succeed())
expectEvents(ctx, "CreateFailure")
})

It("Should emit CreateFailure events if ReconcileNormal causes a failed VM create that reports multiple errors", func() {
fakeVMProvider.CreateOrUpdateVirtualMachineAsyncFn = func(
ctx context.Context,
vm *vmopv1.VirtualMachine) (<-chan error, error) {

ctxop.MarkCreate(ctx)
chanErr := make(chan error, 2)
chanErr <- errors.New("error1")
chanErr <- errors.New("error2")
return chanErr, nil
}

Expect(reconciler.ReconcileNormal(vmCtx)).To(Succeed())
expectEvents(ctx, "CreateFailure", "CreateFailure")
})
})

It("Should emit UpdateSuccess event if ReconcileNormal causes a successful VM update", func() {
Expand Down

0 comments on commit 44f994f

Please sign in to comment.