Skip to content

Commit 2207e61

Browse files
xkl401340100bobklxiao
andauthored
feat:users can delete TKE cluster audit logs and event persistence logs (#1442)
* feat: When you delete a TKE cluster, disable the cluster audit log, and disable the cluster event log, you can choose whether to delete the audit and event log sets and topics created by default * fix:TKE cluster event persistence cannot be disabled twice * fix:formatting code * fix:added error retry logic for deleting tke cluster audit logs and deleting tke cluster event persistence logs * fix:When deleting a cluster, you do not need to determine whether the enable value of the cluster persistence log is modified * supplementary unit tests for removing audit logs and event persistence logs for the tke cluster * instead of getChange, use getKey to get the current status Co-authored-by: bobklxiao <bobklxiao@tencent.com>
1 parent 696a161 commit 2207e61

File tree

11 files changed

+198
-14
lines changed

11 files changed

+198
-14
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ require (
7676
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq v1.0.548
7777
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tem v1.0.527
7878
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo v1.0.529
79-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.519
79+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.549
8080
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199
8181
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.515
8282
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.542/go.mod
520520
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.544 h1:ydXS/Tt3NKxTe10SlnMsyzlHzLOtGJIahvf/CoCVtCM=
521521
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.544/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
522522
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.546 h1:18ZUEkte3A9b5OkF2vAe070HYo3dg0Zonp/rw+GvI8Y=
523+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.546 h1:18ZUEkte3A9b5OkF2vAe070HYo3dg0Zonp/rw+GvI8Y=
524+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.546/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
523525
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.546/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
524526
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.547/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
525527
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.548/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
@@ -616,6 +618,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo v1.0.529 h1:vWUgseU
616618
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo v1.0.529/go.mod h1:vOd23iOVeQqm5LSEXUmE8773kiUCwGuoJnTO0po5D+Q=
617619
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.519 h1:o8NsQPLV6T8TD4sHxufCwtCsqYM4CUM1132zut6toww=
618620
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.519/go.mod h1:ydWSzYKc8AwtT9I8Cd0JdICqiyp9h+u2J5maDWxC9Mo=
621+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.549 h1:WWKxOUMmhH5U2VxKtAKp5Hp0RDSxWJUk87pUjI5hmYM=
622+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.549/go.mod h1:UPffPhIo23aJ3N40bwWhxhr0fIR9jn0Lfboxh17km8E=
619623
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199 h1:6Yt74l4pA5QtzhwMNIEUt0spXdSBKH744DDqTHJOCP0=
620624
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199/go.mod h1:Yw6OQ33z3s4k0HVYTNSffB12qOzEJ2Zf1Vj4+5S3sRs=
621625
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.515 h1:OWA3zSqC+tY42DwnlzAZVNYkB5dWgcwk435n+84jXNM=

tencentcloud/internal/helper/helper.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ func InterfacesHeadMap(d *schema.ResourceData, key string) (result map[string]in
146146
return
147147
}
148148

149+
// ConvertInterfacesHeadToMap returns string key map if argument is MaxItem: 1 List Type
150+
func ConvertInterfacesHeadToMap(v interface{}) (result map[string]interface{}, ok bool) {
151+
interfaces, ok := v.([]interface{})
152+
if !ok || len(interfaces) == 0 {
153+
ok = false
154+
return
155+
}
156+
head := interfaces[0]
157+
result, ok = head.(map[string]interface{})
158+
return
159+
}
160+
149161
func SetMapInterfaces(d *schema.ResourceData, key string, values ...map[string]interface{}) error {
150162
val := make([]interface{}, 0, len(values))
151163
for i := range values {

tencentcloud/resource_tc_kubernetes_cluster.go

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,12 @@ func resourceTencentCloudTkeCluster() *schema.Resource {
13211321
Optional: true,
13221322
Description: "Specify id of existing CLS log topic, or auto create a new topic by leave it empty.",
13231323
},
1324+
"delete_event_log_and_topic": {
1325+
Type: schema.TypeBool,
1326+
Optional: true,
1327+
Description: "when you want to close the cluster event persistence or delete the cluster, you can use this parameter to determine " +
1328+
"whether the event persistence log set and topic created by default will be deleted.",
1329+
},
13241330
},
13251331
},
13261332
},
@@ -1346,6 +1352,13 @@ func resourceTencentCloudTkeCluster() *schema.Resource {
13461352
Optional: true,
13471353
Description: "Specify id of existing CLS log topic, or auto create a new topic by leave it empty.",
13481354
},
1355+
"delete_audit_log_and_topic": {
1356+
Type: schema.TypeBool,
1357+
Optional: true,
1358+
Description: "when you want to close the cluster audit log or delete the cluster, you can use " +
1359+
"this parameter to determine whether the audit log set and topic created by default will" +
1360+
" be deleted.",
1361+
},
13491362
},
13501363
},
13511364
},
@@ -2295,7 +2308,7 @@ func resourceTencentCloudTkeClusterCreate(d *schema.ResourceData, meta interface
22952308
logSetId := v["log_set_id"].(string)
22962309
topicId := v["topic_id"].(string)
22972310
if enabled {
2298-
err := service.SwitchEventPersistence(ctx, id, logSetId, topicId, enabled)
2311+
err := service.SwitchEventPersistence(ctx, id, logSetId, topicId, enabled, false)
22992312
if err != nil {
23002313
return err
23012314
}
@@ -2307,7 +2320,7 @@ func resourceTencentCloudTkeClusterCreate(d *schema.ResourceData, meta interface
23072320
logSetId := v["log_set_id"].(string)
23082321
topicId := v["topic_id"].(string)
23092322
if enabled {
2310-
err := service.SwitchClusterAudit(ctx, id, logSetId, topicId, enabled)
2323+
err := service.SwitchClusterAudit(ctx, id, logSetId, topicId, enabled, false)
23112324
if err != nil {
23122325
return err
23132326
}
@@ -2862,12 +2875,15 @@ func resourceTencentCloudTkeClusterUpdate(d *schema.ResourceData, meta interface
28622875
enabled := false
28632876
logSetId := ""
28642877
topicId := ""
2878+
deleteEventLog := false
28652879
if ok {
28662880
enabled = v["enabled"].(bool)
28672881
logSetId = v["log_set_id"].(string)
28682882
topicId = v["topic_id"].(string)
2883+
deleteEventLog = v["delete_event_log_and_topic"].(bool)
28692884
}
2870-
err := tkeService.SwitchEventPersistence(ctx, id, logSetId, topicId, enabled)
2885+
2886+
err := tkeService.SwitchEventPersistence(ctx, id, logSetId, topicId, enabled, deleteEventLog)
28712887
if err != nil {
28722888
return err
28732889
}
@@ -2878,12 +2894,15 @@ func resourceTencentCloudTkeClusterUpdate(d *schema.ResourceData, meta interface
28782894
enabled := false
28792895
logSetId := ""
28802896
topicId := ""
2897+
deleteAuditLog := false
28812898
if ok {
28822899
enabled = v["enabled"].(bool)
28832900
logSetId = v["log_set_id"].(string)
28842901
topicId = v["topic_id"].(string)
2902+
deleteAuditLog = v["delete_audit_log_and_topic"].(bool)
28852903
}
2886-
err := tkeService.SwitchClusterAudit(ctx, id, logSetId, topicId, enabled)
2904+
2905+
err := tkeService.SwitchClusterAudit(ctx, id, logSetId, topicId, enabled, deleteAuditLog)
28872906
if err != nil {
28882907
return err
28892908
}
@@ -2903,8 +2922,40 @@ func resourceTencentCloudTkeClusterDelete(d *schema.ResourceData, meta interface
29032922
logId := getLogId(contextNil)
29042923
ctx := context.WithValue(context.TODO(), logIdKey, logId)
29052924
service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn}
2925+
deleteEventLogSetAndTopic := false
2926+
enableEventLog := false
2927+
deleteAuditLogSetAndTopic := false
2928+
if v, ok := helper.InterfacesHeadMap(d, "event_persistence"); ok {
2929+
deleteEventLogSetAndTopic = v["delete_event_log_and_topic"].(bool)
2930+
// get cluster current enabled status
2931+
enableEventLog = v["enabled"].(bool)
2932+
}
2933+
2934+
if v, ok := helper.InterfacesHeadMap(d, "cluster_audit"); ok {
2935+
deleteAuditLogSetAndTopic = v["delete_audit_log_and_topic"].(bool)
2936+
}
29062937

29072938
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
2939+
if deleteEventLogSetAndTopic && enableEventLog {
2940+
err := service.SwitchEventPersistence(ctx, d.Id(), "", "", false, true)
2941+
if e, ok := err.(*errors.TencentCloudSDKError); ok {
2942+
if e.GetCode() != "FailedOperation.ClusterNotFound" {
2943+
return retryError(err, InternalError)
2944+
}
2945+
} else if err != nil {
2946+
return retryError(err, InternalError)
2947+
}
2948+
}
2949+
if deleteAuditLogSetAndTopic {
2950+
err := service.SwitchClusterAudit(ctx, d.Id(), "", "", false, true)
2951+
if e, ok := err.(*errors.TencentCloudSDKError); ok {
2952+
if e.GetCode() != "ResourceNotFound.ClusterNotFound" {
2953+
return retryError(err, InternalError)
2954+
}
2955+
} else if err != nil {
2956+
return retryError(err, InternalError)
2957+
}
2958+
}
29082959
err := service.DeleteCluster(ctx, d.Id())
29092960

29102961
if e, ok := err.(*errors.TencentCloudSDKError); ok {

tencentcloud/resource_tc_kubernetes_cluster_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func TestAccTencentCloudTkeResourceBasic(t *testing.T) {
5656
CheckDestroy: testAccCheckTkeDestroy,
5757
Steps: []resource.TestStep{
5858
{
59+
//PreventDiskCleanup: true,
5960
Config: testAccTkeCluster,
6061
Check: resource.ComposeTestCheckFunc(
6162
testAccCheckTkeExists(testTkeClusterResourceKey),
@@ -108,6 +109,7 @@ func TestAccTencentCloudTkeResourceLogs(t *testing.T) {
108109
CheckDestroy: testAccCheckTkeDestroy,
109110
Steps: []resource.TestStep{
110111
{
112+
//PreventDiskCleanup: true,
111113
Config: testAccTkeClusterLogs,
112114
Check: resource.ComposeTestCheckFunc(
113115
testAccCheckTkeExists(testTkeClusterResourceKey),
@@ -132,7 +134,11 @@ func TestAccTencentCloudTkeResourceLogs(t *testing.T) {
132134
resource.TestCheckResourceAttr(testTkeClusterResourceKey, "cluster_desc", "test cluster desc"),
133135
resource.TestCheckResourceAttr(testTkeClusterResourceKey, "log_agent.0.enabled", "true"),
134136
resource.TestCheckResourceAttr(testTkeClusterResourceKey, "event_persistence.0.enabled", "false"),
137+
resource.TestCheckResourceAttr(testTkeClusterResourceKey, "event_persistence.0.delete_event_log_and_topic",
138+
"true"),
135139
resource.TestCheckResourceAttr(testTkeClusterResourceKey, "cluster_audit.0.enabled", "true"),
140+
resource.TestCheckResourceAttr(testTkeClusterResourceKey, "cluster_audit.0.delete_audit_log_and_topic",
141+
"true"),
136142
),
137143
},
138144
},
@@ -477,9 +483,11 @@ resource "tencentcloud_kubernetes_cluster" "managed_cluster" {
477483
478484
event_persistence {
479485
enabled = false
486+
delete_event_log_and_topic = true
480487
}
481488
482489
cluster_audit {
483490
enabled = true
491+
delete_audit_log_and_topic = true
484492
}
485493
}`

tencentcloud/service_tencentcloud_tke.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,8 @@ func (me *TkeService) SwitchLogAgent(ctx context.Context, clusterId, rootDir str
15931593
return me.UninstallLogAgent(ctx, request)
15941594
}
15951595

1596-
func (me *TkeService) SwitchEventPersistence(ctx context.Context, clusterId, logSetId, topicId string, enable bool) error {
1596+
func (me *TkeService) SwitchEventPersistence(ctx context.Context, clusterId, logSetId, topicId string,
1597+
enable, deleteEventLog bool) error {
15971598
if enable {
15981599
request := tke.NewEnableEventPersistenceRequest()
15991600
request.ClusterId = &clusterId
@@ -1605,12 +1606,15 @@ func (me *TkeService) SwitchEventPersistence(ctx context.Context, clusterId, log
16051606
}
16061607
return me.EnableEventPersistence(ctx, request)
16071608
}
1609+
16081610
request := tke.NewDisableEventPersistenceRequest()
16091611
request.ClusterId = &clusterId
1612+
request.DeleteLogSetAndTopic = &deleteEventLog
16101613
return me.DisableEventPersistence(ctx, request)
16111614
}
16121615

1613-
func (me *TkeService) SwitchClusterAudit(ctx context.Context, clusterId, logSetId, topicId string, enable bool) error {
1616+
func (me *TkeService) SwitchClusterAudit(ctx context.Context, clusterId, logSetId, topicId string,
1617+
enable, deleteAuditLog bool) error {
16141618
if enable {
16151619
request := tke.NewEnableClusterAuditRequest()
16161620
request.ClusterId = &clusterId
@@ -1624,6 +1628,7 @@ func (me *TkeService) SwitchClusterAudit(ctx context.Context, clusterId, logSetI
16241628
}
16251629
request := tke.NewDisableClusterAuditRequest()
16261630
request.ClusterId = &clusterId
1631+
request.DeleteLogSetAndTopic = &deleteAuditLog
16271632
return me.DisableClusterAudit(ctx, request)
16281633
}
16291634

0 commit comments

Comments
 (0)