Skip to content

Commit ca66340

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

File tree

2 files changed

+25
-35
lines changed

2 files changed

+25
-35
lines changed

modules/aks/main.tf

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,13 @@ resource "azurerm_kubernetes_cluster" "aks" {
9393
}
9494

9595
locals {
96-
# Create a map with spot instance flags for each node pool
96+
# Pre-compute normalized values for each node pool
9797
node_pool_configs = {
9898
for k, v in var.node_pools : k => {
99-
is_spot = lower(v.priority) == "spot"
99+
is_spot = lower(v.priority) == "spot"
100+
priority = lower(v.priority) == "spot" ? "Spot" : "Regular"
101+
eviction_policy = lower(v.priority) == "spot" ? title(lower(v.eviction_policy)) : null
102+
spot_max_price = lower(v.priority) == "spot" ? v.spot_max_price : null
100103
}
101104
}
102105
}
@@ -128,25 +131,10 @@ resource "azurerm_kubernetes_cluster_node_pool" "additional" {
128131
for taint in coalesce(each.value.node_taints, []) : "${taint.key}=${taint.value}:${taint.effect}"
129132
]
130133

131-
# Normalize to the capitalized form expected by the provider
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
134+
# Use pre-computed normalized values from locals
135+
priority = local.node_pool_configs[each.key].priority
136+
eviction_policy = local.node_pool_configs[each.key].eviction_policy
137+
spot_max_price = local.node_pool_configs[each.key].spot_max_price
135138

136139
tags = merge(var.tags, each.value.tags)
137-
138-
lifecycle {
139-
precondition {
140-
condition = contains(["regular", "spot"], lower(each.value.priority))
141-
error_message = "Node pool '${each.key}' has invalid priority '${each.value.priority}'. Priority must be either 'Regular' or 'Spot' (case-insensitive)."
142-
}
143-
precondition {
144-
condition = !local.node_pool_configs[each.key].is_spot || contains(["delete", "deallocate"], lower(each.value.eviction_policy))
145-
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)."
146-
}
147-
precondition {
148-
condition = each.value.spot_max_price >= -1
149-
error_message = "Node pool '${each.key}' has invalid spot_max_price '${each.value.spot_max_price}'. The spot_max_price must be greater than or equal to -1."
150-
}
151-
}
152140
}

modules/aks/variables.tf

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,30 +85,32 @@ variable "node_pools" {
8585
# Spot instance configuration
8686
priority = optional(string, "Regular") # Valid values: "Regular" or "Spot" (case-insensitive)
8787
eviction_policy = optional(string, "Delete") # Valid values: "Delete" or "Deallocate" (case-insensitive, only used when priority is "Spot")
88-
spot_max_price = optional(number, -1) # Max price for Spot instances (must be >= -1), -1 uses market price
88+
spot_max_price = optional(number, -1) # Max price for Spot instances (must be -1 or >= 0), -1 uses market price
8989
}))
9090
default = {}
9191

9292
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)."
93+
condition = length([
94+
for k, v in var.node_pools : k
95+
if !contains(["regular", "spot"], lower(v.priority))
96+
]) == 0
97+
error_message = "Invalid priority values found. ${join(", ", [for k, v in var.node_pools : "Node pool '${k}' has invalid priority '${v.priority}'" if !contains(["regular", "spot"], lower(v.priority))])}. Priority must be either 'Regular' or 'Spot' (case-insensitive)."
9798
}
9899

99100
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)."
101+
condition = length([
102+
for k, v in var.node_pools : k
103+
if lower(v.priority) == "spot" && !contains(["delete", "deallocate"], lower(v.eviction_policy))
104+
]) == 0
105+
error_message = "Invalid eviction_policy values found. ${join(", ", [for k, v in var.node_pools : "Node pool '${k}' has invalid eviction_policy '${v.eviction_policy}'" if lower(v.priority) == "spot" && !contains(["delete", "deallocate"], lower(v.eviction_policy))])}. When priority is 'Spot', eviction_policy must be either 'Delete' or 'Deallocate' (case-insensitive)."
105106
}
106107

107108
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."
109+
condition = length([
110+
for k, v in var.node_pools : k
111+
if !(v.spot_max_price == -1 || v.spot_max_price >= 0)
112+
]) == 0
113+
error_message = "Invalid spot_max_price values found. ${join(", ", [for k, v in var.node_pools : "Node pool '${k}' has invalid spot_max_price '${v.spot_max_price}'" if !(v.spot_max_price == -1 || v.spot_max_price >= 0)])}. The spot_max_price must be either -1 (market price) or >= 0."
112114
}
113115
}
114116

0 commit comments

Comments
 (0)