Skip to content

[tenant] Allow egress to virt-handler for VM metrics scraping - #2199

Merged
Andrei Kvapil (kvaps) merged 1 commit into
cozystack:mainfrom
mattia-eleuteri:tenant/egress-virt-handler
Mar 10, 2026
Merged

Andrei Kvapil (kvaps) merged 1 commit into
cozystack:mainfrom
mattia-eleuteri:tenant/egress-virt-handler

Conversation

@mattia-eleuteri

@mattia-eleuteri mattia-eleuteri commented Mar 10, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Adds a CiliumClusterwideNetworkPolicy allowing egress from tenant pods to virt-handler in cozy-kubevirt namespace on port 8443/TCP
  • Conditional on .Values.monitoring being enabled

Problem

Tenant vmagent cannot scrape KubeVirt VM metrics from virt-handler because no network policy allows the traffic.

Relates to #2194

Test plan

  • helm template tenant with monitoring: true — virt-handler policy present
  • helm template tenant with monitoring: false — virt-handler policy absent
  • Deploy and verify vmagent can scrape kubevirt_vmi_* metrics
Allow tenant egress to virt-handler for VM metrics scraping

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Added a network egress policy that, when monitoring is enabled, allows tenant namespaces to reach the virt-handler service on TCP port 8443, improving connectivity for monitoring-related traffic.

@dosubot dosubot Bot added size/S This PR changes 10-29 lines, ignoring generated files kind/feature Categorizes issue or PR as related to a new feature labels Mar 10, 2026
@coderabbitai

coderabbitai Bot commented Mar 10, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2cf59c4f-c443-4b6f-aed7-92e07979f1cb

📥 Commits

Reviewing files that changed from the base of the PR and between 05ca58c and 630dfc7.

📒 Files selected for processing (1)
  • packages/apps/tenant/templates/networkpolicy.yaml

📝 Walkthrough

Walkthrough

Adds a conditional Helm template block that defines a CiliumClusterwideNetworkPolicy enabling egress from the tenant namespace to the virt-handler service on TCP port 8443 when .Values.monitoring is enabled.

Changes

Cohort / File(s) Summary
Network Policy Configuration
packages/apps/tenant/templates/networkpolicy.yaml
Added a .Values.monitoring-guarded CiliumClusterwideNetworkPolicy named "-egress-virt-handler" allowing egress from the tenant namespace to the virt-handler service on TCP port 8443.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐇 I nibble bytes beneath the moonlit log,

I stitch a tunnel to the virt-handler's bog,
Port 8443, hop steady, hop light,
Monitoring enabled — the path is right. 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: adding egress permissions to virt-handler for VM metrics scraping, which matches the primary objective of the pull request.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/apps/tenant/templates/networkpolicy.yaml`:
- Around line 217-219: The rule currently uses endpointSelector with matchLabels
"k8s:io.kubernetes.pod.namespace": "{{ include "tenant.name" .}}" which allows
all pods in the tenant namespace to reach virt-handler:8443; restrict it to only
the monitoring scraper pods by replacing the namespace-wide selector with a
pod-level selector that matches the monitoring scraper label (e.g., the vmagent
app label or a scrape opt-in label such as app: vmagent or prometheus.io/scrape:
"true") so only vmagent/monitoring pods are allowed to target virt-handler:8443;
update the endpointSelector/matchLabels in the template accordingly (keeping
tenant.name for scoping if needed) and ensure the selected label matches the
actual label used by your vmagent pods.
- Around line 221-224: The NetworkPolicy uses toEndpoints with matchLabels for
the virt-handler pod (labels "k8s:kubevirt.io": "virt-handler" and
"k8s:io.kubernetes.pod.namespace": "cozy-kubevirt"), but virt-handler runs with
hostNetwork: true so pod-based endpoint selectors won't match; either update the
KubeVirt deployment via the KubeVirt CR's customizeComponents to disable
hostNetwork for virt-handler (so matchLabels/toEndpoints will work) or modify
this NetworkPolicy to target host traffic instead (e.g., replace the toEndpoints
rule with a host-targeting rule such as toEntity: host or equivalent CIDR-based
egress/ingress) and ensure the virt-handler label references are removed or
adjusted accordingly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ac054275-b699-49ac-8219-1c233b114a00

📥 Commits

Reviewing files that changed from the base of the PR and between a13481b and cf66439.

📒 Files selected for processing (1)
  • packages/apps/tenant/templates/networkpolicy.yaml

Comment thread packages/apps/tenant/templates/networkpolicy.yaml
Comment thread packages/apps/tenant/templates/networkpolicy.yaml
- Add CiliumClusterwideNetworkPolicy for vmagent egress to virt-handler
- Restrict endpointSelector to vmagent pods only via app.kubernetes.io/name label

Signed-off-by: Mattia Eleuteri <[email protected]>
Signed-off-by: mattia-eleuteri <[email protected]>
@mattia-eleuteri
mattia-eleuteri force-pushed the tenant/egress-virt-handler branch from 05ca58c to 630dfc7 Compare March 10, 2026 15:57

@kvaps Andrei Kvapil (kvaps) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Mar 10, 2026
@kvaps
Andrei Kvapil (kvaps) merged commit 539b5c3 into cozystack:main Mar 10, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/feature Categorizes issue or PR as related to a new feature lgtm This PR has been approved by a maintainer size/S This PR changes 10-29 lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants