Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Automatically select availability zones based on node type when not specified #102

Merged
merged 13 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ resources that lack official modules.
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | ~> 1.0 |
| <a name="requirement_azapi"></a> [azapi](#requirement\_azapi) | ~> 1.0 |
| <a name="requirement_azurerm"></a> [azurerm](#requirement\_azurerm) | ~> 3.17 |
| <a name="requirement_helm"></a> [helm](#requirement\_helm) | ~> 2.6 |
| <a name="requirement_kubernetes"></a> [kubernetes](#requirement\_kubernetes) | ~> 2.23 |
Expand All @@ -41,6 +42,7 @@ resources that lack official modules.

| Name | Version |
|------|---------|
| <a name="provider_azapi"></a> [azapi](#provider\_azapi) | ~> 1.0 |
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | ~> 3.17 |

## Modules
Expand All @@ -65,6 +67,7 @@ resources that lack official modules.

| Name | Type |
|------|------|
| [azapi_resource_list.az_zones](https://registry.terraform.io/providers/azure/azapi/latest/docs/data-sources/resource_list) | data source |
| [azurerm_subscription.current](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/subscription) | data source |

## Inputs
Expand Down Expand Up @@ -97,7 +100,8 @@ resources that lack official modules.
| <a name="input_location"></a> [location](#input\_location) | n/a | `string` | n/a | yes |
| <a name="input_namespace"></a> [namespace](#input\_namespace) | String used for prefix resources. | `string` | n/a | yes |
| <a name="input_node_max_pods"></a> [node\_max\_pods](#input\_node\_max\_pods) | Maximum number of pods per node | `number` | `30` | no |
| <a name="input_node_pool_zones"></a> [node\_pool\_zones](#input\_node\_pool\_zones) | Availability zones for the node pool | `list(string)` | <pre>[<br> "1",<br> "2"<br>]</pre> | no |
| <a name="input_node_pool_num_zones"></a> [node\_pool\_num\_zones](#input\_node\_pool\_num\_zones) | Number of availability zones to use for the node pool when node\_pool\_zones is not set. | `number` | `2` | no |
| <a name="input_node_pool_zones"></a> [node\_pool\_zones](#input\_node\_pool\_zones) | Availability zones for the node pool | `list(string)` | `null` | no |
| <a name="input_oidc_auth_method"></a> [oidc\_auth\_method](#input\_oidc\_auth\_method) | OIDC auth method | `string` | `"implicit"` | no |
| <a name="input_oidc_client_id"></a> [oidc\_client\_id](#input\_oidc\_client\_id) | The Client ID of application in your identity provider | `string` | `""` | no |
| <a name="input_oidc_issuer"></a> [oidc\_issuer](#input\_oidc\_issuer) | A url to your Open ID Connect identity provider, i.e. https://cognito-idp.us-east-1.amazonaws.com/us-east-1_uiIFNdacd | `string` | `""` | no |
Expand Down
27 changes: 27 additions & 0 deletions examples/public-dns/main.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
terraform {
required_version = "~> 1.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.17"
}
azapi = {
source = "azure/azapi"
version = "~> 1.0"
}
kubernetes = {
source = "hashicorp/kubernetes"
version = "~> 2.23"
}
helm = {
source = "hashicorp/helm"
version = "~> 2.6"
}
}
}

provider "azurerm" {
subscription_id = var.subscription_id
features {}
}

provider "azapi" {
subscription_id = var.subscription_id
}

data "azurerm_subscription" "current" {}

provider "kubernetes" {
Expand Down Expand Up @@ -46,6 +72,7 @@ module "wandb" {
tags = {
"Example" : "PublicDns"
}
node_pool_num_zones = 2
}

# # You'll want to update your DNS with the provisioned IP address
Expand Down
28 changes: 26 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,30 @@ module "app_lb" {
tags = var.tags
}

locals {
kubernetes_instance_type = try(local.deployment_size[var.size].node_type, var.kubernetes_instance_type)
}

data "azapi_resource_list" "az_zones" {
parent_id = "/subscriptions/${data.azurerm_subscription.current.subscription_id}"
type = "Microsoft.Compute/skus@2021-07-01"

response_export_values = ["value"]
}

locals {
vm_skus = [
for sku in jsondecode(data.azapi_resource_list.az_zones.output).value :
sku if(
sku.resourceType == "virtualMachines" &&
lower(sku.locations[0]) == lower(azurerm_resource_group.default.location) &&
sku.name == local.kubernetes_instance_type
)
]
num_zones = var.node_pool_zones != null ? length(var.node_pool_zones) : var.node_pool_num_zones
node_pool_zones = var.node_pool_zones != null ? var.node_pool_zones : slice(sort(local.vm_skus[0].locationInfo[0].zones), 0, local.num_zones)
}

module "app_aks" {
source = "./modules/app_aks"
depends_on = [module.app_lb]
Expand All @@ -118,8 +142,8 @@ module "app_aks" {
location = azurerm_resource_group.default.location
namespace = var.namespace
node_pool_vm_count = try(local.deployment_size[var.size].node_count, var.kubernetes_node_count)
node_pool_vm_size = try(local.deployment_size[var.size].node_instance, var.kubernetes_instance_type)
node_pool_zones = var.node_pool_zones
node_pool_vm_size = local.kubernetes_instance_type
node_pool_zones = local.node_pool_zones
public_subnet = module.networking.public_subnet
resource_group = azurerm_resource_group.default
sku_tier = var.cluster_sku_tier
Expand Down
8 changes: 7 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,13 @@ variable "cluster_sku_tier" {
variable "node_pool_zones" {
type = list(string)
description = "Availability zones for the node pool"
default = ["1", "2"]
default = null
}

variable "node_pool_num_zones" {
type = number
description = "Number of availability zones to use for the node pool when node_pool_zones is not set."
default = 2
}

variable "node_max_pods" {
Expand Down
4 changes: 4 additions & 0 deletions versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ terraform {
source = "hashicorp/azurerm"
version = "~> 3.17"
}
azapi = {
source = "azure/azapi"
version = "~> 1.0"
}
kubernetes = {
source = "hashicorp/kubernetes"
version = "~> 2.23"
Expand Down
Loading