Skip to content

Commit 526019c

Browse files
authored
d/azurerm_kubernetes_cluster extended with kubelet_identity and identity (#6527)
Co-authored-by: kt <[email protected]> Fixes #6514
1 parent 1a074c6 commit 526019c

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

azurerm/internal/services/containers/kubernetes_cluster_data_source.go

+106
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,27 @@ func dataSourceArmKubernetesCluster() *schema.Resource {
251251
Computed: true,
252252
},
253253

254+
"identity": {
255+
Type: schema.TypeList,
256+
Computed: true,
257+
Elem: &schema.Resource{
258+
Schema: map[string]*schema.Schema{
259+
"type": {
260+
Type: schema.TypeString,
261+
Computed: true,
262+
},
263+
"principal_id": {
264+
Type: schema.TypeString,
265+
Computed: true,
266+
},
267+
"tenant_id": {
268+
Type: schema.TypeString,
269+
Computed: true,
270+
},
271+
},
272+
},
273+
},
274+
254275
"kubernetes_version": {
255276
Type: schema.TypeString,
256277
Computed: true,
@@ -338,6 +359,27 @@ func dataSourceArmKubernetesCluster() *schema.Resource {
338359
Sensitive: true,
339360
},
340361

362+
"kubelet_identity": {
363+
Type: schema.TypeList,
364+
Computed: true,
365+
Elem: &schema.Resource{
366+
Schema: map[string]*schema.Schema{
367+
"client_id": {
368+
Type: schema.TypeString,
369+
Computed: true,
370+
},
371+
"object_id": {
372+
Type: schema.TypeString,
373+
Computed: true,
374+
},
375+
"user_assigned_identity_id": {
376+
Type: schema.TypeString,
377+
Computed: true,
378+
},
379+
},
380+
},
381+
},
382+
341383
"linux_profile": {
342384
Type: schema.TypeList,
343385
Computed: true,
@@ -536,6 +578,11 @@ func dataSourceArmKubernetesClusterRead(d *schema.ResourceData, meta interface{}
536578
return fmt.Errorf("Error setting `agent_pool_profile`: %+v", err)
537579
}
538580

581+
kubeletIdentity := flattenKubernetesClusterDataSourceIdentityProfile(props.IdentityProfile)
582+
if err := d.Set("kubelet_identity", kubeletIdentity); err != nil {
583+
return fmt.Errorf("setting `kubelet_identity`: %+v", err)
584+
}
585+
539586
linuxProfile := flattenKubernetesClusterDataSourceLinuxProfile(props.LinuxProfile)
540587
if err := d.Set("linux_profile", linuxProfile); err != nil {
541588
return fmt.Errorf("Error setting `linux_profile`: %+v", err)
@@ -579,6 +626,10 @@ func dataSourceArmKubernetesClusterRead(d *schema.ResourceData, meta interface{}
579626
}
580627
}
581628

629+
if err := d.Set("identity", flattenKubernetesClusterDataSourceManagedClusterIdentity(resp.Identity)); err != nil {
630+
return fmt.Errorf("setting `identity`: %+v", err)
631+
}
632+
582633
kubeConfigRaw, kubeConfig := flattenKubernetesClusterDataSourceAccessProfile(profile)
583634
d.Set("kube_config_raw", kubeConfigRaw)
584635
if err := d.Set("kube_config", kubeConfig); err != nil {
@@ -839,6 +890,38 @@ func flattenKubernetesClusterDataSourceAgentPoolProfiles(input *[]containerservi
839890
return agentPoolProfiles
840891
}
841892

893+
func flattenKubernetesClusterDataSourceIdentityProfile(profile map[string]*containerservice.ManagedClusterPropertiesIdentityProfileValue) []interface{} {
894+
if profile == nil {
895+
return []interface{}{}
896+
}
897+
898+
kubeletIdentity := make([]interface{}, 0)
899+
if kubeletidentity := profile["kubeletidentity"]; kubeletidentity != nil {
900+
clientId := ""
901+
if clientid := kubeletidentity.ClientID; clientid != nil {
902+
clientId = *clientid
903+
}
904+
905+
objectId := ""
906+
if objectid := kubeletidentity.ObjectID; objectid != nil {
907+
objectId = *objectid
908+
}
909+
910+
userAssignedIdentityId := ""
911+
if resourceid := kubeletidentity.ResourceID; resourceid != nil {
912+
userAssignedIdentityId = *resourceid
913+
}
914+
915+
kubeletIdentity = append(kubeletIdentity, map[string]interface{}{
916+
"client_id": clientId,
917+
"object_id": objectId,
918+
"user_assigned_identity_id": userAssignedIdentityId,
919+
})
920+
}
921+
922+
return kubeletIdentity
923+
}
924+
842925
func flattenKubernetesClusterDataSourceLinuxProfile(input *containerservice.LinuxProfile) []interface{} {
843926
values := make(map[string]interface{})
844927
sshKeys := make([]interface{}, 0)
@@ -959,3 +1042,26 @@ func flattenKubernetesClusterDataSourceKubeConfigAAD(config kubernetes.KubeConfi
9591042

9601043
return []interface{}{values}
9611044
}
1045+
1046+
func flattenKubernetesClusterDataSourceManagedClusterIdentity(input *containerservice.ManagedClusterIdentity) []interface{} {
1047+
// if it's none, omit the block
1048+
if input == nil || input.Type == containerservice.None {
1049+
return []interface{}{}
1050+
}
1051+
1052+
identity := make(map[string]interface{})
1053+
1054+
identity["principal_id"] = ""
1055+
if input.PrincipalID != nil {
1056+
identity["principal_id"] = *input.PrincipalID
1057+
}
1058+
1059+
identity["tenant_id"] = ""
1060+
if input.TenantID != nil {
1061+
identity["tenant_id"] = *input.TenantID
1062+
}
1063+
1064+
identity["type"] = string(input.Type)
1065+
1066+
return []interface{}{identity}
1067+
}

azurerm/internal/services/containers/tests/kubernetes_cluster_data_source_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ func testAccDataSourceAzureRMKubernetesCluster_basic(t *testing.T) {
3636
resource.TestCheckResourceAttrSet(data.ResourceName, "kube_config.0.password"),
3737
resource.TestCheckResourceAttr(data.ResourceName, "kube_admin_config.#", "0"),
3838
resource.TestCheckResourceAttr(data.ResourceName, "kube_admin_config_raw", ""),
39+
resource.TestCheckResourceAttrSet(data.ResourceName, "kubelet_identity.0.object_id"),
40+
resource.TestCheckResourceAttrSet(data.ResourceName, "kubelet_identity.0.client_id"),
41+
resource.TestCheckResourceAttrSet(data.ResourceName, "kubelet_identity.0.user_assigned_identity_id"),
42+
resource.TestCheckResourceAttr(data.ResourceName, "identity.0.type", "SystemAssigned"),
43+
resource.TestCheckResourceAttrSet(data.ResourceName, "identity.0.principal_id"),
44+
resource.TestCheckResourceAttrSet(data.ResourceName, "identity.0.tenant_id"),
3945
),
4046
},
4147
},

website/docs/d/kubernetes_cluster.html.markdown

+24
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ The following attributes are exported:
7878

7979
* `service_principal` - A `service_principal` block as documented below.
8080

81+
* `identity` - A `identity` block as documented below.
82+
83+
* `kubelet_identity` - A `kubelet_identity` block as documented below.
84+
8185
* `tags` - A mapping of tags assigned to this resource.
8286

8387
---
@@ -251,6 +255,26 @@ A `service_principal` block supports the following:
251255

252256
---
253257

258+
The `identity` block exports the following:
259+
260+
* `type` - The type of identity used for the managed cluster.
261+
262+
* `principal_id` - The principal id of the system assigned identity which is used by master components.
263+
264+
* `tenant_id` - The tenant id of the system assigned identity which is used by master components.
265+
266+
---
267+
268+
The `kubelet_identity` block exports the following:
269+
270+
* `client_id` - The Client ID of the user-defined Managed Identity assigned to the Kubelets.
271+
272+
* `object_id` - The Object ID of the user-defined Managed Identity assigned to the Kubelets.
273+
274+
* `user_assigned_identity_id` - The ID of the User Assigned Identity assigned to the Kubelets.
275+
276+
---
277+
254278
A `ssh_key` block exports the following:
255279

256280
* `key_data` - The Public SSH Key used to access the cluster.

0 commit comments

Comments
 (0)