Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for user assigned identities in AKS #8737

Merged
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"strings"
"time"

msiparse "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/msi/parse"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2020-09-01/containerservice"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/containers/kubernetes"
msiparse "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/msi/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -266,6 +267,10 @@ func dataSourceArmKubernetesCluster() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"user_assigned_identity_id": {
Type: schema.TypeString,
Computed: true,
},
"principal_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -649,7 +654,12 @@ func dataSourceArmKubernetesClusterRead(d *schema.ResourceData, meta interface{}
}
}

if err := d.Set("identity", flattenKubernetesClusterDataSourceManagedClusterIdentity(resp.Identity)); err != nil {
identity, err := flattenKubernetesClusterDataSourceManagedClusterIdentity(resp.Identity)
if err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

if err := d.Set("identity", identity); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

Expand Down Expand Up @@ -837,11 +847,11 @@ func flattenKubernetesClusterDataSourceOmsAgentIdentityProfile(profile *containe

userAssignedIdentityID := ""
if resourceid := profile.ResourceID; resourceid != nil {
parsedId, err := msiparse.UserAssignedIdentityID(*resourceid)
parsedID, err := msiparse.UserAssignedIdentityID(*resourceid)
if err != nil {
return nil, err
}
userAssignedIdentityID = parsedId.ID()
userAssignedIdentityID = parsedID.ID()
}

identity = append(identity, map[string]interface{}{
Expand Down Expand Up @@ -1092,10 +1102,10 @@ func flattenKubernetesClusterDataSourceKubeConfigAAD(config kubernetes.KubeConfi
return []interface{}{values}
}

func flattenKubernetesClusterDataSourceManagedClusterIdentity(input *containerservice.ManagedClusterIdentity) []interface{} {
func flattenKubernetesClusterDataSourceManagedClusterIdentity(input *containerservice.ManagedClusterIdentity) ([]interface{}, error) {
// if it's none, omit the block
if input == nil || input.Type == containerservice.ResourceIdentityTypeNone {
return []interface{}{}
return []interface{}{}, nil
}

identity := make(map[string]interface{})
Expand All @@ -1110,7 +1120,20 @@ func flattenKubernetesClusterDataSourceManagedClusterIdentity(input *containerse
identity["tenant_id"] = *input.TenantID
}

identity["user_assigned_identity_id"] = ""
if input.UserAssignedIdentities != nil {
keys := []string{}
for key := range input.UserAssignedIdentities {
keys = append(keys, key)
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
}
parsedId, err := msiparse.UserAssignedIdentityID(keys[0])
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
identity["user_assigned_identity_id"] = parsedId.ID()
}

identity["type"] = string(input.Type)

return []interface{}{identity}
return []interface{}{identity}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/containers/parse"
containerValidate "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/containers/validate"
msiparse "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/msi/parse"
msivalidate "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/msi/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
Expand Down Expand Up @@ -180,8 +181,14 @@ func resourceArmKubernetesCluster() *schema.Resource {
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(containerservice.ResourceIdentityTypeSystemAssigned),
string(containerservice.ResourceIdentityTypeUserAssigned),
}, false),
},
"user_assigned_identity_id": {
Type: schema.TypeString,
ValidateFunc: msivalidate.UserAssignedIdentityID,
Optional: true,
},
"principal_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -1262,7 +1269,12 @@ func resourceArmKubernetesClusterRead(d *schema.ResourceData, meta interface{})
}
}

if err := d.Set("identity", flattenKubernetesClusterManagedClusterIdentity(resp.Identity)); err != nil {
identity, err := flattenKubernetesClusterManagedClusterIdentity(resp.Identity)
if err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

if err := d.Set("identity", identity); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

Expand Down Expand Up @@ -1737,6 +1749,17 @@ func expandKubernetesClusterManagedClusterIdentity(input []interface{}) *contain

values := input[0].(map[string]interface{})

if containerservice.ResourceIdentityType(values["type"].(string)) == containerservice.ResourceIdentityTypeUserAssigned {
userAssignedIdentities := map[string]*containerservice.ManagedClusterIdentityUserAssignedIdentitiesValue{
values["user_assigned_identity_id"].(string): {},
}

return &containerservice.ManagedClusterIdentity{
Type: containerservice.ResourceIdentityType(values["type"].(string)),
UserAssignedIdentities: userAssignedIdentities,
}
}

return &containerservice.ManagedClusterIdentity{
Type: containerservice.ResourceIdentityType(values["type"].(string)),
}
Expand Down Expand Up @@ -1882,10 +1905,10 @@ func flattenKubernetesClusterKubeConfigAAD(config kubernetes.KubeConfigAAD) []in
}
}

func flattenKubernetesClusterManagedClusterIdentity(input *containerservice.ManagedClusterIdentity) []interface{} {
func flattenKubernetesClusterManagedClusterIdentity(input *containerservice.ManagedClusterIdentity) ([]interface{}, error) {
// if it's none, omit the block
if input == nil || input.Type == containerservice.ResourceIdentityTypeNone {
return []interface{}{}
return []interface{}{}, nil
}

identity := make(map[string]interface{})
Expand All @@ -1900,9 +1923,22 @@ func flattenKubernetesClusterManagedClusterIdentity(input *containerservice.Mana
identity["tenant_id"] = *input.TenantID
}

identity["user_assigned_identity_id"] = ""
if input.UserAssignedIdentities != nil {
keys := []string{}
for key := range input.UserAssignedIdentities {
keys = append(keys, key)
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
}
parsedId, err := msiparse.UserAssignedIdentityID(keys[0])
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
identity["user_assigned_identity_id"] = parsedId.ID()
}

identity["type"] = string(input.Type)

return []interface{}{identity}
return []interface{}{identity}, nil
}

func flattenKubernetesClusterAutoScalerProfile(profile *containerservice.ManagedClusterPropertiesAutoScalerProfile) []interface{} {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ func validateKubernetesCluster(d *schema.ResourceData, cluster *containerservice
}
}

// ensure conditionally-required identity values are valid
if v, exists := d.GetOk("identity"); exists {
rawIdentity := v.([]interface{})

if len(rawIdentity) != 0 {
identity := rawIdentity[0].(map[string]interface{})

if identityType := identity["type"].(string); identityType == string(containerservice.ResourceIdentityTypeUserAssigned) {
userAssignedIdentityId := identity["user_assigned_identity_id"].(string)

if userAssignedIdentityId == "" {
return fmt.Errorf("when `identity.type` is UserAssigned then `user_assigned_identity_id` must be set")
}
}
}
}
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved

// @tombuildsstuff: As of 2020-03-30 it's no longer possible to create a cluster using a Service Principal
// for authentication (albeit this worked on 2020-03-27 via API version 2019-10-01 :shrug:). However it's
// possible to rotate the Service Principal for an existing Cluster - so this needs to be supported via
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
var kubernetesAuthTests = map[string]func(t *testing.T){
"apiServerAuthorizedIPRanges": testAccAzureRMKubernetesCluster_apiServerAuthorizedIPRanges,
"managedClusterIdentity": testAccAzureRMKubernetesCluster_managedClusterIdentity,
"userAssignedIdentity": testAccAzureRMKubernetesCluster_userAssignedIdentity,
"roleBasedAccessControl": testAccAzureRMKubernetesCluster_roleBasedAccessControl,
"AAD": testAccAzureRMKubernetesCluster_roleBasedAccessControlAAD,
"AADUpdateToManaged": testAccAzureRMKubernetesCluster_roleBasedAccessControlAADUpdateToManaged,
Expand Down Expand Up @@ -61,6 +62,11 @@ func TestAccAzureRMKubernetesCluster_managedClusterIdentity(t *testing.T) {
testAccAzureRMKubernetesCluster_managedClusterIdentity(t)
}

func TestAccAzureRMKubernetesCluster_userAssignedIdentity(t *testing.T) {
checkIfShouldRunTestsIndividually(t)
testAccAzureRMKubernetesCluster_userAssignedIdentity(t)
}

func testAccAzureRMKubernetesCluster_managedClusterIdentity(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kubernetes_cluster", "test")

Expand All @@ -85,6 +91,27 @@ func testAccAzureRMKubernetesCluster_managedClusterIdentity(t *testing.T) {
})
}

func testAccAzureRMKubernetesCluster_userAssignedIdentity(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kubernetes_cluster", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMKubernetesClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMKubernetesCluster_userAssignedIdentityConfig(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKubernetesClusterExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "identity.0.type", "UserAssigned"),
resource.TestCheckResourceAttrSet(data.ResourceName, "identity.0.user_assigned_identity_id"),
),
},
data.ImportStep(),
},
})
}

func TestAccAzureRMKubernetesCluster_roleBasedAccessControl(t *testing.T) {
checkIfShouldRunTestsIndividually(t)
testAccAzureRMKubernetesCluster_roleBasedAccessControl(t)
Expand Down Expand Up @@ -479,6 +506,44 @@ resource "azurerm_kubernetes_cluster" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}

func testAccAzureRMKubernetesCluster_userAssignedIdentityConfig(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-aks-%d"
location = "%s"
}

resource "azurerm_user_assigned_identity" "test" {
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
name = "test_identity"
}

resource "azurerm_kubernetes_cluster" "test" {
name = "acctestaks%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
dns_prefix = "acctestaks%d"

default_node_pool {
name = "default"
node_count = 1
vm_size = "Standard_DS2_v2"
}

identity {
type = "UserAssigned"
user_assigned_identity_id = azurerm_user_assigned_identity.test.id
}

}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}

func testAccAzureRMKubernetesCluster_roleBasedAccessControlConfig(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
7 changes: 4 additions & 3 deletions website/docs/r/kubernetes_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ In addition, one of either `identity` or `service_principal` blocks must be spec

* `disk_encryption_set_id` - (Optional) The ID of the Disk Encryption Set which should be used for the Nodes and Volumes. More information [can be found in the documentation](https://docs.microsoft.com/en-us/azure/aks/azure-disk-customer-managed-keys).

* `identity` - (Optional) A `identity` block as defined below. Changing this forces a new resource to be created.
* `identity` - (Optional) An `identity` block as defined below. Changing this forces a new resource to be created.

-> **NOTE:** One of either `identity` or `service_principal` must be specified.

Expand Down Expand Up @@ -275,9 +275,10 @@ A `http_application_routing` block supports the following:

---

A `identity` block supports the following:
An `identity` block supports the following:

* `type` - The type of identity used for the managed cluster. At this time the only supported value is `SystemAssigned`.
* `type` - The type of identity used for the managed cluster. Possible values are `SystemAssigned` and `UserAssigned`. If `UserAssigned` is set, a `user_assigned_identity_id` must be set as well.
* `user_assigned_identity_id` - (Optional) The ID of a user assigned identity.

---

Expand Down