Skip to content

Commit c0755fb

Browse files
authored
Merge pull request #4372 from PayalJakhar/validations
Added capacity checks for reservations
2 parents bf34199 + 87314bd commit c0755fb

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

modules/compute/gke-node-pool/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ limitations under the License.
305305
| [null_resource.enable_tcpxo_in_workload](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
306306
| [null_resource.install_dependencies](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
307307
| [google_compute_machine_types.machine_info](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_machine_types) | data source |
308+
| [google_compute_reservation.specific](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_reservation) | data source |
308309
| [google_compute_reservation.specific_reservations](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_reservation) | data source |
309310
| [google_container_cluster.gke_cluster](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/container_cluster) | data source |
310311

modules/compute/gke-node-pool/main.tf

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ data "google_container_cluster" "gke_cluster" {
6767
location = local.cluster_location
6868
}
6969

70+
data "google_compute_reservation" "specific" {
71+
count = local.input_specific_reservations_count == 1 ? 1 : 0
72+
project = var.project_id
73+
zone = var.zones[0]
74+
name = var.reservation_affinity.specific_reservations[0].name
75+
}
76+
77+
locals {
78+
reservation_available_count = (local.input_specific_reservations_count == 1) ? (
79+
try(data.google_compute_reservation.specific[0].specific_reservation[0].count, 0) -
80+
try(data.google_compute_reservation.specific[0].specific_reservation[0].in_use_count, 0)
81+
) : 0
82+
}
83+
7084
resource "google_container_node_pool" "node_pool" {
7185
provider = google
7286

@@ -379,6 +393,14 @@ resource "google_container_node_pool" "node_pool" {
379393
condition = var.enable_flex_start == true ? (var.spot == false) : true
380394
error_message = "Both enable_flex_start and spot consumption option cannot be set to true at the same time."
381395
}
396+
precondition {
397+
condition = var.reservation_affinity.consume_reservation_type != "SPECIFIC_RESERVATION" || (var.static_node_count != null && var.static_node_count <= local.reservation_available_count)
398+
error_message = "Requested static_node_count (${coalesce(var.static_node_count, "not set")}) exceeds the available reservation capacity (${local.reservation_available_count})."
399+
}
400+
precondition {
401+
condition = local.input_specific_reservations_count == 1 || var.reservation_affinity.consume_reservation_type != "SPECIFIC_RESERVATION"
402+
error_message = "Exactly one reservation must be specified when using SPECIFIC_RESERVATION."
403+
}
382404
}
383405
}
384406

0 commit comments

Comments
 (0)