2022-05-22 09:39:30 -04:00
|
|
|
terraform {
|
|
|
|
required_providers {
|
|
|
|
libvirt = {
|
|
|
|
source = "dmacvicar/libvirt"
|
2023-10-16 04:43:30 -04:00
|
|
|
version = "0.7.1"
|
2022-05-22 09:39:30 -04:00
|
|
|
}
|
2022-06-30 05:14:26 -04:00
|
|
|
docker = {
|
2022-07-08 04:59:59 -04:00
|
|
|
source = "kreuzwerker/docker"
|
2023-10-26 02:19:08 -04:00
|
|
|
version = "3.0.2"
|
2022-06-30 05:14:26 -04:00
|
|
|
}
|
2022-05-22 09:39:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
provider "libvirt" {
|
2022-10-05 03:11:30 -04:00
|
|
|
uri = var.libvirt_uri
|
2022-05-22 09:39:30 -04:00
|
|
|
}
|
|
|
|
|
2022-06-30 05:14:26 -04:00
|
|
|
provider "docker" {
|
|
|
|
host = "unix:///var/run/docker.sock"
|
|
|
|
}
|
|
|
|
|
2023-10-23 09:06:48 -04:00
|
|
|
locals {
|
|
|
|
cidr_vpc_subnet_nodes = "10.42.0.0/22"
|
|
|
|
cidr_vpc_subnet_control_planes = "10.42.1.0/24"
|
|
|
|
cidr_vpc_subnet_worker = "10.42.2.0/24"
|
|
|
|
}
|
|
|
|
|
2022-11-26 13:44:34 -05:00
|
|
|
resource "random_password" "initSecret" {
|
|
|
|
length = 32
|
|
|
|
special = true
|
|
|
|
override_special = "_%@"
|
|
|
|
}
|
2022-10-05 03:11:30 -04:00
|
|
|
resource "docker_image" "qemu_metadata" {
|
2022-09-27 04:47:45 -04:00
|
|
|
name = var.metadata_api_image
|
2022-07-08 04:59:59 -04:00
|
|
|
keep_locally = true
|
2022-06-30 05:14:26 -04:00
|
|
|
}
|
|
|
|
|
2022-10-05 03:11:30 -04:00
|
|
|
resource "docker_container" "qemu_metadata" {
|
2022-09-26 09:52:31 -04:00
|
|
|
name = "${var.name}-qemu-metadata"
|
2022-10-13 03:38:17 -04:00
|
|
|
image = docker_image.qemu_metadata.image_id
|
2022-06-30 05:14:26 -04:00
|
|
|
network_mode = "host"
|
2022-07-08 04:59:59 -04:00
|
|
|
rm = true
|
2022-09-27 04:47:45 -04:00
|
|
|
command = [
|
2022-09-26 09:52:31 -04:00
|
|
|
"--network",
|
|
|
|
"${var.name}-network",
|
2022-10-05 03:11:30 -04:00
|
|
|
"--libvirt-uri",
|
|
|
|
"${var.metadata_libvirt_uri}",
|
2022-11-26 13:44:34 -05:00
|
|
|
"--initsecrethash",
|
|
|
|
"${random_password.initSecret.bcrypt_hash}",
|
2022-09-26 09:52:31 -04:00
|
|
|
]
|
2022-06-30 05:14:26 -04:00
|
|
|
mounts {
|
2022-10-05 03:11:30 -04:00
|
|
|
source = abspath(var.libvirt_socket_path)
|
2022-06-30 05:14:26 -04:00
|
|
|
target = "/var/run/libvirt/libvirt-sock"
|
2022-07-08 04:59:59 -04:00
|
|
|
type = "bind"
|
2022-06-30 05:14:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-26 13:44:34 -05:00
|
|
|
|
2023-06-28 08:42:34 -04:00
|
|
|
module "node_group" {
|
2023-05-16 08:13:10 -04:00
|
|
|
source = "./modules/instance_group"
|
2023-06-28 08:42:34 -04:00
|
|
|
base_name = var.name
|
|
|
|
for_each = var.node_groups
|
|
|
|
node_group_name = each.key
|
|
|
|
role = each.value.role
|
2023-06-30 04:53:00 -04:00
|
|
|
amount = each.value.initial_count
|
2023-06-28 08:42:34 -04:00
|
|
|
state_disk_size = each.value.disk_size
|
|
|
|
vcpus = each.value.vcpus
|
|
|
|
memory = each.value.memory
|
2023-05-16 08:13:10 -04:00
|
|
|
machine = var.machine
|
2023-10-23 09:06:48 -04:00
|
|
|
cidr = each.value.role == "control-plane" ? local.cidr_vpc_subnet_control_planes : local.cidr_vpc_subnet_worker
|
2023-05-16 08:13:10 -04:00
|
|
|
network_id = libvirt_network.constellation.id
|
|
|
|
pool = libvirt_pool.cluster.name
|
|
|
|
boot_mode = var.constellation_boot_mode
|
|
|
|
boot_volume_id = libvirt_volume.constellation_os_image.id
|
|
|
|
kernel_volume_id = local.kernel_volume_id
|
|
|
|
initrd_volume_id = local.initrd_volume_id
|
2023-06-28 08:42:34 -04:00
|
|
|
kernel_cmdline = each.value.role == "control-plane" ? local.kernel_cmdline : var.constellation_cmdline
|
2023-05-16 08:13:10 -04:00
|
|
|
firmware = var.firmware
|
|
|
|
nvram = var.nvram
|
2022-05-22 09:39:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "libvirt_pool" "cluster" {
|
2022-09-26 09:52:31 -04:00
|
|
|
name = "${var.name}-storage-pool"
|
2022-05-22 09:39:30 -04:00
|
|
|
type = "dir"
|
|
|
|
path = "/var/lib/libvirt/images"
|
|
|
|
}
|
|
|
|
|
2022-10-21 04:16:44 -04:00
|
|
|
resource "libvirt_volume" "constellation_os_image" {
|
2022-09-26 09:52:31 -04:00
|
|
|
name = "${var.name}-node-image"
|
2022-05-22 09:39:30 -04:00
|
|
|
pool = libvirt_pool.cluster.name
|
2022-10-21 04:16:44 -04:00
|
|
|
source = var.constellation_os_image
|
2022-07-04 06:59:43 -04:00
|
|
|
format = var.image_format
|
2022-05-22 09:39:30 -04:00
|
|
|
}
|
|
|
|
|
2023-05-16 08:13:10 -04:00
|
|
|
resource "libvirt_volume" "constellation_kernel" {
|
|
|
|
name = "${var.name}-kernel"
|
|
|
|
pool = libvirt_pool.cluster.name
|
|
|
|
source = var.constellation_kernel
|
|
|
|
format = "raw"
|
|
|
|
count = var.constellation_boot_mode == "direct-linux-boot" ? 1 : 0
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "libvirt_volume" "constellation_initrd" {
|
|
|
|
name = "${var.name}-initrd"
|
|
|
|
pool = libvirt_pool.cluster.name
|
|
|
|
source = var.constellation_initrd
|
|
|
|
format = "raw"
|
|
|
|
count = var.constellation_boot_mode == "direct-linux-boot" ? 1 : 0
|
|
|
|
}
|
|
|
|
|
2022-05-22 09:39:30 -04:00
|
|
|
resource "libvirt_network" "constellation" {
|
2022-09-26 09:52:31 -04:00
|
|
|
name = "${var.name}-network"
|
2022-05-22 09:39:30 -04:00
|
|
|
mode = "nat"
|
|
|
|
addresses = ["10.42.0.0/16"]
|
|
|
|
dhcp {
|
|
|
|
enabled = true
|
|
|
|
}
|
|
|
|
dns {
|
|
|
|
enabled = true
|
|
|
|
}
|
|
|
|
}
|
2023-05-16 08:13:10 -04:00
|
|
|
|
|
|
|
locals {
|
|
|
|
kernel_volume_id = var.constellation_boot_mode == "direct-linux-boot" ? libvirt_volume.constellation_kernel[0].id : null
|
|
|
|
initrd_volume_id = var.constellation_boot_mode == "direct-linux-boot" ? libvirt_volume.constellation_initrd[0].id : null
|
|
|
|
kernel_cmdline = var.constellation_boot_mode == "direct-linux-boot" ? var.constellation_cmdline : null
|
|
|
|
}
|