diff --git a/controllers/virtualmachine/virtualmachine/virtualmachine_controller.go b/controllers/virtualmachine/virtualmachine/virtualmachine_controller.go index f96e8be1c..75367ebea 100644 --- a/controllers/virtualmachine/virtualmachine/virtualmachine_controller.go +++ b/controllers/virtualmachine/virtualmachine/virtualmachine_controller.go @@ -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()) } } diff --git a/controllers/virtualmachine/virtualmachine/virtualmachine_controller_unit_test.go b/controllers/virtualmachine/virtualmachine/virtualmachine_controller_unit_test.go index cb6d2f236..892827018 100644 --- a/controllers/virtualmachine/virtualmachine/virtualmachine_controller_unit_test.go +++ b/controllers/virtualmachine/virtualmachine/virtualmachine_controller_unit_test.go @@ -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() {