-
Notifications
You must be signed in to change notification settings - Fork 6
fix(vm): avoid restartAwaitingChanges flicker for hotplug block devices #2496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -542,6 +542,56 @@ var _ = Describe("SyncKvvmHandler", func() { | |
| ), | ||
| ) | ||
|
|
||
| It("does not expose apply-immediate block device changes in RestartAwaitingChanges while dependencies are not ready", func() { | ||
| ip := makeVMIP() | ||
| vmClass := makeVMClass() | ||
|
|
||
| rootRef := v1alpha2.BlockDeviceSpecRef{Kind: v1alpha2.DiskDevice, Name: "root"} | ||
| blankRef := v1alpha2.BlockDeviceSpecRef{Kind: v1alpha2.DiskDevice, Name: "blank-disk"} | ||
|
|
||
| vm := makeVM(v1alpha2.MachineRunning) | ||
| vm.Spec.EnableParavirtualization = ptr.To(true) | ||
| vm.Spec.BlockDeviceRefs = []v1alpha2.BlockDeviceSpecRef{rootRef, blankRef} | ||
| vm.Status.Conditions = append(vm.Status.Conditions, metav1.Condition{ | ||
| Type: vmcondition.TypeBlockDevicesReady.String(), | ||
| Status: metav1.ConditionFalse, | ||
| Reason: "BlockDevicesNotReady", | ||
| }) | ||
|
|
||
| kvvm := makeKVVM(vm) | ||
| Expect(kvbuilder.SetLastAppliedSpec(kvvm, &v1alpha2.VirtualMachine{ | ||
| Spec: v1alpha2.VirtualMachineSpec{ | ||
| CPU: v1alpha2.CPUSpec{ | ||
| Cores: vm.Spec.CPU.Cores, | ||
| }, | ||
| Memory: v1alpha2.MemorySpec{ | ||
| Size: vm.Spec.Memory.Size, | ||
| }, | ||
| VirtualMachineIPAddress: vm.Spec.VirtualMachineIPAddress, | ||
| RunPolicy: vm.Spec.RunPolicy, | ||
| OsType: vm.Spec.OsType, | ||
| VirtualMachineClassName: vm.Spec.VirtualMachineClassName, | ||
| EnableParavirtualization: ptr.To(true), | ||
| BlockDeviceRefs: []v1alpha2.BlockDeviceSpecRef{rootRef}, | ||
| TerminationGracePeriodSeconds: vm.Spec.TerminationGracePeriodSeconds, | ||
| Disruptions: &v1alpha2.Disruptions{ | ||
| RestartApprovalMode: vm.Spec.Disruptions.RestartApprovalMode, | ||
| }, | ||
| }, | ||
| })).To(Succeed()) | ||
|
|
||
| kvvmi := makeKVVMI() | ||
| fakeClient, reconcileObj, vmState = setupEnvironment(vm, kvvm, kvvmi, ip, vmClass) | ||
|
|
||
| reconcile() | ||
|
|
||
| updatedVM := &v1alpha2.VirtualMachine{} | ||
| Expect(fakeClient.Get(ctx, client.ObjectKeyFromObject(vm), updatedVM)).To(Succeed()) | ||
| Expect(updatedVM.Status.RestartAwaitingChanges).To(BeNil()) | ||
| awaitCond, awaitExists := conditions.GetCondition(vmcondition.TypeAwaitingRestartToApplyConfiguration, updatedVM.Status.Conditions) | ||
| Expect(awaitExists).To(BeFalse(), "ApplyImmediate block device changes should not set %s, got %+v", vmcondition.TypeAwaitingRestartToApplyConfiguration, awaitCond) | ||
| }) | ||
|
|
||
| It("keeps ConfigurationApplied False and requeues while SDN is not ready", func() { | ||
| ip := &v1alpha2.VirtualMachineIPAddress{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "test-ip", Namespace: namespace}, | ||
|
|
@@ -578,6 +628,46 @@ var _ = Describe("SyncKvvmHandler", func() { | |
| Expect(cond.Reason).To(Equal(vmcondition.ReasonConfigurationNotApplied.String())) | ||
| }) | ||
|
|
||
| It("upgrades only block device changes touching non-hotpluggable volumes", func() { | ||
| rootRef := v1alpha2.BlockDeviceSpecRef{Kind: v1alpha2.DiskDevice, Name: "root"} | ||
| dataRef := v1alpha2.BlockDeviceSpecRef{Kind: v1alpha2.DiskDevice, Name: "data"} | ||
| extraRef := v1alpha2.BlockDeviceSpecRef{Kind: v1alpha2.DiskDevice, Name: "extra"} | ||
|
|
||
| kvvmi := newEmptyKVVMI(name, namespace) | ||
| kvvmi.Spec.Volumes = []virtv1.Volume{ | ||
| { | ||
| Name: kvbuilder.GenerateDiskName(rootRef.Kind, rootRef.Name), | ||
| VolumeSource: virtv1.VolumeSource{ | ||
| PersistentVolumeClaim: &virtv1.PersistentVolumeClaimVolumeSource{Hotpluggable: false}, | ||
| }, | ||
| }, | ||
| { | ||
| Name: kvbuilder.GenerateDiskName(dataRef.Kind, dataRef.Name), | ||
| VolumeSource: virtv1.VolumeSource{ | ||
| PersistentVolumeClaim: &virtv1.PersistentVolumeClaimVolumeSource{Hotpluggable: true}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| nonHotpluggableRefs := nonHotpluggableVolumeRefs(kvvmi) | ||
| changes := vmchange.SpecChanges{} | ||
| changes.Add( | ||
| vmchange.FieldChange{Path: "blockDeviceRefs.0", Operation: vmchange.ChangeRemove, CurrentValue: dataRef, ActionRequired: vmchange.ActionApplyImmediate}, | ||
| vmchange.FieldChange{Path: "blockDeviceRefs.1", Operation: vmchange.ChangeAdd, DesiredValue: extraRef, ActionRequired: vmchange.ActionApplyImmediate}, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This DesiredValue (extraRef) is intentionally NOT in the non-hotpluggable set — it only exercises the negative path. There's no positive case where DesiredValue points to a non-hotpluggable volume and the change is expected to upgrade to ActionRestart. Consider adding a ChangeAdd/ChangeReplace change with DesiredValue: rootRef expecting ActionRestart, to cover the DesiredValue branch in blockDeviceChangeTouchesRefs. |
||
| vmchange.FieldChange{Path: "blockDeviceRefs.2", Operation: vmchange.ChangeReplace, CurrentValue: rootRef, DesiredValue: dataRef, ActionRequired: vmchange.ActionApplyImmediate}, | ||
| vmchange.FieldChange{Path: "cpu.cores", Operation: vmchange.ChangeReplace, ActionRequired: vmchange.ActionApplyImmediate}, | ||
| ) | ||
|
|
||
| changes.UpgradeBlockDeviceChangesToRestartIf(func(change vmchange.FieldChange) bool { | ||
| return blockDeviceChangeTouchesRefs(change, nonHotpluggableRefs) | ||
| }) | ||
|
|
||
| Expect(changes.GetAll()[0].ActionRequired).To(Equal(vmchange.ActionApplyImmediate)) | ||
| Expect(changes.GetAll()[1].ActionRequired).To(Equal(vmchange.ActionApplyImmediate)) | ||
| Expect(changes.GetAll()[2].ActionRequired).To(Equal(vmchange.ActionRestart)) | ||
| Expect(changes.GetAll()[3].ActionRequired).To(Equal(vmchange.ActionApplyImmediate)) | ||
| }) | ||
|
|
||
| DescribeTable("isPlacementPolicyChanged", | ||
| func(path string, expected bool) { | ||
| h := &SyncKvvmHandler{} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -230,8 +230,22 @@ func (s *SpecChanges) GetRestartMessages() []string { | |
| } | ||
|
|
||
| func (s *SpecChanges) ConvertPendingChanges() ([]apiextensionsv1.JSON, error) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this code unused? |
||
| return s.convertPendingChanges(func(FieldChange) bool { return true }) | ||
| } | ||
|
|
||
| func (s *SpecChanges) ConvertPendingRestartChanges() ([]apiextensionsv1.JSON, error) { | ||
| return s.convertPendingChanges(func(change FieldChange) bool { | ||
| return change.ActionRequired == ActionRestart | ||
| }) | ||
| } | ||
|
|
||
| func (s *SpecChanges) convertPendingChanges(include func(FieldChange) bool) ([]apiextensionsv1.JSON, error) { | ||
| res := make([]apiextensionsv1.JSON, 0, len(s.changes)) | ||
| for i := range s.changes { | ||
| if !include(s.changes[i]) { | ||
| continue | ||
| } | ||
|
|
||
| b, err := json.Marshal(s.changes[i]) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("change[%d]: %w", i, err) | ||
|
|
@@ -243,9 +257,13 @@ func (s *SpecChanges) ConvertPendingChanges() ([]apiextensionsv1.JSON, error) { | |
| } | ||
|
|
||
| func (s *SpecChanges) UpgradeBlockDeviceChangesToRestart() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's only used in tests. Can we remove it? |
||
| s.UpgradeBlockDeviceChangesToRestartIf(nil) | ||
| } | ||
|
|
||
| func (s *SpecChanges) UpgradeBlockDeviceChangesToRestartIf(shouldUpgrade func(FieldChange) bool) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be UpgradeBlockDeviceChangesToRestartWhere? "...If" doesn't look good |
||
| for i := range s.changes { | ||
| isBlockDeviceChange := s.changes[i].Path == blockDevicesPath || strings.HasPrefix(s.changes[i].Path, blockDevicesPath+".") | ||
| if isBlockDeviceChange && s.changes[i].ActionRequired == ActionApplyImmediate { | ||
| if isBlockDeviceChange && s.changes[i].ActionRequired == ActionApplyImmediate && (shouldUpgrade == nil || shouldUpgrade(s.changes[i])) { | ||
| s.changes[i].ActionRequired = ActionRestart | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why these lines are