124 lines
2.8 KiB
HCL
124 lines
2.8 KiB
HCL
terraform {
|
|
required_providers {
|
|
libvirt = {
|
|
source = "dmacvicar/libvirt"
|
|
version = "0.8.3"
|
|
}
|
|
}
|
|
}
|
|
|
|
provider "libvirt" {
|
|
uri = var.libvirt_uri
|
|
}
|
|
|
|
# --- Base Image Handling ---
|
|
# Download the base cloud image if it doesn't exist locally
|
|
resource "libvirt_volume" "base_image" {
|
|
name = var.base_image_name
|
|
pool = var.libvirt_pool
|
|
source = var.base_image_url
|
|
format = "qcow2"
|
|
}
|
|
|
|
# --- Cloud-Init Configuration ---
|
|
# Common cloud-init data for all nodes
|
|
data "cloudinit_config" "common_init" {
|
|
gzip = false
|
|
base64_encode = false
|
|
|
|
part {
|
|
content_type = "text/cloud-config"
|
|
content = templatefile("${path.module}/cloud_init.cfg.yml", {
|
|
user = var.vm_user
|
|
ssh_authorized_keys = [chomp(file(pathexpand(var.ssh_public_key_path)))]
|
|
})
|
|
}
|
|
}
|
|
|
|
# Create a cloud-init ISO disk using the common config
|
|
resource "libvirt_cloudinit_disk" "common_iso" {
|
|
name = "${var.cluster_name}-common-init.iso"
|
|
user_data = data.cloudinit_config.common_init.rendered
|
|
pool = var.libvirt_pool
|
|
}
|
|
|
|
# # --- Network ---
|
|
# # Use the default libvirt network
|
|
# data "libvirt_network" "default_network" {
|
|
# name = var.libvirt_network_name
|
|
# }
|
|
|
|
# --- K3s Server Node ---
|
|
# Create a volume for the server node based on the base image
|
|
resource "libvirt_volume" "server_disk" {
|
|
name = "${var.server_hostname}-disk.qcow2"
|
|
base_volume_id = libvirt_volume.base_image.id
|
|
pool = var.libvirt_pool
|
|
size = var.vm_disk_size
|
|
format = "qcow2"
|
|
}
|
|
|
|
resource "libvirt_domain" "server" {
|
|
name = var.server_hostname
|
|
memory = var.vm_memory
|
|
vcpu = var.vm_vcpu
|
|
|
|
cloudinit = libvirt_cloudinit_disk.common_iso.id
|
|
|
|
network_interface {
|
|
network_name = var.libvirt_network_name
|
|
wait_for_lease = true
|
|
}
|
|
|
|
disk {
|
|
volume_id = libvirt_volume.server_disk.id
|
|
}
|
|
|
|
console {
|
|
type = "pty"
|
|
target_port = "0"
|
|
target_type = "serial"
|
|
}
|
|
graphics {
|
|
type = "spice"
|
|
listen_type = "address"
|
|
autoport = true
|
|
}
|
|
}
|
|
|
|
# --- K3s Agent Node ---
|
|
resource "libvirt_volume" "agent_disk" {
|
|
name = "${var.agent_hostname}-disk.qcow2"
|
|
base_volume_id = libvirt_volume.base_image.id
|
|
pool = var.libvirt_pool
|
|
size = var.vm_disk_size
|
|
format = "qcow2"
|
|
}
|
|
|
|
resource "libvirt_domain" "agent" {
|
|
name = var.agent_hostname
|
|
memory = var.vm_memory
|
|
vcpu = var.vm_vcpu
|
|
|
|
cloudinit = libvirt_cloudinit_disk.common_iso.id
|
|
|
|
network_interface {
|
|
network_name = var.libvirt_network_name
|
|
wait_for_lease = true
|
|
}
|
|
|
|
disk {
|
|
volume_id = libvirt_volume.agent_disk.id
|
|
}
|
|
|
|
console {
|
|
type = "pty"
|
|
target_port = "0"
|
|
target_type = "serial"
|
|
}
|
|
graphics {
|
|
type = "spice"
|
|
listen_type = "address"
|
|
autoport = true
|
|
}
|
|
}
|