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
78 changes: 78 additions & 0 deletions internal/services/containers/kubernetes_cluster_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,36 @@ func dataSourceKubernetesCluster() *pluginsdk.Resource {
},
},

"storage_profile": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{

"blob_driver_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},
"disk_driver_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},
"disk_driver_version": {
Type: pluginsdk.TypeString,
Computed: true,
},
"file_driver_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},
"snapshot_controller_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},
},
},
},

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

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

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

func flattenKubernetesClusterDataSourceStorageProfile(input *managedclusters.ManagedClusterStorageProfile) []interface{} {

storageProfile := make([]interface{}, 0)

if input != nil {

blobEnabled := false
if input.BlobCSIDriver != nil {
blobEnabled = *input.BlobCSIDriver.Enabled
}

diskEnabled := true
if input.DiskCSIDriver != nil {
diskEnabled = *input.DiskCSIDriver.Enabled
}

diskVersion := ""
if input.FileCSIDriver != nil {
diskVersion = *input.DiskCSIDriver.Version
}

fileEnabled := true
if input.DiskCSIDriver != nil {
fileEnabled = *input.FileCSIDriver.Enabled
}

snapshotController := true
if input.SnapshotController != nil {
snapshotController = *input.SnapshotController.Enabled
}

storageProfile = append(storageProfile, map[string]interface{}{
"blob_driver_enabled": blobEnabled,
"disk_driver_enabled": diskEnabled,
"disk_driver_version": diskVersion,
"file_driver_enabled": fileEnabled,
"snapshot_controller_enabled": snapshotController,
})
}

return storageProfile
}

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

"storage_profile": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{

"blob_driver_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},
"disk_driver_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},
"disk_driver_version": {
Type: pluginsdk.TypeString,
Optional: true,
Default: "v1",
ValidateFunc: validation.StringInSlice([]string{
"v1",
"v2",
}, false),
},
"file_driver_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},
"snapshot_controller_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},
},
},
},

"tags": commonschema.Tags(),

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

storageProfileRaw := d.Get("storage_profile").([]interface{})
storageProfile := expandStorageProfile(storageProfileRaw)

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")
}

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

if d.HasChanges("storage_profile") {
updateCluster = true
storageProfileRaw := d.Get("storage_profile").([]interface{})
clusterStorageProfile := expandStorageProfile(storageProfileRaw)
existing.Model.Properties.StorageProfile = clusterStorageProfile
}

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

func expandStorageProfile(input []interface{}) *managedclusters.ManagedClusterStorageProfile {

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

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

profile := managedclusters.ManagedClusterStorageProfile{
BlobCSIDriver: &managedclusters.ManagedClusterStorageProfileBlobCSIDriver{
Enabled: utils.Bool(raw["blob_driver_enabled"].(bool)),
},
DiskCSIDriver: &managedclusters.ManagedClusterStorageProfileDiskCSIDriver{
Enabled: utils.Bool(raw["disk_driver_enabled"].(bool)),
Version: utils.String(raw["disk_driver_version"].(string)),
},
FileCSIDriver: &managedclusters.ManagedClusterStorageProfileFileCSIDriver{
Enabled: utils.Bool(raw["file_driver_enabled"].(bool)),
},
SnapshotController: &managedclusters.ManagedClusterStorageProfileSnapshotController{
Enabled: utils.Bool(raw["snapshot_controller_enabled"].(bool)),
},
}

return &profile
}

func expandEdgeZone(input string) *edgezones.Model {
normalized := edgezones.Normalize(input)
if normalized == "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ func TestAccKubernetesCluster_runCommand(t *testing.T) {
})
}

func TestAccKubernetesCluster_storageProfile(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kubernetes_cluster", "test")
r := KubernetesClusterResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.storageProfile(data, currentKubernetesVersion),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
})
}

func TestAccKubernetesCluster_workloadAutoscalerProfileKedaOnOff(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kubernetes_cluster", "test")
r := KubernetesClusterResource{}
Expand Down Expand Up @@ -441,3 +455,42 @@ resource "azurerm_kubernetes_cluster" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, controlPlaneVersion, tag)
}

func (KubernetesClusterResource) storageProfile(data acceptance.TestData, controlPlaneVersion string) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

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

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"
kubernetes_version = %q

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

identity {
type = "SystemAssigned"
}

storage_profile {
blob_driver_enabled = true
disk_driver_enabled = true
disk_driver_version = "v2"
file_driver_enabled = false
snapshot_controller_enabled = false
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, controlPlaneVersion)
}
Loading