Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Mikheykin <[email protected]>
  • Loading branch information
diafour committed Dec 10, 2024
1 parent 14fbad4 commit b2b00ba
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,13 @@ func (h *SyncKvvmHandler) syncPowerState(ctx context.Context, s state.VirtualMac
switch vmRunPolicy {
case virtv2.AlwaysOffPolicy:
if kvvmi != nil {
h.recorder.WithLogging(log).Eventf(
s.VirtualMachine().Current(),
corev1.EventTypeNormal,
virtv2.ReasonVMStop,
"Stop initiated by controller to ensure %s policy",
vmRunPolicy,
)
// Ensure KVVMI is absent.
err = h.client.Delete(ctx, kvvmi)
if err != nil && !k8serrors.IsNotFound(err) {
Expand All @@ -579,6 +586,14 @@ func (h *SyncKvvmHandler) syncPowerState(ctx context.Context, s state.VirtualMac
case virtv2.AlwaysOnUnlessStoppedManually:
strategy, _ := kvvm.RunStrategy()
if strategy == virtv1.RunStrategyAlways && kvvmi == nil {
h.recorder.WithLogging(log).Eventf(
s.VirtualMachine().Current(),
corev1.EventTypeNormal,
virtv2.ReasonVMStart,
"Start initiated by controller for %s policy",
vmRunPolicy,
)

if err = powerstate.StartVM(ctx, h.client, kvvm); err != nil {
return fmt.Errorf("automatic start VM for %s policy: %w", vmRunPolicy, err)
}
Expand All @@ -590,13 +605,23 @@ func (h *SyncKvvmHandler) syncPowerState(ctx context.Context, s state.VirtualMac
// Cleanup KVVMI is enough if VM was stopped from inside.
switch shutdownInfo.Reason {
case powerstate.GuestResetReason:
log.Info("Restart for guest initiated reset")
h.recorder.WithLogging(log).Event(
s.VirtualMachine().Current(),
corev1.EventTypeNormal,
virtv2.ReasonVMRestart,
"Restart initiated from inside the VM",
)
err = powerstate.SafeRestartVM(ctx, h.client, kvvm, kvvmi)
if err != nil {
return fmt.Errorf("restart VM on guest-reset: %w", err)
}
default:
log.Info("Cleanup Succeeded KVVMI")
h.recorder.WithLogging(log).Event(
s.VirtualMachine().Current(),
corev1.EventTypeNormal,
virtv2.ReasonVMStop,
"Stop initiated from inside the VM",
)
err = h.client.Delete(ctx, kvvmi)
if err != nil && !k8serrors.IsNotFound(err) {
return fmt.Errorf("delete Succeeded KVVMI: %w", err)
Expand All @@ -611,6 +636,7 @@ func (h *SyncKvvmHandler) syncPowerState(ctx context.Context, s state.VirtualMac
// Reason: "Restart",
// Message: fmt.Sprintf("Restart initiated by controller for %s runPolicy after observing failed VM instance", vmRunPolicy)),
//})

h.recorder.WithLogging(log).Eventf(
s.VirtualMachine().Current(),
corev1.EventTypeNormal,
Expand All @@ -632,16 +658,28 @@ func (h *SyncKvvmHandler) syncPowerState(ctx context.Context, s state.VirtualMac
// All types of shutdown are a final state.
if kvvmi != nil && kvvmi.DeletionTimestamp == nil {
if kvvmi.Status.Phase == virtv1.Succeeded && shutdownInfo.PodCompleted {
// Request to start new KVVMI (with updated settings).
switch shutdownInfo.Reason {
case powerstate.GuestResetReason:
h.recorder.WithLogging(log).Event(
s.VirtualMachine().Current(),
corev1.EventTypeNormal,
virtv2.ReasonVMRestart,
"Restart initiated from inside the VM",
)

// Request to start new KVVMI (with updated settings).
err = powerstate.SafeRestartVM(ctx, h.client, kvvm, kvvmi)
if err != nil {
return fmt.Errorf("restart VM on guest-reset: %w", err)
}
default:
h.recorder.WithLogging(log).Event(
s.VirtualMachine().Current(),
corev1.EventTypeNormal,
virtv2.ReasonVMStop,
"Stop initiated from inside the VM",
)
// Cleanup old version of KVVMI.
log.Info("Cleanup Succeeded KVVMI")
err = h.client.Delete(ctx, kvvmi)
if err != nil && !k8serrors.IsNotFound(err) {
return fmt.Errorf("delete Succeeded KVVMI: %w", err)
Expand Down
16 changes: 16 additions & 0 deletions images/virtualization-artifact/pkg/eventrecord/event.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2024 Flant JSC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package eventrecord

import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2024 Flant JSC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package eventrecord

import (
Expand Down

0 comments on commit b2b00ba

Please sign in to comment.