Skip to content

Feat/support alibabacloud#62

Merged
tianmu2023 merged 29 commits intoantgroup:mainfrom
j3ttt:feat/support-alibabacloud
Aug 11, 2025
Merged

Feat/support alibabacloud#62
tianmu2023 merged 29 commits intoantgroup:mainfrom
j3ttt:feat/support-alibabacloud

Conversation

@j3ttt
Copy link
Collaborator

@j3ttt j3ttt commented Aug 6, 2025

Thank you for your contribution to CloudRec!

What About:

  • Server (java)
  • Collector (go)
  • Rule (opa)

Description:

new resource type support: collection capabilities for ECS Images, ECS Snapshots, API Gateway instances, Simple Application Server (SWAS) instances, VPN Connections, and Bastionhost instances.

Summary by Sourcery

Add Go collector support for multiple new Alibaba Cloud resources by registering clients, constants and platform configuration, and implement detail functions for ECS Image, ECS Snapshot, API Gateway, SWAS, VPN Connection, Bastionhost, DTS, ECI ContainerGroup, and ECI ImageCache; refactor ACK cluster detail retrieval.

New Features:

  • Support collection of ECS Images and ECS Snapshots
  • Support collection of API Gateway resources
  • Support collection of Simple Application Server (SWAS) instances
  • Support collection of VPN Connection resources
  • Support collection of Bastionhost instances
  • Support collection of DTS Instances
  • Support collection of ECI ContainerGroup and ECI ImageCache resources

Enhancements:

  • Extend Services initialization to include new Alibaba Cloud SDK clients
  • Refactor ACK cluster detail to aggregate associated cluster resources

j3ttt and others added 26 commits June 10, 2025 10:28
change docker-image.yaml, add lint.yaml
Feat/support alibabacloud resource:
- ACS::DTS::Instance
- ACS::ECI::ContainerGroup=
- ACS::ECI::ImageCache
@gemini-code-assist
Copy link
Contributor

Important

Installation incomplete: to start using Gemini Code Assist, please ask the organization owner(s) to visit the Gemini Code Assist Admin Console and sign the Terms of Services.

@sourcery-ai
Copy link

sourcery-ai bot commented Aug 6, 2025

Reviewer's Guide

The PR extends the Alibaba Cloud collector by adding support for ECS images and snapshots, API Gateway, SWAS instances, VPN connections, Bastionhost, DTS instances, and ECI container groups/image caches, integrating their clients, resource definitions, and detail-fetch logic across the service initializer and platform configuration.

Sequence diagram for resource detail collection (e.g., ECS Image)

sequenceDiagram
    participant Collector
    participant Services
    participant ECSClient
    participant AlibabaCloudAPI
    Collector->>Services: InitServices(resourceType=ECSImage)
    Services->>ECSClient: NewClientWithAccessKey(...)
    Collector->>ECSClient: listImages()
    ECSClient->>AlibabaCloudAPI: DescribeImages
    AlibabaCloudAPI-->>ECSClient: Images list
    ECSClient-->>Collector: Images
    Collector->>Collector: Aggregate ImageDetail
Loading

Sequence diagram for API Gateway resource detail collection

sequenceDiagram
    participant Collector
    participant Services
    participant CloudAPIClient
    participant AlibabaCloudAPI
    Collector->>Services: InitServices(resourceType=APIGateway)
    Services->>CloudAPIClient: CreateCloudAPIClient(...)
    Collector->>CloudAPIClient: listAPIs()
    CloudAPIClient->>AlibabaCloudAPI: DescribeApis
    AlibabaCloudAPI-->>CloudAPIClient: API list
    loop for each API
        Collector->>CloudAPIClient: describeAPIDetail(apiId)
        CloudAPIClient->>AlibabaCloudAPI: DescribeApi
        AlibabaCloudAPI-->>CloudAPIClient: Api detail
        CloudAPIClient-->>Collector: APIGatewayDetail
    end
    Collector->>Collector: Aggregate APIGatewayDetail
Loading

Class diagram for ECS Image and Snapshot resource detail

classDiagram
    class ImageDetail {
        +ecs.Image Image
    }
    class SnapshotDetail {
        +ecs.Snapshot Snapshot
    }
    ECSImage <|-- ImageDetail
    ECSSnapshot <|-- SnapshotDetail
Loading

Class diagram for API Gateway, SWAS, Bastionhost, VPN Connection, DTS, and ECI resource details

classDiagram
    class APIGatewayDetail {
        +ApiSummary
        +ApiInfo
    }
    class Detail {
        +Instance
        +FirewallRules
    }
    class BastionhostDetail {
        +Instance
        +InstanceAttribute
    }
    class VPNConnectionDetail {
        +VpnConnection
    }
    class DTSInstanceDetail {
        +MigrationJob
        +MigrationJobDetail
    }
    class ECIContainerGroupDetail {
        +ContainerGroup
    }
    class ECIImageCacheDetail {
        +ImageCache
    }
    APIGateway <|-- APIGatewayDetail
    SWAS <|-- Detail
    Bastionhost <|-- BastionhostDetail
    VpnConnection <|-- VPNConnectionDetail
    DTSInstance <|-- DTSInstanceDetail
    ECIContainerGroup <|-- ECIContainerGroupDetail
    ECIImageCache <|-- ECIImageCacheDetail
Loading

File-Level Changes

Change Details Files
Extended service initialization to include new Alibaba Cloud clients
  • Imported resourcecenter, dts, eci, eflo, swas-open, oss SDKs in services.go
  • Added DTS, ECI, Eflo, SWAS fields to Services struct
  • Updated InitServices switch to initialize new clients for ECSImage, ECSSnapshot, DTSInstance, ECI, SWAS, VpnConnection, Bastionhost, APIGateway
collector/alicloud/collector/services.go
Added new resource types and definitions in platform configuration
  • Imported dts, eci, swas, eci packages in platform_config.go
  • Appended calls to GetImagesResource, GetSnapshotsResource, GetAPIGatewayResource, GetInstanceResource (SWAS), GetVPNConnectionResource, GetBastionhostResource, GetDTSInstanceResource, GetECIContainerGroupResource, GetECIImageCacheResource
collector/alicloud/platform/platform_config.go
Defined constants for new Alibaba Cloud resource types
  • Added ECSImage, ECSSnapshot, VpnConnection, Bastionhost, APIGateway, DTSInstance, ECIContainerGroup, ECIImageCache, EfloCluster, EfloNode, SWAS to constant.go
collector/alicloud/collector/constant.go
Introduced dedicated collector modules for each new resource
  • Created cloudapi/apigateway.go with parallel detail retrieval for APIs
  • Added ecs/images.go, ecs/snapshots.go to list and detail ECS images and snapshots
  • Added collector modules: dts/instance.go, eci/container_group.go, eci/image_cache.go, swas/swas.go, yundun/bastionhost.go, vpc/vpnconnection.go
collector/alicloud/collector/cloudapi/apigateway.go
collector/alicloud/collector/ecs/images.go
collector/alicloud/collector/ecs/snapshots.go
collector/alicloud/collector/dts/instance.go
collector/alicloud/collector/eci/container_group.go
collector/alicloud/collector/eci/image_cache.go
collector/alicloud/collector/swas/swas.go
collector/alicloud/collector/yundun/bastionhost.go
collector/alicloud/collector/vpc/vpnconnection.go
Refined ACK cluster detail logic
  • Merged duplicated Detail type and split describeClusterResources logic
  • Populated AssociatedResource in cluster detail items
collector/alicloud/collector/ack/cluster.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

Copy link

@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 @j3ttt - I've reviewed your changes - here's some feedback:

  • Consolidate the repeated pagination+worker patterns into shared helper functions to reduce duplication and centralize concurrency and error‐handling logic.
  • Replace the growing switch in InitServices with a declarative resource–to–client registry to make it easier to add new resource types (e.g. Eflo) without forgetting to wire them up.
  • Ensure detail fetchers honor context cancellation and close their task channels on error to avoid goroutine leaks or hanging workers.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consolidate the repeated pagination+worker patterns into shared helper functions to reduce duplication and centralize concurrency and error‐handling logic.
- Replace the growing switch in InitServices with a declarative resource–to–client registry to make it easier to add new resource types (e.g. Eflo) without forgetting to wire them up.
- Ensure detail fetchers honor context cancellation and close their task channels on error to avoid goroutine leaks or hanging workers.

## Individual Comments

### Comment 1
<location> `collector/alicloud/collector/eci/container_group.go:55` </location>
<code_context>
+	req.PageNumber = tea.Int32(constant.DefaultPage)
+
+	var count int32 = 0
+	for {
+		resp, err := c.DescribeApis(req)
+		if err != nil {
</code_context>

<issue_to_address>
Potential risk of infinite loop if API returns a non-empty NextToken with no results.

Add a safeguard, such as a maximum iteration count or a check for empty results, to prevent infinite loops in this scenario.
</issue_to_address>

### Comment 2
<location> `collector/alicloud/collector/eci/image_cache.go:55` </location>
<code_context>
+	request := eci.CreateDescribeImageCachesRequest()
+	request.Scheme = "https"
+
+	response, err := cli.DescribeImageCaches(request)
+	if err != nil {
+		log.CtxLogger(ctx).Warn("DescribeImageCaches error", zap.Error(err))
+		return err
+	}
+
+	// 处理镜像缓存
+	for _, ic := range response.ImageCaches {
+		detail := ECIImageCacheDetail{
+			ImageCache: ic,
</code_context>

<issue_to_address>
No pagination handling for image cache listing.

Currently, only the first page of image caches is retrieved. Please add pagination handling to ensure all image caches are processed.
</issue_to_address>

### Comment 3
<location> `collector/alicloud/collector/dts/instance.go:79` </location>
<code_context>
+
+		count += int64(response.PageRecordCount)
+
+		if count >= response.TotalRecordCount || response.PageRecordCount == 0 {
+			break
+		}
</code_context>

<issue_to_address>
Potential for missing jobs if API returns inconsistent page counts.

To prevent missing jobs due to inconsistent API counts, also break the loop if the returned job list is empty.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
		count += int64(response.PageRecordCount)

		if count >= response.TotalRecordCount || response.PageRecordCount == 0 {
			break
		}
=======
		count += int64(response.PageRecordCount)

		if count >= response.TotalRecordCount || response.PageRecordCount == 0 || len(response.MigrationJobs.MigrationJob) == 0 {
			break
		}
>>>>>>> REPLACE

</suggested_fix>

### Comment 4
<location> `collector/alicloud/collector/yundun/bastionhost.go:105` </location>
<code_context>
+		instances = append(instances, resp.Body.Instances...)
+		count += len(resp.Body.Instances)
+
+		if count >= int(*resp.Body.TotalCount) || len(resp.Body.Instances) < constant.DefaultPageSize {
+			break
+		}
</code_context>

<issue_to_address>
Potential nil pointer dereference for resp.Body.TotalCount.

Add a nil check for resp.Body.TotalCount before dereferencing to prevent a potential panic.
</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.

Comment on lines +55 to +64
for {
response, err := cli.DescribeContainerGroups(request)
if err != nil {
log.CtxLogger(ctx).Warn("DescribeContainerGroups error", zap.Error(err))
return err
}

// 处理容器组
for _, cg := range response.ContainerGroups {
detail := ECIContainerGroupDetail{
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): Potential risk of infinite loop if API returns a non-empty NextToken with no results.

Add a safeguard, such as a maximum iteration count or a check for empty results, to prevent infinite loops in this scenario.

Comment on lines +55 to +62
response, err := cli.DescribeImageCaches(request)
if err != nil {
log.CtxLogger(ctx).Warn("DescribeImageCaches error", zap.Error(err))
return err
}

// 处理镜像缓存
for _, ic := range response.ImageCaches {
Copy link

Choose a reason for hiding this comment

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

issue: No pagination handling for image cache listing.

Currently, only the first page of image caches is retrieved. Please add pagination handling to ensure all image caches are processed.

Comment on lines +77 to +81
count += int64(response.PageRecordCount)

if count >= response.TotalRecordCount || response.PageRecordCount == 0 {
break
}
Copy link

Choose a reason for hiding this comment

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

suggestion (bug_risk): Potential for missing jobs if API returns inconsistent page counts.

To prevent missing jobs due to inconsistent API counts, also break the loop if the returned job list is empty.

Suggested change
count += int64(response.PageRecordCount)
if count >= response.TotalRecordCount || response.PageRecordCount == 0 {
break
}
count += int64(response.PageRecordCount)
if count >= response.TotalRecordCount || response.PageRecordCount == 0 || len(response.MigrationJobs.MigrationJob) == 0 {
break
}

instances = append(instances, resp.Body.Instances...)
count += len(resp.Body.Instances)

if count >= int(*resp.Body.TotalCount) || len(resp.Body.Instances) < constant.DefaultPageSize {
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): Potential nil pointer dereference for resp.Body.TotalCount.

Add a nil check for resp.Body.TotalCount before dereferencing to prevent a potential panic.

@tianmu2023 tianmu2023 merged commit 67a6ad1 into antgroup:main Aug 11, 2025
1 check passed
@gemini-code-assist
Copy link
Contributor

Important

Installation incomplete: to start using Gemini Code Assist, please ask the organization owner(s) to visit the Gemini Code Assist Admin Console and sign the Terms of Services.

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