Skip to content

Commit 9e0e061

Browse files
tongyimingmikatong
and
mikatong
authored
feat(tke): [118573480]add tencentcloud_kubernetes_addon_config (#2725)
* add tencentcloud_kubernetes_addon_config * update doc * add changelog * update --------- Co-authored-by: mikatong <[email protected]>
1 parent 6fc0f1f commit 9e0e061

8 files changed

+313
-0
lines changed

.changelog/2725.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_kubernetes_addon_config
3+
```

tencentcloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,6 +1978,7 @@ func Provider() *schema.Provider {
19781978
"tencentcloud_organization_org_share_unit_member": tco.ResourceTencentCloudOrganizationOrgShareUnitMember(),
19791979
"tencentcloud_organization_org_share_unit": tco.ResourceTencentCloudOrganizationOrgShareUnit(),
19801980
"tencentcloud_kubernetes_addon": tke.ResourceTencentCloudKubernetesAddon(),
1981+
"tencentcloud_kubernetes_addon_config": tke.ResourceTencentCloudKubernetesAddonConfig(),
19811982
"tencentcloud_kubernetes_native_node_pool": tke.ResourceTencentCloudKubernetesNativeNodePool()},
19821983

19831984
ConfigureFunc: providerConfigure,

tencentcloud/provider.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,7 @@ Tencent Kubernetes Engine(TKE)
671671
tencentcloud_kubernetes_addon_attachment
672672
tencentcloud_kubernetes_cluster_endpoint
673673
tencentcloud_kubernetes_addon
674+
tencentcloud_kubernetes_addon_config
674675
tencentcloud_kubernetes_native_node_pool
675676

676677
TDMQ for Pulsar(tpulsar)
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
package tke
2+
3+
import (
4+
"context"
5+
"encoding/base64"
6+
"fmt"
7+
"log"
8+
"strings"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525"
13+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
14+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
15+
)
16+
17+
func ResourceTencentCloudKubernetesAddonConfig() *schema.Resource {
18+
return &schema.Resource{
19+
Create: resourceTencentCloudKubernetesAddonConfigCreate,
20+
Read: resourceTencentCloudKubernetesAddonConfigRead,
21+
Update: resourceTencentCloudKubernetesAddonConfigUpdate,
22+
Delete: resourceTencentCloudKubernetesAddonConfigDelete,
23+
Schema: map[string]*schema.Schema{
24+
"cluster_id": {
25+
Type: schema.TypeString,
26+
Required: true,
27+
ForceNew: true,
28+
Description: "ID of cluster.",
29+
},
30+
31+
"addon_name": {
32+
Type: schema.TypeString,
33+
Required: true,
34+
ForceNew: true,
35+
Description: "Name of addon.",
36+
},
37+
38+
"addon_version": {
39+
Type: schema.TypeString,
40+
Optional: true,
41+
Computed: true,
42+
Description: "Version of addon.",
43+
},
44+
45+
"raw_values": {
46+
Type: schema.TypeString,
47+
Optional: true,
48+
Computed: true,
49+
Description: "Params of addon, base64 encoded json format.",
50+
},
51+
52+
"phase": {
53+
Type: schema.TypeString,
54+
Computed: true,
55+
Description: "Status of addon.",
56+
},
57+
58+
"reason": {
59+
Type: schema.TypeString,
60+
Computed: true,
61+
Description: "Reason of addon failed.",
62+
},
63+
},
64+
}
65+
}
66+
67+
func resourceTencentCloudKubernetesAddonConfigCreate(d *schema.ResourceData, meta interface{}) error {
68+
defer tccommon.LogElapsed("resource.tencentcloud_kubernetes_addon_config.create")()
69+
defer tccommon.InconsistentCheck(d, meta)()
70+
71+
clusterId := d.Get("cluster_id").(string)
72+
addonName := d.Get("addon_name").(string)
73+
74+
d.SetId(clusterId + tccommon.FILED_SP + addonName)
75+
return resourceTencentCloudKubernetesAddonConfigUpdate(d, meta)
76+
}
77+
78+
func resourceTencentCloudKubernetesAddonConfigRead(d *schema.ResourceData, meta interface{}) error {
79+
defer tccommon.LogElapsed("resource.tencentcloud_kubernetes_addon_config.read")()
80+
defer tccommon.InconsistentCheck(d, meta)()
81+
82+
logId := tccommon.GetLogId(tccommon.ContextNil)
83+
84+
ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
85+
86+
service := TkeService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
87+
88+
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
89+
if len(idSplit) != 2 {
90+
return fmt.Errorf("id is broken,%s", d.Id())
91+
}
92+
clusterId := idSplit[0]
93+
addonName := idSplit[1]
94+
95+
_ = d.Set("cluster_id", clusterId)
96+
_ = d.Set("addon_name", addonName)
97+
98+
respData, err := service.DescribeKubernetesAddonById(ctx, clusterId, addonName)
99+
if err != nil {
100+
return err
101+
}
102+
103+
if respData == nil {
104+
d.SetId("")
105+
log.Printf("[WARN]%s resource `kubernetes_addon_config` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
106+
return nil
107+
}
108+
109+
if respData.AddonVersion != nil {
110+
_ = d.Set("addon_version", respData.AddonVersion)
111+
}
112+
113+
if respData.RawValues != nil {
114+
rawValues := respData.RawValues
115+
base64DecodeValues, _ := base64.StdEncoding.DecodeString(*rawValues)
116+
jsonValues := string(base64DecodeValues)
117+
_ = d.Set("raw_values", jsonValues)
118+
}
119+
120+
if respData.Phase != nil {
121+
_ = d.Set("phase", respData.Phase)
122+
}
123+
124+
if respData.Reason != nil {
125+
_ = d.Set("reason", respData.Reason)
126+
}
127+
128+
_ = addonName
129+
return nil
130+
}
131+
132+
func resourceTencentCloudKubernetesAddonConfigUpdate(d *schema.ResourceData, meta interface{}) error {
133+
defer tccommon.LogElapsed("resource.tencentcloud_kubernetes_addon_config.update")()
134+
defer tccommon.InconsistentCheck(d, meta)()
135+
136+
logId := tccommon.GetLogId(tccommon.ContextNil)
137+
138+
ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
139+
140+
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
141+
if len(idSplit) != 2 {
142+
return fmt.Errorf("id is broken,%s", d.Id())
143+
}
144+
clusterId := idSplit[0]
145+
addonName := idSplit[1]
146+
147+
needChange := false
148+
mutableArgs := []string{"addon_version", "raw_values"}
149+
for _, v := range mutableArgs {
150+
if d.HasChange(v) {
151+
needChange = true
152+
break
153+
}
154+
}
155+
156+
if needChange {
157+
request := tke.NewUpdateAddonRequest()
158+
159+
request.ClusterId = &clusterId
160+
request.AddonName = &addonName
161+
162+
if v, ok := d.GetOk("addon_version"); ok {
163+
request.AddonVersion = helper.String(v.(string))
164+
}
165+
166+
if v, ok := d.GetOk("raw_values"); ok {
167+
jsonValues := helper.String(v.(string))
168+
rawValues := base64.StdEncoding.EncodeToString([]byte(*jsonValues))
169+
request.RawValues = &rawValues
170+
}
171+
172+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
173+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTkeClient().UpdateAddonWithContext(ctx, request)
174+
if e != nil {
175+
return tccommon.RetryError(e)
176+
} else {
177+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
178+
}
179+
return nil
180+
})
181+
if err != nil {
182+
log.Printf("[CRITAL]%s update kubernetes addon failed, reason:%+v", logId, err)
183+
return err
184+
}
185+
}
186+
187+
_ = addonName
188+
return resourceTencentCloudKubernetesAddonConfigRead(d, meta)
189+
}
190+
191+
func resourceTencentCloudKubernetesAddonConfigDelete(d *schema.ResourceData, meta interface{}) error {
192+
defer tccommon.LogElapsed("resource.tencentcloud_kubernetes_addon_config.delete")()
193+
defer tccommon.InconsistentCheck(d, meta)()
194+
195+
return nil
196+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Provide a resource to configure addon that kubernetes comes with.
2+
3+
Example Usage
4+
5+
Update cluster-autoscaler addon
6+
7+
```hcl
8+
9+
resource "tencentcloud_kubernetes_addon_config" "kubernetes_addon_config" {
10+
cluster_id = "cls-xxxxxx"
11+
addon_name = "cluster-autoscaler"
12+
raw_values = "{\"extraArgs\":{\"scale-down-enabled\":true,\"max-empty-bulk-delete\":11,\"scale-down-delay-after-add\":\"10mm\",\"scale-down-unneeded-time\":\"10mm\",\"scale-down-utilization-threshold\":0.005,\"ignore-daemonsets-utilization\":false,\"skip-nodes-with-local-storage\":true,\"skip-nodes-with-system-pods\":true}}"
13+
}
14+
`
15+
16+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package tke_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
8+
)
9+
10+
func TestAccTencentCloudKubernetesAddonConfigResource_basic(t *testing.T) {
11+
t.Parallel()
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() {
14+
tcacctest.AccPreCheck(t)
15+
},
16+
Providers: tcacctest.AccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccKubernetesAddonConfig,
20+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_kubernetes_addon_config.kubernetes_addon_config", "id"),
21+
resource.TestCheckResourceAttr("tencentcloud_kubernetes_addon_config.kubernetes_addon_config", "addon_name", "cluster-autoscaler"),
22+
resource.TestCheckResourceAttr("tencentcloud_kubernetes_addon_config.kubernetes_addon_config", "raw_values", "{\"extraArgs\":{\"scale-down-enabled\":true,\"max-empty-bulk-delete\":11,\"scale-down-delay-after-add\":\"10mm\",\"scale-down-unneeded-time\":\"10mm\",\"scale-down-utilization-threshold\":0.005,\"ignore-daemonsets-utilization\":false,\"skip-nodes-with-local-storage\":true,\"skip-nodes-with-system-pods\":true}}"),
23+
),
24+
},
25+
{
26+
Config: testAccKubernetesAddonConfigUpdate,
27+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_kubernetes_addon_config.kubernetes_addon_config", "id"),
28+
resource.TestCheckResourceAttr("tencentcloud_kubernetes_addon_config.kubernetes_addon_config", "addon_name", "cluster-autoscaler"),
29+
resource.TestCheckResourceAttr("tencentcloud_kubernetes_addon_config.kubernetes_addon_config", "raw_values", "{\"extraArgs\":{\"scale-down-enabled\":false,\"max-empty-bulk-delete\":10,\"scale-down-delay-after-add\":\"10mm\",\"scale-down-unneeded-time\":\"10mm\",\"scale-down-utilization-threshold\":0.005,\"ignore-daemonsets-utilization\":false,\"skip-nodes-with-local-storage\":true,\"skip-nodes-with-system-pods\":true}}"),
30+
),
31+
},
32+
},
33+
})
34+
}
35+
36+
const testAccKubernetesAddonConfig = `
37+
resource "tencentcloud_kubernetes_addon_config" "kubernetes_addon_config" {
38+
cluster_id = "cls-bzoq8t02"
39+
addon_name = "cluster-autoscaler"
40+
raw_values = "{\"extraArgs\":{\"scale-down-enabled\":true,\"max-empty-bulk-delete\":11,\"scale-down-delay-after-add\":\"10mm\",\"scale-down-unneeded-time\":\"10mm\",\"scale-down-utilization-threshold\":0.005,\"ignore-daemonsets-utilization\":false,\"skip-nodes-with-local-storage\":true,\"skip-nodes-with-system-pods\":true}}"
41+
}
42+
`
43+
const testAccKubernetesAddonConfigUpdate = `
44+
resource "tencentcloud_kubernetes_addon_config" "kubernetes_addon_config" {
45+
cluster_id = "cls-bzoq8t02"
46+
addon_name = "cluster-autoscaler"
47+
raw_values = "{\"extraArgs\":{\"scale-down-enabled\":false,\"max-empty-bulk-delete\":10,\"scale-down-delay-after-add\":\"10mm\",\"scale-down-unneeded-time\":\"10mm\",\"scale-down-utilization-threshold\":0.005,\"ignore-daemonsets-utilization\":false,\"skip-nodes-with-local-storage\":true,\"skip-nodes-with-system-pods\":true}}"
48+
}
49+
`
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
subcategory: "Tencent Kubernetes Engine(TKE)"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_kubernetes_addon_config"
5+
sidebar_current: "docs-tencentcloud-resource-kubernetes_addon_config"
6+
description: |-
7+
Provide a resource to configure addon that kubernetes comes with.
8+
---
9+
10+
# tencentcloud_kubernetes_addon_config
11+
12+
Provide a resource to configure addon that kubernetes comes with.
13+
14+
## Example Usage
15+
16+
### Update cluster-autoscaler addon
17+
18+
```hcl
19+
resource "tencentcloud_kubernetes_addon_config" "kubernetes_addon_config" {
20+
cluster_id = "cls-xxxxxx"
21+
addon_name = "cluster-autoscaler"
22+
raw_values = "{\"extraArgs\":{\"scale-down-enabled\":true,\"max-empty-bulk-delete\":11,\"scale-down-delay-after-add\":\"10mm\",\"scale-down-unneeded-time\":\"10mm\",\"scale-down-utilization-threshold\":0.005,\"ignore-daemonsets-utilization\":false,\"skip-nodes-with-local-storage\":true,\"skip-nodes-with-system-pods\":true}}"
23+
}
24+
`
25+
```
26+
27+
## Argument Reference
28+
29+
The following arguments are supported:
30+
31+
* `addon_name` - (Required, String, ForceNew) Name of addon.
32+
* `cluster_id` - (Required, String, ForceNew) ID of cluster.
33+
* `addon_version` - (Optional, String) Version of addon.
34+
* `raw_values` - (Optional, String) Params of addon, base64 encoded json format.
35+
36+
## Attributes Reference
37+
38+
In addition to all arguments above, the following attributes are exported:
39+
40+
* `id` - ID of the resource.
41+
* `phase` - Status of addon.
42+
* `reason` - Reason of addon failed.
43+
44+

website/tencentcloud.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4583,6 +4583,9 @@
45834583
<li>
45844584
<a href="/docs/providers/tencentcloud/r/kubernetes_addon_attachment.html">tencentcloud_kubernetes_addon_attachment</a>
45854585
</li>
4586+
<li>
4587+
<a href="/docs/providers/tencentcloud/r/kubernetes_addon_config.html">tencentcloud_kubernetes_addon_config</a>
4588+
</li>
45864589
<li>
45874590
<a href="/docs/providers/tencentcloud/r/kubernetes_auth_attachment.html">tencentcloud_kubernetes_auth_attachment</a>
45884591
</li>

0 commit comments

Comments
 (0)