← Back to Pull requests

fix(controller): wait for each required CRD, not only its group #169

Merged
0xFelix merged 2 files (102 lines) on Nov 30, 2026 20:07 UTC
View on GitHub
0xFelix 0xFelix commented on Nov 27, 2026 14:06 UTC
Author

What this PR does / why we need it:

The availability controller started the VMTR controller as soon as the cdi.kubevirt.io and snapshot.kubevirt.io groups were served. CRDs of a group are registered one by one, so one could still be missing. Check each resource (datavolumes, virtualmachinesnapshots, virtualmachinesnapshotcontents) instead, also for the cache config.

While waiting, it also skips the status patch of requests whose status did not change.

It also disables the client cache for VirtualMachineSnapshotContents unconditionally. Before, reading them failed if their CRD was installed after startup.

Which issue(s) this PR fixes:

Special notes for your reviewer:

Release note:
``release-note
Fix requests failing if the snapshot CRDs are installed after virt-template started
``

Generated with Claude Code

Diff: 102 lines (+79 / -23) across 2 files Reviewer: jcanocan Review duration: 4680m
All Vouch review confidence checks passed
Residual risk 0.03 = Change Risk (0.37) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.37 · 102 lines diff across 2 files
0.37
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.89 · 5 substantive comment(s)
0.89
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
90%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.277
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
15 items
coderabbitai[bot] coderabbitai[bot] commented on Nov 27, 2026 14:07 UTC
Bot

<!-- coderabbit-ai-generated:v1 -->

<!-- This is an auto-generated comment: summarize by coderabbit.ai -->
<!-- review_stack_entry_start -->

<a href="https://app.coderabbit.ai/change-stack/kubevirt/virt-template/pull/169?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg" alt="Explain Change →" height="32"></picture></a>

<!-- review_stack_entry_end -->
<!-- recent_review_status_start -->

No actionable comments were generated in the recent review.

<!-- recent_review_status_end -->
<!-- walkthrough_start -->

<details>
<summary><strong>Walkthrough</strong></summary>
<dl>
<dd>
<br>

The controller now checks availability for each required resource during startup and polling. It reports missing resources and waits until all required resources are served. Cache entries are configured for served DataVolume and VirtualMachineSnapshot resources, while VirtualMachineSnapshotContent is always excluded from cached reads. The controller also skips status patches when status is unchanged. Tests cover waiting for a missing resource and a NotFound response when reading an unavailable VirtualMachineSnapshotContent.

<!-- change_assessment_start -->
Estimated code review effort: 3 (Moderate) | ~20 minutes

<!-- change_assessment_commit:"d8110e7a4b84a2fb2c2031f70098397ac7fa4d14" -->
<!-- change_assessment_priority:{"version":1,"urgency":"p2","manual":false} -->

<!-- change_assessment_end -->

</dd>
</dl>
</details>

<!-- walkthrough_end -->
<details>
<summary><strong>Merge Risk:</strong> <strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/risk-unassessed-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minimal</strong> · up to <code>d8110</code></summary>
<dl>
<dd>
<br>

<!-- final_review_risk_start -->
<!-- final_review_risk_label
Merge Risk: ⚪ Minimal · up to d8110
-->
<!-- final_review_risk_coverage:{"sourceCommitId":"d8110e7a4b84a2fb2c2031f70098397ac7fa4d14","coveredCommitId":"d8110e7a4b84a2fb2c2031f70098397ac7fa4d14","kind":"reviewed"} -->

The change makes the controller wait for each required CRD individually and avoids cache failures when snapshot CRDs are installed after startup. No concrete merge-blocking risk was identified.
<!-- final_review_risk_end -->

</dd>
</dl>
</details>
<!-- autopilot:start -->

  • <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autofix</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

  • <!-- autopilot:end -->
    <!-- operational_warnings_start -->

    <!-- This is an auto-generated comment: resource warnings by coderabbit.ai -->

    [!WARNING]

    Linked repositories: Public OSS repositories can only analyze public repositories installed in this organization. Analyzed kubevirt/kubevirt, kubevirt/enhancements, skipped kubevirt/virt-template-api, kubevirt/virt-template-client-go, kubevirt/virt-template-engine, kubevirt/containerized-data-importer.

    <!-- end of auto-generated comment: resource warnings by coderabbit.ai -->

    <!-- operational_warnings_end -->
    <!-- tips_start -->

    Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

    <details>
    <summary><strong>Share</strong></summary>
    <dl>
    <dd>
    <br>

  • X

  • Mastodon

  • Reddit

  • LinkedIn
  • </dd>
    </dl>
    </details>

    <!-- tips_end -->
    <!-- recent_review_start -->

    <h6>
    <details>
    <summary><strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="12" height="12" align="absmiddle" alt=""></picture> Recent review info</strong></summary>
    <dl>
    <dd>
    <br>

    <!-- review_info_content -->
    <details>
    <summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog.svg" width="16" height="16" align="absmiddle" alt=""></picture> Run configuration</strong></summary>
    <dl>
    <dd>
    <br>

  • Source mode: Auto

  • Repository: [kubevirt/virt-template](<https://github.com/kubevirt/virt-template>)

  • Branch: [main](<https://github.com/kubevirt/virt-template/tree/main>)

  • File: [.coderabbit.yaml](<https://github.com/kubevirt/virt-template/blob/8f6cf73cd18dbe61405a3b976c10aacd09ec9f4d/.coderabbit.yaml>)

  • Commit: [8f6cf73](<https://github.com/kubevirt/virt-template/commit/8f6cf73cd18dbe61405a3b976c10aacd09ec9f4d>)

  • Layers: Repository YAML

  • Review profile: CHILL

  • Plan: Advanced

  • Run ID: 9418f1d4-d563-4ded-b46d-6ba086950989
  • </dd>
    </dl>
    </details>

    <details>
    <summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal.svg" width="16" height="16" align="absmiddle" alt=""></picture> Commits</strong></summary>
    <dl>
    <dd>
    <br>

    Reviewing files that changed from the base of the PR and between 8f6cf73cd18dbe61405a3b976c10aacd09ec9f4d and d8110e7a4b84a2fb2c2031f70098397ac7fa4d14.

    </dd>
    </dl>
    </details>

    <details>
    <summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files selected for processing: 2</strong></summary>
    <dl>
    <dd>
    <br>

    internal/controller/vmtr_availability_controller.go
    internal/controller/vmtr_availability_controller_test.go

    </dd>
    </dl>
    </details>

    <details>
    <summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/linked-repositories-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/linked-repositories.svg" width="16" height="16" align="absmiddle" alt=""></picture> Linked repositories identified</strong></summary>
    <dl>
    <dd>
    <br>

    CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • kubevirt/kubevirt _(manual)_

  • kubevirt/enhancements _(manual)_
  • </dd>
    </dl>
    </details>

    Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

    </dd>
    </dl>
    </details>
    </h6>

    <!-- recent_review_end -->

    0xFelix 0xFelix commented on Nov 27, 2026 14:10 UTC
    Author

    /cherry-pick release-0.3

    kubevirt-bot kubevirt-bot commented on Nov 27, 2026 14:11 UTC
    Bot

    @0xFelix: once the present PR merges, I will cherry-pick it on top of release-0.3 in a new PR and assign it to you.

    <details>

    In response to this:

    >/cherry-pick release-0.3

    Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.
    </details>

    jcanocan jcanocan commented on code on Nov 30, 2026 14:24 UTC
    Code Review
    internal/controller/vmtr_availability_controller.go:1
    @@ -140,33 +150,42 @@ func (c *VMTRAvailabilityController) setMissingCRDStatus(ctx context.Context, mi
     	}
     }
     
    -func (c *VMTRAvailabilityController) missingGroups() []string {
    +func (c *VMTRAvailabilityController) missingResources() []string {
     	var missing []string
    -	for _, group := range requiredGroups {
    -		if !isAPIGroupAvailable(c.DiscoveryClient, group) {
    -			missing = append(missing, group)
    +	for _, resource := range requiredResources {
    +		if !isResourceServed(c.DiscoveryClient, resource) {
    +			missing = append(missing, resource.GroupResource().String())
     		}
     	}
     	return missing
     }
     
    -func isAPIGroupAvailable(dc discovery.DiscoveryInterface, groupVersion string) bool {
    -	_, err := dc.ServerResourcesForGroupVersion(groupVersion)
    -	return err == nil
    +func isResourceServed(dc discovery.DiscoveryInterface, resource schema.GroupVersionResource) bool {
    +	list, err := dc.ServerResourcesForGroupVersion(resource.GroupVersion().String())
    +	if err != nil {
    +		return false
    +	}
    +	return slices.ContainsFunc(list.APIResources, func(r metav1.APIResource) bool {
    +		return r.Name == resource.Resource
    +	})
     }
     
    +// ExternalCRDCacheConfig only adds entries for served resources, since a cache.ByObject for a
    +// missing CRD fails manager construction.
     func ExternalCRDCacheConfig(dc discovery.DiscoveryInterface) (map[client.Object]cache.ByObject, []client.Object) {
     	uidReq, _ := labels.NewRequirement(v1beta1.LabelRequestUID, selection.Exists, nil)
     	uidSelector := labels.NewSelector().Add(*uidReq)
     
     	cacheByObject := map[client.Object]cache.ByObject{}
     	var clientDisableFor []client.Object
     
    -	if isAPIGroupAvailable(dc, cdiGroupVersion) {
    +	if isResourceServed(dc, dataVolumesResource) {
     		cacheByObject[&cdiv1beta1.DataVolume{}] = cache.ByObject{Label: uidSelector}
     	}
    -	if isAPIGroupAvailable(dc, snapshotGroupVersion) {
    +	if isResourceServed(dc, snapshotsResource) {
     		cacheByObject[&snapshotv1beta1.VirtualMachineSnapshot{}] = cache.ByObject{Label: uidSelector}
    +	}
    +	if isResourceServed(dc, snapshotContentsResource) {

    I think what you meat here is !isResourceServed. Otherwise the client is disabled when the resource is served

    0xFelix 0xFelix commented on code on Nov 30, 2026 15:34 UTC
    Code Review
    internal/controller/vmtr_availability_controller.go:1
    @@ -140,33 +150,42 @@ func (c *VMTRAvailabilityController) setMissingCRDStatus(ctx context.Context, mi
     	}
     }
     
    -func (c *VMTRAvailabilityController) missingGroups() []string {
    +func (c *VMTRAvailabilityController) missingResources() []string {
     	var missing []string
    -	for _, group := range requiredGroups {
    -		if !isAPIGroupAvailable(c.DiscoveryClient, group) {
    -			missing = append(missing, group)
    +	for _, resource := range requiredResources {
    +		if !isResourceServed(c.DiscoveryClient, resource) {
    +			missing = append(missing, resource.GroupResource().String())
     		}
     	}
     	return missing
     }
     
    -func isAPIGroupAvailable(dc discovery.DiscoveryInterface, groupVersion string) bool {
    -	_, err := dc.ServerResourcesForGroupVersion(groupVersion)
    -	return err == nil
    +func isResourceServed(dc discovery.DiscoveryInterface, resource schema.GroupVersionResource) bool {
    +	list, err := dc.ServerResourcesForGroupVersion(resource.GroupVersion().String())
    +	if err != nil {
    +		return false
    +	}
    +	return slices.ContainsFunc(list.APIResources, func(r metav1.APIResource) bool {
    +		return r.Name == resource.Resource
    +	})
     }
     
    +// ExternalCRDCacheConfig only adds entries for served resources, since a cache.ByObject for a
    +// missing CRD fails manager construction.
     func ExternalCRDCacheConfig(dc discovery.DiscoveryInterface) (map[client.Object]cache.ByObject, []client.Object) {
     	uidReq, _ := labels.NewRequirement(v1beta1.LabelRequestUID, selection.Exists, nil)
     	uidSelector := labels.NewSelector().Add(*uidReq)
     
     	cacheByObject := map[client.Object]cache.ByObject{}
     	var clientDisableFor []client.Object
     
    -	if isAPIGroupAvailable(dc, cdiGroupVersion) {
    +	if isResourceServed(dc, dataVolumesResource) {
     		cacheByObject[&cdiv1beta1.DataVolume{}] = cache.ByObject{Label: uidSelector}
     	}
    -	if isAPIGroupAvailable(dc, snapshotGroupVersion) {
    +	if isResourceServed(dc, snapshotsResource) {
     		cacheByObject[&snapshotv1beta1.VirtualMachineSnapshot{}] = cache.ByObject{Label: uidSelector}
    +	}
    +	if isResourceServed(dc, snapshotContentsResource) {

    It's intentional, we don't want VirtualMachineSnapshotContents to be read from the informer/cache

    0xFelix 0xFelix commented on code on Nov 30, 2026 15:38 UTC
    Code Review
    internal/controller/vmtr_availability_controller.go:1
    @@ -140,33 +150,42 @@ func (c *VMTRAvailabilityController) setMissingCRDStatus(ctx context.Context, mi
     	}
     }
     
    -func (c *VMTRAvailabilityController) missingGroups() []string {
    +func (c *VMTRAvailabilityController) missingResources() []string {
     	var missing []string
    -	for _, group := range requiredGroups {
    -		if !isAPIGroupAvailable(c.DiscoveryClient, group) {
    -			missing = append(missing, group)
    +	for _, resource := range requiredResources {
    +		if !isResourceServed(c.DiscoveryClient, resource) {
    +			missing = append(missing, resource.GroupResource().String())
     		}
     	}
     	return missing
     }
     
    -func isAPIGroupAvailable(dc discovery.DiscoveryInterface, groupVersion string) bool {
    -	_, err := dc.ServerResourcesForGroupVersion(groupVersion)
    -	return err == nil
    +func isResourceServed(dc discovery.DiscoveryInterface, resource schema.GroupVersionResource) bool {
    +	list, err := dc.ServerResourcesForGroupVersion(resource.GroupVersion().String())
    +	if err != nil {
    +		return false
    +	}
    +	return slices.ContainsFunc(list.APIResources, func(r metav1.APIResource) bool {
    +		return r.Name == resource.Resource
    +	})
     }
     
    +// ExternalCRDCacheConfig only adds entries for served resources, since a cache.ByObject for a
    +// missing CRD fails manager construction.
     func ExternalCRDCacheConfig(dc discovery.DiscoveryInterface) (map[client.Object]cache.ByObject, []client.Object) {
     	uidReq, _ := labels.NewRequirement(v1beta1.LabelRequestUID, selection.Exists, nil)
     	uidSelector := labels.NewSelector().Add(*uidReq)
     
     	cacheByObject := map[client.Object]cache.ByObject{}
     	var clientDisableFor []client.Object
     
    -	if isAPIGroupAvailable(dc, cdiGroupVersion) {
    +	if isResourceServed(dc, dataVolumesResource) {
     		cacheByObject[&cdiv1beta1.DataVolume{}] = cache.ByObject{Label: uidSelector}
     	}
    -	if isAPIGroupAvailable(dc, snapshotGroupVersion) {
    +	if isResourceServed(dc, snapshotsResource) {
     		cacheByObject[&snapshotv1beta1.VirtualMachineSnapshot{}] = cache.ByObject{Label: uidSelector}
    +	}
    +	if isResourceServed(dc, snapshotContentsResource) {

    But... it doesn't have to be conditional, you're right!

    0xFelix 0xFelix commented on code on Nov 30, 2026 15:48 UTC
    Code Review
    internal/controller/vmtr_availability_controller.go:1
    @@ -140,33 +150,42 @@ func (c *VMTRAvailabilityController) setMissingCRDStatus(ctx context.Context, mi
     	}
     }
     
    -func (c *VMTRAvailabilityController) missingGroups() []string {
    +func (c *VMTRAvailabilityController) missingResources() []string {
     	var missing []string
    -	for _, group := range requiredGroups {
    -		if !isAPIGroupAvailable(c.DiscoveryClient, group) {
    -			missing = append(missing, group)
    +	for _, resource := range requiredResources {
    +		if !isResourceServed(c.DiscoveryClient, resource) {
    +			missing = append(missing, resource.GroupResource().String())
     		}
     	}
     	return missing
     }
     
    -func isAPIGroupAvailable(dc discovery.DiscoveryInterface, groupVersion string) bool {
    -	_, err := dc.ServerResourcesForGroupVersion(groupVersion)
    -	return err == nil
    +func isResourceServed(dc discovery.DiscoveryInterface, resource schema.GroupVersionResource) bool {
    +	list, err := dc.ServerResourcesForGroupVersion(resource.GroupVersion().String())
    +	if err != nil {
    +		return false
    +	}
    +	return slices.ContainsFunc(list.APIResources, func(r metav1.APIResource) bool {
    +		return r.Name == resource.Resource
    +	})
     }
     
    +// ExternalCRDCacheConfig only adds entries for served resources, since a cache.ByObject for a
    +// missing CRD fails manager construction.
     func ExternalCRDCacheConfig(dc discovery.DiscoveryInterface) (map[client.Object]cache.ByObject, []client.Object) {
     	uidReq, _ := labels.NewRequirement(v1beta1.LabelRequestUID, selection.Exists, nil)
     	uidSelector := labels.NewSelector().Add(*uidReq)
     
     	cacheByObject := map[client.Object]cache.ByObject{}
     	var clientDisableFor []client.Object
     
    -	if isAPIGroupAvailable(dc, cdiGroupVersion) {
    +	if isResourceServed(dc, dataVolumesResource) {
     		cacheByObject[&cdiv1beta1.DataVolume{}] = cache.ByObject{Label: uidSelector}
     	}
    -	if isAPIGroupAvailable(dc, snapshotGroupVersion) {
    +	if isResourceServed(dc, snapshotsResource) {
     		cacheByObject[&snapshotv1beta1.VirtualMachineSnapshot{}] = cache.ByObject{Label: uidSelector}
    +	}
    +	if isResourceServed(dc, snapshotContentsResource) {

    Actually it doesn't need to be conditional, and that breaks if the CRD shows up after startup. Fixed

    0xFelix 0xFelix commented on Nov 30, 2026 15:48 UTC
    Author

    /cherry-pick release-0.2

    kubevirt-bot kubevirt-bot commented on Nov 30, 2026 15:48 UTC
    Bot

    @0xFelix: once the present PR merges, I will cherry-pick it on top of release-0.2 in a new PR and assign it to you.

    <details>

    In response to this:

    >/cherry-pick release-0.2

    Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.
    </details>

    jcanocan jcanocan commented on Nov 30, 2026 15:56 UTC
    Contributor

    /lgtm

    ksimon1 ksimon1 commented on Nov 30, 2026 19:45 UTC
    Contributor

    /approve

    kubevirt-prow[bot] kubevirt-prow[bot] commented on Nov 30, 2026 19:45 UTC
    Bot

    [APPROVALNOTIFIER] This PR is APPROVED

    This pull-request has been approved by: <a href="https://github.com/kubevirt/virt-template/pull/169#issuecomment-7289889378" title="Approved">ksimon1</a>

    The full list of commands accepted by this bot can be found here.

    The pull request process is described here

    <details >
    Needs approval from an approver in each of these files:

  • ~~OWNERS~~ [ksimon1]
  • Approvers can indicate their approval by writing /approve in a comment
    Approvers can cancel approval by writing /approve cancel in a comment
    </details>
    <!-- META={"approvers":[]} -->

    kubevirt-bot kubevirt-bot commented on Nov 30, 2026 20:08 UTC
    Bot

    @0xFelix: new pull request created: #230

    <details>

    In response to this:

    >/cherry-pick release-0.3

    Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.
    </details>

    kubevirt-bot kubevirt-bot commented on Nov 30, 2026 20:10 UTC
    Bot

    @0xFelix: new pull request created: #231

    <details>

    In response to this:

    >/cherry-pick release-0.2

    Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.
    </details>

    Reviewers
    Assignees
    0xFelix 0xFelix
    Labels
    risk: low confidence: 90.6% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 37.2% (MEDIUM) · Higher = larger blast radius
    37.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 90.6% · Lower = rushed or shallow review
    90.6%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 3.5% · Safe to merge
    3.5%