Skip to content

Commit 19f37a6

Browse files
committed
fix(core): run firmware upgrade migration only for running vmi
Signed-off-by: Daniil Antoshin <daniil.antoshin@flant.com>
1 parent 3870ea9 commit 19f37a6

3 files changed

Lines changed: 37 additions & 6 deletions

File tree

images/virtualization-artifact/pkg/controller/vmop/migration/internal/handler/lifecycle_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,5 @@ var _ = Describe("LifecycleHandler", func() {
246246
false, // targetMigrationEnabled
247247
),
248248
)
249+
249250
})

images/virtualization-artifact/pkg/controller/workload-updater/internal/handler/firmware.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ import (
2424

2525
appsv1 "k8s.io/api/apps/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
virtv1 "kubevirt.io/api/core/v1"
2728
"sigs.k8s.io/controller-runtime/pkg/client"
2829
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2930

31+
"github.com/deckhouse/virtualization-controller/pkg/common/object"
32+
3033
"github.com/deckhouse/virtualization-controller/pkg/common/annotations"
3134
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
3235
"github.com/deckhouse/virtualization-controller/pkg/logger"
@@ -66,6 +69,14 @@ func (h *FirmwareHandler) Handle(ctx context.Context, vm *v1alpha2.VirtualMachin
6669
log := logger.FromContext(ctx).With(logger.SlogHandler(firmwareHandler))
6770
ctx = logger.ToContext(ctx, log)
6871

72+
kvvmi := &virtv1.VirtualMachineInstance{}
73+
if err := h.client.Get(ctx, object.NamespacedName(vm), kvvmi); err != nil {
74+
return reconcile.Result{}, client.IgnoreNotFound(err)
75+
}
76+
if kvvmi.Status.Phase != virtv1.Running {
77+
return reconcile.Result{}, nil
78+
}
79+
6980
if ready, err := h.isVirtControllerUpToDate(ctx); err != nil {
7081
return reconcile.Result{}, err
7182
} else if !ready {

images/virtualization-artifact/pkg/controller/workload-updater/internal/handler/firmware_test.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
appsv1 "k8s.io/api/apps/v1"
2626
corev1 "k8s.io/api/core/v1"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
virtv1 "kubevirt.io/api/core/v1"
2829
"sigs.k8s.io/controller-runtime/pkg/client"
2930

3031
vmbuilder "github.com/deckhouse/virtualization-controller/pkg/builder/vm"
@@ -67,6 +68,22 @@ var _ = Describe("TestFirmwareHandler", func() {
6768
return vm
6869
}
6970

71+
newKVVMI := func(phase virtv1.VirtualMachineInstancePhase) *virtv1.VirtualMachineInstance {
72+
return &virtv1.VirtualMachineInstance{
73+
TypeMeta: metav1.TypeMeta{
74+
APIVersion: virtv1.GroupVersion.String(),
75+
Kind: "VirtualMachineInstance",
76+
},
77+
ObjectMeta: metav1.ObjectMeta{
78+
Name: name,
79+
Namespace: namespace,
80+
},
81+
Status: virtv1.VirtualMachineInstanceStatus{
82+
Phase: phase,
83+
},
84+
}
85+
}
86+
7087
newVirtController := func(ready bool, launcherImage string) *appsv1.Deployment {
7188
deploy := &appsv1.Deployment{
7289
TypeMeta: metav1.TypeMeta{
@@ -99,8 +116,8 @@ var _ = Describe("TestFirmwareHandler", func() {
99116
}
100117

101118
DescribeTable("FirmwareHandler should return serviceCompleteErr if migration executed",
102-
func(vm *v1alpha2.VirtualMachine, deploy *appsv1.Deployment, needMigrate bool) {
103-
fakeClient = setupEnvironment(vm, deploy)
119+
func(vm *v1alpha2.VirtualMachine, kvvmi *virtv1.VirtualMachineInstance, deploy *appsv1.Deployment, needMigrate bool) {
120+
fakeClient = setupEnvironment(vm, kvvmi, deploy)
104121

105122
mockMigration := &OneShotMigrationMock{
106123
OnceMigrateFunc: func(ctx context.Context, vm *v1alpha2.VirtualMachine, annotationKey, annotationExpectedValue string) (bool, error) {
@@ -118,9 +135,11 @@ var _ = Describe("TestFirmwareHandler", func() {
118135
Expect(err).NotTo(HaveOccurred())
119136
}
120137
},
121-
Entry("Migration should be executed", newVM(true), newVirtController(true, firmwareImage), true),
122-
Entry("Migration not should be executed", newVM(false), newVirtController(true, firmwareImage), false),
123-
Entry("Migration not should be executed because virt-controller not ready", newVM(false), newVirtController(false, firmwareImage), false),
124-
Entry("Migration not should be executed because virt-controller ready but has wrong image", newVM(false), newVirtController(true, "wrong-image"), false),
138+
Entry("Migration should be executed", newVM(true), newKVVMI(virtv1.Running), newVirtController(true, firmwareImage), true),
139+
Entry("Migration not should be executed", newVM(false), newKVVMI(virtv1.Running), newVirtController(true, firmwareImage), false),
140+
Entry("Migration not should be executed because kvvmi is stopped", newVM(true), newKVVMI(virtv1.Succeeded), newVirtController(true, firmwareImage), false),
141+
Entry("Migration not should be executed because kvvmi is pending", newVM(true), newKVVMI(virtv1.Pending), newVirtController(true, firmwareImage), false),
142+
Entry("Migration not should be executed because virt-controller not ready", newVM(false), newKVVMI(virtv1.Running), newVirtController(false, firmwareImage), false),
143+
Entry("Migration not should be executed because virt-controller ready but has wrong image", newVM(false), newKVVMI(virtv1.Running), newVirtController(true, "wrong-image"), false),
125144
)
126145
})

0 commit comments

Comments
 (0)