Skip to content

Conversation

@eofff
Copy link
Contributor

@eofff eofff commented Oct 27, 2025

Description

Validate dvcr section in ModuleConfig.

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: module
type: feature
summary: Added validation for the virtualization ModuleConfig that prevents decreasing the DVCR storage size and changing its StorageClass.

Signed-off-by: Valeriy Khorunzhin <[email protected]>
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Oct 27, 2025

Reviewer's Guide

This PR adds validation for the dvcr section within ModuleConfig by introducing a new dvcrValidator that is integrated into the existing webhook pipeline to enforce immutability of storageClassName and prevent size reduction when the associated PVC already exists.

Sequence diagram for dvcrValidator validation on ModuleConfig update

sequenceDiagram
    participant "ModuleConfig Webhook"
    participant "dvcrValidator"
    participant "Kubernetes API (PVC)"
    "ModuleConfig Webhook"->>"dvcrValidator": ValidateUpdate(ctx, oldMC, newMC)
    "dvcrValidator"->>"Kubernetes API (PVC)": checkPVCExists(ctx)
    "Kubernetes API (PVC)"-->>"dvcrValidator": PVC exists?
    alt PVC exists
        "dvcrValidator"->>"dvcrValidator": Compare storageClassName and size
        alt storageClassName changed
            "dvcrValidator"-->>"ModuleConfig Webhook": error: changing storageClassName forbidden
        else size reduced
            "dvcrValidator"-->>"ModuleConfig Webhook": error: reducing size forbidden
        else valid
            "dvcrValidator"-->>"ModuleConfig Webhook": success
        end
    else PVC does not exist
        "dvcrValidator"-->>"ModuleConfig Webhook": success
    end
Loading

Class diagram for dvcrValidator and related types

classDiagram
    class dvcrValidator {
        +client: Client
        +ValidateUpdate(ctx, oldMC, newMC): (Warnings, error)
        +checkPVCExists(ctx): (bool, error)
    }
    class dvcrSettings {
        +StorageType: string
        +StorageClassName: string
        +Size: string
    }
    class ModuleConfig {
        +Spec: Spec
    }
    class Spec {
        +Settings: SettingsValues
    }
    class SettingsValues
    dvcrValidator --> dvcrSettings
    dvcrValidator --> ModuleConfig
    ModuleConfig --> Spec
    Spec --> SettingsValues
    dvcrValidator ..> Client : uses
    dvcrValidator ..> PersistentVolumeClaim : checks existence
    dvcrSettings <.. parseDvcrSettings : returned by
Loading

File-Level Changes

Change Details Files
Integrated dvcrValidator into the ModuleConfig validation pipeline
  • Instantiated dvcrValidator alongside existing validators
  • Added dvcrValidator to WithUpdateValidators call
images/virtualization-artifact/pkg/controller/moduleconfig/moduleconfig_webhook.go
Implemented dvcrValidator with parsing and enforcement logic
  • Parsed dvcr settings from ModuleConfig.Spec.Settings
  • Checked for existing dvcr PVC in cluster
  • Rejected changes to storageClassName when PVC exists
  • Rejected reductions in PVC size when PVC exists
images/virtualization-artifact/pkg/controller/moduleconfig/dvcr_validator.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

@eofff eofff changed the title add validator feat(module): validate dvcr section in ModuleConfig Oct 27, 2025
@eofff eofff added this to the v1.2.0 milestone Oct 27, 2025
@eofff eofff added the e2e/run Run e2e test on cluster of PR author label Oct 27, 2025
@deckhouse-BOaTswain
Copy link
Contributor

deckhouse-BOaTswain commented Oct 27, 2025

Workflow has started.
Follow the progress here: Workflow Run

The target step completed with status: failure.

@deckhouse-BOaTswain deckhouse-BOaTswain removed the e2e/run Run e2e test on cluster of PR author label Oct 27, 2025
@eofff eofff marked this pull request as ready for review October 28, 2025 07:49
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:

  • dvcrValidator only runs on updates, so invalid dvcr settings in the initial ModuleConfig won’t be caught; consider adding a ValidateCreate implementation to enforce dvcr rules on creation as well.
  • parseDvcrSettings returns an error whenever the dvcr section or nested fields are absent, which may block configs that simply omit dvcr; skip validation instead of erroring when dvcr isn’t present.
  • The PVC name and namespace are hardcoded in dvcrValidator; consider making these configurable or reusing shared constants to improve flexibility and avoid future copy-paste updates.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- dvcrValidator only runs on updates, so invalid dvcr settings in the initial ModuleConfig won’t be caught; consider adding a ValidateCreate implementation to enforce dvcr rules on creation as well.
- parseDvcrSettings returns an error whenever the dvcr section or nested fields are absent, which may block configs that simply omit dvcr; skip validation instead of erroring when dvcr isn’t present.
- The PVC name and namespace are hardcoded in dvcrValidator; consider making these configurable or reusing shared constants to improve flexibility and avoid future copy-paste updates.

## Individual Comments

### Comment 1
<location> `images/virtualization-artifact/pkg/controller/moduleconfig/dvcr_validator.go:114-124` </location>
<code_context>
+	return nil, nil
+}
+
+func (v dvcrValidator) checkPVCExists(ctx context.Context) (bool, error) {
+	pvc, err := object.FetchObject(ctx, types.NamespacedName{
+		Name:      dvcrPVCName,
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Error handling for PVC fetch may block updates unnecessarily.

Consider implementing a retry or fallback for transient errors, and handle specific cases like NotFound separately to avoid blocking updates unnecessarily.

```suggestion
import (
	"time"
	"k8s.io/apimachinery/pkg/api/errors"
)

func (v dvcrValidator) checkPVCExists(ctx context.Context) (bool, error) {
	const maxRetries = 3
	const retryDelay = 200 * time.Millisecond

	var pvc interface{}
	var err error

	for attempt := 0; attempt < maxRetries; attempt++ {
		pvc, err = object.FetchObject(ctx, types.NamespacedName{
			Name:      dvcrPVCName,
			Namespace: dvcrNamespace,
		}, v.client, &corev1.PersistentVolumeClaim{})

		if err == nil {
			return pvc != nil, nil
		}

		if errors.IsNotFound(err) {
			return false, nil
		}

		// For transient errors, retry
		if errors.IsTimeout(err) || errors.IsServerTimeout(err) || errors.IsTooManyRequests(err) {
			time.Sleep(retryDelay)
			continue
		}

		// For other errors, return immediately
		return false, err
	}

	// If we exhausted retries, return last error
	return false, err
}
```
</issue_to_address>

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.

Signed-off-by: Valeriy Khorunzhin <[email protected]>
@eofff eofff requested a review from yaroslavborbat October 28, 2025 08:33
@eofff eofff merged commit 42f27b4 into main Oct 29, 2025
28 of 29 checks passed
@eofff eofff deleted the feat/core/validate-mc-dvcr-pvc-changes branch October 29, 2025 08:17
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