Skip to content

Commit b2d47e4

Browse files
the-technatskolobov
authored andcommitted
feat!: add create_before_destroy=true to node pools (Azure#357)
1 parent a054963 commit b2d47e4

5 files changed

Lines changed: 155 additions & 133 deletions

File tree

README.md

Lines changed: 130 additions & 129 deletions
Large diffs are not rendered by default.

examples/multiple_node_pools/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ resource "azurerm_subnet" "test" {
3434
locals {
3535
nodes = {
3636
for i in range(3) : "worker${i}" => {
37-
name = substr("worker${i}${random_id.prefix.hex}", 0, 12)
37+
name = substr("worker${i}${random_id.prefix.hex}", 0, 8)
3838
vm_size = "Standard_D2s_v3"
3939
node_count = 1
4040
vnet_subnet_id = azurerm_subnet.test.id

examples/multiple_node_pools/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ variable "location" {
1111
variable "resource_group_name" {
1212
type = string
1313
default = null
14-
}
14+
}

main.tf

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ resource "azurerm_kubernetes_cluster_node_pool" "node_pool" {
470470
for_each = var.node_pools
471471

472472
kubernetes_cluster_id = azurerm_kubernetes_cluster.main.id
473-
name = each.value.name
473+
name = "${each.value.name}${substr(md5(jsonencode(each.value)), 0, 4)}"
474474
vm_size = each.value.vm_size
475475
capacity_reservation_group_id = each.value.capacity_reservation_group_id
476476
custom_ca_trust_enabled = each.value.custom_ca_trust_enabled
@@ -592,10 +592,31 @@ resource "azurerm_kubernetes_cluster_node_pool" "node_pool" {
592592
depends_on = [azapi_update_resource.aks_cluster_post_create]
593593

594594
lifecycle {
595+
create_before_destroy = true
596+
ignore_changes = [
597+
name
598+
]
599+
replace_triggered_by = [
600+
null_resource.pool_name_keeper[each.key],
601+
]
602+
595603
precondition {
596604
condition = var.agents_type == "VirtualMachineScaleSets"
597605
error_message = "Multiple Node Pools are only supported when the Kubernetes Cluster is using Virtual Machine Scale Sets."
598606
}
607+
608+
precondition {
609+
condition = can(regex("[a-z0-9]{1,8}", each.value.name))
610+
error_message = "A Node Pools name must consist of alphanumeric characters and have a maximum lenght of 8 characters (4 random chars added)"
611+
}
612+
}
613+
}
614+
615+
resource "null_resource" "pool_name_keeper" {
616+
for_each = var.node_pools
617+
618+
triggers = {
619+
pool_name = each.value.name
599620
}
600621
}
601622

variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ variable "node_pools" {
779779
description = <<-EOT
780780
A map of node pools that about to be created and attached on the Kubernetes cluster. The key of the map can be the name of the node pool, and the key must be static string. The value of the map is a `node_pool` block as defined below:
781781
map(object({
782-
name = (Required) The name of the Node Pool which should be created within the Kubernetes Cluster. Changing this forces a new resource to be created. A Windows Node Pool cannot have a `name` longer than 6 characters.
782+
name = (Required) The name of the Node Pool which should be created within the Kubernetes Cluster. Changing this forces a new resource to be created. A Windows Node Pool cannot have a `name` longer than 6 characters. A random suffix of 4 characters is always added to the name to avoid clashes during recreates.
783783
node_count = (Optional) The initial number of nodes which should exist within this Node Pool. Valid values are between `0` and `1000` (inclusive) for user pools and between `1` and `1000` (inclusive) for system pools and must be a value in the range `min_count` - `max_count`.
784784
tags = (Optional) A mapping of tags to assign to the resource. At this time there's a bug in the AKS API where Tags for a Node Pool are not stored in the correct case - you [may wish to use Terraform's `ignore_changes` functionality to ignore changes to the casing](https://www.terraform.io/language/meta-arguments/lifecycle#ignore_changess) until this is fixed in the AKS API.
785785
vm_size = (Required) The SKU which should be used for the Virtual Machines used in this Node Pool. Changing this forces a new resource to be created.

0 commit comments

Comments
 (0)