homelab-k3s/Terraform/main.tf
2024-10-29 23:33:00 +03:00

129 lines
3.3 KiB
HCL

resource "proxmox_vm_qemu" "proxmox_vm_master" {
for_each = { for i, ip in var.master_ips : i => ip }
name = "k3s-master-${each.key}"
target_node = var.pm_node_name
clone = var.tamplate_vm_name
os_type = "cloud-init"
agent = 1
memory = var.num_k3s_masters_mem
cores = 4
cpu = "host"
vcpus = 1
scsihw = "virtio-scsi-pci"
bootdisk = "scsi0"
disk {
slot = "scsi0"
size = "50G"
type = "disk"
storage = "local-lvm"
iothread = true
}
disk {
slot = "scsi1"
type = "cloudinit"
storage = "local-lvm"
backup = true
}
full_clone = true
ciuser = "root"
cipassword = "8148"
sshkeys = file("/root/homelab-k3s/Terraform/key/id_rsa.pub")
cicustom = "user=local:snippets/cloud_init_master_${each.key}.yml"
ipconfig0 = "ip=${each.value}/${var.networkrange},gw=${var.gateway}"
lifecycle {
ignore_changes = [
ciuser,
sshkeys,
disk,
network
]
}
}
resource "proxmox_vm_qemu" "proxmox_vm_workers" {
for_each = { for i, ip in var.worker_ips : i => ip }
name = "k3s-worker-${each.key}"
target_node = var.pm_node_name
clone = var.tamplate_vm_name
os_type = "cloud-init"
agent = 1
memory = var.num_k3s_nodes_mem
cores = 4
cpu = "host"
vcpus = 1
scsihw = "virtio-scsi-pci"
bootdisk = "scsi0"
disk {
slot = "scsi0"
size = "50G"
type = "disk"
storage = "local-lvm"
iothread = true
}
disk {
slot = "scsi1"
type = "cloudinit"
storage = "local-lvm"
backup = true
}
full_clone = true
ciuser = "root"
cipassword = "8148"
sshkeys = file("/root/homelab-k3s/Terraform/key/id_rsa.pub")
cicustom = "user=local:snippets/cloud_init_worker_${each.key}.yml"
ipconfig0 = "ip=${each.value}/${var.networkrange},gw=${var.gateway}"
lifecycle {
ignore_changes = [
ciuser,
sshkeys,
disk,
network
]
}
}
locals {
k8s_config = templatefile("./templates/k8s.tpl", {
k3s_master_ip = join("\n", [for instance in proxmox_vm_qemu.proxmox_vm_master : join("", [instance.default_ipv4_address, " ansible_ssh_private_key_file=", var.pvt_key])]),
k3s_node_ip = join("\n", [for instance in proxmox_vm_qemu.proxmox_vm_workers : join("", [instance.default_ipv4_address, " ansible_ssh_private_key_file=", var.pvt_key])])
})
}
resource "local_file" "k8s_file" {
content = local.k8s_config
filename = "../inventory/my-cluster/hosts.ini"
}
resource "local_file" "var_file" {
source = "/root/homelab-k3s/inventory/group_vars/all.yml"
filename = "/root/homelab-k3s/inventory/my-cluster/group_vars/all.yml"
}
resource "local_file" "cloud_init_master" {
for_each = { for i, ip in var.master_ips : i => ip }
content = templatefile("/root/homelab-k3s/snippets/cloud_init_master.yml", {
hostname = "k3s-master-${each.key}",
})
filename = "/root/homelab-k3s/snippets/cloud_init_master_${each.key}.yml"
}
resource "local_file" "cloud_init_worker" {
for_each = { for i, ip in var.worker_ips : i => ip }
content = templatefile("/root/homelab-k3s/snippets/cloud_init_worker.yml", {
hostname = "k3s-worker-${each.key}",
})
filename = "/root/homelab-k3s/snippets/cloud_init_worker_${each.key}.yml"
}