Skip to content

Commit 9b0e388

Browse files
lonegunmanbCopilot
andcommitted
fix: handle null undrainable_node_behavior in precondition validation
The contains() function in Terraform does not accept null arguments, and Terraform does not short-circuit || evaluation. When undrainable_node_behavior is explicitly set to null, try() passes null through (since accessing a null attribute is not an error), causing contains() to crash with 'argument must not be null'. Fix by wrapping with coalesce() to convert null to an empty string before passing to contains(). Also harden the blob driver subnet check in main.tf with the same pattern. Fixes #760 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0f8086c commit 9b0e388

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

extra_node_pool.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ resource "azurerm_kubernetes_cluster_node_pool" "node_pool_create_before_destroy
180180
error_message = "Exactly one of `max_surge` or `max_unavailable` must be specified in `upgrade_settings` for node pool '${each.value.name}'."
181181
}
182182
precondition {
183-
condition = each.value.upgrade_settings == null || try(each.value.upgrade_settings.undrainable_node_behavior, null) == null || contains(["Cordon", "Schedule"], try(each.value.upgrade_settings.undrainable_node_behavior, ""))
183+
condition = each.value.upgrade_settings == null || try(each.value.upgrade_settings.undrainable_node_behavior, null) == null || contains(["Cordon", "Schedule"], coalesce(try(each.value.upgrade_settings.undrainable_node_behavior, ""), ""))
184184
error_message = "`undrainable_node_behavior` in `upgrade_settings` must be `null`, `\"Cordon\"`, or `\"Schedule\"` for node pool '${each.value.name}'."
185185
}
186186
}
@@ -347,7 +347,7 @@ resource "azurerm_kubernetes_cluster_node_pool" "node_pool_create_after_destroy"
347347
error_message = "Exactly one of `max_surge` or `max_unavailable` must be specified in `upgrade_settings` for node pool '${each.value.name}'."
348348
}
349349
precondition {
350-
condition = each.value.upgrade_settings == null || try(each.value.upgrade_settings.undrainable_node_behavior, null) == null || contains(["Cordon", "Schedule"], try(each.value.upgrade_settings.undrainable_node_behavior, ""))
350+
condition = each.value.upgrade_settings == null || try(each.value.upgrade_settings.undrainable_node_behavior, null) == null || contains(["Cordon", "Schedule"], coalesce(try(each.value.upgrade_settings.undrainable_node_behavior, ""), ""))
351351
error_message = "`undrainable_node_behavior` in `upgrade_settings` must be `null`, `\"Cordon\"`, or `\"Schedule\"` for node pool '${each.value.name}'."
352352
}
353353
}

main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ data "azurerm_subnet" "blob_driver_check" {
745745

746746
check "blob_driver_subnet_service_endpoint" {
747747
assert {
748-
condition = var.vnet_subnet == null || !var.storage_profile_enabled || !var.storage_profile_blob_driver_enabled || contains(try(data.azurerm_subnet.blob_driver_check[0].service_endpoints, []), "Microsoft.Storage")
748+
condition = var.vnet_subnet == null || !var.storage_profile_enabled || !var.storage_profile_blob_driver_enabled || contains(coalesce(try(data.azurerm_subnet.blob_driver_check[0].service_endpoints, null), []), "Microsoft.Storage")
749749
error_message = "The subnet used by the default node pool does not have 'Microsoft.Storage' in its service_endpoints. When storage_profile_blob_driver_enabled is true, the AKS Blob CSI driver may automatically add this service endpoint out-of-band, causing Terraform state drift. To prevent this, add service_endpoints = [\"Microsoft.Storage\"] to your subnet configuration. See: https://github.com/Azure/terraform-azurerm-aks/issues/424"
750750
}
751751
}

0 commit comments

Comments
 (0)