Skip to content

Conversation

LopatinDmitr
Copy link
Contributor

@LopatinDmitr LopatinDmitr commented Oct 7, 2025

Description

This update enhances the validation logic for Virtual Machines' network configurations by prohibiting duplicate network names within the specification.

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.

Changelog entries

section: vm
type: fix
summary: prohibit duplicating networks in the VirtualMachine `.spec`

@LopatinDmitr LopatinDmitr added this to the v1.2.0 milestone Oct 7, 2025
@LopatinDmitr LopatinDmitr self-assigned this Oct 7, 2025
Copy link
Contributor

sourcery-ai bot commented Oct 7, 2025

Reviewer's Guide

This PR refactors the VirtualMachine network validation by splitting the generic Validate function into dedicated ValidateCreate and ValidateUpdate methods, and adds logic to prohibit duplicate network names in both creation and update scenarios, with accompanying unit test enhancements.

Class diagram for updated NetworksValidator methods and logic

classDiagram
    class NetworksValidator {
        +ValidateCreate(ctx, vm)
        +ValidateUpdate(ctx, oldVM, newVM)
        -featureGate
    }
    class VirtualMachine {
        Spec: VirtualMachineSpec
    }
    class VirtualMachineSpec {
        Networks: []Network
    }
    class Network {
        Type
        Name
    }
    NetworksValidator --> VirtualMachine
    VirtualMachine --> VirtualMachineSpec
    VirtualMachineSpec --> Network
Loading

Flow diagram for duplicate network name validation in VirtualMachine spec

flowchart TD
    A["Start validation"] --> B["Iterate over Networks in spec"]
    B --> C{Is network name empty?}
    C -- Yes --> D["Error: network must have non-empty name"]
    C -- No --> E{Is network name already in namesSet?}
    E -- Yes --> F["Error: network name is duplicated"]
    E -- No --> G["Add name to namesSet"]
    G --> H["Continue iteration"]
    H --> B
    F --> I["End validation"]
    D --> I
    B --> J["All networks validated"]
    J --> K["Validation successful"]
Loading

File-Level Changes

Change Details Files
Refactor NetworksValidator to separate create and update validation and enforce duplicate name checks
  • Removed the old Validate method and replaced it with ValidateCreate and ValidateUpdate
  • In ValidateCreate, introduced a namesSet map to detect and reject duplicate network names
  • In ValidateUpdate, added deep equality check to skip duplicate name logic when spec is unchanged
  • Added SDN feature-gate enforcement, first-network type/name rules, and duplicate name checks in the update path
  • Imported k8s.io/apimachinery/pkg/api/equality for spec comparison
pkg/controller/vm/internal/validators/networks_validator.go
Extend and rename unit tests to cover new duplicate-network scenarios in create and update
  • Renamed TestNetworksValidate to TestNetworksValidateCreate and added duplicate-name cases
  • Created TestNetworksValidateUpdate with multiple valid/invalid scenarios for update logic
  • Updated test calls from Validate to ValidateCreate/ValidateUpdate and improved error assertions
  • Adjusted t.Run identifiers and enhanced error messages for clarity
pkg/controller/vm/internal/validators/networks_validator_test.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

Copy link
Contributor

@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 - here's some feedback:

  • Consider extracting the common network validation steps (main-type placement, name presence, duplicate checks) into a shared helper to avoid duplicating logic in ValidateCreate and ValidateUpdate.
  • ValidateCreate currently skips checking the SDN feature gate while ValidateUpdate blocks configurations when SDN is disabled—add a feature-gate check to the create path for consistency.
  • The isUnchanged branch in ValidateUpdate bypasses duplicate name checks when specs match, which may let invalid existing configurations persist—consider always validating duplicates on the new spec.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider extracting the common network validation steps (main-type placement, name presence, duplicate checks) into a shared helper to avoid duplicating logic in ValidateCreate and ValidateUpdate.
- ValidateCreate currently skips checking the SDN feature gate while ValidateUpdate blocks configurations when SDN is disabled—add a feature-gate check to the create path for consistency.
- The isUnchanged branch in ValidateUpdate bypasses duplicate name checks when specs match, which may let invalid existing configurations persist—consider always validating duplicates on the new spec.

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.

@LopatinDmitr LopatinDmitr force-pushed the fix/vm/fix-validation-networks-in-spec branch 2 times, most recently from 865ae20 to 355af94 Compare October 7, 2025 13:48
@LopatinDmitr LopatinDmitr added the e2e/run Run e2e test on cluster of PR author label Oct 7, 2025
@deckhouse-BOaTswain
Copy link
Contributor

deckhouse-BOaTswain commented Oct 7, 2025

Workflow has started.
Follow the progress here: Workflow Run

The target step completed with status: success.

@deckhouse-BOaTswain deckhouse-BOaTswain removed the e2e/run Run e2e test on cluster of PR author label Oct 7, 2025
@LopatinDmitr LopatinDmitr force-pushed the fix/vm/fix-validation-networks-in-spec branch from 88d4d39 to 589c253 Compare October 7, 2025 16:04
@LopatinDmitr LopatinDmitr force-pushed the fix/vm/fix-validation-networks-in-spec branch from 589c253 to 3b193ce Compare October 7, 2025 16:27
@eofff eofff self-requested a review October 8, 2025 08:03
@LopatinDmitr LopatinDmitr merged commit 480f3e2 into main Oct 8, 2025
27 of 28 checks passed
@LopatinDmitr LopatinDmitr deleted the fix/vm/fix-validation-networks-in-spec branch October 8, 2025 08:32
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