Skip to content

Commit

Permalink
feat!: add create_before_destroy=true to node pools (Azure#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
the-technat authored and skolobov committed Oct 29, 2023
1 parent a054963 commit b2d47e4
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 133 deletions.
259 changes: 130 additions & 129 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/multiple_node_pools/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ resource "azurerm_subnet" "test" {
locals {
nodes = {
for i in range(3) : "worker${i}" => {
name = substr("worker${i}${random_id.prefix.hex}", 0, 12)
name = substr("worker${i}${random_id.prefix.hex}", 0, 8)
vm_size = "Standard_D2s_v3"
node_count = 1
vnet_subnet_id = azurerm_subnet.test.id
Expand Down
2 changes: 1 addition & 1 deletion examples/multiple_node_pools/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ variable "location" {
variable "resource_group_name" {
type = string
default = null
}
}
23 changes: 22 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ resource "azurerm_kubernetes_cluster_node_pool" "node_pool" {
for_each = var.node_pools

kubernetes_cluster_id = azurerm_kubernetes_cluster.main.id
name = each.value.name
name = "${each.value.name}${substr(md5(jsonencode(each.value)), 0, 4)}"
vm_size = each.value.vm_size
capacity_reservation_group_id = each.value.capacity_reservation_group_id
custom_ca_trust_enabled = each.value.custom_ca_trust_enabled
Expand Down Expand Up @@ -592,10 +592,31 @@ resource "azurerm_kubernetes_cluster_node_pool" "node_pool" {
depends_on = [azapi_update_resource.aks_cluster_post_create]

lifecycle {
create_before_destroy = true
ignore_changes = [
name
]
replace_triggered_by = [
null_resource.pool_name_keeper[each.key],
]

precondition {
condition = var.agents_type == "VirtualMachineScaleSets"
error_message = "Multiple Node Pools are only supported when the Kubernetes Cluster is using Virtual Machine Scale Sets."
}

precondition {
condition = can(regex("[a-z0-9]{1,8}", each.value.name))
error_message = "A Node Pools name must consist of alphanumeric characters and have a maximum lenght of 8 characters (4 random chars added)"
}
}
}

resource "null_resource" "pool_name_keeper" {
for_each = var.node_pools

triggers = {
pool_name = each.value.name
}
}

Expand Down
2 changes: 1 addition & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ variable "node_pools" {
description = <<-EOT
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:
map(object({
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.
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.
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`.
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.
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.
Expand Down

0 comments on commit b2d47e4

Please sign in to comment.