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

azurerm_kubernetes_cluster: Support for CSI arguments #19396

Merged
merged 11 commits into from
Dec 7, 2022
76 changes: 76 additions & 0 deletions internal/services/containers/kubernetes_cluster_data_source.go
Original file line number Diff line number Diff line change
@@ -613,6 +613,38 @@ func dataSourceKubernetesCluster() *pluginsdk.Resource {
},
},

"storage_blob_driver": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"storage_disk_driver": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},
"version": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},

"storage_file_driver": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"storage_snapshot_controller": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"tags": commonschema.TagsDataSource(),
},
}
@@ -729,6 +761,34 @@ func dataSourceKubernetesClusterRead(d *pluginsdk.ResourceData, meta interface{}
return fmt.Errorf("setting `oidc_issuer_url`: %+v", err)
}

if storageProfile := props.StorageProfile; storageProfile != nil {
if storageProfile.BlobCSIDriver != nil {
blobDriver := *storageProfile.BlobCSIDriver.Enabled
if err := d.Set("storage_blob_driver", blobDriver); err != nil {
return fmt.Errorf("setting `storage_blob_driver`: %+v", err)
}
}

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

if storageProfile.FileCSIDriver != nil {
fileDriver := *storageProfile.FileCSIDriver.Enabled
if err := d.Set("storage_file_driver", fileDriver); err != nil {
return fmt.Errorf("setting `storage_file_driver`: %+v", err)
}
}

if storageProfile.SnapshotController != nil {
snapshotController := *storageProfile.SnapshotController.Enabled
if err := d.Set("storage_snapshot_controller", snapshotController); err != nil {
return fmt.Errorf("setting `storage_snapshot_controller`: %+v", err)
}
}
}

rbacEnabled := true
if props.EnableRBAC != nil {
rbacEnabled = *props.EnableRBAC
@@ -793,6 +853,22 @@ func dataSourceKubernetesClusterRead(d *pluginsdk.ResourceData, meta interface{}
return nil
}

func flattenKubernetesClusterDataSourceStorageProfileDiskDriver(input *managedclusters.ManagedClusterStorageProfile) []interface{} {
if input == nil || input.DiskCSIDriver == nil {
return []interface{}{}
}

diskDriver := *input.DiskCSIDriver.Enabled
diskVersion := *input.DiskCSIDriver.Version

return []interface{}{
map[string]interface{}{
"enabled": diskDriver,
"version": diskVersion,
},
}
}

func flattenKubernetesClusterDataSourceAccessProfile(profile managedclusters.ManagedClusterAccessProfile) (*string, []interface{}) {
if profile.Properties == nil {
return nil, []interface{}{}
92 changes: 91 additions & 1 deletion internal/services/containers/kubernetes_cluster_resource.go
Original file line number Diff line number Diff line change
@@ -1004,6 +1004,48 @@ func resourceKubernetesCluster() *pluginsdk.Resource {
}, false),
},

"storage_blob_driver": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
CorrenSoft marked this conversation as resolved.
Show resolved Hide resolved
},

"storage_disk_driver": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
CorrenSoft marked this conversation as resolved.
Show resolved Hide resolved
},
"version": {
Type: pluginsdk.TypeString,
Optional: true,
Default: "V1",
ValidateFunc: validation.StringInSlice([]string{
"V1",
"V2",
}, false),
},
},
},
},

"storage_file_driver": {
CorrenSoft marked this conversation as resolved.
Show resolved Hide resolved
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"storage_snapshot_controller": {
CorrenSoft marked this conversation as resolved.
Show resolved Hide resolved
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},
CorrenSoft marked this conversation as resolved.
Show resolved Hide resolved

"tags": commonschema.Tags(),

"windows_profile": {
@@ -1208,11 +1250,13 @@ func resourceKubernetesClusterCreate(d *pluginsdk.ResourceData, meta interface{}
microsoftDefenderRaw := d.Get("microsoft_defender").([]interface{})
securityProfile := expandKubernetesClusterMicrosoftDefender(d, microsoftDefenderRaw)

clusterStorageProfile := expandClusterStorageProfile(d)

workloadIdentity := false
if v, ok := d.GetOk("workload_identity_enabled"); ok {
workloadIdentity = v.(bool)

if workloadIdentity == true && enableOidcIssuer == false {
if workloadIdentity && !enableOidcIssuer {
return fmt.Errorf("`oidc_issuer_enabled` must be set to `true` to enable Azure AD Workload Identity")
}

@@ -1251,6 +1295,7 @@ func resourceKubernetesClusterCreate(d *pluginsdk.ResourceData, meta interface{}
HTTPProxyConfig: httpProxyConfig,
OidcIssuerProfile: oidcIssuerProfile,
SecurityProfile: securityProfile,
StorageProfile: clusterStorageProfile,
WorkloadAutoScalerProfile: workloadAutoscalerProfile,
},
Tags: tags.Expand(t),
@@ -1717,6 +1762,12 @@ func resourceKubernetesClusterUpdate(d *pluginsdk.ResourceData, meta interface{}
existing.Model.Properties.SecurityProfile = microsoftDefender
}

if d.HasChanges("storage_disk_driver") || d.HasChanges("storage_blob_driver") || d.HasChanges("storage_file_driver") || d.HasChanges("storage_snapshot_controller") {
updateCluster = true
clusterStorageProfile := expandClusterStorageProfile(d)
existing.Model.Properties.StorageProfile = clusterStorageProfile
}

if d.HasChange("workload_autoscaler_profile") {
updateCluster = true
workloadAutoscalerProfileRaw := d.Get("workload_autoscaler_profile").([]interface{})
@@ -3317,6 +3368,45 @@ func flattenKubernetesClusterMicrosoftDefender(input *managedclusters.ManagedClu
}
}

func expandClusterStorageProfile(d *pluginsdk.ResourceData) *managedclusters.ManagedClusterStorageProfile {

blobEnabled := d.Get("storage_blob_driver").(bool)
diskInterfrace := d.Get("storage_disk_driver").([]interface{})
fileEnabled := d.Get("storage_file_driver").(bool)
snapshotControllerEnabled := d.Get("storage_snapshot_controller").(bool)

profile := managedclusters.ManagedClusterStorageProfile{
BlobCSIDriver: &managedclusters.ManagedClusterStorageProfileBlobCSIDriver{
Enabled: utils.Bool(blobEnabled),
},
DiskCSIDriver: expandClusterStorageProfileDiskDriver(diskInterfrace),
FileCSIDriver: &managedclusters.ManagedClusterStorageProfileFileCSIDriver{
Enabled: utils.Bool(fileEnabled),
},
SnapshotController: &managedclusters.ManagedClusterStorageProfileSnapshotController{
Enabled: utils.Bool(snapshotControllerEnabled),
},
}

return &profile
}

func expandClusterStorageProfileDiskDriver(input []interface{}) *managedclusters.ManagedClusterStorageProfileDiskCSIDriver {

if (input == nil) || len(input) == 0 {
return nil
}

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

diskDriver := managedclusters.ManagedClusterStorageProfileDiskCSIDriver{
Enabled: utils.Bool(raw["enabled"].(bool)),
Version: utils.String(raw["version"].(string)),
}

return &diskDriver
}

func expandEdgeZone(input string) *edgezones.Model {
normalized := edgezones.Normalize(input)
if normalized == "" {