@@ -55,6 +55,11 @@ type LifeCycleHandler struct {
5555 recorder eventrecord.EventRecorderLogger
5656}
5757
58+ type podVolumeErrorEvent struct {
59+ Reason string
60+ Message string
61+ }
62+
5863func (h * LifeCycleHandler ) Handle (ctx context.Context , s state.VirtualMachineState ) (reconcile.Result , error ) {
5964 if s .VirtualMachine ().IsEmpty () {
6065 return reconcile.Result {}, nil
@@ -111,19 +116,7 @@ func (h *LifeCycleHandler) Handle(ctx context.Context, s state.VirtualMachineSta
111116 if volumeErr := h .checkPodVolumeErrors (ctx , changed , log ); volumeErr != nil {
112117 cb .Status (metav1 .ConditionFalse ).
113118 Reason (vmcondition .ReasonPodVolumeErrors ).
114- Message (fmt .Sprintf ("Volume errors detected on Pod: %s" , volumeErr .Error ()))
115- conditions .SetCondition (cb , & changed .Status .Conditions )
116- return reconcile.Result {}, nil
117- }
118-
119- isVMInContainerCreating , err := h .isVMInContainerCreatingState (ctx , changed , log )
120- if err != nil {
121- return reconcile.Result {}, err
122- }
123- if isVMInContainerCreating {
124- cb .Status (metav1 .ConditionFalse ).
125- Reason (vmcondition .ReasonPodContainerCreating ).
126- Message ("Pod is in ContainerCreating phase. Check the pod for more details." )
119+ Message (fmt .Sprintf ("Error attaching block devices to virtual machine: %s: %s" , volumeErr .Reason , volumeErr .Message ))
127120 conditions .SetCondition (cb , & changed .Status .Conditions )
128121 return reconcile.Result {}, nil
129122 }
@@ -150,8 +143,8 @@ func (h *LifeCycleHandler) syncRunning(ctx context.Context, vm *v1alpha2.Virtual
150143
151144 if volumeError := h .checkPodVolumeErrors (ctx , vm , log ); volumeError != nil {
152145 cb .Status (metav1 .ConditionFalse ).
153- Reason (vmcondition .ReasonPodNotStarted ).
154- Message (volumeError . Error ( ))
146+ Reason (vmcondition .ReasonPodVolumeErrors ).
147+ Message (fmt . Sprintf ( "Error attaching block devices to virtual machine: %s: %s" , volumeError . Reason , volumeError . Message ))
155148 conditions .SetCondition (cb , & vm .Status .Conditions )
156149 return
157150 }
@@ -163,7 +156,7 @@ func (h *LifeCycleHandler) syncRunning(ctx context.Context, vm *v1alpha2.Virtual
163156 if podScheduled .Message != "" {
164157 cb .Status (metav1 .ConditionFalse ).
165158 Reason (vmcondition .ReasonPodNotStarted ).
166- Message (fmt .Sprintf ("%s: %s" , podScheduled .Reason , podScheduled .Message ))
159+ Message (fmt .Sprintf ("Could not schedule the virtual machine: %s: %s" , podScheduled .Reason , podScheduled .Message ))
167160 conditions .SetCondition (cb , & vm .Status .Conditions )
168161 }
169162
@@ -240,27 +233,7 @@ func (h *LifeCycleHandler) syncRunning(ctx context.Context, vm *v1alpha2.Virtual
240233 conditions .SetCondition (cb , & vm .Status .Conditions )
241234}
242235
243- func (h * LifeCycleHandler ) isVMInContainerCreatingState (ctx context.Context , vm * v1alpha2.VirtualMachine , log * slog.Logger ) (bool , error ) {
244- var podList corev1.PodList
245- err := h .client .List (ctx , & podList , & client.ListOptions {
246- Namespace : vm .Namespace ,
247- LabelSelector : labels .SelectorFromSet (map [string ]string {
248- virtv1 .VirtualMachineNameLabel : vm .Name ,
249- }),
250- })
251- if err != nil {
252- log .Error ("Failed to list pods" , "error" , err )
253- return false , err
254- }
255-
256- if len (podList .Items ) == 1 {
257- return isContainerCreating (& podList .Items [0 ]), nil
258- }
259-
260- return false , nil
261- }
262-
263- func (h * LifeCycleHandler ) checkPodVolumeErrors (ctx context.Context , vm * v1alpha2.VirtualMachine , log * slog.Logger ) error {
236+ func (h * LifeCycleHandler ) checkPodVolumeErrors (ctx context.Context , vm * v1alpha2.VirtualMachine , log * slog.Logger ) * podVolumeErrorEvent {
264237 var podList corev1.PodList
265238 err := h .client .List (ctx , & podList , & client.ListOptions {
266239 Namespace : vm .Namespace ,
@@ -297,7 +270,7 @@ func isContainerCreating(pod *corev1.Pod) bool {
297270 return false
298271}
299272
300- func (h * LifeCycleHandler ) getPodVolumeError (ctx context.Context , pod * corev1.Pod , log * slog.Logger ) error {
273+ func (h * LifeCycleHandler ) getPodVolumeError (ctx context.Context , pod * corev1.Pod , log * slog.Logger ) * podVolumeErrorEvent {
301274 if ! isContainerCreating (pod ) {
302275 return nil
303276 }
@@ -323,7 +296,10 @@ func (h *LifeCycleHandler) getPodVolumeError(ctx context.Context, pod *corev1.Po
323296 return a .LastTimestamp .Compare (b .LastTimestamp .Time )
324297 })
325298 if last .Reason == watcher .ReasonFailedAttachVolume || last .Reason == watcher .ReasonFailedMount {
326- return fmt .Errorf ("%s: %s" , last .Reason , last .Message )
299+ return & podVolumeErrorEvent {
300+ Reason : last .Reason ,
301+ Message : last .Message ,
302+ }
327303 }
328304
329305 return nil
0 commit comments