|
| 1 | +/** |
| 2 | +Provide a resource to configure kubernetes cluster authentication info. |
| 3 | +
|
| 4 | +~> **NOTE:** Only avaliable for cluster version >= 1.20 |
| 5 | +
|
| 6 | +Example Usage |
| 7 | +
|
| 8 | +```hcl |
| 9 | +variable "availability_zone" { |
| 10 | + default = "ap-guangzhou-3" |
| 11 | +} |
| 12 | +
|
| 13 | +variable "cluster_cidr" { |
| 14 | + default = "172.16.0.0/16" |
| 15 | +} |
| 16 | +
|
| 17 | +variable "default_instance_type" { |
| 18 | + default = "S1.SMALL1" |
| 19 | +} |
| 20 | +
|
| 21 | +data "tencentcloud_images" "default" { |
| 22 | + image_type = ["PUBLIC_IMAGE"] |
| 23 | + os_name = "centos" |
| 24 | +} |
| 25 | +
|
| 26 | +data "tencentcloud_vpc_subnets" "vpc" { |
| 27 | + is_default = true |
| 28 | + availability_zone = var.availability_zone |
| 29 | +} |
| 30 | +
|
| 31 | +resource "tencentcloud_kubernetes_cluster" "managed_cluster" { |
| 32 | + vpc_id = data.tencentcloud_vpc_subnets.vpc.instance_list.0.vpc_id |
| 33 | + cluster_cidr = "10.31.0.0/16" |
| 34 | + cluster_max_pod_num = 32 |
| 35 | + cluster_name = "keep" |
| 36 | + cluster_desc = "test cluster desc" |
| 37 | + cluster_version = "1.20.6" |
| 38 | + cluster_max_service_num = 32 |
| 39 | +
|
| 40 | + worker_config { |
| 41 | + count = 1 |
| 42 | + availability_zone = var.availability_zone |
| 43 | + instance_type = var.default_instance_type |
| 44 | + system_disk_type = "CLOUD_SSD" |
| 45 | + system_disk_size = 60 |
| 46 | + internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR" |
| 47 | + internet_max_bandwidth_out = 100 |
| 48 | + public_ip_assigned = true |
| 49 | + subnet_id = data.tencentcloud_vpc_subnets.vpc.instance_list.0.subnet_id |
| 50 | +
|
| 51 | + data_disk { |
| 52 | + disk_type = "CLOUD_PREMIUM" |
| 53 | + disk_size = 50 |
| 54 | + } |
| 55 | +
|
| 56 | + enhanced_security_service = false |
| 57 | + enhanced_monitor_service = false |
| 58 | + user_data = "dGVzdA==" |
| 59 | + password = "ZZXXccvv1212" |
| 60 | + } |
| 61 | +
|
| 62 | + cluster_deploy_type = "MANAGED_CLUSTER" |
| 63 | +} |
| 64 | +
|
| 65 | +resource "tencentcloud_kubernetes_auth_attachment" "test_auth_attach" { |
| 66 | + cluster_id = tencentcloud_kubernetes_cluster.managed_cluster.id |
| 67 | + jwks_uri = "https://${tencentcloud_kubernetes_cluster.managed_cluster.id}.ccs.tencent-cloud.com/openid/v1/jwks" |
| 68 | + issuer = "https://${tencentcloud_kubernetes_cluster.managed_cluster.id}.ccs.tencent-cloud.com" |
| 69 | + auto_create_discovery_anonymous_auth = true |
| 70 | +} |
| 71 | +``` |
| 72 | +*/ |
| 73 | +package tencentcloud |
| 74 | + |
| 75 | +import ( |
| 76 | + "context" |
| 77 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 78 | + tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525" |
| 79 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 80 | +) |
| 81 | + |
| 82 | +func resourceTencentCloudTKEAuthAttachment() *schema.Resource { |
| 83 | + return &schema.Resource{ |
| 84 | + Schema: map[string]*schema.Schema{ |
| 85 | + "cluster_id": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Required: true, |
| 88 | + Description: "ID of clusters.", |
| 89 | + }, |
| 90 | + "issuer": { |
| 91 | + Type: schema.TypeString, |
| 92 | + Required: true, |
| 93 | + Description: "Specify service-account-issuer.", |
| 94 | + }, |
| 95 | + "jwks_uri": { |
| 96 | + Type: schema.TypeString, |
| 97 | + Optional: true, |
| 98 | + Description: "Specify service-account-jwks-uri.", |
| 99 | + }, |
| 100 | + "auto_create_discovery_anonymous_auth": { |
| 101 | + Type: schema.TypeBool, |
| 102 | + Optional: true, |
| 103 | + Default: false, |
| 104 | + Description: "If set to `true`, the rbac rule will be created automatically which allow anonymous user to access '/.well-known/openid-configuration' and '/openid/v1/jwks'.", |
| 105 | + }, |
| 106 | + }, |
| 107 | + Create: resourceTencentCloudTKEAuthAttachmentCreate, |
| 108 | + Update: resourceTencentCloudTKEAuthAttachmentUpdate, |
| 109 | + Read: resourceTencentCloudTKEAuthAttachmentRead, |
| 110 | + Delete: resourceTencentCloudTKEAuthAttachmentDelete, |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func resourceTencentCloudTKEAuthAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 115 | + defer logElapsed("resource.resource_tc_kubernetes_auth_attachment.create")() |
| 116 | + logId := getLogId(contextNil) |
| 117 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 118 | + id := d.Get("cluster_id").(string) |
| 119 | + |
| 120 | + service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 121 | + request := tke.NewModifyClusterAuthenticationOptionsRequest() |
| 122 | + request.ClusterId = &id |
| 123 | + request.ServiceAccounts = &tke.ServiceAccountAuthenticationOptions{ |
| 124 | + Issuer: helper.String(d.Get("issuer").(string)), |
| 125 | + } |
| 126 | + |
| 127 | + if v, ok := d.GetOk("jwks_uri"); ok { |
| 128 | + request.ServiceAccounts.JWKSURI = helper.String(v.(string)) |
| 129 | + } |
| 130 | + |
| 131 | + if v, ok := d.GetOk("auto_create_discovery_anonymous_auth"); ok { |
| 132 | + request.ServiceAccounts.AutoCreateDiscoveryAnonymousAuth = helper.Bool(v.(bool)) |
| 133 | + } |
| 134 | + |
| 135 | + if err := service.ModifyClusterAuthenticationOptions(ctx, request); err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + |
| 139 | + d.SetId(id) |
| 140 | + return resourceTencentCloudTKEAuthAttachmentRead(d, meta) |
| 141 | +} |
| 142 | +func resourceTencentCloudTKEAuthAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 143 | + defer logElapsed("resource.resource_tc_kubernetes_auth_attachment.read")() |
| 144 | + logId := getLogId(contextNil) |
| 145 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 146 | + |
| 147 | + id := d.Id() |
| 148 | + |
| 149 | + service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 150 | + info, err := service.WaitForAuthenticationOptionsUpdateSuccess(ctx, id) |
| 151 | + |
| 152 | + if err != nil { |
| 153 | + d.SetId("") |
| 154 | + return err |
| 155 | + } |
| 156 | + |
| 157 | + d.SetId(id) |
| 158 | + |
| 159 | + _ = d.Set("jwks_uri", info.JWKSURI) |
| 160 | + _ = d.Set("issuer", info.Issuer) |
| 161 | + |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +func resourceTencentCloudTKEAuthAttachmentUpdate(d *schema.ResourceData, meta interface{}) error { |
| 166 | + defer logElapsed("resource.resource_tc_kubernetes_auth_attachment.update")() |
| 167 | + logId := getLogId(contextNil) |
| 168 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 169 | + |
| 170 | + id := d.Id() |
| 171 | + |
| 172 | + service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 173 | + request := tke.NewModifyClusterAuthenticationOptionsRequest() |
| 174 | + request.ClusterId = &id |
| 175 | + request.ServiceAccounts = &tke.ServiceAccountAuthenticationOptions{} |
| 176 | + |
| 177 | + if d.HasChange("jwks_uri") { |
| 178 | + request.ServiceAccounts.JWKSURI = helper.String(d.Get("jwks_uri").(string)) |
| 179 | + } |
| 180 | + if d.HasChange("issuer") { |
| 181 | + issuer := d.Get("issuer").(string) |
| 182 | + request.ServiceAccounts.Issuer = helper.String(issuer) |
| 183 | + } |
| 184 | + |
| 185 | + if err := service.ModifyClusterAuthenticationOptions(ctx, request); err != nil { |
| 186 | + return err |
| 187 | + } |
| 188 | + |
| 189 | + return resourceTencentCloudTKEAuthAttachmentRead(d, meta) |
| 190 | +} |
| 191 | + |
| 192 | +func resourceTencentCloudTKEAuthAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 193 | + defer logElapsed("resource.resource_tc_kubernetes_auth_attachment.delete")() |
| 194 | + logId := getLogId(contextNil) |
| 195 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 196 | + |
| 197 | + id := d.Id() |
| 198 | + |
| 199 | + service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 200 | + request := tke.NewModifyClusterAuthenticationOptionsRequest() |
| 201 | + request.ClusterId = &id |
| 202 | + request.ServiceAccounts = &tke.ServiceAccountAuthenticationOptions{ |
| 203 | + JWKSURI: helper.String(""), |
| 204 | + Issuer: helper.String(DefaultAuthenticationOptionsIssuer), |
| 205 | + } |
| 206 | + |
| 207 | + if err := service.ModifyClusterAuthenticationOptions(ctx, request); err != nil { |
| 208 | + return err |
| 209 | + } |
| 210 | + |
| 211 | + _, err := service.WaitForAuthenticationOptionsUpdateSuccess(ctx, id) |
| 212 | + |
| 213 | + if err != nil { |
| 214 | + return err |
| 215 | + } |
| 216 | + |
| 217 | + return nil |
| 218 | +} |
0 commit comments