Skip to content

Commit

Permalink
fix: Query zone info via azapi instead of using external data provider
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpanzella committed Sep 27, 2024
1 parent 2141a2a commit c081926
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
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
31 changes: 15 additions & 16 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,24 @@ locals {
kubernetes_instance_type = try(local.deployment_size[var.size].node_type, var.kubernetes_instance_type)
}

resource "null_resource" "install_az_cli" {
provisioner "local-exec" {
command = <<EOF
. /etc/lsb-release
wget https://packages.microsoft.com/repos/azure-cli/pool/main/a/azure-cli/azure-cli_2.36.0-1~$${DISTRIB_CODENAME}_all.deb
mkdir ./env && dpkg -x *.deb ./env
EOF
}
triggers = {
always_run = uuid()
}
}
data "azapi_resource_list" "az_zones" {
parent_id = "/subscriptions/${data.azurerm_subscription.current.subscription_id}"
type = "Microsoft.Compute/skus@2021-07-01"

data "external" "az_zones" {
program = ["bash", "${path.module}/vmtype_to_az.sh", local.kubernetes_instance_type, azurerm_resource_group.default.location, var.node_pool_num_zones]
response_export_values = ["value"]
}

locals {
node_pool_zones = (var.node_pool_zones == null) ? jsondecode(data.external.az_zones.result.zones) : var.node_pool_zones
vm_skus = [
for sku in jsondecode(data.azapi_resource_list.az_zones.output).value :
sku if (
sku.resourceType == "virtualMachines" &&
contains(sku.locations, 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" {
Expand All @@ -144,7 +143,7 @@ module "app_aks" {
namespace = var.namespace
node_pool_vm_count = try(local.deployment_size[var.size].node_count, var.kubernetes_node_count)
node_pool_vm_size = local.kubernetes_instance_type
node_pool_zones = var.node_pool_zones
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
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

0 comments on commit c081926

Please sign in to comment.