Skip to content

Conversation

@shinny-taojiachun
Copy link
Collaborator

@shinny-taojiachun shinny-taojiachun commented Jul 14, 2025

Summary by CodeRabbit

  • 新功能

    • 支持在创建云服务器实例时指定多个候选实例类型,系统将按顺序尝试,提升实例创建的灵活性和成功率。
    • 配置项新增 instance_type_candidates,允许用户传入多个实例类型候选列表。
  • 修复与优化

    • 优化了子网查询逻辑,改为使用用户指定的单一区域,简化过滤条件并取消自动选择可用区。
    • 改进了实例创建失败后的资源清理机制,避免资源泄漏。
    • 移除镜像查询时的实例类型过滤,简化错误提示。
    • 修正了部分注释中的拼写错误。

@coderabbitai
Copy link

coderabbitai bot commented Jul 14, 2025

📝 Walkthrough
📝 Walkthrough

此次变更在腾讯云CVM构建器中引入了实例类型候选列表,替代原有单一实例类型字段。相关配置结构体、实例创建逻辑及验证流程均做了相应调整,支持按优先级顺序尝试多个实例类型。同时,子网和可用区查询逻辑也进行了优化,文档和测试代码同步更新。

Changes

文件路径(简写) 变更摘要
builder/tencentcloud/cvm/*.go 多文件将单一InstanceType字段替换为InstanceTypeCandidates切片;实例创建逻辑改为依次尝试多个实例类型和子网;配置验证逻辑调整,确保两者互斥且至少指定一个;优化子网和可用区查询,取消自动按实例类型筛选,改为使用指定可用区。
builder/tencentcloud/cvm/builder.hcl2spec.go FlatConfig结构体新增InstanceTypeCandidates字段,InstanceType改为非必填;更新HCL2Spec属性定义。
builder/tencentcloud/cvm/run_config_test.go 测试辅助函数testConfig中实例类型字段由单字符串改为字符串切片;新增验证逻辑测试,确保InstanceTypeInstanceTypeCandidates的互斥和必填规则。
builder/tencentcloud/cvm/step_check_source_image.go 移除按实例类型过滤镜像请求参数,简化无镜像错误提示信息。
docs-partials/builder/tencentcloud/cvm/*.mdx 新增instance_type_candidatesinstance_type字段的文档说明,均为可选参数。

Sequence Diagram(s)

sequenceDiagram
    participant Builder
    participant StepRunInstance
    participant TencentCloudAPI

    Builder->>StepRunInstance: 传递InstanceTypeCandidates列表
    StepRunInstance->>TencentCloudAPI: 依次尝试每个实例类型+每个子网创建实例
    alt 创建成功
        TencentCloudAPI-->>StepRunInstance: 返回实例ID
        StepRunInstance-->>Builder: 返回成功结果
    else 创建失败
        TencentCloudAPI-->>StepRunInstance: 返回错误及可能的实例ID
        StepRunInstance->>TencentCloudAPI: 尝试终止已创建实例
        StepRunInstance-->>Builder: 返回错误
    end
Loading

Poem

兔子跳跃新天地,
多型实例任我驰。
候选清单排排坐,
子网遍历不曾迟。
云端筑梦添新翼,
代码跃动喜洋洋! 🐇☁️✨

Warning

Review ran into problems

🔥 Problems

Errors were encountered while retrieving linked issues.

Errors (1)
  • JIRA integration encountered authorization issues. Please disconnect and reconnect the integration in the CodeRabbit UI.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 91d3e92 and 2e7c645.

📒 Files selected for processing (2)
  • builder/tencentcloud/cvm/step_config_subnet.go (3 hunks)
  • builder/tencentcloud/cvm/step_run_instance.go (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • builder/tencentcloud/cvm/step_run_instance.go
🧰 Additional context used
🪛 GitHub Actions: Go Test Linux
builder/tencentcloud/cvm/step_config_subnet.go

[error] 9-9: Go compiler error: imported and not used: "strings"

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Generate check
  • GitHub Check: Lint check
🔇 Additional comments (3)
builder/tencentcloud/cvm/step_config_subnet.go (3)

36-49: 子网过滤逻辑简化得当

新的过滤逻辑更加直观,始终包含 VPC ID 过滤器,并在指定可用区时添加区域过滤器。这种简化符合新的设计理念,即直接使用用户指定的可用区而不是基于实例类型进行动态发现。


53-59: 子网名称过滤逻辑优化

子网名称过滤逻辑已简化,直接在存在子网名称时添加过滤器。代码结构清晰,逻辑正确。


80-103: 子网创建逻辑重构合理

子网创建逻辑已从尝试多个候选可用区改为在单个指定可用区创建。这种改变与支持候选实例类型列表的整体设计保持一致,避免了复杂的区域遍历逻辑。

代码逻辑清晰:

  • 生成随机子网名称
  • 在指定可用区创建子网
  • 创建成功后直接继续,不再尝试其他区域
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch add-instance-candidates

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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: 2

🔭 Outside diff range comments (1)
builder/tencentcloud/cvm/step_config_subnet.go (1)

73-90: 可用区顺序不确定可能导致不一致的行为

从 map 遍历得到的 zones 切片顺序是不确定的,取"最后5个"可能在不同运行时得到不同结果。建议对可用区进行排序以确保一致性。

 	var zones []string
 	for zone := range zonesSet {
 		zones = append(zones, zone)
 	}
+	// 对可用区进行排序以确保一致性
+	sort.Strings(zones)

 	// 如果指定了子网ID或子网名称,则尝试使用已有子网

记得在文件顶部导入 sort 包。

🧹 Nitpick comments (3)
builder/tencentcloud/cvm/step_config_subnet.go (1)

56-56: 改进日志消息的清晰度

当前的日志消息过于简短,建议提供更多上下文信息。

-				Say(state, fmt.Sprintf("length:%d", len(resp.Response.InstanceTypeQuotaSet)), "")
+				Say(state, fmt.Sprintf("找到 %d 个可用区", len(resp.Response.InstanceTypeQuotaSet)), "")
builder/tencentcloud/cvm/step_run_instance.go (2)

193-193: 修正注释中的错别字

-				// instanceId不存在代表之前开机不成功,此处不需要再次删除。若是LAUNCH_FAILED会预到Code=InvalidInstanceId.NotFound,跳过尝试下一个subnet继续尝试开机即可
+				// instanceId不存在代表之前开机不成功,此处不需要再次删除。若是LAUNCH_FAILED会遇到Code=InvalidInstanceId.NotFound,跳过尝试下一个subnet继续尝试开机即可

197-202: 简化实例ID列表的构建

使用 strings.Join 可以更简洁地构建实例ID列表,并避免末尾多余的逗号。

-					var builder strings.Builder
-					for _, instanceId := range instanceIds {
-						builder.WriteString(*instanceId)
-						builder.WriteString(",")
-					}
-					return Halt(state, terminateErr, fmt.Sprintf("Failed to terminate instance %s may need to delete it manually", builder.String()))
+					instanceIdStrs := make([]string, len(instanceIds))
+					for i, instanceId := range instanceIds {
+						instanceIdStrs[i] = *instanceId
+					}
+					return Halt(state, terminateErr, fmt.Sprintf("Failed to terminate instance %s may need to delete it manually", strings.Join(instanceIdStrs, ",")))
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0f052fc and 93b9268.

📒 Files selected for processing (4)
  • builder/tencentcloud/cvm/builder.go (1 hunks)
  • builder/tencentcloud/cvm/run_config.go (3 hunks)
  • builder/tencentcloud/cvm/step_config_subnet.go (1 hunks)
  • builder/tencentcloud/cvm/step_run_instance.go (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
builder/tencentcloud/cvm/step_config_subnet.go (1)
builder/tencentcloud/cvm/common.go (3)
  • Say (238-249)
  • Retry (199-222)
  • Halt (272-277)
builder/tencentcloud/cvm/step_run_instance.go (1)
builder/tencentcloud/cvm/common.go (2)
  • Retry (199-222)
  • Halt (272-277)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Lint check
🔇 Additional comments (1)
builder/tencentcloud/cvm/builder.go (1)

126-126: 确认:多候选机型规格支持已正确实现

以下关键位置已验证:

  • builder/tencentcloud/cvm/run_config.go
    • instanceTypeCandidates []string 字段定义、互斥校验与归一化逻辑已生效
  • builder/tencentcloud/cvm/step_run_instance.go
    • stepRunInstance 结构体包含 InstanceTypeCandidates []string
    • Run() 方法第172行开始已按顺序遍历 InstanceTypeCandidates 并设置 req.InstanceType
  • builder/tencentcloud/cvm/builder.go
    • 已将 b.config.instanceTypeCandidates 传递给 stepRunInstance.InstanceTypeCandidates

变更与“添加候选机型规格列表支持”目标完全一致,且相关配置、结构体与运行逻辑均已正确更新,无需额外修改。

@shinny-taojiachun shinny-taojiachun force-pushed the add-instance-candidates branch from 93b9268 to b4cabe9 Compare July 16, 2025 02:56
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: 0

🔭 Outside diff range comments (3)
builder/tencentcloud/cvm/run_config_test.go (2)

33-36: 修复过时的测试逻辑

此测试期望在InstanceType为空时出现错误,但由于测试配置现在设置了InstanceTypeCandidates,根据新的验证逻辑(要求InstanceTypeInstanceTypeCandidates中恰好设置一个),此测试应该通过而不是失败。

应用此差异来修复测试逻辑:

-	cf.InstanceType = ""
-	if err := cf.Prepare(nil); err == nil {
-		t.Fatal("should have err")
-	}
+	cf.InstanceType = ""
+	cf.InstanceTypeCandidates = nil
+	if err := cf.Prepare(nil); err == nil {
+		t.Fatal("should have err")
+	}

38-42: 确保测试覆盖新的验证逻辑

此测试设置了InstanceType,但没有清除InstanceTypeCandidates。根据新的验证逻辑,这应该会导致错误,因为两个字段都被设置了。请考虑添加测试用例来验证互斥性。

建议添加以下测试用例:

+	// Test that both InstanceType and InstanceTypeCandidates cannot be set
+	cf.InstanceType = "S3.SMALL2"
+	cf.InstanceTypeCandidates = []string{"S3.SMALL2"}
+	if err := cf.Prepare(nil); err == nil {
+		t.Fatal("should have err when both instance_type and instance_type_candidates are set")
+	}
builder/tencentcloud/cvm/builder.hcl2spec.go (1)

132-241: HCL2Spec方法中缺少instance_type_candidates字段定义

FlatConfig结构体中添加了InstanceTypeCandidates字段,但HCL2Spec方法中缺少相应的规范定义。这会导致HCL配置无法识别instance_type_candidates字段。

在HCL2Spec方法中添加缺失的字段定义:

 		"instance_charge_type":         &hcldec.AttrSpec{Name: "instance_charge_type", Type: cty.String, Required: false},
+		"instance_type_candidates":     &hcldec.AttrSpec{Name: "instance_type_candidates", Type: cty.List(cty.String), Required: false},
 		"instance_type":                &hcldec.AttrSpec{Name: "instance_type", Type: cty.String, Required: false},
🧹 Nitpick comments (1)
builder/tencentcloud/cvm/step_check_source_image.go (1)

65-65: 错误消息简化符合新的候选机型支持逻辑

将错误消息简化为"No image found"是合理的,因为现在系统支持多个候选机型规格,不再按单一实例类型过滤镜像。这个改动与PR目标保持一致。

不过建议考虑在错误消息中提供更多上下文信息,帮助用户调试:

-return Halt(state, fmt.Errorf("No image found"), "")
+return Halt(state, fmt.Errorf("No image found matching the specified criteria"), "")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 93b9268 and b4cabe9.

📒 Files selected for processing (8)
  • builder/tencentcloud/cvm/builder.go (1 hunks)
  • builder/tencentcloud/cvm/builder.hcl2spec.go (1 hunks)
  • builder/tencentcloud/cvm/run_config.go (3 hunks)
  • builder/tencentcloud/cvm/run_config_test.go (1 hunks)
  • builder/tencentcloud/cvm/step_check_source_image.go (1 hunks)
  • builder/tencentcloud/cvm/step_config_subnet.go (2 hunks)
  • builder/tencentcloud/cvm/step_run_instance.go (4 hunks)
  • docs-partials/builder/tencentcloud/cvm/TencentCloudRunConfig-not-required.mdx (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • docs-partials/builder/tencentcloud/cvm/TencentCloudRunConfig-not-required.mdx
🚧 Files skipped from review as they are similar to previous changes (4)
  • builder/tencentcloud/cvm/builder.go
  • builder/tencentcloud/cvm/step_run_instance.go
  • builder/tencentcloud/cvm/step_config_subnet.go
  • builder/tencentcloud/cvm/run_config.go
🧰 Additional context used
🧬 Code Graph Analysis (1)
builder/tencentcloud/cvm/step_check_source_image.go (1)
builder/tencentcloud/cvm/common.go (1)
  • Halt (272-277)
🪛 GitHub Actions: Go Test Linux
builder/tencentcloud/cvm/run_config_test.go

[error] 35-35: Test failure in TestTencentCloudRunConfig_Prepare: expected an error but did not receive one.

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Lint check
🔇 Additional comments (2)
builder/tencentcloud/cvm/run_config_test.go (1)

17-17: 更新测试配置以使用新的候选机型列表字段

配置更新正确地反映了新的设计,使用InstanceTypeCandidates字段而不是单个InstanceType

builder/tencentcloud/cvm/builder.hcl2spec.go (1)

42-43: 正确添加了候选机型字段并调整了实例类型字段的要求

结构体更新正确地反映了新设计:

  • 添加了InstanceTypeCandidates字段作为可选的字符串切片
  • InstanceType字段从必需改为可选

@shinny-taojiachun shinny-taojiachun force-pushed the add-instance-candidates branch 2 times, most recently from 3ed853a to 91d3e92 Compare July 16, 2025 03:19
@shinny-taojiachun shinny-taojiachun force-pushed the add-instance-candidates branch from 2e7c645 to ce0821e Compare July 17, 2025 05:43
@shinny-taojiachun shinny-taojiachun merged commit bf68f89 into master Jul 28, 2025
9 checks passed
@shinny-taojiachun shinny-taojiachun deleted the add-instance-candidates branch July 28, 2025 05:54
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.

3 participants