Skip to content

Commit

Permalink
Rename var.system_assigned_identity to cluster_identity, add vali…
Browse files Browse the repository at this point in the history
…dation and precondition for identity-related variables. Bump Terraform required version to 1.2.0 since we've used precondition.
  • Loading branch information
lonegunmanb committed Jul 13, 2022
1 parent da2918f commit 5b0feb6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
13 changes: 12 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,19 @@ resource "azurerm_kubernetes_cluster" "main" {
oidc_issuer_enabled = var.oidc_issuer_enabled

tags = var.tags
}

lifecycle {
precondition {
condition = (var.client_id != "" && var.client_secret != "") || (var.identity_type != "")
error_message = "Either `client_id` and `client_secret` or `identity_type` must be set."
}
precondition {
# Why don't use var.identity_ids != null && length(var.identity_ids)>0 ? Because bool expression in Terraform is not short circuit so even var.identity_ids is null Terraform will still invoke length function with null and cause error. https://github.com/hashicorp/terraform/issues/24128
condition = (var.client_id != "" && var.client_secret != "") || (var.identity_type == "SystemAssigned") || (var.identity_ids == null ? false :length(var.identity_ids) > 0)
error_message = "If use identity and `UserAssigned` or `SystemAssigned, UserAssigned` is set, an `identity_ids` must be set as well."
}
}
}

resource "azurerm_log_analytics_workspace" "main" {
count = var.enable_log_analytics_workspace && var.log_analytics_workspace == null ? 1 : 0
Expand Down
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ output "http_application_routing_zone_name" {
value = azurerm_kubernetes_cluster.main.http_application_routing_zone_name != null ? azurerm_kubernetes_cluster.main.http_application_routing_zone_name : ""
}

output "system_assigned_identity" {
output "cluster_identity" {
value = azurerm_kubernetes_cluster.main.identity
}

Expand Down
9 changes: 8 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ variable "client_id" {
description = "(Optional) The Client ID (appId) for the Service Principal used for the AKS deployment"
type = string
default = ""
nullable = false
}

variable "client_secret" {
description = "(Optional) The Client Secret (password) for the Service Principal used for the AKS deployment"
type = string
default = ""
nullable = false
}

variable "api_server_authorized_ip_ranges" {
Expand Down Expand Up @@ -364,9 +366,14 @@ variable "ingress_application_gateway_subnet_id" {
}

variable "identity_type" {
description = "(Optional) The type of identity used for the managed cluster. Conflict with `client_id` and `client_secret`. Possible values are `SystemAssigned` and `UserAssigned`. If `UserAssigned` is set, a `user_assigned_identity_id` must be set as well."
description = "(Optional) The type of identity used for the managed cluster. Conflict with `client_id` and `client_secret`. Possible values are `SystemAssigned`, `UserAssigned`, `SystemAssigned, UserAssigned`(to enable both). If `UserAssigned` or `SystemAssigned, UserAssigned` is set, an `identity_ids` must be set as well."
type = string
default = "SystemAssigned"

validation {
condition = var.identity_type == "SystemAssigned" || var.identity_type == "UserAssigned" || var.identity_type == "SystemAssigned, UserAssigned"
error_message = "`identity_type`'s possible values are `SystemAssigned`, `UserAssigned`, `SystemAssigned, UserAssigned`(to enable both)."
}
}

variable "identity_ids" {
Expand Down
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ terraform {
}
}

required_version = ">= 1.1.0"
required_version = ">= 1.2"
}

0 comments on commit 5b0feb6

Please sign in to comment.