Skip to content

Commit ff30190

Browse files
authored
Merge pull request #1767 from tencentcloudstack/feat/support_tke_auth
add tke cluster auth query
2 parents 4708d89 + c970230 commit ff30190

7 files changed

+587
-314
lines changed

.changelog/1767.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-data-source
2+
tencentcloud_kubernetes_cluster_authentication_options
3+
```
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
Use this data source to query detailed information of kubernetes cluster_authentication_options
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_kubernetes_cluster_authentication_options" "cluster_authentication_options" {
8+
cluster_id = "cls-kzilgv5m"
9+
}
10+
```
11+
*/
12+
package tencentcloud
13+
14+
import (
15+
"context"
16+
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
18+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
19+
kubernetes "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525"
20+
)
21+
22+
func dataSourceTencentCloudKubernetesClusterAuthenticationOptions() *schema.Resource {
23+
return &schema.Resource{
24+
Read: dataSourceTencentCloudKubernetesClusterAuthenticationOptionsRead,
25+
Schema: map[string]*schema.Schema{
26+
"cluster_id": {
27+
Required: true,
28+
Type: schema.TypeString,
29+
Description: "Cluster ID.",
30+
},
31+
32+
"service_accounts": {
33+
Computed: true,
34+
Type: schema.TypeList,
35+
Description: "ServiceAccount authentication configuration. Note: this field may return `null`, indicating that no valid values can be obtained.",
36+
Elem: &schema.Resource{
37+
Schema: map[string]*schema.Schema{
38+
"use_tke_default": {
39+
Type: schema.TypeBool,
40+
Computed: true,
41+
Description: "Use TKE default issuer and jwksuri. Note: This field may return `null`, indicating that no valid values can be obtained.",
42+
},
43+
"issuer": {
44+
Type: schema.TypeString,
45+
Computed: true,
46+
Description: "service-account-issuer. Note: this field may return `null`, indicating that no valid values can be obtained.",
47+
},
48+
"jwks_uri": {
49+
Type: schema.TypeString,
50+
Computed: true,
51+
Description: "service-account-jwks-uri. Note: this field may return `null`, indicating that no valid values can be obtained.",
52+
},
53+
"auto_create_discovery_anonymous_auth": {
54+
Type: schema.TypeBool,
55+
Computed: true,
56+
Description: "If it is set to `true`, a RABC rule is automatically created to allow anonymous users to access `/.well-known/openid-configuration` and `/openid/v1/jwks`. Note: this field may return `null`, indicating that no valid values can be obtained.",
57+
},
58+
},
59+
},
60+
},
61+
62+
"latest_operation_state": {
63+
Computed: true,
64+
Type: schema.TypeString,
65+
Description: "Result of the last modification. Values: `Updating`, `Success`, `Failed` or `TimeOut`. Note: this field may return `null`, indicating that no valid values can be obtained.",
66+
},
67+
68+
"oidc_config": {
69+
Computed: true,
70+
Type: schema.TypeList,
71+
Description: "OIDC authentication configurations. Note: This field may return `null`, indicating that no valid value can be obtained.",
72+
Elem: &schema.Resource{
73+
Schema: map[string]*schema.Schema{
74+
"auto_create_oidc_config": {
75+
Type: schema.TypeBool,
76+
Computed: true,
77+
Description: "Creating an identity provider. Note: This field may return `null`, indicating that no valid value can be obtained.",
78+
},
79+
"auto_create_client_id": {
80+
Type: schema.TypeSet,
81+
Elem: &schema.Schema{
82+
Type: schema.TypeString,
83+
},
84+
Computed: true,
85+
Description: "Creating ClientId of the identity provider. Note: This field may return `null`, indicating that no valid value can be obtained.",
86+
},
87+
"auto_install_pod_identity_webhook_addon": {
88+
Type: schema.TypeBool,
89+
Computed: true,
90+
Description: "Creating the PodIdentityWebhook component. Note: This field may return `null`, indicating that no valid value can be obtained.",
91+
},
92+
},
93+
},
94+
},
95+
96+
"result_output_file": {
97+
Type: schema.TypeString,
98+
Optional: true,
99+
Description: "Used to save results.",
100+
},
101+
},
102+
}
103+
}
104+
105+
func dataSourceTencentCloudKubernetesClusterAuthenticationOptionsRead(d *schema.ResourceData, meta interface{}) error {
106+
defer logElapsed("data_source.tencentcloud_kubernetes_cluster_authentication_options.read")()
107+
defer inconsistentCheck(d, meta)()
108+
109+
logId := getLogId(contextNil)
110+
111+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
112+
113+
clusterId := d.Get("cluster_id").(string)
114+
115+
service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn}
116+
117+
var (
118+
options *kubernetes.ServiceAccountAuthenticationOptions
119+
oidcConfig *kubernetes.OIDCConfigAuthenticationOptions
120+
state string
121+
e error
122+
)
123+
124+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
125+
options, state, oidcConfig, e = service.DescribeClusterAuthenticationOptions(ctx, clusterId)
126+
if e != nil {
127+
return retryError(e)
128+
}
129+
return nil
130+
})
131+
if err != nil {
132+
return err
133+
}
134+
135+
tmpList := make([]map[string]interface{}, 0)
136+
137+
if options != nil {
138+
serviceAccountAuthenticationOptionsMap := map[string]interface{}{}
139+
140+
if options.UseTKEDefault != nil {
141+
serviceAccountAuthenticationOptionsMap["use_tke_default"] = options.UseTKEDefault
142+
}
143+
144+
if options.Issuer != nil {
145+
serviceAccountAuthenticationOptionsMap["issuer"] = options.Issuer
146+
}
147+
148+
if options.JWKSURI != nil {
149+
serviceAccountAuthenticationOptionsMap["jwks_uri"] = options.JWKSURI
150+
}
151+
152+
if options.AutoCreateDiscoveryAnonymousAuth != nil {
153+
serviceAccountAuthenticationOptionsMap["auto_create_discovery_anonymous_auth"] = options.AutoCreateDiscoveryAnonymousAuth
154+
}
155+
tmpList = append(tmpList, serviceAccountAuthenticationOptionsMap)
156+
_ = d.Set("service_accounts", []interface{}{serviceAccountAuthenticationOptionsMap})
157+
}
158+
159+
if state != "" {
160+
_ = d.Set("latest_operation_state", state)
161+
}
162+
163+
if oidcConfig != nil {
164+
oIDCConfigAuthenticationOptionsMap := map[string]interface{}{}
165+
166+
if oidcConfig.AutoCreateOIDCConfig != nil {
167+
oIDCConfigAuthenticationOptionsMap["auto_create_oidc_config"] = oidcConfig.AutoCreateOIDCConfig
168+
}
169+
170+
if oidcConfig.AutoCreateClientId != nil {
171+
oIDCConfigAuthenticationOptionsMap["auto_create_client_id"] = oidcConfig.AutoCreateClientId
172+
}
173+
174+
if oidcConfig.AutoInstallPodIdentityWebhookAddon != nil {
175+
oIDCConfigAuthenticationOptionsMap["auto_install_pod_identity_webhook_addon"] = oidcConfig.AutoInstallPodIdentityWebhookAddon
176+
}
177+
tmpList = append(tmpList, oIDCConfigAuthenticationOptionsMap)
178+
_ = d.Set("oidc_config", []interface{}{oIDCConfigAuthenticationOptionsMap})
179+
}
180+
181+
d.SetId(clusterId)
182+
output, ok := d.GetOk("result_output_file")
183+
if ok && output.(string) != "" {
184+
if e := writeToFile(output.(string), tmpList); e != nil {
185+
return e
186+
}
187+
}
188+
return nil
189+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudKubernetesClusterAuthenticationOptionsDataSource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccKubernetesClusterAuthenticationOptionsDataSource,
19+
Check: resource.ComposeTestCheckFunc(testAccCheckTencentCloudDataSourceID("data.tencentcloud_kubernetes_cluster_authentication_options.cluster_authentication_options")),
20+
},
21+
},
22+
})
23+
}
24+
25+
const testAccKubernetesClusterAuthenticationOptionsDataSource = `
26+
27+
data "tencentcloud_kubernetes_cluster_authentication_options" "cluster_authentication_options" {
28+
cluster_id = "cls-kzilgv5m"
29+
}
30+
31+
`

0 commit comments

Comments
 (0)