2022-10-06 05:51:26 -04:00
|
|
|
terraform {
|
|
|
|
required_providers {
|
|
|
|
azurerm = {
|
|
|
|
source = "hashicorp/azurerm"
|
2023-09-26 07:17:17 -04:00
|
|
|
version = "3.74.0"
|
2022-10-06 05:51:26 -04:00
|
|
|
}
|
|
|
|
random = {
|
|
|
|
source = "hashicorp/random"
|
2023-05-16 10:01:47 -04:00
|
|
|
version = "3.5.1"
|
2022-10-06 05:51:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
provider "azurerm" {
|
2023-04-19 02:30:11 -04:00
|
|
|
features {
|
|
|
|
resource_group {
|
|
|
|
prevent_deletion_if_contains_resources = false
|
|
|
|
}
|
|
|
|
}
|
2022-10-06 05:51:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
locals {
|
2023-07-21 10:43:51 -04:00
|
|
|
uid = random_id.uid.hex
|
|
|
|
name = "${var.name}-${local.uid}"
|
|
|
|
initSecretHash = random_password.initSecret.bcrypt_hash
|
|
|
|
tags = {
|
|
|
|
constellation-uid = local.uid,
|
|
|
|
}
|
2022-10-06 05:51:26 -04:00
|
|
|
ports_node_range = "30000-32767"
|
2023-10-25 18:32:49 -04:00
|
|
|
cidr_vpc_subnet_nodes = "10.9.0.0/16"
|
2023-10-05 06:34:02 -04:00
|
|
|
ports = flatten([
|
|
|
|
{ name = "kubernetes", port = "6443", health_check_protocol = "Https", path = "/readyz", priority = 100 },
|
|
|
|
{ name = "bootstrapper", port = "9000", health_check_protocol = "Tcp", path = null, priority = 101 },
|
|
|
|
{ name = "verify", port = "30081", health_check_protocol = "Tcp", path = null, priority = 102 },
|
|
|
|
{ name = "konnectivity", port = "8132", health_check_protocol = "Tcp", path = null, priority = 103 },
|
|
|
|
{ name = "recovery", port = "9999", health_check_protocol = "Tcp", path = null, priority = 104 },
|
|
|
|
{ name = "join", port = "30090", health_check_protocol = "Tcp", path = null, priority = 105 },
|
|
|
|
var.debug ? [{ name = "debugd", port = "4000", health_check_protocol = "Tcp", path = null, priority = 106 }] : [],
|
|
|
|
])
|
2023-07-21 10:43:51 -04:00
|
|
|
// wildcard_lb_dns_name is the DNS name of the load balancer with a wildcard for the name.
|
|
|
|
// example: given "name-1234567890.location.cloudapp.azure.com" it will return "*.location.cloudapp.azure.com"
|
2023-10-17 09:46:15 -04:00
|
|
|
wildcard_lb_dns_name = var.internal_load_balancer ? "" : replace(data.azurerm_public_ip.loadbalancer_ip[0].fqdn, "/^[^.]*\\./", "*.")
|
2023-08-03 07:22:26 -04:00
|
|
|
// deduce from format (subscriptions)/$ID/resourceGroups/$RG/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$NAME"
|
|
|
|
// move from the right as to ignore the optional prefixes
|
|
|
|
uai_resource_group = element(split("/", var.user_assigned_identity), length(split("/", var.user_assigned_identity)) - 5)
|
|
|
|
// deduce as above
|
|
|
|
uai_name = element(split("/", var.user_assigned_identity), length(split("/", var.user_assigned_identity)) - 1)
|
2023-10-17 09:46:15 -04:00
|
|
|
|
|
|
|
in_cluster_endpoint = var.internal_load_balancer ? azurerm_lb.loadbalancer.frontend_ip_configuration[0].private_ip_address : azurerm_public_ip.loadbalancer_ip[0].ip_address
|
|
|
|
out_of_cluster_endpoint = var.debug && var.internal_load_balancer ? module.jump_host[0].ip : local.in_cluster_endpoint
|
2022-10-06 05:51:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "random_id" "uid" {
|
|
|
|
byte_length = 4
|
|
|
|
}
|
|
|
|
|
2022-11-26 13:44:34 -05:00
|
|
|
resource "random_password" "initSecret" {
|
|
|
|
length = 32
|
|
|
|
special = true
|
|
|
|
override_special = "_%@"
|
|
|
|
}
|
|
|
|
|
2023-03-20 08:33:04 -04:00
|
|
|
resource "azurerm_attestation_provider" "attestation_provider" {
|
2023-03-20 12:53:00 -04:00
|
|
|
count = var.create_maa ? 1 : 0
|
|
|
|
# name must be between 3 and 24 characters in length and use numbers and lower-case letters only.
|
2023-03-21 05:41:48 -04:00
|
|
|
name = format("constell%s", local.uid)
|
2023-03-20 08:33:04 -04:00
|
|
|
resource_group_name = var.resource_group
|
|
|
|
location = var.location
|
2023-06-02 04:47:44 -04:00
|
|
|
|
|
|
|
lifecycle {
|
|
|
|
# Attestation policies will be set automatically upon creation, even if not specified in the resource,
|
|
|
|
# while they aren't being incorporated into the Terraform state correctly.
|
|
|
|
# To prevent them from being set to null when applying an upgrade, ignore the changes until the issue
|
|
|
|
# is resolved by Azure.
|
|
|
|
# Related issue: https://github.com/hashicorp/terraform-provider-azurerm/issues/21998
|
2023-08-02 02:15:22 -04:00
|
|
|
ignore_changes = [open_enclave_policy_base64, sgx_enclave_policy_base64, tpm_policy_base64, sev_snp_policy_base64]
|
2023-06-02 04:47:44 -04:00
|
|
|
}
|
2023-03-20 08:33:04 -04:00
|
|
|
}
|
|
|
|
|
2022-10-06 05:51:26 -04:00
|
|
|
resource "azurerm_application_insights" "insights" {
|
|
|
|
name = local.name
|
|
|
|
location = var.location
|
|
|
|
resource_group_name = var.resource_group
|
|
|
|
application_type = "other"
|
|
|
|
tags = local.tags
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_public_ip" "loadbalancer_ip" {
|
2023-10-17 09:46:15 -04:00
|
|
|
count = var.internal_load_balancer ? 0 : 1
|
2023-04-17 05:00:35 -04:00
|
|
|
name = "${local.name}-lb"
|
2023-07-21 10:43:51 -04:00
|
|
|
domain_name_label = local.name
|
2022-10-06 05:51:26 -04:00
|
|
|
resource_group_name = var.resource_group
|
|
|
|
location = var.location
|
|
|
|
allocation_method = "Static"
|
|
|
|
sku = "Standard"
|
|
|
|
tags = local.tags
|
2023-07-19 04:15:23 -04:00
|
|
|
|
|
|
|
lifecycle {
|
|
|
|
ignore_changes = [name]
|
|
|
|
}
|
2022-10-06 05:51:26 -04:00
|
|
|
}
|
|
|
|
|
2023-07-26 09:23:37 -04:00
|
|
|
// Reads data from the resource of the same name.
|
|
|
|
// Used to wait to the actual resource to become ready, before using data from that resource.
|
|
|
|
// Property "fqdn" only becomes available on azurerm_public_ip resources once domain_name_label is set.
|
|
|
|
// Since we are setting domain_name_label starting with 2.10 we need to migrate
|
|
|
|
// resources for clusters created before 2.9. In those cases we need to wait until loadbalancer_ip has
|
|
|
|
// been updated before reading from it.
|
|
|
|
data "azurerm_public_ip" "loadbalancer_ip" {
|
2023-10-17 09:46:15 -04:00
|
|
|
count = var.internal_load_balancer ? 0 : 1
|
2023-07-26 09:23:37 -04:00
|
|
|
name = "${local.name}-lb"
|
|
|
|
resource_group_name = var.resource_group
|
|
|
|
depends_on = [azurerm_public_ip.loadbalancer_ip]
|
|
|
|
}
|
|
|
|
|
2023-04-17 05:00:35 -04:00
|
|
|
resource "azurerm_public_ip" "nat_gateway_ip" {
|
|
|
|
name = "${local.name}-nat"
|
|
|
|
resource_group_name = var.resource_group
|
|
|
|
location = var.location
|
|
|
|
allocation_method = "Static"
|
|
|
|
sku = "Standard"
|
|
|
|
tags = local.tags
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_nat_gateway" "gateway" {
|
|
|
|
name = local.name
|
|
|
|
location = var.location
|
|
|
|
resource_group_name = var.resource_group
|
|
|
|
sku_name = "Standard"
|
|
|
|
idle_timeout_in_minutes = 10
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_subnet_nat_gateway_association" "example" {
|
|
|
|
nat_gateway_id = azurerm_nat_gateway.gateway.id
|
|
|
|
subnet_id = azurerm_subnet.node_subnet.id
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_nat_gateway_public_ip_association" "example" {
|
|
|
|
nat_gateway_id = azurerm_nat_gateway.gateway.id
|
|
|
|
public_ip_address_id = azurerm_public_ip.nat_gateway_ip.id
|
|
|
|
}
|
|
|
|
|
2022-10-06 05:51:26 -04:00
|
|
|
resource "azurerm_lb" "loadbalancer" {
|
|
|
|
name = local.name
|
|
|
|
location = var.location
|
|
|
|
resource_group_name = var.resource_group
|
|
|
|
sku = "Standard"
|
|
|
|
tags = local.tags
|
|
|
|
|
2023-10-17 09:46:15 -04:00
|
|
|
dynamic "frontend_ip_configuration" {
|
|
|
|
for_each = var.internal_load_balancer ? [] : [1]
|
|
|
|
content {
|
|
|
|
name = "PublicIPAddress"
|
|
|
|
public_ip_address_id = azurerm_public_ip.loadbalancer_ip[0].id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dynamic "frontend_ip_configuration" {
|
|
|
|
for_each = var.internal_load_balancer ? [1] : []
|
|
|
|
content {
|
|
|
|
name = "PrivateIPAddress"
|
|
|
|
private_ip_address_allocation = "Dynamic"
|
|
|
|
subnet_id = azurerm_subnet.loadbalancer_subnet[0].id
|
|
|
|
}
|
2022-10-06 05:51:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module "loadbalancer_backend_control_plane" {
|
|
|
|
source = "./modules/load_balancer_backend"
|
|
|
|
|
2023-10-05 06:34:02 -04:00
|
|
|
name = "${local.name}-control-plane"
|
|
|
|
loadbalancer_id = azurerm_lb.loadbalancer.id
|
|
|
|
frontend_ip_configuration_name = azurerm_lb.loadbalancer.frontend_ip_configuration[0].name
|
|
|
|
ports = local.ports
|
2022-10-06 05:51:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module "loadbalancer_backend_worker" {
|
|
|
|
source = "./modules/load_balancer_backend"
|
|
|
|
|
2023-10-05 06:34:02 -04:00
|
|
|
name = "${local.name}-worker"
|
|
|
|
loadbalancer_id = azurerm_lb.loadbalancer.id
|
|
|
|
frontend_ip_configuration_name = azurerm_lb.loadbalancer.frontend_ip_configuration[0].name
|
|
|
|
ports = []
|
2022-10-06 05:51:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_lb_backend_address_pool" "all" {
|
|
|
|
loadbalancer_id = azurerm_lb.loadbalancer.id
|
|
|
|
name = "${var.name}-all"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_virtual_network" "network" {
|
|
|
|
name = local.name
|
|
|
|
resource_group_name = var.resource_group
|
|
|
|
location = var.location
|
|
|
|
address_space = ["10.0.0.0/8"]
|
|
|
|
tags = local.tags
|
|
|
|
}
|
|
|
|
|
2023-10-17 09:46:15 -04:00
|
|
|
resource "azurerm_subnet" "loadbalancer_subnet" {
|
|
|
|
count = var.internal_load_balancer ? 1 : 0
|
|
|
|
name = "${local.name}-lb"
|
|
|
|
resource_group_name = var.resource_group
|
|
|
|
virtual_network_name = azurerm_virtual_network.network.name
|
|
|
|
address_prefixes = ["10.10.0.0/16"]
|
|
|
|
}
|
|
|
|
|
2022-10-06 05:51:26 -04:00
|
|
|
resource "azurerm_subnet" "node_subnet" {
|
|
|
|
name = "${local.name}-node"
|
|
|
|
resource_group_name = var.resource_group
|
|
|
|
virtual_network_name = azurerm_virtual_network.network.name
|
2023-10-25 18:32:49 -04:00
|
|
|
address_prefixes = [local.cidr_vpc_subnet_nodes]
|
2022-10-06 05:51:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_network_security_group" "security_group" {
|
|
|
|
name = local.name
|
|
|
|
location = var.location
|
|
|
|
resource_group_name = var.resource_group
|
|
|
|
tags = local.tags
|
|
|
|
|
|
|
|
dynamic "security_rule" {
|
2023-10-05 06:34:02 -04:00
|
|
|
for_each = concat(
|
|
|
|
local.ports,
|
|
|
|
[{ name = "nodeports", port = local.ports_node_range, priority = 200 }]
|
|
|
|
)
|
2022-10-06 05:51:26 -04:00
|
|
|
content {
|
|
|
|
name = security_rule.value.name
|
|
|
|
priority = security_rule.value.priority
|
|
|
|
direction = "Inbound"
|
|
|
|
access = "Allow"
|
|
|
|
protocol = "Tcp"
|
|
|
|
source_port_range = "*"
|
2023-10-05 06:34:02 -04:00
|
|
|
destination_port_range = security_rule.value.port
|
2022-10-06 05:51:26 -04:00
|
|
|
source_address_prefix = "*"
|
|
|
|
destination_address_prefix = "*"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 10:53:40 -04:00
|
|
|
module "scale_set_group" {
|
|
|
|
source = "./modules/scale_set"
|
|
|
|
for_each = var.node_groups
|
|
|
|
base_name = local.name
|
|
|
|
node_group_name = each.key
|
|
|
|
role = each.value.role
|
|
|
|
zones = each.value.zones
|
2023-03-20 08:33:04 -04:00
|
|
|
tags = merge(
|
|
|
|
local.tags,
|
|
|
|
{ constellation-init-secret-hash = local.initSecretHash },
|
|
|
|
{ constellation-maa-url = var.create_maa ? azurerm_attestation_provider.attestation_provider[0].attestation_uri : "" },
|
|
|
|
)
|
2023-06-22 10:53:40 -04:00
|
|
|
|
2023-06-30 04:53:00 -04:00
|
|
|
initial_count = each.value.initial_count
|
2023-06-22 10:53:40 -04:00
|
|
|
state_disk_size = each.value.disk_size
|
|
|
|
state_disk_type = each.value.disk_type
|
|
|
|
location = var.location
|
|
|
|
instance_type = each.value.instance_type
|
|
|
|
confidential_vm = var.confidential_vm
|
|
|
|
secure_boot = var.secure_boot
|
|
|
|
resource_group = var.resource_group
|
2022-10-06 05:51:26 -04:00
|
|
|
user_assigned_identity = var.user_assigned_identity
|
2023-06-22 10:53:40 -04:00
|
|
|
image_id = var.image_id
|
2022-10-06 05:51:26 -04:00
|
|
|
network_security_group_id = azurerm_network_security_group.security_group.id
|
|
|
|
subnet_id = azurerm_subnet.node_subnet.id
|
2023-06-22 10:53:40 -04:00
|
|
|
backend_address_pool_ids = each.value.role == "control-plane" ? [
|
2022-10-06 05:51:26 -04:00
|
|
|
azurerm_lb_backend_address_pool.all.id,
|
|
|
|
module.loadbalancer_backend_control_plane.backendpool_id
|
2023-06-22 10:53:40 -04:00
|
|
|
] : [
|
|
|
|
azurerm_lb_backend_address_pool.all.id,
|
|
|
|
module.loadbalancer_backend_worker.backendpool_id
|
2022-10-06 05:51:26 -04:00
|
|
|
]
|
2023-12-08 08:40:31 -05:00
|
|
|
marketplace_image = var.marketplace_image
|
2022-10-06 05:51:26 -04:00
|
|
|
}
|
|
|
|
|
2023-10-17 09:46:15 -04:00
|
|
|
module "jump_host" {
|
|
|
|
count = var.internal_load_balancer && var.debug ? 1 : 0
|
|
|
|
source = "./modules/jump_host"
|
|
|
|
base_name = local.name
|
|
|
|
resource_group = var.resource_group
|
|
|
|
location = var.location
|
|
|
|
subnet_id = azurerm_subnet.loadbalancer_subnet[0].id
|
|
|
|
ports = [for port in local.ports : port.port]
|
|
|
|
lb_internal_ip = azurerm_lb.loadbalancer.frontend_ip_configuration[0].private_ip_address
|
|
|
|
}
|
|
|
|
|
2023-07-31 04:53:05 -04:00
|
|
|
data "azurerm_subscription" "current" {
|
|
|
|
}
|
|
|
|
|
2023-08-01 02:40:44 -04:00
|
|
|
data "azurerm_user_assigned_identity" "uaid" {
|
|
|
|
name = local.uai_name
|
|
|
|
resource_group_name = local.uai_resource_group
|
|
|
|
}
|