From 032ea97d624dbeb558756a3f8e1b1dbcec6f785a Mon Sep 17 00:00:00 2001 From: nellyk <3062772+nellyk@users.noreply.github.com> Date: Tue, 26 Mar 2024 15:33:45 +0000 Subject: [PATCH] Add var subnets --- README.md | 10 +++++- examples/with_availability_zone/README.md | 37 +++++++++++++++-------- examples/with_availability_zone/main.tf | 36 +++++++++++++++------- main.tf | 2 +- variables.tf | 8 ++++- 5 files changed, 67 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 1601c6ad..1132be9e 100644 --- a/README.md +++ b/README.md @@ -262,7 +262,7 @@ map(object({ name = string vm_size = string orchestrator_version = string - vnet_subnet_id = string + vnet_subnet_id = any # do not add nodecount because we enforce the use of auto-scaling max_count = optional(number) min_count = optional(number) @@ -369,6 +369,14 @@ map(object({ Default: `{}` +### [subnets](#input\_subnets) + +Description: (Optional) A list of Subnet IDs to associate with the Kubernetes Cluster. + +Type: `list(string)` + +Default: `[]` + ### [tags](#input\_tags) Description: The map of tags to be applied to the resource diff --git a/examples/with_availability_zone/README.md b/examples/with_availability_zone/README.md index b5497296..758dad54 100644 --- a/examples/with_availability_zone/README.md +++ b/examples/with_availability_zone/README.md @@ -44,6 +44,7 @@ locals { location = "East US 2" } module "test" { + for_each = toset(["1", "2", "3"]) source = "../../" kubernetes_version = "1.28" vnet_subnet_id = module.vnet.vnet_subnets_name_id["subnet1"] @@ -51,13 +52,14 @@ module "test" { name = module.naming.kubernetes_cluster.name_unique resource_group_name = azurerm_resource_group.this.name identity_ids = [azurerm_user_assigned_identity.this.id] + subnets = ["subnet2", "subnet3", "subnet4"] location = local.location # Hardcoded because we have to test in a region with availability zones node_pools = { workload = { name = "workload" vm_size = "Standard_D2d_v5" orchestrator_version = "1.28" - vnet_subnet_id = module.vnet.vnet_subnets_name_id["subnet2"] + vnet_subnet_id = module.vnet.vnet_subnets_name_id max_count = 110 min_count = 2 os_sku = "Ubuntu" @@ -73,15 +75,35 @@ module "vnet" { resource_group_name = azurerm_resource_group.this.name subnets = { subnet1 = { - address_prefixes = ["10.31.0.0/24"] + address_prefixes = ["10.31.0.0/17"] + nat_gateway = { + id = azurerm_nat_gateway.example["3"].id + } } subnet2 = { - address_prefixes = ["10.31.1.0/24"] + address_prefixes = ["10.31.128.0/18"] + nat_gateway = { + id = azurerm_nat_gateway.example["1"].id + } + } + subnet3 = { + address_prefixes = ["10.31.192.0/19"] + nat_gateway = { + id = azurerm_nat_gateway.example["2"].id + } + + } + subnet4 = { + address_prefixes = ["10.31.224.0/20"] + nat_gateway = { + id = azurerm_nat_gateway.example["3"].id + } } } virtual_network_address_space = ["10.31.0.0/16"] virtual_network_location = local.location virtual_network_name = "vnet" + depends_on = [azurerm_nat_gateway.example] } @@ -95,14 +117,6 @@ resource "azurerm_nat_gateway" "example" { zones = [each.key] } -resource "azurerm_subnet_nat_gateway_association" "example" { - for_each = toset(["1", "2"]) - - nat_gateway_id = azurerm_nat_gateway.example[each.key].id - subnet_id = module.vnet.vnet_subnets_name_id["subnet${each.key}"] -} - -# use resource "azurerm_nat_gateway_public_ip_prefix_association" "example" { for_each = toset(["1", "2", "3"]) @@ -146,7 +160,6 @@ The following resources are used by this module: - [azurerm_nat_gateway_public_ip_prefix_association.example](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/nat_gateway_public_ip_prefix_association) (resource) - [azurerm_public_ip_prefix.example](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/public_ip_prefix) (resource) - [azurerm_resource_group.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) (resource) -- [azurerm_subnet_nat_gateway_association.example](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet_nat_gateway_association) (resource) - [azurerm_user_assigned_identity.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/user_assigned_identity) (resource) diff --git a/examples/with_availability_zone/main.tf b/examples/with_availability_zone/main.tf index a4cdf400..2b752687 100644 --- a/examples/with_availability_zone/main.tf +++ b/examples/with_availability_zone/main.tf @@ -38,6 +38,7 @@ locals { location = "East US 2" } module "test" { + for_each = toset(["1", "2", "3"]) source = "../../" kubernetes_version = "1.28" vnet_subnet_id = module.vnet.vnet_subnets_name_id["subnet1"] @@ -45,13 +46,14 @@ module "test" { name = module.naming.kubernetes_cluster.name_unique resource_group_name = azurerm_resource_group.this.name identity_ids = [azurerm_user_assigned_identity.this.id] + subnets = ["subnet2", "subnet3", "subnet4"] location = local.location # Hardcoded because we have to test in a region with availability zones node_pools = { workload = { name = "workload" vm_size = "Standard_D2d_v5" orchestrator_version = "1.28" - vnet_subnet_id = module.vnet.vnet_subnets_name_id["subnet2"] + vnet_subnet_id = module.vnet.vnet_subnets_name_id max_count = 110 min_count = 2 os_sku = "Ubuntu" @@ -67,15 +69,35 @@ module "vnet" { resource_group_name = azurerm_resource_group.this.name subnets = { subnet1 = { - address_prefixes = ["10.31.0.0/24"] + address_prefixes = ["10.31.0.0/17"] + nat_gateway = { + id = azurerm_nat_gateway.example["3"].id + } } subnet2 = { - address_prefixes = ["10.31.1.0/24"] + address_prefixes = ["10.31.128.0/18"] + nat_gateway = { + id = azurerm_nat_gateway.example["1"].id + } + } + subnet3 = { + address_prefixes = ["10.31.192.0/19"] + nat_gateway = { + id = azurerm_nat_gateway.example["2"].id + } + + } + subnet4 = { + address_prefixes = ["10.31.224.0/20"] + nat_gateway = { + id = azurerm_nat_gateway.example["3"].id + } } } virtual_network_address_space = ["10.31.0.0/16"] virtual_network_location = local.location virtual_network_name = "vnet" + depends_on = [azurerm_nat_gateway.example] } @@ -89,14 +111,6 @@ resource "azurerm_nat_gateway" "example" { zones = [each.key] } -resource "azurerm_subnet_nat_gateway_association" "example" { - for_each = toset(["1", "2"]) - - nat_gateway_id = azurerm_nat_gateway.example[each.key].id - subnet_id = module.vnet.vnet_subnets_name_id["subnet${each.key}"] -} - -# use resource "azurerm_nat_gateway_public_ip_prefix_association" "example" { for_each = toset(["1", "2", "3"]) diff --git a/main.tf b/main.tf index d17e443e..364ac87a 100644 --- a/main.tf +++ b/main.tf @@ -223,7 +223,7 @@ resource "azurerm_kubernetes_cluster_node_pool" "this" { orchestrator_version = each.value.orchestrator_version os_sku = each.value.os_sku tags = var.tags - vnet_subnet_id = each.value.vnet_subnet_id + vnet_subnet_id = each.value.zone == "" ? tostring(each.value.vnet_subnet_id) : each.value.vnet_subnet_id[var.subnets[(tonumber(each.value.zone) - 1)]] zones = each.value.zone == "" ? null : [each.value.zone] depends_on = [azapi_update_resource.aks_cluster_post_create] diff --git a/variables.tf b/variables.tf index d9688023..bb84df13 100644 --- a/variables.tf +++ b/variables.tf @@ -141,7 +141,7 @@ variable "node_pools" { name = string vm_size = string orchestrator_version = string - vnet_subnet_id = string + vnet_subnet_id = any # do not add nodecount because we enforce the use of auto-scaling max_count = optional(number) min_count = optional(number) @@ -254,6 +254,12 @@ A map of role assignments to create on this resource. The map key is deliberatel DESCRIPTION } +variable "subnets" { + type = list(string) + default = [] + description = "(Optional) A list of Subnet IDs to associate with the Kubernetes Cluster." +} + # tflint-ignore: terraform_unused_declarations variable "tags" { type = map(any)