Skip to content

Commit

Permalink
Make kubernetes log analytics workspace optional (#4513)
Browse files Browse the repository at this point in the history
This fixes #3239
  • Loading branch information
maxlegault authored and katbyte committed Oct 5, 2019
1 parent 28dccdb commit 9abf684
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 3 deletions.
4 changes: 2 additions & 2 deletions azurerm/resource_arm_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func resourceArmKubernetesCluster() *schema.Resource {
},
"log_analytics_workspace_id": {
Type: schema.TypeString,
Required: true,
Optional: true,
ValidateFunc: azure.ValidateResourceID,
},
},
Expand Down Expand Up @@ -1006,7 +1006,7 @@ 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 {
if workspaceId, ok := value["log_analytics_workspace_id"]; ok && workspaceId != "" {
config["logAnalyticsWorkspaceResourceID"] = utils.String(workspaceId.(string))
}

Expand Down
128 changes: 128 additions & 0 deletions azurerm/resource_arm_kubernetes_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,58 @@ func TestAccAzureRMKubernetesCluster_addonProfileOMS(t *testing.T) {
})
}

func TestAccAzureRMKubernetesCluster_addonProfileOMSToggle(t *testing.T) {
resourceName := "azurerm_kubernetes_cluster.test"
ri := tf.AccRandTimeInt()
clientId := os.Getenv("ARM_CLIENT_ID")
clientSecret := os.Getenv("ARM_CLIENT_SECRET")
location := testLocation()
enablingConfig := testAccAzureRMKubernetesCluster_addonProfileOMS(ri, clientId, clientSecret, location)
disablingConfig := testAccAzureRMKubernetesCluster_addonProfileOMSDisabled(ri, clientId, clientSecret, location)
scaleDownConfig := testAccAzureRMKubernetesCluster_addonProfileOMSScaleWithoutBlock(ri, clientId, clientSecret, location)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKubernetesClusterDestroy,
Steps: []resource.TestStep{
{
Config: enablingConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKubernetesClusterExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "addon_profile.0.http_application_routing.#", "0"),
resource.TestCheckResourceAttr(resourceName, "agent_pool_profile.0.count", "1"),
resource.TestCheckResourceAttr(resourceName, "addon_profile.0.oms_agent.#", "1"),
resource.TestCheckResourceAttr(resourceName, "addon_profile.0.oms_agent.0.enabled", "true"),
resource.TestCheckResourceAttrSet(resourceName, "addon_profile.0.oms_agent.0.log_analytics_workspace_id"),
),
},
{
Config: disablingConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKubernetesClusterExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "addon_profile.0.http_application_routing.#", "0"),
resource.TestCheckResourceAttr(resourceName, "agent_pool_profile.0.count", "1"),
resource.TestCheckResourceAttr(resourceName, "addon_profile.0.oms_agent.#", "1"),
resource.TestCheckResourceAttr(resourceName, "addon_profile.0.oms_agent.0.enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "addon_profile.0.oms_agent.0.log_analytics_workspace_id", ""),
),
},
{
Config: scaleDownConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKubernetesClusterExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "addon_profile.0.http_application_routing.#", "0"),
resource.TestCheckResourceAttr(resourceName, "agent_pool_profile.0.count", "2"),
resource.TestCheckResourceAttr(resourceName, "addon_profile.0.oms_agent.#", "1"),
resource.TestCheckResourceAttr(resourceName, "addon_profile.0.oms_agent.0.enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "addon_profile.0.oms_agent.0.log_analytics_workspace_id", ""),
),
},
},
})
}

func TestAccAzureRMKubernetesCluster_addonProfileRouting(t *testing.T) {
resourceName := "azurerm_kubernetes_cluster.test"
ri := tf.AccRandTimeInt()
Expand Down Expand Up @@ -1450,6 +1502,82 @@ 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 [email protected]"
}
}
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_addonProfileOMSScaleWithoutBlock(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 [email protected]"
}
}
agent_pool_profile {
name = "default"
count = "2"
vm_size = "Standard_DS2_v2"
}
service_principal {
client_id = "%s"
client_secret = "%s"
}
}
`, 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" {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/kubernetes_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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 present if `enabled` is `true`.

---

Expand Down

0 comments on commit 9abf684

Please sign in to comment.