Skip to content

<feature>[kvm]: add libvirt TLS config#3458

Open
zstack-robot-2 wants to merge 1 commit into5.5.12from
sync/yingzhe.hu/ZSTAC-81343@@2
Open

<feature>[kvm]: add libvirt TLS config#3458
zstack-robot-2 wants to merge 1 commit into5.5.12from
sync/yingzhe.hu/ZSTAC-81343@@2

Conversation

@zstack-robot-2
Copy link
Collaborator

Add libvirt.tls.enabled GlobalConfig and useTls field in MigrateVmCmd to support TLS-encrypted libvirt connections for migration and V2V.

Resolves: ZSTAC-81343

Change-Id: I391fa36c0dd63c25c5d85d102bc3579c8eb3d685

sync from gitlab !9317

@coderabbitai
Copy link

coderabbitai bot commented Mar 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: http://open.zstack.ai:20001/code-reviews/zstack-cloud.yaml (via .coderabbit.yaml)

Review profile: CHILL

Plan: Pro

Run ID: 39787020-4203-49e9-a415-a93341a8d563

📥 Commits

Reviewing files that changed from the base of the PR and between 69e99d0 and 5e7f24f.

📒 Files selected for processing (3)
  • plugin/kvm/src/main/java/org/zstack/kvm/KVMAgentCommands.java
  • plugin/kvm/src/main/java/org/zstack/kvm/KVMGlobalConfig.java
  • plugin/kvm/src/main/java/org/zstack/kvm/KVMHost.java
🚧 Files skipped from review as they are similar to previous changes (3)
  • plugin/kvm/src/main/java/org/zstack/kvm/KVMAgentCommands.java
  • plugin/kvm/src/main/java/org/zstack/kvm/KVMHost.java
  • plugin/kvm/src/main/java/org/zstack/kvm/KVMGlobalConfig.java

Walkthrough

为 KVM 虚拟机迁移命令新增 TLS 支持与源主机管理 IP:添加全局配置 LIBVIRT_TLS_ENABLED,在 MigrateVmCmd 中新增 useTlssrcHostManagementIp 字段及其访问器,并在构建迁移命令时设置这些字段。

Changes

Cohort / File(s) Summary
命令对象变更
plugin/kvm/src/main/java/org/zstack/kvm/KVMAgentCommands.java
KVMAgentCommands.MigrateVmCmd 中新增私有字段 useTls(@GrayVersion("5.5.12"))与 srcHostManagementIp(@GrayVersion("5.5.12")),并添加对应的 getter/setter 方法。
全局配置
plugin/kvm/src/main/java/org/zstack/kvm/KVMGlobalConfig.java
新增全局配置 LIBVIRT_TLS_ENABLED(Boolean,默认 "true"),并添加 @GlobalConfigValidation(validValues = {"true","false"}) 与描述,控制 libvirt 远程连接是否启用 TLS。
迁移流程集成
plugin/kvm/src/main/java/org/zstack/kvm/KVMHost.java
在构建迁移命令时调用 cmd.setUseTls(...)(从 KVMGlobalConfig.LIBVIRT_TLS_ENABLED 读取)并调用 cmd.setSrcHostManagementIp(...) 将源主机管理 IP 设置到命令中。

Sequence Diagram(s)

sequenceDiagram
    participant KVMHost as KVMHost
    participant Config as KVMGlobalConfig
    participant AgentCmd as KVMAgentCommands.MigrateVmCmd
    participant Libvirt as Libvirt (remote)

    KVMHost->>Config: read LIBVIRT_TLS_ENABLED
    Config-->>KVMHost: boolean value
    KVMHost->>AgentCmd: new MigrateVmCmd(...)\nsetUseTls(value)\nsetSrcHostManagementIp(srcHostMnIp)
    KVMHost->>Libvirt: send migration request (includes useTls, srcHostManagementIp)
    Libvirt-->>KVMHost: respond to migration request
Loading

预计代码审查工作量

🎯 2 (简单) | ⏱️ ~12 分钟

🐰 小兔穿上 TLS 衣,
源主机 IP 握在臂,
命令轻声走远路,
配置点头保平安,
花间归来笑盈盈 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed 标题完全符合指定格式 [scope]: ,且长度为38字符,远在72字符限制以内。标题清晰地总结了变更的主要内容——添加libvirt TLS配置。
Description check ✅ Passed 描述内容与变更集相关,明确说明了添加GlobalConfig libvirt.tls.enabled和MigrateVmCmd中useTls字段的目的,以支持TLS加密的libvirt连接。

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

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch sync/yingzhe.hu/ZSTAC-81343@@2
📝 Coding Plan
  • Generate coding plan for human review comments

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ast-grep (0.41.1)
plugin/kvm/src/main/java/org/zstack/kvm/KVMHost.java

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

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

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

Inline comments:
In `@plugin/kvm/src/main/java/org/zstack/kvm/KVMHost.java`:
- Line 3177: The call to
KVMGlobalConfig.LIBVIRT_TLS_ENABLED.value(Boolean.class) in KVMHost (affecting
cmd.setUseTls) bypasses cluster-scoped overrides; change it to fetch the
cluster-level value via the resource config facade (use
rcf.getResourceConfigValue) for the host's cluster (use the host/cluster UUID
available in KVMHost) and pass that Boolean into cmd.setUseTls, matching the
pattern used elsewhere in this class (e.g., where rcf.getResourceConfigValue is
used around lines ~3157, ~3931, ~1782).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: http://open.zstack.ai:20001/code-reviews/zstack-cloud.yaml (via .coderabbit.yaml)

Review profile: CHILL

Plan: Pro

Run ID: 7bb792e6-a9b5-4822-b465-e9ee96620481

📥 Commits

Reviewing files that changed from the base of the PR and between 1912469 and 32af041.

📒 Files selected for processing (3)
  • plugin/kvm/src/main/java/org/zstack/kvm/KVMAgentCommands.java
  • plugin/kvm/src/main/java/org/zstack/kvm/KVMGlobalConfig.java
  • plugin/kvm/src/main/java/org/zstack/kvm/KVMHost.java

cmd.setDownTime(s.downTime);
cmd.setBandwidth(s.bandwidth);
cmd.setNics(nicTos);
cmd.setUseTls(KVMGlobalConfig.LIBVIRT_TLS_ENABLED.value(Boolean.class));
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== Inspect LIBVIRT_TLS_ENABLED declaration =="
fd 'KVMGlobalConfig.java$' | while read -r f; do
  echo "-- $f --"
  rg -n -C4 'LIBVIRT_TLS_ENABLED|ClusterVO|bind' "$f" || true
done

echo
echo "== Inspect current use in KVMHost =="
fd 'KVMHost.java$' | while read -r f; do
  echo "-- $f --"
  rg -n -C3 'setUseTls|LIBVIRT_TLS_ENABLED' "$f" || true
done

echo
echo "== Compare with resource-scoped KVM config reads in KVMHost =="
fd 'KVMHost.java$' | while read -r f; do
  echo "-- $f --"
  rg -n -C1 'rcf\.getResourceConfigValue\(KVMGlobalConfig\.' "$f" || true
done

Repository: MatheMatrix/zstack

Length of output: 8576


配置作用域错误,绕过了集群级别的 TLS 设置

LIBVIRT_TLS_ENABLEDKVMGlobalConfig.java 中被标注为 @BindResourceConfig({ClusterVO.class}),是集群级资源配置。但第 3177 行直接使用 .value(Boolean.class) 读取全局默认值,绕过了集群维度的覆盖值。这导致某集群即使显式配置禁用 TLS,迁移命令仍会沿用默认值,新增的集群级开关失效,也违反了向后兼容原则。

应按照文件中其他资源配置的读取方式(如第 3157 行、3931 行、1782 行等),通过 rcf.getResourceConfigValue() 按 cluster 读取资源配置。

🔧 建议修改
-                        cmd.setUseTls(KVMGlobalConfig.LIBVIRT_TLS_ENABLED.value(Boolean.class));
+                        cmd.setUseTls(rcf.getResourceConfigValue(
+                                KVMGlobalConfig.LIBVIRT_TLS_ENABLED,
+                                self.getClusterUuid(),
+                                Boolean.class
+                        ));
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@plugin/kvm/src/main/java/org/zstack/kvm/KVMHost.java` at line 3177, The call
to KVMGlobalConfig.LIBVIRT_TLS_ENABLED.value(Boolean.class) in KVMHost
(affecting cmd.setUseTls) bypasses cluster-scoped overrides; change it to fetch
the cluster-level value via the resource config facade (use
rcf.getResourceConfigValue) for the host's cluster (use the host/cluster UUID
available in KVMHost) and pass that Boolean into cmd.setUseTls, matching the
pattern used elsewhere in this class (e.g., where rcf.getResourceConfigValue is
used around lines ~3157, ~3931, ~1782).

Add libvirt.tls.enabled GlobalConfig and useTls field in MigrateVmCmd to support TLS-encrypted libvirt connections for migration and V2V.

Resolves: ZSTAC-81343

Change-Id: I391fa36c0dd63c25c5d85d102bc3579c8eb3d685
@MatheMatrix MatheMatrix force-pushed the sync/yingzhe.hu/ZSTAC-81343@@2 branch from 69e99d0 to 5e7f24f Compare March 13, 2026 06:41
@zstack-robot-2
Copy link
Collaborator Author

Comment from yaohua.wu:

Review: MR !9317 — ZSTAC-81343 (libvirt TLS 配置 - Java 侧)

关联 MR: zstack-utility !6727 | premium !13134

正面评价

  • LIBVIRT_TLS_ENABLED 正确定义为全局配置(移除了 @BindResourceConfig),避免集群间 TLS 配置不一致导致的迁移失败
  • defaultValue = "true" 确保新安装环境默认安全
  • srcHostManagementIp 字段正确补充了反向迁移场景(migrateFromDestination=true)的 TLS 证书 SAN 匹配需求
  • @GrayVersion(value = "5.5.12") 灰度版本标注正确

Warning

  1. [KVMHost.java] 其他构造 MigrateVmCmd 的代码路径

    当前 diff 仅展示了一处设置 cmd.setUseTls()cmd.setSrcHostManagementIp() 的位置。如果项目中还有其他地方构造 MigrateVmCmd(如 V2V 模块、存储迁移模块、mini 残留路径),这些位置需要同步设置 TLS 参数。

    kvmagent 侧通过 getattr(cmd, 'useTls', False) 做了向后兼容,遗漏的构造点会 fallback 到 TCP——而 TCP 端口 16509 已被 libvirtd.conf 关闭,会导致连接拒绝。

    建议全局搜索 new MigrateVmCmd()MigrateVmCmd cmd 确认所有构造点已覆盖。

Suggestion

  1. [KVMGlobalConfig.java] 补充回退约束注释

    建议在 LIBVIRT_TLS_ENABLED 声明处添加注释说明:设为 false 仅影响迁移协议选择,不会自动恢复物理机的 TCP 监听(需手动修改 libvirtd.conf + 重启 libvirtd)。避免后续维护者误解该配置的完整回退能力。

Verdict: APPROVED

Java 侧改动简洁正确,架构决策合理(全局配置 vs 集群级配置的选择正确)。Warning #1 建议确认但不阻塞合并。


🤖 Robot Reviewer

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.

2 participants