Skip to content

feat(tke): [116471075]optimize cluster instance queries #2560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2560.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
datasource/tencentcloud_kubernetes_cluster_instances: optimize cluster instance queries
```
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestAccTencentCloudKubernetesClusterInstancesDataSource_basic(t *testing.T)
Check: resource.ComposeTestCheckFunc(
tcacctest.AccCheckTencentCloudDataSourceID("data.tencentcloud_kubernetes_cluster_instances.cluster_instances"),
resource.TestCheckResourceAttr("data.tencentcloud_kubernetes_cluster_instances.cluster_instances", "instance_set.#", "1"),
resource.TestCheckResourceAttr("data.tencentcloud_kubernetes_cluster_instances.cluster_instances", "instance_set.0.instance_id", "ins-kqmx8dm2"),
resource.TestCheckResourceAttr("data.tencentcloud_kubernetes_cluster_instances.cluster_instances", "instance_set.0.instance_id", "ins-1fb82v28"),
resource.TestCheckResourceAttr("data.tencentcloud_kubernetes_cluster_instances.cluster_instances", "instance_set.0.instance_role", "WORKER"),
resource.TestCheckResourceAttr("data.tencentcloud_kubernetes_cluster_instances.cluster_instances", "instance_set.0.instance_state", "running"),
),
Expand All @@ -30,7 +30,7 @@ func TestAccTencentCloudKubernetesClusterInstancesDataSource_basic(t *testing.T)
Config: testAccKubernetesClusterInstancesDataSourceFilter,
Check: resource.ComposeTestCheckFunc(
tcacctest.AccCheckTencentCloudDataSourceID("data.tencentcloud_kubernetes_cluster_instances.cluster_instances"),
resource.TestCheckResourceAttr("data.tencentcloud_kubernetes_cluster_instances.cluster_instances", "instance_set.#", "0"),
resource.TestCheckResourceAttr("data.tencentcloud_kubernetes_cluster_instances.cluster_instances", "instance_set.#", "1"),
),
},
},
Expand All @@ -39,20 +39,20 @@ func TestAccTencentCloudKubernetesClusterInstancesDataSource_basic(t *testing.T)

const testAccKubernetesClusterInstancesDataSource = `
data "tencentcloud_kubernetes_cluster_instances" "cluster_instances" {
cluster_id = "cls-ely08ic4"
instance_ids = ["ins-kqmx8dm2"]
cluster_id = "cls-6l3thsra"
instance_ids = ["ins-1fb82v28"]
instance_role = "WORKER"
}
`

const testAccKubernetesClusterInstancesDataSourceFilter = `
data "tencentcloud_kubernetes_cluster_instances" "cluster_instances" {
cluster_id = "cls-ely08ic4"
instance_ids = ["ins-kqmx8dm2"]
cluster_id = "cls-6l3thsra"
instance_ids = ["ins-qzkwjklk"]
instance_role = "WORKER"
filters {
name = "nodepool-id"
values = ["np-p4e6whqu"]
values = ["np-qrez0ayk"]
}
}
`
40 changes: 29 additions & 11 deletions tencentcloud/services/tke/service_tencentcloud_tke.go
Original file line number Diff line number Diff line change
Expand Up @@ -2716,20 +2716,38 @@ func (me *TkeService) DescribeKubernetesClusterInstancesByFilter(ctx context.Con
}
}

ratelimit.Check(request.GetAction())
var offset int64 = 0
var limit int64 = 20
var total int64 = -1

response, err := me.client.UseTkeClient().DescribeClusterInstances(request)
if err != nil {
errRet = err
return
}
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
for {
if total >= 0 && offset >= total {
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里为啥需要判断 total >=0 呢

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

total >=0说明存在数据,然后判断offset >= total以结束循环

break
}
request.Offset = &offset
request.Limit = &limit
ratelimit.Check(request.GetAction())

if len(response.Response.InstanceSet) < 1 {
return
}
response, err := me.client.UseTkeClient().DescribeClusterInstances(request)
if err != nil {
errRet = err
return
}
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())

if total < 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里为啥需要判断 total < 0 呢

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

获取所有数据数量存入total

Copy link
Collaborator

Choose a reason for hiding this comment

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

去掉 if total < 0 { 这段会有问题吗

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

如果去掉,每次都会给total赋值,加上判断只会在第一次查询后赋值,当然去掉也没问题

total = int64(*response.Response.TotalCount)
}

clusterInstances = response.Response.InstanceSet
if len(response.Response.InstanceSet) == 0 {
// get empty set, we're done
break
}

offset += limit

clusterInstances = append(clusterInstances, response.Response.InstanceSet...)
}
return
}

Expand Down