Skip to content

Conversation

hardcoretime
Copy link
Contributor

@hardcoretime hardcoretime commented Sep 30, 2025

Description

Why do we need it, and what problem does it solve?

This workaround is required due to a bug in the KVVM workflow.
When a KVVM is created with conflicting placement rules and cannot be scheduled, it remains unschedulable even if these rules are changed or removed.

What is the expected result?

A VirtualMachine can now be scheduled if it was created with conflicting placement rules that are changed after creation.

Checklist

  • The code is covered by unit tests.
  • e2e tests passed.
  • Documentation updated according to the changes.
  • Changes were tested in the Kubernetes cluster manually.

Copy link

sourcery-ai bot commented Sep 30, 2025

Reviewer's Guide

Introduces a workaround in the KVVM sync handler to detect VMs stuck in Unschedulable phase, refresh their spec when the NodeSelector is changed, and recreate the associated pod to allow new scheduling.

Sequence diagram for handling Unschedulable VirtualMachine with changed NodeSelector

sequenceDiagram
    participant H as SyncKvvmHandler
    participant VM as VirtualMachine
    participant KVVM as InternalKVVM
    participant Pod as Pod
    participant Equality as equality.Semantic
    H->>VM: Check VM status
    H->>KVVM: Check KVVM status
    alt VM is Unschedulable
        H->>H: isNodeSelectorChanged()
        H->>KVVM: Get current KVVM
        H->>KVVM: Create new KVVM from VM spec
        H->>Equality: Compare NodeSelector
        alt NodeSelector changed
            H->>KVVM: updateKVVM()
            H->>Pod: Delete Pod
        end
    end
Loading

Class diagram for updated SyncKvvmHandler methods

classDiagram
    class SyncKvvmHandler {
        +syncKVVM(ctx, s)
        +isVMUnschedulable(vm, kvvm) bool
        +isNodeSelectorChanged(ctx, s) (bool, error)
        +updateKVVM(ctx, s) error
    }
    class VirtualMachineState {
        +KVVM(ctx)
    }
    class VirtualMachine {
        Status: Phase
    }
    class InternalKVVM {
        Status: PrintableStatus
        Spec: TemplateSpec
    }
    class TemplateSpec {
        NodeSelector
    }
    SyncKvvmHandler --> VirtualMachineState
    SyncKvvmHandler --> VirtualMachine
    SyncKvvmHandler --> InternalKVVM
    InternalKVVM --> TemplateSpec
Loading

File-Level Changes

Change Details Files
Handle unschedulable VMs by updating and deleting their internal pods when NodeSelector changes
  • Added a new switch-case branch in syncKVVM for unschedulable state
  • Checked for NodeSelector modifications before proceeding
  • Invoked updateKVVM and deleted the old pod to force a reschedule
  • Returned early after handling this case
images/virtualization-artifact/pkg/controller/vm/internal/sync_kvvm.go
Introduce helper methods to detect unschedulable status and NodeSelector updates
  • Implemented isVMUnschedulable to check VM phase and PrintableStatus
  • Implemented isNodeSelectorChanged to compare current and new spec NodeSelectors using deep-equal
images/virtualization-artifact/pkg/controller/vm/internal/sync_kvvm.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@hardcoretime hardcoretime added this to the v1.1.0 milestone Sep 30, 2025
@hardcoretime hardcoretime modified the milestones: v1.1.0, v1.2.0 Sep 30, 2025
@hardcoretime hardcoretime force-pushed the fix/vm/recreate-unschedulable-intvirtvmi branch 2 times, most recently from e0b5355 to 526973b Compare September 30, 2025 21:25
@hardcoretime hardcoretime changed the title fix(vm): recreate unschedulable intvirtvmi fix(vm): recreate unschedulable intvirtvmi's pod Sep 30, 2025
@hardcoretime hardcoretime modified the milestones: v1.2.0, v1.1.0 Sep 30, 2025
@hardcoretime hardcoretime added the e2e/run Run e2e test on cluster of PR author label Sep 30, 2025
@deckhouse-BOaTswain
Copy link
Contributor

deckhouse-BOaTswain commented Sep 30, 2025

Workflow has started.
Follow the progress here: Workflow Run

The target step completed with status: success.

@hardcoretime hardcoretime marked this pull request as ready for review September 30, 2025 21:53
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@deckhouse-BOaTswain deckhouse-BOaTswain removed the e2e/run Run e2e test on cluster of PR author label Sep 30, 2025
return false, fmt.Errorf("failed to update internal virtual machine: %w", err)
}

err = object.DeleteObject(ctx, h.client, pod)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why pod? I think, kvvmi will be better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought so too, but when we delete a KVVMI, the KVVM becomes Stopped if the runPolicy is Manual or AlwaysOnUnlessStoppedManually, and the VM remains in the Pending state. This doesn't provide a good user experience, even if we stop the VM in this case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, we have patch on kubevirt, it will restart kvvm if kvvm has condition restartrequired and kvvmi in pending or scheduling phases. Can you check it, mb we should do nothing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This problem arises without the RestartRequired condition present.

Roman Sysoev added 3 commits October 2, 2025 16:29
This workaround is required due to a bug in the KVVM workflow.
When a KVVM is created with a non-existent nodeSelector and cannot be scheduled,
it remains unschedulable even if the nodeSelector is changed or removed.

Signed-off-by: Roman Sysoev <roman.sysoev@flant.com>
Signed-off-by: Roman Sysoev <roman.sysoev@flant.com>
Signed-off-by: Roman Sysoev <roman.sysoev@flant.com>
@hardcoretime hardcoretime force-pushed the fix/vm/recreate-unschedulable-intvirtvmi branch from 2d3b9a3 to 2648246 Compare October 2, 2025 13:29
@hardcoretime hardcoretime merged commit 69d4f1a into main Oct 2, 2025
27 of 28 checks passed
@hardcoretime hardcoretime deleted the fix/vm/recreate-unschedulable-intvirtvmi branch October 2, 2025 14:46
Isteb4k pushed a commit that referenced this pull request Oct 3, 2025
This workaround is required due to a bug in the KVVM workflow.
When a KVVM is created with conflicting placement rules and cannot be scheduled,
it remains unschedulable even if these rules are changed or removed.

Signed-off-by: Roman Sysoev <roman.sysoev@flant.com>
(cherry picked from commit 69d4f1a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants