Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
Aris van Ommeren committed Oct 13, 2022
1 parent c7efca8 commit 5bebf31
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 189 deletions.
9 changes: 4 additions & 5 deletions internal/services/containers/kubernetes_addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
subnetValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

const (
Expand Down Expand Up @@ -292,13 +293,11 @@ func expandKubernetesAddOns(d *pluginsdk.ResourceData, input map[string]interfac

if ok := d.HasChange("azure_policy_enabled"); ok {
v := input["azure_policy_enabled"].(bool)

config := make(map[string]string)
config["version"] = "v2"

props := managedclusters.ManagedClusterAddonProfile{
Enabled: v,
Config: &config,
Config: utils.ToPtr(map[string]string{
"version": "v2",
}),
}
addonProfiles[azurePolicyKey] = props
}
Expand Down
12 changes: 6 additions & 6 deletions internal/services/containers/kubernetes_cluster_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ func dataSourceKubernetesClusterRead(d *pluginsdk.ResourceData, meta interface{}
return fmt.Errorf("setting `agent_pool_profile`: %+v", err)
}

kubeletIdentity, err := flattenKubernetesClusterDataSourceIdentityProfile(*props.IdentityProfile)
kubeletIdentity, err := flattenKubernetesClusterDataSourceIdentityProfile(props.IdentityProfile)
if err != nil {
return err
}
Expand Down Expand Up @@ -1042,7 +1042,7 @@ func flattenKubernetesClusterDataSourceAgentPoolProfiles(input *[]managedcluster
"os_type": string(*profile.OsType),
"tags": tags.Flatten(profile.Tags),
"type": string(*profile.Type),
"upgrade_settings": flattenManagedClusterUpgradeSettings(profile.UpgradeSettings),
"upgrade_settings": flattenKubernetesClusterDataSourceUpgradeSettings(profile.UpgradeSettings),
"vm_size": vmSize,
"vnet_subnet_id": vnetSubnetId,
"zones": zones.Flatten(profile.AvailabilityZones),
Expand Down Expand Up @@ -1096,13 +1096,13 @@ func flattenKubernetesClusterDataSourceAzureActiveDirectoryRoleBasedAccessContro
return results
}

func flattenKubernetesClusterDataSourceIdentityProfile(profile map[string]managedclusters.UserAssignedIdentity) ([]interface{}, error) {
if profile == nil {
func flattenKubernetesClusterDataSourceIdentityProfile(profile *map[string]managedclusters.UserAssignedIdentity) ([]interface{}, error) {
if profile == nil || *profile == nil {
return []interface{}{}, nil
}

kubeletIdentity := make([]interface{}, 0)
kubeletidentity := profile["kubeletidentity"]
kubeletidentity := (*profile)["kubeletidentity"]
clientId := ""
if clientid := kubeletidentity.ClientId; clientid != nil {
clientId = *clientid
Expand Down Expand Up @@ -1273,7 +1273,7 @@ func flattenKubernetesClusterDataSourceMicrosoftDefender(input *managedclusters.
}
}

func flattenManagedClusterUpgradeSettings(input *managedclusters.AgentPoolUpgradeSettings) []interface{} {
func flattenKubernetesClusterDataSourceUpgradeSettings(input *managedclusters.AgentPoolUpgradeSettings) []interface{} {
maxSurge := ""
if input != nil && input.MaxSurge != nil {
maxSurge = *input.MaxSurge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ func dataSourceKubernetesClusterNodePoolRead(d *pluginsdk.ResourceData, meta int
osDiskType = *props.OsDiskType
}
d.Set("os_disk_type", string(osDiskType))
d.Set("os_type", string(*props.OsType))

if props.OsDiskType != nil {
d.Set("os_type", string(*props.OsType))
}

// not returned from the API if not Spot
priority := string(agentpools.ScaleSetPriorityRegular)
Expand Down
141 changes: 55 additions & 86 deletions internal/services/containers/kubernetes_cluster_node_pool_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ func resourceKubernetesClusterNodePool() *pluginsdk.Resource {
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(managedclusters.ScaleSetEvictionPolicyDelete),
string(managedclusters.ScaleSetEvictionPolicyDeallocate),
string(agentpools.ScaleSetEvictionPolicyDelete),
string(agentpools.ScaleSetEvictionPolicyDeallocate),
}, false),
},

Expand All @@ -142,8 +142,8 @@ func resourceKubernetesClusterNodePool() *pluginsdk.Resource {
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
string(managedclusters.KubeletDiskTypeOS),
string(managedclusters.KubeletDiskTypeTemporary),
string(agentpools.KubeletDiskTypeOS),
string(agentpools.KubeletDiskTypeTemporary),
}, false),
},

Expand All @@ -170,10 +170,10 @@ func resourceKubernetesClusterNodePool() *pluginsdk.Resource {
"mode": {
Type: pluginsdk.TypeString,
Optional: true,
Default: string(managedclusters.AgentPoolModeUser),
Default: string(agentpools.AgentPoolModeUser),
ValidateFunc: validation.StringInSlice([]string{
string(managedclusters.AgentPoolModeSystem),
string(managedclusters.AgentPoolModeUser),
string(agentpools.AgentPoolModeSystem),
string(agentpools.AgentPoolModeUser),
}, false),
},

Expand Down Expand Up @@ -230,10 +230,10 @@ func resourceKubernetesClusterNodePool() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Default: managedclusters.OSDiskTypeManaged,
Default: agentpools.OSDiskTypeManaged,
ValidateFunc: validation.StringInSlice([]string{
string(managedclusters.OSDiskTypeEphemeral),
string(managedclusters.OSDiskTypeManaged),
string(agentpools.OSDiskTypeEphemeral),
string(agentpools.OSDiskTypeManaged),
}, false),
},

Expand All @@ -243,19 +243,19 @@ func resourceKubernetesClusterNodePool() *pluginsdk.Resource {
ForceNew: true,
Computed: true, // defaults to Ubuntu if using Linux
ValidateFunc: validation.StringInSlice([]string{
string(managedclusters.OSSKUUbuntu),
string(managedclusters.OSSKUCBLMariner),
string(agentpools.OSSKUUbuntu),
string(agentpools.OSSKUCBLMariner),
}, false),
},

"os_type": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Default: string(managedclusters.OSTypeLinux),
Default: string(agentpools.OSTypeLinux),
ValidateFunc: validation.StringInSlice([]string{
string(managedclusters.OSTypeLinux),
string(managedclusters.OSTypeWindows),
string(agentpools.OSTypeLinux),
string(agentpools.OSTypeWindows),
}, false),
},

Expand All @@ -270,10 +270,10 @@ func resourceKubernetesClusterNodePool() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Default: string(managedclusters.ScaleSetPriorityRegular),
Default: string(agentpools.ScaleSetPriorityRegular),
ValidateFunc: validation.StringInSlice([]string{
string(managedclusters.ScaleSetPriorityRegular),
string(managedclusters.ScaleSetPrioritySpot),
string(agentpools.ScaleSetPriorityRegular),
string(agentpools.ScaleSetPrioritySpot),
}, false),
},

Expand All @@ -295,10 +295,10 @@ func resourceKubernetesClusterNodePool() *pluginsdk.Resource {
"scale_down_mode": {
Type: pluginsdk.TypeString,
Optional: true,
Default: string(managedclusters.ScaleDownModeDelete),
Default: string(agentpools.ScaleDownModeDelete),
ValidateFunc: validation.StringInSlice([]string{
string(managedclusters.ScaleDownModeDeallocate),
string(managedclusters.ScaleDownModeDelete),
string(agentpools.ScaleDownModeDeallocate),
string(agentpools.ScaleDownModeDelete),
}, false),
},

Expand All @@ -322,8 +322,8 @@ func resourceKubernetesClusterNodePool() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(managedclusters.WorkloadRuntimeOCIContainer),
string(managedclusters.WorkloadRuntimeWasmWasi),
string(agentpools.WorkloadRuntimeOCIContainer),
string(agentpools.WorkloadRuntimeWasmWasi),
}, false),
},
"zones": commonschema.ZonesMultipleOptionalForceNew(),
Expand Down Expand Up @@ -361,7 +361,7 @@ func resourceKubernetesClusterNodePoolCreate(d *pluginsdk.ResourceData, meta int
props := model.Properties
if pools := props.AgentPoolProfiles; pools != nil {
for _, p := range *pools {
if *p.Type == managedclusters.AgentPoolTypeVirtualMachineScaleSets {
if p.Type != nil && *p.Type == managedclusters.AgentPoolTypeVirtualMachineScaleSets {
defaultPoolIsVMSS = true
break
}
Expand All @@ -385,67 +385,49 @@ func resourceKubernetesClusterNodePoolCreate(d *pluginsdk.ResourceData, meta int

count := d.Get("node_count").(int)
enableAutoScaling := d.Get("enable_auto_scaling").(bool)
evictionPolicyRaw := d.Get("eviction_policy").(string)
evictionPolicy := d.Get("eviction_policy").(string)
mode := agentpools.AgentPoolMode(d.Get("mode").(string))
osType := d.Get("os_type").(string)
priority := d.Get("priority").(string)
spotMaxPrice := d.Get("spot_max_price").(float64)
t := d.Get("tags").(map[string]interface{})

vmss := agentpools.AgentPoolTypeVirtualMachineScaleSets
profile := agentpools.ManagedClusterAgentPoolProfileProperties{
OsType: utils.ToPtr(agentpools.OSType(osType)),
EnableAutoScaling: utils.Bool(enableAutoScaling),
EnableFIPS: utils.Bool(d.Get("fips_enabled").(bool)),
EnableEncryptionAtHost: utils.Bool(d.Get("enable_host_encryption").(bool)),
EnableUltraSSD: utils.Bool(d.Get("ultra_ssd_enabled").(bool)),
EnableNodePublicIP: utils.Bool(d.Get("enable_node_public_ip").(bool)),
KubeletDiskType: utils.ToPtr(agentpools.KubeletDiskType(d.Get("kubelet_disk_type").(string))),
Mode: utils.ToPtr(mode),
ScaleSetPriority: utils.ToPtr(agentpools.ScaleSetPriority(d.Get("priority").(string))),
Tags: tags.Expand(t),
Type: &vmss,
Type: utils.ToPtr(agentpools.AgentPoolTypeVirtualMachineScaleSets),
VmSize: utils.String(d.Get("vm_size").(string)),
UpgradeSettings: expandAgentPoolUpgradeSettings(d.Get("upgrade_settings").([]interface{})),

// this must always be sent during creation, but is optional for auto-scaled clusters during update
// Count: utils.Int32(int32(count)),
Count: utils.Int64(int64(count)),
}

if mode := d.Get("mode").(string); mode != "" {
agentPoolMode := agentpools.AgentPoolMode(mode)
profile.Mode = &agentPoolMode
if osSku := d.Get("os_sku").(string); osSku != "" {
profile.OsSKU = utils.ToPtr(agentpools.OSSKU(osSku))
}

if kubeletDiskTypeRaw := d.Get("kubelet_disk_type").(string); kubeletDiskTypeRaw != "" {
kubeletDiskType := agentpools.KubeletDiskType(kubeletDiskTypeRaw)
profile.KubeletDiskType = &kubeletDiskType
if scaleDownMode := d.Get("scale_down_mode").(string); scaleDownMode != "" {
profile.ScaleDownMode = utils.ToPtr(agentpools.ScaleDownMode(scaleDownMode))
}

if osTypeRaw := d.Get("os_type").(string); osTypeRaw != "" {
osType := agentpools.OSType(osTypeRaw)
profile.OsType = &osType
if workloadRuntime := d.Get("workload_runtime").(string); workloadRuntime != "" {
profile.WorkloadRuntime = utils.ToPtr(agentpools.WorkloadRuntime(workloadRuntime))
}

if osSkuRaw := d.Get("os_sku").(string); osSkuRaw != "" {
osSku := agentpools.OSSKU(osSkuRaw)
profile.OsSKU = &osSku
}

if scaleDownModeRaw := d.Get("scale_down_mode").(string); scaleDownModeRaw != "" {
scaleDownMode := agentpools.ScaleDownMode(scaleDownModeRaw)
profile.ScaleDownMode = &scaleDownMode
}

if workloadRuntimeRaw := d.Get("workload_runtime").(string); workloadRuntimeRaw != "" {
workloadRuntime := agentpools.WorkloadRuntime(workloadRuntimeRaw)
profile.WorkloadRuntime = &workloadRuntime
}

if priorityRaw := d.Get("priority").(string); priorityRaw != "" {
priority := agentpools.ScaleSetPriority(priorityRaw)
profile.ScaleSetPriority = &priority
}

if d.Get("priority").(string) == string(managedclusters.ScaleSetPrioritySpot) {
evictionPolicy := agentpools.ScaleSetEvictionPolicy(evictionPolicyRaw)
profile.ScaleSetEvictionPolicy = &evictionPolicy
if priority == string(managedclusters.ScaleSetPrioritySpot) {
profile.ScaleSetEvictionPolicy = utils.ToPtr(agentpools.ScaleSetEvictionPolicy(evictionPolicy))
profile.SpotMaxPrice = utils.Float(spotMaxPrice)
} else {
if evictionPolicyRaw != "" {
if evictionPolicy != "" {
return fmt.Errorf("`eviction_policy` can only be set when `priority` is set to `Spot`")
}

Expand All @@ -472,9 +454,9 @@ func resourceKubernetesClusterNodePoolCreate(d *pluginsdk.ResourceData, meta int
profile.MaxPods = utils.Int64(maxPods)
}

nodeLabelsRaw := d.Get("node_labels").(map[string]string)
if len(nodeLabelsRaw) > 0 {
profile.NodeLabels = &nodeLabelsRaw
nodeLabels := d.Get("node_labels").(map[string]string)
if len(nodeLabels) > 0 {
profile.NodeLabels = utils.ToPtr(nodeLabels)
}

if nodePublicIPPrefixID := d.Get("node_public_ip_prefix_id").(string); nodePublicIPPrefixID != "" {
Expand Down Expand Up @@ -503,9 +485,8 @@ func resourceKubernetesClusterNodePoolCreate(d *pluginsdk.ResourceData, meta int
profile.ProximityPlacementGroupID = &proximityPlacementGroupId
}

if osDiskTypeRaw := d.Get("os_disk_type").(string); osDiskTypeRaw != "" {
osDiskType := agentpools.OSDiskType(osDiskTypeRaw)
profile.OsDiskType = &osDiskType
if osDiskType := d.Get("os_disk_type").(string); osDiskType != "" {
profile.OsDiskType = utils.ToPtr(agentpools.OSDiskType(osDiskType))
}

if podSubnetID := d.Get("pod_subnet_id").(string); podSubnetID != "" {
Expand Down Expand Up @@ -557,7 +538,7 @@ func resourceKubernetesClusterNodePoolCreate(d *pluginsdk.ResourceData, meta int
}

if linuxOSConfig := d.Get("linux_os_config").([]interface{}); len(linuxOSConfig) > 0 {
if d.Get("os_type").(string) != string(managedclusters.OSTypeLinux) {
if osType != string(managedclusters.OSTypeLinux) {
return fmt.Errorf("`linux_os_config` can only be configured when `os_type` is set to `linux`")
}
linuxOSConfig, err := expandAgentPoolLinuxOSConfig(linuxOSConfig)
Expand Down Expand Up @@ -787,14 +768,17 @@ func resourceKubernetesClusterNodePoolRead(d *pluginsdk.ResourceData, meta inter
d.Set("enable_host_encryption", props.EnableEncryptionAtHost)
d.Set("fips_enabled", props.EnableFIPS)
d.Set("ultra_ssd_enabled", props.EnableUltraSSD)

if v := props.KubeletDiskType; v != nil {
d.Set("kubelet_disk_type", string(*v))
}

scaleDownMode := string(managedclusters.ScaleDownModeDelete)
if v := props.ScaleDownMode; v != nil {
scaleDownMode = string(*v)
}
d.Set("scale_down_mode", scaleDownMode)

if v := props.WorkloadRuntime; v != nil {
d.Set("workload_runtime", string(*v))
}
Expand Down Expand Up @@ -925,13 +909,11 @@ func resourceKubernetesClusterNodePoolDelete(d *pluginsdk.ResourceData, meta int
if err != nil {
return err
}
ignorePodDisruptionBudget := true

deleteOperationsOptions := agentpools.DeleteOperationOptions{
ignorePodDisruptionBudget := true
future, err := client.Delete(ctx, *id, agentpools.DeleteOperationOptions{
IgnorePodDisruptionBudget: &ignorePodDisruptionBudget,
}

future, err := client.Delete(ctx, *id, deleteOperationsOptions)
})
if err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}
Expand Down Expand Up @@ -1028,19 +1010,6 @@ func expandAgentPoolUpgradeSettings(input []interface{}) *agentpools.AgentPoolUp
return setting
}

func expandClusterNodePoolUpgradeSettings(input []interface{}) *managedclusters.AgentPoolUpgradeSettings {
setting := &managedclusters.AgentPoolUpgradeSettings{}
if len(input) == 0 || input[0] == nil {
return setting
}

v := input[0].(map[string]interface{})
if maxSurgeRaw := v["max_surge"].(string); maxSurgeRaw != "" {
setting.MaxSurge = utils.String(maxSurgeRaw)
}
return setting
}

func flattenAgentPoolUpgradeSettings(input *agentpools.AgentPoolUpgradeSettings) []interface{} {
maxSurge := ""
if input != nil && input.MaxSurge != nil {
Expand Down
Loading

0 comments on commit 5bebf31

Please sign in to comment.