From b91dbe4a5b35083d41f66a3fbfdf450605b284e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois-Xavier=20Gsell?= Date: Sat, 5 Oct 2019 10:55:09 +0800 Subject: [PATCH] Support disable addon_profile for oms_agent --- azurerm/resource_arm_kubernetes_cluster.go | 44 ++++++++----- .../resource_arm_kubernetes_cluster_test.go | 66 +++++++++++++++++++ .../docs/r/kubernetes_cluster.html.markdown | 2 +- 3 files changed, 93 insertions(+), 19 deletions(-) diff --git a/azurerm/resource_arm_kubernetes_cluster.go b/azurerm/resource_arm_kubernetes_cluster.go index 9759c8f16e84..5e794999889c 100644 --- a/azurerm/resource_arm_kubernetes_cluster.go +++ b/azurerm/resource_arm_kubernetes_cluster.go @@ -276,7 +276,7 @@ func resourceArmKubernetesCluster() *schema.Resource { }, "log_analytics_workspace_id": { Type: schema.TypeString, - Required: true, + Optional: true, ValidateFunc: azure.ValidateResourceID, }, }, @@ -295,7 +295,7 @@ func resourceArmKubernetesCluster() *schema.Resource { }, "subnet_name": { Type: schema.TypeString, - Required: true, + Optional: true, ValidateFunc: validate.NoEmptyStrings, }, }, @@ -1006,8 +1006,10 @@ func expandKubernetesClusterAddonProfiles(d *schema.ResourceData) map[string]*co config := make(map[string]*string) enabled := value["enabled"].(bool) - if workspaceId, ok := value["log_analytics_workspace_id"]; ok { - config["logAnalyticsWorkspaceResourceID"] = utils.String(workspaceId.(string)) + if enabled { + if workspaceId, ok := value["log_analytics_workspace_id"]; ok { + config["logAnalyticsWorkspaceResourceID"] = utils.String(workspaceId.(string)) + } } addonProfiles["omsagent"] = &containerservice.ManagedClusterAddonProfile{ @@ -1022,8 +1024,10 @@ func expandKubernetesClusterAddonProfiles(d *schema.ResourceData) map[string]*co config := make(map[string]*string) enabled := value["enabled"].(bool) - if subnetName, ok := value["subnet_name"]; ok { - config["SubnetName"] = utils.String(subnetName.(string)) + if enabled { + if subnetName, ok := value["subnet_name"]; ok { + config["SubnetName"] = utils.String(subnetName.(string)) + } } addonProfiles["aciConnectorLinux"] = &containerservice.ManagedClusterAddonProfile{ @@ -1076,14 +1080,16 @@ func flattenKubernetesClusterAddonProfiles(profile map[string]*containerservice. enabled = *enabledVal } - workspaceId := "" - if workspaceResourceID := omsAgent.Config["logAnalyticsWorkspaceResourceID"]; workspaceResourceID != nil { - workspaceId = *workspaceResourceID + output := map[string]interface{}{ + "enabled": enabled, } - output := map[string]interface{}{ - "enabled": enabled, - "log_analytics_workspace_id": workspaceId, + if enabled { + workspaceId := "" + if workspaceResourceID := omsAgent.Config["logAnalyticsWorkspaceResourceID"]; workspaceResourceID != nil { + workspaceId = *workspaceResourceID + } + output["log_analytics_workspace_id"] = workspaceId } agents = append(agents, output) } @@ -1096,14 +1102,16 @@ func flattenKubernetesClusterAddonProfiles(profile map[string]*containerservice. enabled = *enabledVal } - subnetName := "" - if v := aciConnector.Config["SubnetName"]; v != nil { - subnetName = *v + output := map[string]interface{}{ + "enabled": enabled, } - output := map[string]interface{}{ - "enabled": enabled, - "subnet_name": subnetName, + if enabled { + subnetName := "" + if v := aciConnector.Config["SubnetName"]; v != nil { + subnetName = *v + } + output["subnet_name"] = subnetName } aciConnectors = append(aciConnectors, output) } diff --git a/azurerm/resource_arm_kubernetes_cluster_test.go b/azurerm/resource_arm_kubernetes_cluster_test.go index 3ee78bdd04cb..1c57ef984430 100644 --- a/azurerm/resource_arm_kubernetes_cluster_test.go +++ b/azurerm/resource_arm_kubernetes_cluster_test.go @@ -389,6 +389,31 @@ func TestAccAzureRMKubernetesCluster_addonProfileOMS(t *testing.T) { }) } +func TestAccAzureRMKubernetesCluster_addonProfileOMSDisabled(t *testing.T) { + resourceName := "azurerm_kubernetes_cluster.test" + ri := tf.AccRandTimeInt() + clientId := os.Getenv("ARM_CLIENT_ID") + clientSecret := os.Getenv("ARM_CLIENT_SECRET") + config := testAccAzureRMKubernetesCluster_addonProfileOMSDisabled(ri, clientId, clientSecret, testLocation()) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMKubernetesClusterDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMKubernetesClusterExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "addon_profile.0.http_application_routing.#", "0"), + resource.TestCheckResourceAttr(resourceName, "addon_profile.0.oms_agent.#", "1"), + resource.TestCheckResourceAttr(resourceName, "addon_profile.0.oms_agent.0.enabled", "false"), + ), + }, + }, + }) +} + func TestAccAzureRMKubernetesCluster_addonProfileRouting(t *testing.T) { resourceName := "azurerm_kubernetes_cluster.test" ri := tf.AccRandTimeInt() @@ -1450,6 +1475,47 @@ resource "azurerm_kubernetes_cluster" "test" { `, rInt, location, rInt, rInt, rInt, rInt, clientId, clientSecret) } +func testAccAzureRMKubernetesCluster_addonProfileOMSDisabled(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" + } + + addon_profile { + oms_agent { + enabled = false + } + } +} +`, rInt, location, rInt, rInt, rInt, clientId, clientSecret) +} + func testAccAzureRMKubernetesCluster_addonProfileRouting(rInt int, clientId string, clientSecret string, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { diff --git a/website/docs/r/kubernetes_cluster.html.markdown b/website/docs/r/kubernetes_cluster.html.markdown index e70f4ae123bc..a64389cf1658 100644 --- a/website/docs/r/kubernetes_cluster.html.markdown +++ b/website/docs/r/kubernetes_cluster.html.markdown @@ -241,7 +241,7 @@ A `oms_agent` block supports the following: * `enabled` - (Required) Is the OMS Agent Enabled? -* `log_analytics_workspace_id` - (Required) The ID of the Log Analytics Workspace which the OMS Agent should send data to. +* `log_analytics_workspace_id` - (Optional) The ID of the Log Analytics Workspace which the OMS Agent should send data to. Must be specified when `enabled` is set to `true`. ---