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

AKS enable autoscaling #3174

Closed
wants to merge 14 commits into from
2 changes: 1 addition & 1 deletion azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-06-01/compute"
"github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
"github.com/Azure/azure-sdk-for-go/services/containerregistry/mgmt/2017-10-01/containerregistry"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2018-03-31/containerservice"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-02-01/containerservice"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2015-04-08/documentdb"
"github.com/Azure/azure-sdk-for-go/services/databricks/mgmt/2018-04-01/databricks"
"github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory"
Expand Down
31 changes: 29 additions & 2 deletions azurerm/data_source_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2018-03-31/containerservice"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-02-01/containerservice"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/kubernetes"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -112,6 +112,21 @@ func dataSourceArmKubernetesCluster() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},

"enable_autoscaling": {
Type: schema.TypeBool,
Computed: true,
},

"min_count": {
Type: schema.TypeInt,
Computed: true,
},

"max_count": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
Expand Down Expand Up @@ -579,6 +594,18 @@ func flattenKubernetesClusterDataSourceAgentPoolProfiles(input *[]containerservi
agentPoolProfile["max_pods"] = int(*profile.MaxPods)
}

if profile.MinCount != nil {
agentPoolProfile["min_count"] = int(*profile.MinCount)
}

if profile.MaxCount != nil {
agentPoolProfile["max_count"] = int(*profile.MaxCount)
}

if profile.EnableAutoScaling != nil {
agentPoolProfile["enable_autoscaling"] = *profile.EnableAutoScaling
}

agentPoolProfiles = append(agentPoolProfiles, agentPoolProfile)
}

Expand Down Expand Up @@ -612,7 +639,7 @@ func flattenKubernetesClusterDataSourceLinuxProfile(input *containerservice.Linu
return []interface{}{values}
}

func flattenKubernetesClusterDataSourceNetworkProfile(profile *containerservice.NetworkProfile) []interface{} {
func flattenKubernetesClusterDataSourceNetworkProfile(profile *containerservice.NetworkProfileType) []interface{} {
values := make(map[string]interface{})

values["network_plugin"] = profile.NetworkPlugin
Expand Down
50 changes: 50 additions & 0 deletions azurerm/data_source_kubernetes_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,44 @@ func TestAccDataSourceAzureRMKubernetesCluster_basic(t *testing.T) {
})
}

func TestAccDataSourceAzureRMKubernetesCluster_autoscaling(t *testing.T) {
dataSourceName := "data.azurerm_kubernetes_cluster.test"
ri := tf.AccRandTimeInt()
clientId := os.Getenv("ARM_CLIENT_ID")
clientSecret := os.Getenv("ARM_CLIENT_SECRET")
location := testLocation()
config := testAccDataSourceAzureRMKubernetesCluster_autoscaling(ri, clientId, clientSecret, location)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKubernetesClusterDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKubernetesClusterExists(dataSourceName),
resource.TestCheckResourceAttr(dataSourceName, "role_based_access_control.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "role_based_access_control.0.enabled", "false"),
resource.TestCheckResourceAttrSet(dataSourceName, "kube_config.0.client_key"),
resource.TestCheckResourceAttrSet(dataSourceName, "kube_config.0.client_certificate"),
resource.TestCheckResourceAttrSet(dataSourceName, "kube_config.0.cluster_ca_certificate"),
resource.TestCheckResourceAttrSet(dataSourceName, "kube_config.0.host"),
resource.TestCheckResourceAttrSet(dataSourceName, "kube_config.0.username"),
resource.TestCheckResourceAttrSet(dataSourceName, "kube_config.0.password"),
resource.TestCheckResourceAttr(dataSourceName, "kube_admin_config.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "kube_admin_config_raw", ""),
resource.TestCheckResourceAttr(dataSourceName, "agent_pool_profile.0.type", "VirtualMachineScaleSets"),
resource.TestCheckResourceAttr(dataSourceName, "agent_pool_profile.0.enable_autoscaling", "true"),
resource.TestCheckResourceAttr(dataSourceName, "agent_pool_profile.0.min_count", "1"),
resource.TestCheckResourceAttr(dataSourceName, "agent_pool_profile.0.count", "1"),
resource.TestCheckResourceAttr(dataSourceName, "agent_pool_profile.0.max_count", "3"),
),
},
},
})
}

func TestAccDataSourceAzureRMKubernetesCluster_roleBasedAccessControl(t *testing.T) {
dataSourceName := "data.azurerm_kubernetes_cluster.test"
ri := tf.AccRandTimeInt()
Expand Down Expand Up @@ -429,6 +467,18 @@ data "azurerm_kubernetes_cluster" "test" {
`, r)
}

func testAccDataSourceAzureRMKubernetesCluster_autoscaling(rInt int, clientId string, clientSecret string, location string) string {
r := testAccAzureRMKubernetesCluster_vmss_autoscale(rInt, clientId, clientSecret, location)
return fmt.Sprintf(`
%s

data "azurerm_kubernetes_cluster" "test" {
name = "${azurerm_kubernetes_cluster.test.name}"
resource_group_name = "${azurerm_kubernetes_cluster.test.resource_group_name}"
}
`, r)
}

func testAccDataSourceAzureRMKubernetesCluster_roleBasedAccessControl(rInt int, location, clientId, clientSecret string) string {
resource := testAccAzureRMKubernetesCluster_roleBasedAccessControl(rInt, location, clientId, clientSecret)
return fmt.Sprintf(`
Expand Down
2 changes: 1 addition & 1 deletion azurerm/resource_arm_container_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"bytes"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2018-03-31/containerservice"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-02-01/containerservice"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
Expand Down
116 changes: 104 additions & 12 deletions azurerm/resource_arm_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"
"strings"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2018-03-31/containerservice"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-02-01/containerservice"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
Expand Down Expand Up @@ -163,6 +163,35 @@ func resourceArmKubernetesCluster() *schema.Resource {
Computed: true,
ForceNew: true,
},

"enable_autoscaling": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},

"min_count": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 100),
},

"type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: containerservice.AvailabilitySet,
ValidateFunc: validation.StringInSlice([]string{
string(containerservice.AvailabilitySet),
string(containerservice.VirtualMachineScaleSets),
}, false),
},

"max_count": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 100),
},
},
},
},
Expand Down Expand Up @@ -319,8 +348,8 @@ func resourceArmKubernetesCluster() *schema.Resource {
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(containerservice.Calico),
string(containerservice.Azure),
string(containerservice.NetworkPolicyCalico),
string(containerservice.NetworkPolicyAzure),
}, false),
},

Expand Down Expand Up @@ -549,6 +578,43 @@ func resourceArmKubernetesClusterCreateUpdate(d *schema.ResourceData, meta inter
networkProfile := expandKubernetesClusterNetworkProfile(d)
addonProfiles := expandKubernetesClusterAddonProfiles(d)

if agentProfiles[0].Type == containerservice.AvailabilitySet && agentProfiles[0].EnableAutoScaling != nil && *agentProfiles[0].EnableAutoScaling {
return fmt.Errorf("`enable_autoscaling` must be `false` if `type` is `AvailabilitySet`.")
}

if agentProfiles[0].EnableAutoScaling != nil && !*agentProfiles[0].EnableAutoScaling {
if agentProfiles[0].MaxCount != nil {
return fmt.Errorf("`max_count` cannot be set unless `enable_autoscaling` is set to `true`.")
}
if agentProfiles[0].MinCount != nil {
return fmt.Errorf("`min_count` cannot be set unless `enable_autoscaling` is set to `true`.")
}
}

if agentProfiles[0].MaxCount != nil && agentProfiles[0].MinCount != nil {
if *agentProfiles[0].MaxCount < *agentProfiles[0].MinCount {
return fmt.Errorf("`max_count` must be greater than `min_count`.")
}

if agentProfiles[0].Count != nil {
if *agentProfiles[0].Count < *agentProfiles[0].MinCount {
return fmt.Errorf("`count` must be greater than `min_count`.")
}

if *agentProfiles[0].Count > *agentProfiles[0].MaxCount {
return fmt.Errorf("`count` must be less than `max_count`.")
}
}
}

if *agentProfiles[0].EnableAutoScaling && agentProfiles[0].MaxCount == nil {
return fmt.Errorf("`max_count` must be set when `enable_autoscaling` is set to `true`.")
}

if *agentProfiles[0].EnableAutoScaling && agentProfiles[0].MinCount == nil {
return fmt.Errorf("`min_count` must be set when `enable_autoscaling` is set to `true`.")
}

tags := d.Get("tags").(map[string]interface{})

// we can't do this in the CustomizeDiff since the interpolations aren't evaluated at that point
Expand Down Expand Up @@ -883,14 +949,28 @@ func expandKubernetesClusterAgentPoolProfiles(d *schema.ResourceData) []containe
vmSize := config["vm_size"].(string)
osDiskSizeGB := int32(config["os_disk_size_gb"].(int))
osType := config["os_type"].(string)
enableAutoscaling := config["enable_autoscaling"].(bool)

profile := containerservice.ManagedClusterAgentPoolProfile{
Name: utils.String(name),
Count: utils.Int32(count),
VMSize: containerservice.VMSizeTypes(vmSize),
OsDiskSizeGB: utils.Int32(osDiskSizeGB),
StorageProfile: containerservice.ManagedDisks,
OsType: containerservice.OSType(osType),
EnableAutoScaling: utils.Bool(enableAutoscaling),
Name: utils.String(name),
Count: utils.Int32(count),
VMSize: containerservice.VMSizeTypes(vmSize),
OsDiskSizeGB: utils.Int32(osDiskSizeGB),
OsType: containerservice.OSType(osType),
}

agentType := config["type"].(string)
profile.Type = containerservice.AgentPoolType(agentType)

if enableAutoscaling {
if minCount := int32(config["min_count"].(int)); minCount >= 0 {
profile.MinCount = utils.Int32(minCount)
}

if maxCount := int32(config["max_count"].(int)); maxCount > 0 {
profile.MaxCount = utils.Int32(maxCount)
}
}

if maxPods := int32(config["max_pods"].(int)); maxPods > 0 {
Expand Down Expand Up @@ -948,6 +1028,18 @@ func flattenKubernetesClusterAgentPoolProfiles(profiles *[]containerservice.Mana
agentPoolProfile["max_pods"] = int(*profile.MaxPods)
}

agentPoolProfile["type"] = string(profile.Type)
if profile.MinCount != nil {
agentPoolProfile["min_count"] = int(*profile.MinCount)
}
if profile.MaxCount != nil {
agentPoolProfile["max_count"] = int(*profile.MaxCount)
}

if profile.EnableAutoScaling != nil {
agentPoolProfile["enable_autoscaling"] = *profile.EnableAutoScaling
}

agentPoolProfiles = append(agentPoolProfiles, agentPoolProfile)
}

Expand Down Expand Up @@ -1015,7 +1107,7 @@ func flattenKubernetesClusterLinuxProfile(profile *containerservice.LinuxProfile
return []interface{}{values}
}

func expandKubernetesClusterNetworkProfile(d *schema.ResourceData) *containerservice.NetworkProfile {
func expandKubernetesClusterNetworkProfile(d *schema.ResourceData) *containerservice.NetworkProfileType {
configs := d.Get("network_profile").([]interface{})
if len(configs) == 0 {
return nil
Expand All @@ -1027,7 +1119,7 @@ func expandKubernetesClusterNetworkProfile(d *schema.ResourceData) *containerser

networkPolicy := config["network_policy"].(string)

networkProfile := containerservice.NetworkProfile{
networkProfile := containerservice.NetworkProfileType{
NetworkPlugin: containerservice.NetworkPlugin(networkPlugin),
NetworkPolicy: containerservice.NetworkPolicy(networkPolicy),
}
Expand Down Expand Up @@ -1055,7 +1147,7 @@ func expandKubernetesClusterNetworkProfile(d *schema.ResourceData) *containerser
return &networkProfile
}

func flattenKubernetesClusterNetworkProfile(profile *containerservice.NetworkProfile) []interface{} {
func flattenKubernetesClusterNetworkProfile(profile *containerservice.NetworkProfileType) []interface{} {
if profile == nil {
return []interface{}{}
}
Expand Down
Loading