Skip to content

Commit 99dc2eb

Browse files
committed
refactor: Enhance node pool validation for priority, eviction policy, and spot max price
1 parent 5ce520b commit 99dc2eb

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

modules/aks/main.tf

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ resource "azurerm_kubernetes_cluster" "aks" {
9292
tags = var.tags
9393
}
9494

95+
locals {
96+
# Create a map with spot instance flags for each node pool
97+
node_pool_configs = {
98+
for k, v in var.node_pools : k => {
99+
is_spot = lower(v.priority) == "spot"
100+
}
101+
}
102+
}
103+
95104
resource "azurerm_kubernetes_cluster_node_pool" "additional" {
96105
for_each = var.node_pools
97106

@@ -120,9 +129,9 @@ resource "azurerm_kubernetes_cluster_node_pool" "additional" {
120129
]
121130

122131
# Normalize to the capitalized form expected by the provider
123-
priority = lower(each.value.priority) == "spot" ? "Spot" : "Regular"
124-
eviction_policy = lower(each.value.priority) == "spot" ? each.value.eviction_policy : null
125-
spot_max_price = lower(each.value.priority) == "spot" ? each.value.spot_max_price : null
132+
priority = local.node_pool_configs[each.key].is_spot ? "Spot" : "Regular"
133+
eviction_policy = local.node_pool_configs[each.key].is_spot ? title(lower(each.value.eviction_policy)) : null
134+
spot_max_price = local.node_pool_configs[each.key].is_spot ? each.value.spot_max_price : null
126135

127136
tags = merge(var.tags, each.value.tags)
128137

@@ -132,7 +141,7 @@ resource "azurerm_kubernetes_cluster_node_pool" "additional" {
132141
error_message = "Node pool '${each.key}' has invalid priority '${each.value.priority}'. Priority must be either 'Regular' or 'Spot' (case-insensitive)."
133142
}
134143
precondition {
135-
condition = lower(each.value.priority) != "spot" || contains(["delete", "deallocate"], lower(each.value.eviction_policy))
144+
condition = !local.node_pool_configs[each.key].is_spot || contains(["delete", "deallocate"], lower(each.value.eviction_policy))
136145
error_message = "Node pool '${each.key}' has invalid eviction_policy '${each.value.eviction_policy}'. When priority is 'Spot', eviction_policy must be either 'Delete' or 'Deallocate' (case-insensitive)."
137146
}
138147
precondition {

modules/aks/variables.tf

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,28 @@ variable "node_pools" {
8888
spot_max_price = optional(number, -1) # Max price for Spot instances (must be >= -1), -1 uses market price
8989
}))
9090
default = {}
91+
92+
validation {
93+
condition = alltrue([
94+
for k, v in var.node_pools : contains(["regular", "spot"], lower(v.priority))
95+
])
96+
error_message = "Each node pool's priority must be either 'Regular' or 'Spot' (case-insensitive)."
97+
}
98+
99+
validation {
100+
condition = alltrue([
101+
for k, v in var.node_pools :
102+
lower(v.priority) != "spot" || contains(["delete", "deallocate"], lower(v.eviction_policy))
103+
])
104+
error_message = "When priority is 'Spot', eviction_policy must be either 'Delete' or 'Deallocate' (case-insensitive)."
105+
}
106+
107+
validation {
108+
condition = alltrue([
109+
for k, v in var.node_pools : v.spot_max_price >= -1
110+
])
111+
error_message = "Each node pool's spot_max_price must be greater than or equal to -1."
112+
}
91113
}
92114

93115
variable "network_profile" {

0 commit comments

Comments
 (0)