Skip to content

Commit d0d6414

Browse files
Merge pull request #359 from vmware/add-support-for-aks-managed-identities
Add support for aks managed identities
2 parents 33c27c9 + 92c4692 commit d0d6414

File tree

10 files changed

+284
-0
lines changed

10 files changed

+284
-0
lines changed

docs/data-sources/akscluster.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Optional:
109109
- `api_server_access_config` (Block List, Max: 1) API Server Access Config (see [below for nested schema](#nestedblock--spec--config--api_server_access_config))
110110
- `auto_upgrade_config` (Block List, Max: 1) Auto Upgrade Config (see [below for nested schema](#nestedblock--spec--config--auto_upgrade_config))
111111
- `disk_encryption_set` (String) Resource ID of the disk encryption set to use for enabling
112+
- `identity_config` (Block List, Max: 1) Managed Identity Config (see [below for nested schema](#nestedblock--spec--config--identity_config))
112113
- `linux_config` (Block List, Max: 1) Linux Config (see [below for nested schema](#nestedblock--spec--config--linux_config))
113114
- `node_resource_group_name` (String) Name of the resource group containing nodepools.
114115
- `sku` (Block List, Max: 1) Azure Kubernetes Service SKU (see [below for nested schema](#nestedblock--spec--config--sku))
@@ -212,6 +213,23 @@ Optional:
212213
- `upgrade_channel` (String) Upgrade Channel. Allowed values include: NONE, PATCH, STABLE, RAPID or NODE_IMAGE
213214

214215

216+
<a id="nestedblock--spec--config--identity_config"></a>
217+
### Nested Schema for `spec.config.identity_config`
218+
219+
Optional:
220+
221+
- `type` (String) Type of managed identity used by the cluster (default IDENTITY_TYPE_SYSTEM_ASSIGNED). Allowed values include: IDENTITY_TYPE_SYSTEM_ASSIGNED or IDENTITY_TYPE_USER_ASSIGNED
222+
- `user_assigned` (Block List, Max: 1) User Assigned Managed Identity Config (see [below for nested schema](#nestedblock--spec--config--identity_config--user_assigned))
223+
224+
<a id="nestedblock--spec--config--identity_config--user_assigned"></a>
225+
### Nested Schema for `spec.config.identity_config.user_assigned`
226+
227+
Required:
228+
229+
- `resource_id` (String) The ARM resource ID of user assigned identity in the form: '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{identityName}'
230+
231+
232+
215233
<a id="nestedblock--spec--config--linux_config"></a>
216234
### Nested Schema for `spec.config.linux_config`
217235

docs/resources/akscluster.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ Optional:
130130
- `api_server_access_config` (Block List, Max: 1) API Server Access Config (see [below for nested schema](#nestedblock--spec--config--api_server_access_config))
131131
- `auto_upgrade_config` (Block List, Max: 1) Auto Upgrade Config (see [below for nested schema](#nestedblock--spec--config--auto_upgrade_config))
132132
- `disk_encryption_set` (String) Resource ID of the disk encryption set to use for enabling
133+
- `identity_config` (Block List, Max: 1) Managed Identity Config (see [below for nested schema](#nestedblock--spec--config--identity_config))
133134
- `linux_config` (Block List, Max: 1) Linux Config (see [below for nested schema](#nestedblock--spec--config--linux_config))
134135
- `node_resource_group_name` (String) Name of the resource group containing nodepools.
135136
- `sku` (Block List, Max: 1) Azure Kubernetes Service SKU (see [below for nested schema](#nestedblock--spec--config--sku))
@@ -233,6 +234,23 @@ Optional:
233234
- `upgrade_channel` (String) Upgrade Channel. Allowed values include: NONE, PATCH, STABLE, RAPID or NODE_IMAGE
234235

235236

237+
<a id="nestedblock--spec--config--identity_config"></a>
238+
### Nested Schema for `spec.config.identity_config`
239+
240+
Optional:
241+
242+
- `type` (String) Type of managed identity used by the cluster (default IDENTITY_TYPE_SYSTEM_ASSIGNED). Allowed values include: IDENTITY_TYPE_SYSTEM_ASSIGNED or IDENTITY_TYPE_USER_ASSIGNED
243+
- `user_assigned` (Block List, Max: 1) User Assigned Managed Identity Config (see [below for nested schema](#nestedblock--spec--config--identity_config--user_assigned))
244+
245+
<a id="nestedblock--spec--config--identity_config--user_assigned"></a>
246+
### Nested Schema for `spec.config.identity_config.user_assigned`
247+
248+
Required:
249+
250+
- `resource_id` (String) The ARM resource ID of user assigned identity in the form: '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{identityName}'
251+
252+
253+
236254
<a id="nestedblock--spec--config--linux_config"></a>
237255
### Nested Schema for `spec.config.linux_config`
238256

internal/models/akscluster/cluster_config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ type VmwareTanzuManageV1alpha1AksclusterClusterConfig struct {
5050
// The metadata to apply to the cluster to assist with categorization and organization.
5151
Tags map[string]string `json:"tags,omitempty"`
5252

53+
// The managed identity to apply to the cluster.
54+
IdentityConfig *VmwareTanzuManageV1alpha1AksclusterManagedIdentityConfig `json:"identityConfig,omitempty"`
55+
5356
// Kubernetes version of the cluster.
5457
Version string `json:"version,omitempty"`
5558
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2023 VMware, Inc. All Rights Reserved.
3+
SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
package models
7+
8+
import "github.com/go-openapi/swag"
9+
10+
// VmwareTanzuManageV1alpha1AksclusterManagedIdentityConfig The managed identity config.
11+
//
12+
// swagger:model vmware.tanzu.manage.v1alpha1.akscluster.ManagedIdentityConfig
13+
type VmwareTanzuManageV1alpha1AksclusterManagedIdentityConfig struct {
14+
Type *VmwareTanzuManageV1alpha1AksclusterManagedIdentityType `json:"type,omitempty"`
15+
16+
UserAssignedIdentityType *VmwareTanzuManageV1alpha1AksclusterUserAssignedIdentityTypeConfig `json:"userAssigned,omitempty"`
17+
}
18+
19+
// MarshalBinary interface implementation.
20+
func (m *VmwareTanzuManageV1alpha1AksclusterManagedIdentityConfig) MarshalBinary() ([]byte, error) {
21+
if m == nil {
22+
return nil, nil
23+
}
24+
25+
return swag.WriteJSON(m)
26+
}
27+
28+
// UnmarshalBinary interface implementation.
29+
func (m *VmwareTanzuManageV1alpha1AksclusterManagedIdentityConfig) UnmarshalBinary(b []byte) error {
30+
var res VmwareTanzuManageV1alpha1AksclusterManagedIdentityConfig
31+
if err := swag.ReadJSON(b, &res); err != nil {
32+
return err
33+
}
34+
35+
*m = res
36+
37+
return nil
38+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2023 VMware, Inc. All Rights Reserved.
3+
SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
package models
7+
8+
import (
9+
"encoding/json"
10+
)
11+
12+
// VmwareTanzuManageV1alpha1AksclusterManagedIdentityType Managed identity type options of identity config.
13+
//
14+
// - IDENTITY_TYPE_SYSTEM_ASSIGNED: Indicates that a system assigned managed identity should be used by the cluster.
15+
// - IDENTITY_TYPE_USER_ASSIGNED: Indicates that a user assigned managed identity should be used by the cluster.
16+
//
17+
// swagger:model vmware.tanzu.manage.v1alpha1.akscluster.ManagedIdentityType
18+
type VmwareTanzuManageV1alpha1AksclusterManagedIdentityType string
19+
20+
func NewVmwareTanzuManageV1alpha1AksclusterManagedIdentityType(value VmwareTanzuManageV1alpha1AksclusterManagedIdentityType) *VmwareTanzuManageV1alpha1AksclusterManagedIdentityType {
21+
return &value
22+
}
23+
24+
// Pointer returns a pointer to a freshly-allocated VmwareTanzuManageV1alpha1AksclusterManagedIdentityType.
25+
func (m VmwareTanzuManageV1alpha1AksclusterManagedIdentityType) Pointer() *VmwareTanzuManageV1alpha1AksclusterManagedIdentityType {
26+
return &m
27+
}
28+
29+
const (
30+
31+
// VmwareTanzuManageV1alpha1AksclusterManagedIdentityTypeSYSTEMASSIGNED captures enum value "IDENTITY_TYPE_SYSTEM_ASSIGNED".
32+
VmwareTanzuManageV1alpha1AksclusterManagedIdentityTypeSYSTEMASSIGNED VmwareTanzuManageV1alpha1AksclusterManagedIdentityType = "IDENTITY_TYPE_SYSTEM_ASSIGNED"
33+
34+
// VmwareTanzuManageV1alpha1AksclusterManagedIdentityTypeUSERASSIGNED captures enum value "IDENTITY_TYPE_USER_ASSIGNED".
35+
VmwareTanzuManageV1alpha1AksclusterManagedIdentityTypeUSERASSIGNED VmwareTanzuManageV1alpha1AksclusterManagedIdentityType = "IDENTITY_TYPE_USER_ASSIGNED"
36+
)
37+
38+
// for schema.
39+
var vmwareTanzuManageV1alpha1AksclusterManagedIdentityTypeEnum []interface{}
40+
41+
func init() {
42+
var res []VmwareTanzuManageV1alpha1AksclusterManagedIdentityType
43+
if err := json.Unmarshal([]byte(`["IDENTITY_TYPE_SYSTEM_ASSIGNED","IDENTITY_TYPE_USER_ASSIGNED"]`), &res); err != nil {
44+
panic(err)
45+
}
46+
47+
for _, v := range res {
48+
vmwareTanzuManageV1alpha1AksclusterManagedIdentityTypeEnum = append(vmwareTanzuManageV1alpha1AksclusterManagedIdentityTypeEnum, v)
49+
}
50+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2023 VMware, Inc. All Rights Reserved.
3+
SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
package models
7+
8+
import "github.com/go-openapi/swag"
9+
10+
// VmwareTanzuManageV1alpha1AksclusterUserAssignedIdentityTypeConfig The managed identity config.
11+
//
12+
// swagger:model vmware.tanzu.manage.v1alpha1.akscluster.UserAssignedIdentityTypeConfig
13+
14+
type VmwareTanzuManageV1alpha1AksclusterUserAssignedIdentityTypeConfig struct {
15+
ManagedResourceID string `json:"resourceId,omitempty"`
16+
}
17+
18+
// MarshalBinary interface implementation.
19+
func (m *VmwareTanzuManageV1alpha1AksclusterUserAssignedIdentityTypeConfig) MarshalBinary() ([]byte, error) {
20+
if m == nil {
21+
return nil, nil
22+
}
23+
24+
return swag.WriteJSON(m)
25+
}
26+
27+
// UnmarshalBinary interface implementation.
28+
func (m *VmwareTanzuManageV1alpha1AksclusterUserAssignedIdentityTypeConfig) UnmarshalBinary(b []byte) error {
29+
var res VmwareTanzuManageV1alpha1AksclusterUserAssignedIdentityTypeConfig
30+
if err := swag.ReadJSON(b, &res); err != nil {
31+
return err
32+
}
33+
34+
*m = res
35+
36+
return nil
37+
}

internal/resources/akscluster/akscluster_mapper.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ func constructConfig(data []any) *models.VmwareTanzuManageV1alpha1AksclusterClus
140140
helper.SetPrimitiveValue(v, &config.NodeResourceGroupName, nodeResourceGroupNameKey)
141141
}
142142

143+
if v, ok := configData[identityConfigKey]; ok {
144+
data, _ := v.([]any)
145+
config.IdentityConfig = constructManagedIdentityConfig(data)
146+
}
147+
143148
return config
144149
}
145150

@@ -433,6 +438,44 @@ func constructAutoUpgradeConfig(data []any) *models.VmwareTanzuManageV1alpha1Aks
433438
return autoUpgradeConfig
434439
}
435440

441+
func constructManagedIdentityConfig(data []any) *models.VmwareTanzuManageV1alpha1AksclusterManagedIdentityConfig {
442+
if len(data) < 1 {
443+
return nil
444+
}
445+
446+
// ManagedIdentityConfig schema defines max 1
447+
managedIdentityConfigData, _ := data[0].(map[string]any)
448+
managedIdentityConfig := &models.VmwareTanzuManageV1alpha1AksclusterManagedIdentityConfig{}
449+
450+
if v, ok := managedIdentityConfigData[typeKey]; ok {
451+
identityType := models.VmwareTanzuManageV1alpha1AksclusterManagedIdentityType(v.(string))
452+
managedIdentityConfig.Type = &identityType
453+
}
454+
455+
if v, ok := managedIdentityConfigData[userAssignedKey]; ok {
456+
data, _ := v.([]any)
457+
managedIdentityConfig.UserAssignedIdentityType = constructUserAssignedIdentityConfig(data)
458+
}
459+
460+
return managedIdentityConfig
461+
}
462+
463+
func constructUserAssignedIdentityConfig(data []any) *models.VmwareTanzuManageV1alpha1AksclusterUserAssignedIdentityTypeConfig {
464+
if len(data) < 1 {
465+
return nil
466+
}
467+
468+
// UserAssignedIdentityConfig schema defines max 1
469+
userAssignedIdentityConfigData, _ := data[0].(map[string]any)
470+
userAssignedIdentityConfig := &models.VmwareTanzuManageV1alpha1AksclusterUserAssignedIdentityTypeConfig{}
471+
472+
if v, ok := userAssignedIdentityConfigData[resourceIDKey]; ok {
473+
helper.SetPrimitiveValue(v, &userAssignedIdentityConfig.ManagedResourceID, resourceIDKey)
474+
}
475+
476+
return userAssignedIdentityConfig
477+
}
478+
436479
func ToAKSClusterMap(cluster *models.VmwareTanzuManageV1alpha1AksCluster, nodepools []*models.VmwareTanzuManageV1alpha1AksclusterNodepoolNodepool) any {
437480
if cluster == nil {
438481
return []any{}
@@ -483,6 +526,7 @@ func toConfigMap(config *models.VmwareTanzuManageV1alpha1AksclusterClusterConfig
483526
data[storageConfigKey] = toStorageConfigMap(config.StorageConfig)
484527
data[addonsConfigKey] = toAddonConfigMap(config.AddonsConfig)
485528
data[autoUpgradeConfigKey] = toAutoUpgradeConfigMap(config.AutoUpgradeConfig)
529+
data[identityConfigKey] = toManagedIdentityConfigMap(config.IdentityConfig)
486530

487531
return []any{data}
488532
}
@@ -642,6 +686,29 @@ func toAutoUpgradeConfigMap(config *models.VmwareTanzuManageV1alpha1AksclusterAu
642686
return []any{data}
643687
}
644688

689+
func toManagedIdentityConfigMap(config *models.VmwareTanzuManageV1alpha1AksclusterManagedIdentityConfig) []any {
690+
if config == nil {
691+
return []any{}
692+
}
693+
694+
data := make(map[string]any)
695+
data[typeKey] = helper.PtrString(config.Type)
696+
data[userAssignedKey] = toUserAssignedIdentityTypeConfigMap(config.UserAssignedIdentityType)
697+
698+
return []any{data}
699+
}
700+
701+
func toUserAssignedIdentityTypeConfigMap(config *models.VmwareTanzuManageV1alpha1AksclusterUserAssignedIdentityTypeConfig) []any {
702+
if config == nil {
703+
return []any{}
704+
}
705+
706+
data := make(map[string]any)
707+
data[resourceIDKey] = config.ManagedResourceID
708+
709+
return []any{data}
710+
}
711+
645712
func toNodePoolList(nodepools []*models.VmwareTanzuManageV1alpha1AksclusterNodepoolNodepool) []any {
646713
n := make([]any, 0, len(nodepools))
647714
for _, v := range nodepools {

internal/resources/akscluster/constants.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,6 @@ const (
106106
upgradeConfigKey = "upgrade_config"
107107
maxSurgeKey = "max_surge"
108108
kubeconfigKey = "kubeconfig"
109+
identityConfigKey = "identity_config"
110+
userAssignedKey = "user_assigned"
109111
)

internal/resources/akscluster/helpers_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ func aTestCluster(w ...clusterWither) *models.VmwareTanzuManageV1alpha1AksCluste
159159
AutoUpgradeConfig: &models.VmwareTanzuManageV1alpha1AksclusterAutoUpgradeConfig{
160160
Channel: models.VmwareTanzuManageV1alpha1AksclusterChannelSTABLE.Pointer(),
161161
},
162+
IdentityConfig: &models.VmwareTanzuManageV1alpha1AksclusterManagedIdentityConfig{
163+
Type: models.VmwareTanzuManageV1alpha1AksclusterManagedIdentityTypeUSERASSIGNED.Pointer(),
164+
UserAssignedIdentityType: &models.VmwareTanzuManageV1alpha1AksclusterUserAssignedIdentityTypeConfig{
165+
ManagedResourceID: "resource-id-for-a-user-assigned-managed-identity",
166+
},
167+
},
162168
},
163169
ProxyName: "my-proxy",
164170
AgentName: "my-agent-name",
@@ -372,6 +378,12 @@ func aTestClusterDataMap(w ...mapWither) map[string]any {
372378
"auto_upgrade_config": []any{map[string]any{
373379
"upgrade_channel": "STABLE",
374380
}},
381+
"identity_config": []any{map[string]any{
382+
"type": "IDENTITY_TYPE_USER_ASSIGNED",
383+
"user_assigned": []any{map[string]any{
384+
"resource_id": "resource-id-for-a-user-assigned-managed-identity",
385+
}},
386+
}},
375387
}},
376388
"nodepool": []any{
377389
aTestNodepoolDataMap(),

internal/resources/akscluster/schema.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ var ClusterConfig = &schema.Resource{
205205
MaxItems: 1,
206206
Elem: AutoUpgradeConfig,
207207
},
208+
identityConfigKey: {
209+
Type: schema.TypeList,
210+
Description: "Managed Identity Config",
211+
Optional: true,
212+
MaxItems: 1,
213+
Elem: ManagedIdentityConfig,
214+
},
208215
},
209216
}
210217

@@ -514,6 +521,38 @@ var AutoUpgradeConfig = &schema.Resource{
514521
},
515522
}
516523

524+
var ManagedIdentityConfig = &schema.Resource{
525+
Schema: map[string]*schema.Schema{
526+
typeKey: {
527+
Type: schema.TypeString,
528+
Description: "Type of managed identity used by the cluster (default IDENTITY_TYPE_SYSTEM_ASSIGNED). Allowed values include: IDENTITY_TYPE_SYSTEM_ASSIGNED or IDENTITY_TYPE_USER_ASSIGNED",
529+
Optional: true,
530+
Default: "IDENTITY_TYPE_SYSTEM_ASSIGNED",
531+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{
532+
string(aksmodel.VmwareTanzuManageV1alpha1AksclusterManagedIdentityTypeSYSTEMASSIGNED),
533+
string(aksmodel.VmwareTanzuManageV1alpha1AksclusterManagedIdentityTypeUSERASSIGNED),
534+
}, false)),
535+
},
536+
userAssignedKey: {
537+
Type: schema.TypeList,
538+
Description: "User Assigned Managed Identity Config",
539+
Optional: true,
540+
MaxItems: 1,
541+
Elem: UserAssignedManagedIdentityConfig,
542+
},
543+
},
544+
}
545+
546+
var UserAssignedManagedIdentityConfig = &schema.Resource{
547+
Schema: map[string]*schema.Schema{
548+
resourceIDKey: {
549+
Type: schema.TypeString,
550+
Description: "The ARM resource ID of user assigned identity in the form: '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{identityName}'",
551+
Required: true,
552+
},
553+
},
554+
}
555+
517556
// NodepoolConfig defines the info and nodepool spec for AKS clusters.
518557
//
519558
// Note: ForceNew is not used in any of the elements because this is a part of

0 commit comments

Comments
 (0)