From cc648451a28d60986fdf2f51d33eabe42cb4f654 Mon Sep 17 00:00:00 2001 From: Su Shi <1684739+metacpp@users.noreply.github.com> Date: Thu, 23 Aug 2018 18:13:03 -0700 Subject: [PATCH 01/10] :bug: (Linux profile of AKS.) Make linux_profile be optional. The AKS API does not require linux_profile to be set by default, this is enabled by default on Azure portal. Terraform's schema does not align with this, which causes importing issue. --- .../data_source_kubernetes_cluster_test.go | 2 +- azurerm/resource_arm_kubernetes_cluster.go | 13 +++-- .../resource_arm_kubernetes_cluster_test.go | 54 ++++++++++++++----- .../docs/r/kubernetes_cluster.html.markdown | 10 +--- 4 files changed, 53 insertions(+), 26 deletions(-) diff --git a/azurerm/data_source_kubernetes_cluster_test.go b/azurerm/data_source_kubernetes_cluster_test.go index 7380984a9d90..c7ec0249ade2 100644 --- a/azurerm/data_source_kubernetes_cluster_test.go +++ b/azurerm/data_source_kubernetes_cluster_test.go @@ -233,7 +233,7 @@ func TestAccDataSourceAzureRMKubernetesCluster_addOnProfileRouting(t *testing.T) } func testAccDataSourceAzureRMKubernetesCluster_basic(rInt int, clientId string, clientSecret string, location string) string { - resource := testAccAzureRMKubernetesCluster_basic(rInt, clientId, clientSecret, location) + resource := testAccAzureRMKubernetesCluster_basic(rInt, clientId, clientSecret, location, "") return fmt.Sprintf(` %s diff --git a/azurerm/resource_arm_kubernetes_cluster.go b/azurerm/resource_arm_kubernetes_cluster.go index 2e245b57d09c..48f48296bed8 100644 --- a/azurerm/resource_arm_kubernetes_cluster.go +++ b/azurerm/resource_arm_kubernetes_cluster.go @@ -135,7 +135,7 @@ func resourceArmKubernetesCluster() *schema.Resource { "linux_profile": { Type: schema.TypeList, - Required: true, + Optional: true, MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -403,7 +403,7 @@ func resourceArmKubernetesClusterCreate(d *schema.ResourceData, meta interface{} AgentPoolProfiles: &agentProfiles, DNSPrefix: &dnsPrefix, KubernetesVersion: &kubernetesVersion, - LinuxProfile: &linuxProfile, + LinuxProfile: linuxProfile, ServicePrincipalProfile: servicePrincipalProfile, NetworkProfile: networkProfile, }, @@ -690,8 +690,13 @@ func flattenKubernetesClusterKubeConfig(config kubernetes.KubeConfig) []interfac return []interface{}{values} } -func expandAzureRmKubernetesClusterLinuxProfile(d *schema.ResourceData) containerservice.LinuxProfile { +func expandAzureRmKubernetesClusterLinuxProfile(d *schema.ResourceData) *containerservice.LinuxProfile { profiles := d.Get("linux_profile").([]interface{}) + + if len(profiles) == 0 { + return nil + } + config := profiles[0].(map[string]interface{}) adminUsername := config["admin_username"].(string) @@ -714,7 +719,7 @@ func expandAzureRmKubernetesClusterLinuxProfile(d *schema.ResourceData) containe }, } - return profile + return &profile } func expandAzureRmKubernetesClusterServicePrincipal(d *schema.ResourceData) *containerservice.ServicePrincipalProfile { diff --git a/azurerm/resource_arm_kubernetes_cluster_test.go b/azurerm/resource_arm_kubernetes_cluster_test.go index f40e23808205..d9bd2d8ca5fc 100644 --- a/azurerm/resource_arm_kubernetes_cluster_test.go +++ b/azurerm/resource_arm_kubernetes_cluster_test.go @@ -74,7 +74,43 @@ func TestAccAzureRMKubernetesCluster_basic(t *testing.T) { ri := acctest.RandInt() clientId := os.Getenv("ARM_CLIENT_ID") clientSecret := os.Getenv("ARM_CLIENT_SECRET") - config := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation()) + config := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation(), "") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMKubernetesClusterDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMKubernetesClusterExists(resourceName), + resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.client_key"), + resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.client_certificate"), + resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.cluster_ca_certificate"), + resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.host"), + resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.username"), + resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.password"), + resource.TestCheckResourceAttrSet(resourceName, "agent_pool_profile.0.max_pods"), + ), + }, + }, + }) +} + +func TestAccAzureRMKubernetesCluster_basicWithLinuxProfile(t *testing.T) { + resourceName := "azurerm_kubernetes_cluster.test" + ri := acctest.RandInt() + clientId := os.Getenv("ARM_CLIENT_ID") + clientSecret := os.Getenv("ARM_CLIENT_SECRET") + config := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation(), fmt.Sprintf(` +linux_profile { + admin_username = "acctestuser%d" + + ssh_key { + key_data = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCqaZoyiz1qbdOQ8xEf6uEu1cCwYowo5FHtsBhqLoDnnp7KUTEBN+L2NxRIfQ781rxV6Iq5jSav6b2Q8z5KiseOlvKA/RF2wqU0UPYqQviQhLmW6THTpmrv/YkUCuzxDpsH7DUDhZcwySLKVVe0Qm3+5N2Ta6UYH3lsDf9R9wTP2K/+vAnflKebuypNlmocIvakFWoZda18FOmsOoIVXQ8HWFNCuw9ZCunMSN62QGamCe3dL5cXlkgHYv7ekJE15IA9aOJcM7e90oeTqo+7HTcWfdu0qQqPWY5ujyMw/llas8tsXY85LFqRnr3gJ02bAscjc477+X+j/gkpFoN1QEmt terraform@demo.tld" + } +}`, ri)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -103,7 +139,7 @@ func TestAccAzureRMKubernetesCluster_addAgent(t *testing.T) { ri := acctest.RandInt() clientId := os.Getenv("ARM_CLIENT_ID") clientSecret := os.Getenv("ARM_CLIENT_SECRET") - initConfig := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation()) + initConfig := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation(), "") addAgentConfig := testAccAzureRMKubernetesCluster_addAgent(ri, clientId, clientSecret, testLocation()) resource.Test(t, resource.TestCase{ @@ -132,7 +168,7 @@ func TestAccAzureRMKubernetesCluster_upgradeConfig(t *testing.T) { ri := acctest.RandInt() clientId := os.Getenv("ARM_CLIENT_ID") clientSecret := os.Getenv("ARM_CLIENT_SECRET") - initConfig := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation()) + initConfig := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation(), "") upgradeConfig := testAccAzureRMKubernetesCluster_upgrade(ri, clientId, clientSecret, testLocation()) resource.Test(t, resource.TestCase{ @@ -324,7 +360,7 @@ func TestAccAzureRMKubernetesCluster_advancedNetworkingAzureComplete(t *testing. }) } -func testAccAzureRMKubernetesCluster_basic(rInt int, clientId string, clientSecret string, location string) string { +func testAccAzureRMKubernetesCluster_basic(rInt int, clientId string, clientSecret string, location string, linuxProfile string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { name = "acctestRG-%d" @@ -338,13 +374,7 @@ resource "azurerm_kubernetes_cluster" "test" { dns_prefix = "acctestaks%d" kubernetes_version = "1.7.7" - linux_profile { - admin_username = "acctestuser%d" - - ssh_key { - key_data = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCqaZoyiz1qbdOQ8xEf6uEu1cCwYowo5FHtsBhqLoDnnp7KUTEBN+L2NxRIfQ781rxV6Iq5jSav6b2Q8z5KiseOlvKA/RF2wqU0UPYqQviQhLmW6THTpmrv/YkUCuzxDpsH7DUDhZcwySLKVVe0Qm3+5N2Ta6UYH3lsDf9R9wTP2K/+vAnflKebuypNlmocIvakFWoZda18FOmsOoIVXQ8HWFNCuw9ZCunMSN62QGamCe3dL5cXlkgHYv7ekJE15IA9aOJcM7e90oeTqo+7HTcWfdu0qQqPWY5ujyMw/llas8tsXY85LFqRnr3gJ02bAscjc477+X+j/gkpFoN1QEmt terraform@demo.tld" - } - } + %s agent_pool_profile { name = "default" @@ -489,7 +519,7 @@ resource "azurerm_kubernetes_cluster" "test" { client_id = "%s" client_secret = "%s" } - + addon_profile { oms_agent { enabled = true diff --git a/website/docs/r/kubernetes_cluster.html.markdown b/website/docs/r/kubernetes_cluster.html.markdown index 49113a917c71..c34215d04c17 100644 --- a/website/docs/r/kubernetes_cluster.html.markdown +++ b/website/docs/r/kubernetes_cluster.html.markdown @@ -27,14 +27,6 @@ resource "azurerm_kubernetes_cluster" "test" { resource_group_name = "${azurerm_resource_group.test.name}" dns_prefix   = "acctestagent1" - linux_profile { - admin_username = "acctestuser1" - - ssh_key { - key_data = "ssh-rsa ..." - } - } - agent_pool_profile { name = "default" count = 1 @@ -180,7 +172,7 @@ The following arguments are supported: * `dns_prefix` - (Required) DNS prefix specified when creating the managed cluster. -* `linux_profile` - (Required) A Linux Profile block as documented below. +* `linux_profile` - (Optional) A Linux Profile block as documented below. * `agent_pool_profile` - (Required) One or more Agent Pool Profile's block as documented below. From db8ec61037daa4af5d2a98df903ac15dcc14b578 Mon Sep 17 00:00:00 2001 From: Su Shi <1684739+metacpp@users.noreply.github.com> Date: Thu, 23 Aug 2018 18:22:05 -0700 Subject: [PATCH 02/10] :green_heart: (Import test of AKS.) Pass empty string to refactored function. Pass empty string to refactored function in import test of AKS. --- azurerm/import_arm_kubernetes_cluster_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/import_arm_kubernetes_cluster_test.go b/azurerm/import_arm_kubernetes_cluster_test.go index a90fa889a9d4..505c693a9716 100644 --- a/azurerm/import_arm_kubernetes_cluster_test.go +++ b/azurerm/import_arm_kubernetes_cluster_test.go @@ -14,7 +14,7 @@ func TestAccAzureRMKubernetesCluster_importBasic(t *testing.T) { ri := acctest.RandInt() clientId := os.Getenv("ARM_CLIENT_ID") clientSecret := os.Getenv("ARM_CLIENT_SECRET") - config := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation()) + config := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation(), "") resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, From 535c434badf392a97c523c7a57d0589348e5acc1 Mon Sep 17 00:00:00 2001 From: Su Shi <1684739+metacpp@users.noreply.github.com> Date: Thu, 23 Aug 2018 18:44:45 -0700 Subject: [PATCH 03/10] :green_heart: (Basic test of AKS.) Missing the linux_profile configuration string. Added the linux_profile configuration string while baking the configuration. --- azurerm/resource_arm_kubernetes_cluster_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/resource_arm_kubernetes_cluster_test.go b/azurerm/resource_arm_kubernetes_cluster_test.go index d9bd2d8ca5fc..52a012066b78 100644 --- a/azurerm/resource_arm_kubernetes_cluster_test.go +++ b/azurerm/resource_arm_kubernetes_cluster_test.go @@ -387,7 +387,7 @@ resource "azurerm_kubernetes_cluster" "test" { client_secret = "%s" } } -`, rInt, location, rInt, rInt, rInt, clientId, clientSecret) +`, rInt, location, rInt, rInt, linuxProfile, clientId, clientSecret) } func testAccAzureRMKubernetesCluster_addAgent(rInt int, clientId string, clientSecret string, location string) string { From 30280d64917a088ae282770fe04230b90d08f1dd Mon Sep 17 00:00:00 2001 From: Su Shi <1684739+metacpp@users.noreply.github.com> Date: Thu, 23 Aug 2018 20:25:21 -0700 Subject: [PATCH 04/10] :green_heart: (Basic test of AKS.) Remove useless basic test with linux profile. Remove the basic test with linux profile to avoid string concat issue. --- .../data_source_kubernetes_cluster_test.go | 2 +- azurerm/import_arm_kubernetes_cluster_test.go | 2 +- .../resource_arm_kubernetes_cluster_test.go | 48 ++----------------- 3 files changed, 7 insertions(+), 45 deletions(-) diff --git a/azurerm/data_source_kubernetes_cluster_test.go b/azurerm/data_source_kubernetes_cluster_test.go index c7ec0249ade2..7380984a9d90 100644 --- a/azurerm/data_source_kubernetes_cluster_test.go +++ b/azurerm/data_source_kubernetes_cluster_test.go @@ -233,7 +233,7 @@ func TestAccDataSourceAzureRMKubernetesCluster_addOnProfileRouting(t *testing.T) } func testAccDataSourceAzureRMKubernetesCluster_basic(rInt int, clientId string, clientSecret string, location string) string { - resource := testAccAzureRMKubernetesCluster_basic(rInt, clientId, clientSecret, location, "") + resource := testAccAzureRMKubernetesCluster_basic(rInt, clientId, clientSecret, location) return fmt.Sprintf(` %s diff --git a/azurerm/import_arm_kubernetes_cluster_test.go b/azurerm/import_arm_kubernetes_cluster_test.go index 505c693a9716..a90fa889a9d4 100644 --- a/azurerm/import_arm_kubernetes_cluster_test.go +++ b/azurerm/import_arm_kubernetes_cluster_test.go @@ -14,7 +14,7 @@ func TestAccAzureRMKubernetesCluster_importBasic(t *testing.T) { ri := acctest.RandInt() clientId := os.Getenv("ARM_CLIENT_ID") clientSecret := os.Getenv("ARM_CLIENT_SECRET") - config := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation(), "") + config := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation()) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, diff --git a/azurerm/resource_arm_kubernetes_cluster_test.go b/azurerm/resource_arm_kubernetes_cluster_test.go index 52a012066b78..6f704e0a8d45 100644 --- a/azurerm/resource_arm_kubernetes_cluster_test.go +++ b/azurerm/resource_arm_kubernetes_cluster_test.go @@ -74,43 +74,7 @@ func TestAccAzureRMKubernetesCluster_basic(t *testing.T) { ri := acctest.RandInt() clientId := os.Getenv("ARM_CLIENT_ID") clientSecret := os.Getenv("ARM_CLIENT_SECRET") - config := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation(), "") - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testCheckAzureRMKubernetesClusterDestroy, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc( - testCheckAzureRMKubernetesClusterExists(resourceName), - resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.client_key"), - resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.client_certificate"), - resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.cluster_ca_certificate"), - resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.host"), - resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.username"), - resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.password"), - resource.TestCheckResourceAttrSet(resourceName, "agent_pool_profile.0.max_pods"), - ), - }, - }, - }) -} - -func TestAccAzureRMKubernetesCluster_basicWithLinuxProfile(t *testing.T) { - resourceName := "azurerm_kubernetes_cluster.test" - ri := acctest.RandInt() - clientId := os.Getenv("ARM_CLIENT_ID") - clientSecret := os.Getenv("ARM_CLIENT_SECRET") - config := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation(), fmt.Sprintf(` -linux_profile { - admin_username = "acctestuser%d" - - ssh_key { - key_data = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCqaZoyiz1qbdOQ8xEf6uEu1cCwYowo5FHtsBhqLoDnnp7KUTEBN+L2NxRIfQ781rxV6Iq5jSav6b2Q8z5KiseOlvKA/RF2wqU0UPYqQviQhLmW6THTpmrv/YkUCuzxDpsH7DUDhZcwySLKVVe0Qm3+5N2Ta6UYH3lsDf9R9wTP2K/+vAnflKebuypNlmocIvakFWoZda18FOmsOoIVXQ8HWFNCuw9ZCunMSN62QGamCe3dL5cXlkgHYv7ekJE15IA9aOJcM7e90oeTqo+7HTcWfdu0qQqPWY5ujyMw/llas8tsXY85LFqRnr3gJ02bAscjc477+X+j/gkpFoN1QEmt terraform@demo.tld" - } -}`, ri)) + config := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation()) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -139,7 +103,7 @@ func TestAccAzureRMKubernetesCluster_addAgent(t *testing.T) { ri := acctest.RandInt() clientId := os.Getenv("ARM_CLIENT_ID") clientSecret := os.Getenv("ARM_CLIENT_SECRET") - initConfig := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation(), "") + initConfig := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation()) addAgentConfig := testAccAzureRMKubernetesCluster_addAgent(ri, clientId, clientSecret, testLocation()) resource.Test(t, resource.TestCase{ @@ -168,7 +132,7 @@ func TestAccAzureRMKubernetesCluster_upgradeConfig(t *testing.T) { ri := acctest.RandInt() clientId := os.Getenv("ARM_CLIENT_ID") clientSecret := os.Getenv("ARM_CLIENT_SECRET") - initConfig := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation(), "") + initConfig := testAccAzureRMKubernetesCluster_basic(ri, clientId, clientSecret, testLocation()) upgradeConfig := testAccAzureRMKubernetesCluster_upgrade(ri, clientId, clientSecret, testLocation()) resource.Test(t, resource.TestCase{ @@ -360,7 +324,7 @@ func TestAccAzureRMKubernetesCluster_advancedNetworkingAzureComplete(t *testing. }) } -func testAccAzureRMKubernetesCluster_basic(rInt int, clientId string, clientSecret string, location string, linuxProfile string) string { +func testAccAzureRMKubernetesCluster_basic(rInt int, clientId string, clientSecret string, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { name = "acctestRG-%d" @@ -374,8 +338,6 @@ resource "azurerm_kubernetes_cluster" "test" { dns_prefix = "acctestaks%d" kubernetes_version = "1.7.7" - %s - agent_pool_profile { name = "default" count = "1" @@ -387,7 +349,7 @@ resource "azurerm_kubernetes_cluster" "test" { client_secret = "%s" } } -`, rInt, location, rInt, rInt, linuxProfile, clientId, clientSecret) +`, rInt, location, rInt, rInt, clientId, clientSecret) } func testAccAzureRMKubernetesCluster_addAgent(rInt int, clientId string, clientSecret string, location string) string { From 966a0e6676cf718866ce840c725f2d6ddc92124b Mon Sep 17 00:00:00 2001 From: Su Shi <1684739+metacpp@users.noreply.github.com> Date: Thu, 23 Aug 2018 22:25:17 -0700 Subject: [PATCH 05/10] :bug: (Linux profile of AKS.) Make linux_profile be computed. linux_profile needs to be computed since it's optional. Otherwise, return `terraform plan` will warn with change. --- azurerm/resource_arm_kubernetes_cluster.go | 1 + 1 file changed, 1 insertion(+) diff --git a/azurerm/resource_arm_kubernetes_cluster.go b/azurerm/resource_arm_kubernetes_cluster.go index 48f48296bed8..cfcfc212c380 100644 --- a/azurerm/resource_arm_kubernetes_cluster.go +++ b/azurerm/resource_arm_kubernetes_cluster.go @@ -136,6 +136,7 @@ func resourceArmKubernetesCluster() *schema.Resource { "linux_profile": { Type: schema.TypeList, Optional: true, + Computed: true, MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ From c10542faf86c49ba1f69e7c28ca40b836a93dc19 Mon Sep 17 00:00:00 2001 From: Su Shi <1684739+metacpp@users.noreply.github.com> Date: Fri, 24 Aug 2018 19:21:05 -0700 Subject: [PATCH 06/10] :bug: (AKS resource.) Return empty array if profile is nil. If the response of field is not returned in response body, need to return empty array. --- azurerm/resource_arm_kubernetes_cluster.go | 38 ++++++++++++++-------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/azurerm/resource_arm_kubernetes_cluster.go b/azurerm/resource_arm_kubernetes_cluster.go index cfcfc212c380..125903ef607a 100644 --- a/azurerm/resource_arm_kubernetes_cluster.go +++ b/azurerm/resource_arm_kubernetes_cluster.go @@ -136,7 +136,6 @@ func resourceArmKubernetesCluster() *schema.Resource { "linux_profile": { Type: schema.TypeList, Optional: true, - Computed: true, MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -533,33 +532,40 @@ func resourceArmKubernetesClusterDelete(d *schema.ResourceData, meta interface{} return future.WaitForCompletionRef(ctx, kubernetesClustersClient.Client) } -func flattenAzureRmKubernetesClusterLinuxProfile(input *containerservice.LinuxProfile) []interface{} { +func flattenAzureRmKubernetesClusterLinuxProfile(profile *containerservice.LinuxProfile) []interface{} { + if profile == nil { + return []interface{}{} + } + values := make(map[string]interface{}) sshKeys := make([]interface{}, 0) - if profile := input; profile != nil { - if username := profile.AdminUsername; username != nil { - values["admin_username"] = *username - } + if username := profile.AdminUsername; username != nil { + values["admin_username"] = *username + } - if ssh := profile.SSH; ssh != nil { - if keys := ssh.PublicKeys; keys != nil { - for _, sshKey := range *keys { - outputs := make(map[string]interface{}, 0) - if keyData := sshKey.KeyData; keyData != nil { - outputs["key_data"] = *keyData - } - sshKeys = append(sshKeys, outputs) + if ssh := profile.SSH; ssh != nil { + if keys := ssh.PublicKeys; keys != nil { + for _, sshKey := range *keys { + outputs := make(map[string]interface{}, 0) + if keyData := sshKey.KeyData; keyData != nil { + outputs["key_data"] = *keyData } + sshKeys = append(sshKeys, outputs) } } } + values["ssh_key"] = sshKeys return []interface{}{values} } func flattenAzureRmKubernetesClusterAgentPoolProfiles(profiles *[]containerservice.ManagedClusterAgentPoolProfile, fqdn *string) []interface{} { + if profiles == nil { + return []interface{}{} + } + agentPoolProfiles := make([]interface{}, 0) for _, profile := range *profiles { @@ -651,6 +657,10 @@ func flattenAzureRmKubernetesClusterAccessProfile(profile *containerservice.Mana } func flattenAzureRmKubernetesClusterNetworkProfile(profile *containerservice.NetworkProfile) []interface{} { + if profile == nil { + return []interface{}{} + } + values := make(map[string]interface{}) values["network_plugin"] = profile.NetworkPlugin From 2704c652ed58f58ce2a797b62f3d3d0fed9c782d Mon Sep 17 00:00:00 2001 From: Su Shi <1684739+metacpp@users.noreply.github.com> Date: Wed, 29 Aug 2018 15:18:38 -0700 Subject: [PATCH 07/10] :white_check_mark: (Tests for AKS.) Add 2 tests for linuxProfile of AKS. Add 2 tests to have more coverage on linuxProfile specific scenarios. --- azurerm/import_arm_kubernetes_cluster_test.go | 25 +++++++ .../resource_arm_kubernetes_cluster_test.go | 67 ++++++++++++++++++- 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/azurerm/import_arm_kubernetes_cluster_test.go b/azurerm/import_arm_kubernetes_cluster_test.go index a90fa889a9d4..c458261ec3bf 100644 --- a/azurerm/import_arm_kubernetes_cluster_test.go +++ b/azurerm/import_arm_kubernetes_cluster_test.go @@ -32,3 +32,28 @@ func TestAccAzureRMKubernetesCluster_importBasic(t *testing.T) { }, }) } + +func TestAccAzureRMKubernetesCluster_importLinuxProfile(t *testing.T) { + resourceName := "azurerm_kubernetes_cluster.test" + + ri := acctest.RandInt() + clientId := os.Getenv("ARM_CLIENT_ID") + clientSecret := os.Getenv("ARM_CLIENT_SECRET") + config := testAccAzureRMKubernetesCluster_linuxProfile(ri, clientId, clientSecret, testLocation()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMKubernetesClusterDestroy, + Steps: []resource.TestStep{ + { + Config: config, + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} \ No newline at end of file diff --git a/azurerm/resource_arm_kubernetes_cluster_test.go b/azurerm/resource_arm_kubernetes_cluster_test.go index 6f704e0a8d45..5a578c47eaba 100644 --- a/azurerm/resource_arm_kubernetes_cluster_test.go +++ b/azurerm/resource_arm_kubernetes_cluster_test.go @@ -98,6 +98,37 @@ func TestAccAzureRMKubernetesCluster_basic(t *testing.T) { }) } +func TestAccAzureRMKubernetesCluster_linuxProfile(t *testing.T) { + resourceName := "azurerm_kubernetes_cluster.test" + ri := acctest.RandInt() + clientId := os.Getenv("ARM_CLIENT_ID") + clientSecret := os.Getenv("ARM_CLIENT_SECRET") + config := testAccAzureRMKubernetesCluster_linuxProfile(ri, clientId, clientSecret, testLocation()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMKubernetesClusterDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMKubernetesClusterExists(resourceName), + resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.client_key"), + resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.client_certificate"), + resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.cluster_ca_certificate"), + resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.host"), + resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.username"), + resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.password"), + resource.TestCheckResourceAttrSet(resourceName, "agent_pool_profile.0.max_pods"), + resource.TestCheckResourceAttrSet(resourceName, "linux_pool_profile.0.admin_username"), + resource.TestCheckResourceAttrSet(resourceName, "linux_pool_profile.0.ssh_key"), + ), + }, + }, + }) +} + func TestAccAzureRMKubernetesCluster_addAgent(t *testing.T) { resourceName := "azurerm_kubernetes_cluster.test" ri := acctest.RandInt() @@ -336,7 +367,6 @@ resource "azurerm_kubernetes_cluster" "test" { location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" dns_prefix = "acctestaks%d" - kubernetes_version = "1.7.7" agent_pool_profile { name = "default" @@ -352,6 +382,41 @@ resource "azurerm_kubernetes_cluster" "test" { `, rInt, location, rInt, rInt, clientId, clientSecret) } +func testAccAzureRMKubernetesCluster_linuxProfile(rInt int, clientId string, clientSecret string, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%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" + + linux_profile { + admin_username = "acctestuser%d" + + ssh_key { + key_data = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCqaZoyiz1qbdOQ8xEf6uEu1cCwYowo5FHtsBhqLoDnnp7KUTEBN+L2NxRIfQ781rxV6Iq5jSav6b2Q8z5KiseOlvKA/RF2wqU0UPYqQviQhLmW6THTpmrv/YkUCuzxDpsH7DUDhZcwySLKVVe0Qm3+5N2Ta6UYH3lsDf9R9wTP2K/+vAnflKebuypNlmocIvakFWoZda18FOmsOoIVXQ8HWFNCuw9ZCunMSN62QGamCe3dL5cXlkgHYv7ekJE15IA9aOJcM7e90oeTqo+7HTcWfdu0qQqPWY5ujyMw/llas8tsXY85LFqRnr3gJ02bAscjc477+X+j/gkpFoN1QEmt terraform@demo.tld" + } + } + + agent_pool_profile { + name = "default" + count = "1" + vm_size = "Standard_DS2_v2" + } + + service_principal { + client_id = "%s" + client_secret = "%s" + } +} +`, rInt, location, rInt, rInt, rInt, clientId, clientSecret) +} + func testAccAzureRMKubernetesCluster_addAgent(rInt int, clientId string, clientSecret string, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { From e091e40cc9c1fa37c1aa39a53ad25e8dcbab1298 Mon Sep 17 00:00:00 2001 From: Su Shi <1684739+metacpp@users.noreply.github.com> Date: Wed, 29 Aug 2018 15:34:53 -0700 Subject: [PATCH 08/10] :recycle: (Test code for AKS.) Refactor the test code for AKS. Removed useless linux_profile and kubernetes_version settings for tests. --- .../resource_arm_kubernetes_cluster_test.go | 24 ++++++------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/azurerm/resource_arm_kubernetes_cluster_test.go b/azurerm/resource_arm_kubernetes_cluster_test.go index 5a578c47eaba..b59e84db9ea6 100644 --- a/azurerm/resource_arm_kubernetes_cluster_test.go +++ b/azurerm/resource_arm_kubernetes_cluster_test.go @@ -181,7 +181,7 @@ func TestAccAzureRMKubernetesCluster_upgradeConfig(t *testing.T) { Config: upgradeConfig, Check: resource.ComposeTestCheckFunc( testCheckAzureRMKubernetesClusterExists(resourceName), - resource.TestCheckResourceAttr(resourceName, "kubernetes_version", "1.8.1"), + resource.TestCheckResourceAttr(resourceName, "kubernetes_version", "1.11.2"), ), }, }, @@ -367,6 +367,7 @@ resource "azurerm_kubernetes_cluster" "test" { location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" dns_prefix = "acctestaks%d" + kubernetes_version = "1.10.7" agent_pool_profile { name = "default" @@ -429,15 +430,7 @@ resource "azurerm_kubernetes_cluster" "test" { location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" dns_prefix = "acctestaks%d" - kubernetes_version = "1.7.7" - - linux_profile { - admin_username = "acctestuser%d" - - ssh_key { - key_data = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCqaZoyiz1qbdOQ8xEf6uEu1cCwYowo5FHtsBhqLoDnnp7KUTEBN+L2NxRIfQ781rxV6Iq5jSav6b2Q8z5KiseOlvKA/RF2wqU0UPYqQviQhLmW6THTpmrv/YkUCuzxDpsH7DUDhZcwySLKVVe0Qm3+5N2Ta6UYH3lsDf9R9wTP2K/+vAnflKebuypNlmocIvakFWoZda18FOmsOoIVXQ8HWFNCuw9ZCunMSN62QGamCe3dL5cXlkgHYv7ekJE15IA9aOJcM7e90oeTqo+7HTcWfdu0qQqPWY5ujyMw/llas8tsXY85LFqRnr3gJ02bAscjc477+X+j/gkpFoN1QEmt terraform@demo.tld" - } - } + kubernetes_version = "1.10.7" agent_pool_profile { name = "default" @@ -450,7 +443,7 @@ resource "azurerm_kubernetes_cluster" "test" { client_secret = "%s" } } -`, rInt, location, rInt, rInt, rInt, clientId, clientSecret) +`, rInt, location, rInt, rInt, clientId, clientSecret) } func testAccAzureRMKubernetesCluster_internalNetwork(rInt int, clientId string, clientSecret string, location string) string { @@ -483,7 +476,6 @@ resource "azurerm_kubernetes_cluster" "test" { location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" dns_prefix = "acctestaks%d" - kubernetes_version = "1.7.7" linux_profile { admin_username = "acctestuser%d" @@ -528,7 +520,7 @@ resource "azurerm_kubernetes_cluster" "test" { location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" dns_prefix = "acctestaks%d" - kubernetes_version = "1.7.7" + linux_profile { admin_username = "acctestuser%d" ssh_key { @@ -569,7 +561,7 @@ resource "azurerm_kubernetes_cluster" "test" { location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" dns_prefix = "acctestaks%d" - kubernetes_version = "1.7.7" + linux_profile { admin_username = "acctestuser%d" ssh_key { @@ -609,7 +601,7 @@ resource "azurerm_kubernetes_cluster" "test" { location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" dns_prefix = "acctestaks%d" - kubernetes_version = "1.8.1" + kubernetes_version = "1.11.2" linux_profile { admin_username = "acctestuser%d" @@ -663,7 +655,6 @@ resource "azurerm_kubernetes_cluster" "test" { location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" dns_prefix = "acctestaks%d" - kubernetes_version = "1.7.7" linux_profile { admin_username = "acctestuser%d" @@ -722,7 +713,6 @@ resource "azurerm_kubernetes_cluster" "test" { location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" dns_prefix = "acctestaks%d" - kubernetes_version = "1.7.7" linux_profile { admin_username = "acctestuser%d" From e76e1c2d457901c5db5f8d19cb1c0bdb112914f2 Mon Sep 17 00:00:00 2001 From: Su Shi <1684739+metacpp@users.noreply.github.com> Date: Wed, 29 Aug 2018 16:22:08 -0700 Subject: [PATCH 09/10] :rotating_light: (Import test for AKS.) Fix the go fmt error. Add newline to import test file for AKS. --- azurerm/import_arm_kubernetes_cluster_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/import_arm_kubernetes_cluster_test.go b/azurerm/import_arm_kubernetes_cluster_test.go index c458261ec3bf..c1e8c6776da0 100644 --- a/azurerm/import_arm_kubernetes_cluster_test.go +++ b/azurerm/import_arm_kubernetes_cluster_test.go @@ -56,4 +56,4 @@ func TestAccAzureRMKubernetesCluster_importLinuxProfile(t *testing.T) { }, }, }) -} \ No newline at end of file +} From 095ddf680ea16c5f74164466be2070358c7cfa71 Mon Sep 17 00:00:00 2001 From: Su Shi <1684739+metacpp@users.noreply.github.com> Date: Wed, 29 Aug 2018 18:54:43 -0700 Subject: [PATCH 10/10] :green_heart: (Resource tests for AKS.) Fix the path issue for linux_profile. Fix the path issue to correct naming for linux_profile. --- azurerm/resource_arm_kubernetes_cluster_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/azurerm/resource_arm_kubernetes_cluster_test.go b/azurerm/resource_arm_kubernetes_cluster_test.go index b59e84db9ea6..86fde1c0837a 100644 --- a/azurerm/resource_arm_kubernetes_cluster_test.go +++ b/azurerm/resource_arm_kubernetes_cluster_test.go @@ -121,8 +121,7 @@ func TestAccAzureRMKubernetesCluster_linuxProfile(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.username"), resource.TestCheckResourceAttrSet(resourceName, "kube_config.0.password"), resource.TestCheckResourceAttrSet(resourceName, "agent_pool_profile.0.max_pods"), - resource.TestCheckResourceAttrSet(resourceName, "linux_pool_profile.0.admin_username"), - resource.TestCheckResourceAttrSet(resourceName, "linux_pool_profile.0.ssh_key"), + resource.TestCheckResourceAttrSet(resourceName, "linux_profile.0.admin_username"), ), }, },