Skip to content

Commit

Permalink
Add var subnets
Browse files Browse the repository at this point in the history
  • Loading branch information
nellyk committed Mar 26, 2024
1 parent 896e7f3 commit 032ea97
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 26 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -369,6 +369,14 @@ map(object({

Default: `{}`

### <a name="input_subnets"></a> [subnets](#input\_subnets)

Description: (Optional) A list of Subnet IDs to associate with the Kubernetes Cluster.

Type: `list(string)`

Default: `[]`

### <a name="input_tags"></a> [tags](#input\_tags)

Description: The map of tags to be applied to the resource
Expand Down
37 changes: 25 additions & 12 deletions examples/with_availability_zone/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,22 @@ 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"]
enable_telemetry = var.enable_telemetry # see variables.tf
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"
Expand All @@ -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]
}
Expand All @@ -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"])
Expand Down Expand Up @@ -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)

<!-- markdownlint-disable MD013 -->
Expand Down
36 changes: 25 additions & 11 deletions examples/with_availability_zone/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,22 @@ 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"]
enable_telemetry = var.enable_telemetry # see variables.tf
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"
Expand All @@ -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]
}


Expand All @@ -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"])
Expand Down
2 changes: 1 addition & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 7 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 032ea97

Please sign in to comment.