@@ -183,12 +183,28 @@ func (h *SyncPowerStateHandler) syncPowerState(ctx context.Context, s state.Virt
183
183
return fmt .Errorf ("restart VM on guest-reset: %w" , err )
184
184
}
185
185
default :
186
- h .recordStopEventf (ctx , s .VirtualMachine ().Current (),
187
- "Stop initiated from inside the guest VM" ,
188
- )
189
- err = h .client .Delete (ctx , kvvmi )
190
- if err != nil && ! k8serrors .IsNotFound (err ) {
191
- return fmt .Errorf ("delete Succeeded KVVMI: %w" , err )
186
+ vmPod , err := s .Pod (ctx )
187
+ if err != nil {
188
+ return fmt .Errorf ("get virtual machine pod: %w" , err )
189
+ }
190
+
191
+ if vmPod != nil && ! vmPod .GetObjectMeta ().GetDeletionTimestamp ().IsZero () {
192
+ h .recordRestartEventf (ctx , s .VirtualMachine ().Current (),
193
+ "Restart initiated by controller for %s runPolicy after the deletion of pod VM." ,
194
+ runPolicy ,
195
+ )
196
+ err = powerstate .SafeRestartVM (ctx , h .client , kvvm , kvvmi )
197
+ if err != nil {
198
+ return fmt .Errorf ("automatic restart of failed VM: %w" , err )
199
+ }
200
+ } else {
201
+ h .recordStopEventf (ctx , s .VirtualMachine ().Current (),
202
+ "Stop initiated from inside the guest VM" ,
203
+ )
204
+ err = h .client .Delete (ctx , kvvmi )
205
+ if err != nil && ! k8serrors .IsNotFound (err ) {
206
+ return fmt .Errorf ("delete Succeeded KVVMI: %w" , err )
207
+ }
192
208
}
193
209
}
194
210
}
0 commit comments