From 1e0f492b1d7f998cb7996e0f307c1e32a07cea36 Mon Sep 17 00:00:00 2001 From: clyhtsuriva Date: Thu, 6 Feb 2025 22:13:14 +0100 Subject: opentofu: completely migrated k8-cluster.tf to modules Successfully built on proxmox --- opentofu/inventory.ini | 8 +- opentofu/inventory.tf | 6 +- opentofu/k8s-cluster.tf | 125 ----------------------- opentofu/main.tf | 68 ++++++++++++ opentofu/modules/ansible_provisioner/versions.tf | 11 ++ opentofu/modules/k8s_control_plane/main.tf | 55 ++++++++++ opentofu/modules/k8s_control_plane/outputs.tf | 3 + opentofu/modules/k8s_control_plane/variables.tf | 25 +++++ opentofu/modules/k8s_control_plane/versions.tf | 11 ++ opentofu/modules/k8s_worker/main.tf | 56 ++++++++++ opentofu/modules/k8s_worker/outputs.tf | 3 + opentofu/modules/k8s_worker/variables.tf | 26 +++++ opentofu/modules/k8s_worker/versions.tf | 10 ++ opentofu/outputs.tf | 4 +- opentofu/versions.tf | 4 - 15 files changed, 277 insertions(+), 138 deletions(-) delete mode 100644 opentofu/k8s-cluster.tf create mode 100644 opentofu/main.tf create mode 100644 opentofu/modules/ansible_provisioner/versions.tf create mode 100644 opentofu/modules/k8s_control_plane/main.tf create mode 100644 opentofu/modules/k8s_control_plane/outputs.tf create mode 100644 opentofu/modules/k8s_control_plane/variables.tf create mode 100644 opentofu/modules/k8s_control_plane/versions.tf create mode 100644 opentofu/modules/k8s_worker/main.tf create mode 100644 opentofu/modules/k8s_worker/outputs.tf create mode 100644 opentofu/modules/k8s_worker/variables.tf create mode 100644 opentofu/modules/k8s_worker/versions.tf diff --git a/opentofu/inventory.ini b/opentofu/inventory.ini index 6ddc132..a694cef 100755 --- a/opentofu/inventory.ini +++ b/opentofu/inventory.ini @@ -1,12 +1,12 @@ [kube_control_plane] -192.168.1.103 +192.168.1.113 [etcd] -192.168.1.103 +192.168.1.113 [kube_node] -192.168.1.105 -192.168.1.104 +192.168.1.110 +192.168.1.111 [k8s_cluster:children] diff --git a/opentofu/inventory.tf b/opentofu/inventory.tf index ddf3ba4..16d183a 100644 --- a/opentofu/inventory.tf +++ b/opentofu/inventory.tf @@ -2,13 +2,13 @@ resource "local_file" "ansible_inventory" { filename = "${path.module}/inventory.ini" content = <<-EOT [kube_control_plane] - ${proxmox_vm_qemu.k8s_cp.default_ipv4_address} + ${module.k8s_control_plane.vm.default_ipv4_address} [etcd] - ${proxmox_vm_qemu.k8s_cp.default_ipv4_address} + ${module.k8s_control_plane.vm.default_ipv4_address} [kube_node] - %{for vm in proxmox_vm_qemu.k8s_worker[*]}${vm.default_ipv4_address} + %{for vm in module.k8s_worker.vms}${vm.default_ipv4_address} %{endfor} [k8s_cluster:children] diff --git a/opentofu/k8s-cluster.tf b/opentofu/k8s-cluster.tf deleted file mode 100644 index c6392cd..0000000 --- a/opentofu/k8s-cluster.tf +++ /dev/null @@ -1,125 +0,0 @@ -resource "proxmox_vm_qemu" "k8s_cp" { - - lifecycle { - ignore_changes = [ - bootdisk, - ] - } - - name = "k8s-cp-01" - desc = "k8s control plane" - agent = 1 # Qemu Guest Agent - target_node = var.proxmox_node - tags = "debian;k8s" - - clone = var.debian_server_bookworm_packer_image_name - full_clone = true - - qemu_os = "other" - cores = 2 - sockets = 1 - cpu_type = "host" - memory = 6144 - - scsihw = "virtio-scsi-pci" - bootdisk = "scsi0" - - disks { - ide { - ide0 { - cloudinit { - storage = "local-lvm" - } - } - } - virtio { - virtio0 { - disk { - storage = "local-lvm" - size = "32G" - iothread = true - replicate = false - } - } - } - } - - network { - id = 0 - model = "virtio" - bridge = "vmbr0" - } - - # Cloud-Init settings - ipconfig0 = "ip=dhcp" - ciuser = "mas" - sshkeys = var.ssh_public_key -} - -resource "proxmox_vm_qemu" "k8s_worker" { - - lifecycle { - ignore_changes = [ - bootdisk, - ] - } - - count = var.k8s_worker_vm_count - name = "${var.k8s_worker_vm_name_prefix}-${count.index}" - desc = "k8s worker" - agent = 1 # Qemu Guest Agent - target_node = var.proxmox_node - tags = "debian;k8s" - - clone = var.debian_server_bookworm_packer_image_name - full_clone = true - - qemu_os = "other" - cores = 1 - sockets = 1 - cpu_type = "host" - memory = 2048 - - scsihw = "virtio-scsi-pci" - bootdisk = "scsi0" - - disks { - ide { - ide0 { - cloudinit { - storage = "local-lvm" - } - } - } - virtio { - virtio0 { - disk { - storage = "local-lvm" - size = "32G" - iothread = true - replicate = false - } - } - } - } - - network { - id = 0 - model = "virtio" - bridge = "vmbr0" - } - - # Cloud-Init settings - ipconfig0 = "ip=dhcp" - ciuser = "mas" - sshkeys = var.ssh_public_key -} - -# Provision the control plane node and the workers -module "ansible_provision_k8s" { - source = "./modules/ansible_provisioner" - inventory_file_path = local_file.ansible_inventory.filename # Pass inventory path here - vm_username = var.vm_username - ssh_private_key_path = var.ssh_private_key_path - ansible_playbook_path = var.k8s_ansible_playbook_path -} diff --git a/opentofu/main.tf b/opentofu/main.tf new file mode 100644 index 0000000..3da6045 --- /dev/null +++ b/opentofu/main.tf @@ -0,0 +1,68 @@ +module "k8s_control_plane" { + source = "./modules/k8s_control_plane" + + name = "k8s-cp-01" + desc = "k8s control plane" + agent = 1 + target_node = var.proxmox_node + tags = "debian;k8s" + clone = var.debian_server_bookworm_packer_image_name + full_clone = true + qemu_os = "other" + cores = 2 + sockets = 1 + cpu_type = "host" + memory = 6144 + scsihw = "virtio-scsi-pci" + bootdisk = "scsi0" + cloudinit_storage = "local-lvm" + disk_storage = "local-lvm" + disk_size = "32G" + iothread = true + replicate = false + network_id = 0 + network_model = "virtio" + network_bridge = "vmbr0" + ipconfig0 = "ip=dhcp" + ciuser = "mas" + sshkeys = var.ssh_public_key +} + +module "k8s_worker" { + source = "./modules/k8s_worker" + + vm_count = var.k8s_worker_vm_count + name_prefix = var.k8s_worker_vm_name_prefix + desc = "k8s worker" + agent = 1 + target_node = var.proxmox_node + tags = "debian;k8s" + clone = var.debian_server_bookworm_packer_image_name + full_clone = true + qemu_os = "other" + cores = 1 + sockets = 1 + cpu_type = "host" + memory = 2048 + scsihw = "virtio-scsi-pci" + bootdisk = "scsi0" + cloudinit_storage = "local-lvm" + disk_storage = "local-lvm" + disk_size = "32G" + iothread = true + replicate = false + network_id = 0 + network_model = "virtio" + network_bridge = "vmbr0" + ipconfig0 = "ip=dhcp" + ciuser = "mas" + sshkeys = var.ssh_public_key +} + +module "ansible_provision_k8s" { + source = "./modules/ansible_provisioner" + inventory_file_path = local_file.ansible_inventory.filename + vm_username = var.vm_username + ssh_private_key_path = var.ssh_private_key_path + ansible_playbook_path = var.k8s_ansible_playbook_path +} diff --git a/opentofu/modules/ansible_provisioner/versions.tf b/opentofu/modules/ansible_provisioner/versions.tf new file mode 100644 index 0000000..d1135ae --- /dev/null +++ b/opentofu/modules/ansible_provisioner/versions.tf @@ -0,0 +1,11 @@ +terraform { + required_version = ">= 1.8.0" + + required_providers { + null = { + source = "hashicorp/null" + version = ">= 0.0.0" + } + } +} + diff --git a/opentofu/modules/k8s_control_plane/main.tf b/opentofu/modules/k8s_control_plane/main.tf new file mode 100644 index 0000000..d8477af --- /dev/null +++ b/opentofu/modules/k8s_control_plane/main.tf @@ -0,0 +1,55 @@ +resource "proxmox_vm_qemu" "k8s_cp" { + lifecycle { + ignore_changes = [ + bootdisk, + ] + } + + name = var.name + desc = var.desc + agent = var.agent + target_node = var.target_node + tags = var.tags + + clone = var.clone + full_clone = var.full_clone + + qemu_os = var.qemu_os + cores = var.cores + sockets = var.sockets + cpu_type = var.cpu_type + memory = var.memory + + scsihw = var.scsihw + bootdisk = var.bootdisk + + disks { + ide { + ide0 { + cloudinit { + storage = var.cloudinit_storage + } + } + } + virtio { + virtio0 { + disk { + storage = var.disk_storage + size = var.disk_size + iothread = var.iothread + replicate = var.replicate + } + } + } + } + + network { + id = var.network_id + model = var.network_model + bridge = var.network_bridge + } + + ipconfig0 = var.ipconfig0 + ciuser = var.ciuser + sshkeys = var.sshkeys +} diff --git a/opentofu/modules/k8s_control_plane/outputs.tf b/opentofu/modules/k8s_control_plane/outputs.tf new file mode 100644 index 0000000..a081907 --- /dev/null +++ b/opentofu/modules/k8s_control_plane/outputs.tf @@ -0,0 +1,3 @@ +output "vm" { + value = proxmox_vm_qemu.k8s_cp +} diff --git a/opentofu/modules/k8s_control_plane/variables.tf b/opentofu/modules/k8s_control_plane/variables.tf new file mode 100644 index 0000000..e359f77 --- /dev/null +++ b/opentofu/modules/k8s_control_plane/variables.tf @@ -0,0 +1,25 @@ +variable "name" {} +variable "desc" {} +variable "agent" {} +variable "target_node" {} +variable "tags" {} +variable "clone" {} +variable "full_clone" {} +variable "qemu_os" {} +variable "cores" {} +variable "sockets" {} +variable "cpu_type" {} +variable "memory" {} +variable "scsihw" {} +variable "bootdisk" {} +variable "cloudinit_storage" {} +variable "disk_storage" {} +variable "disk_size" {} +variable "iothread" {} +variable "replicate" {} +variable "network_id" {} +variable "network_model" {} +variable "network_bridge" {} +variable "ipconfig0" {} +variable "ciuser" {} +variable "sshkeys" {} diff --git a/opentofu/modules/k8s_control_plane/versions.tf b/opentofu/modules/k8s_control_plane/versions.tf new file mode 100644 index 0000000..07c0f16 --- /dev/null +++ b/opentofu/modules/k8s_control_plane/versions.tf @@ -0,0 +1,11 @@ +terraform { + required_version = ">= 1.8.0" + + required_providers { + proxmox = { + source = "telmate/proxmox" + version = "3.0.1-rc6" + } + } +} + diff --git a/opentofu/modules/k8s_worker/main.tf b/opentofu/modules/k8s_worker/main.tf new file mode 100644 index 0000000..ff039b2 --- /dev/null +++ b/opentofu/modules/k8s_worker/main.tf @@ -0,0 +1,56 @@ +resource "proxmox_vm_qemu" "k8s_worker" { + lifecycle { + ignore_changes = [ + bootdisk, + ] + } + + count = var.vm_count + name = "${var.name_prefix}-${count.index}" + desc = var.desc + agent = var.agent + target_node = var.target_node + tags = var.tags + + clone = var.clone + full_clone = var.full_clone + + qemu_os = var.qemu_os + cores = var.cores + sockets = var.sockets + cpu_type = var.cpu_type + memory = var.memory + + scsihw = var.scsihw + bootdisk = var.bootdisk + + disks { + ide { + ide0 { + cloudinit { + storage = var.cloudinit_storage + } + } + } + virtio { + virtio0 { + disk { + storage = var.disk_storage + size = var.disk_size + iothread = var.iothread + replicate = var.replicate + } + } + } + } + + network { + id = var.network_id + model = var.network_model + bridge = var.network_bridge + } + + ipconfig0 = var.ipconfig0 + ciuser = var.ciuser + sshkeys = var.sshkeys +} diff --git a/opentofu/modules/k8s_worker/outputs.tf b/opentofu/modules/k8s_worker/outputs.tf new file mode 100644 index 0000000..c72c51e --- /dev/null +++ b/opentofu/modules/k8s_worker/outputs.tf @@ -0,0 +1,3 @@ +output "vms" { + value = proxmox_vm_qemu.k8s_worker +} diff --git a/opentofu/modules/k8s_worker/variables.tf b/opentofu/modules/k8s_worker/variables.tf new file mode 100644 index 0000000..1328088 --- /dev/null +++ b/opentofu/modules/k8s_worker/variables.tf @@ -0,0 +1,26 @@ +variable "vm_count" {} +variable "name_prefix" {} +variable "desc" {} +variable "agent" {} +variable "target_node" {} +variable "tags" {} +variable "clone" {} +variable "full_clone" {} +variable "qemu_os" {} +variable "cores" {} +variable "sockets" {} +variable "cpu_type" {} +variable "memory" {} +variable "scsihw" {} +variable "bootdisk" {} +variable "cloudinit_storage" {} +variable "disk_storage" {} +variable "disk_size" {} +variable "iothread" {} +variable "replicate" {} +variable "network_id" {} +variable "network_model" {} +variable "network_bridge" {} +variable "ipconfig0" {} +variable "ciuser" {} +variable "sshkeys" {} diff --git a/opentofu/modules/k8s_worker/versions.tf b/opentofu/modules/k8s_worker/versions.tf new file mode 100644 index 0000000..d9656aa --- /dev/null +++ b/opentofu/modules/k8s_worker/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = ">= 1.8.0" + + required_providers { + proxmox = { + source = "telmate/proxmox" + version = "3.0.1-rc6" + } + } +} diff --git a/opentofu/outputs.tf b/opentofu/outputs.tf index c6c3d88..f0c8b7a 100644 --- a/opentofu/outputs.tf +++ b/opentofu/outputs.tf @@ -1,7 +1,7 @@ locals { all_vms = flatten([ - [proxmox_vm_qemu.k8s_cp], - proxmox_vm_qemu.k8s_worker, + [module.k8s_control_plane.vm], + module.k8s_worker.vms, [proxmox_vm_qemu.docker_server], ]) } diff --git a/opentofu/versions.tf b/opentofu/versions.tf index 04a162a..d9656aa 100644 --- a/opentofu/versions.tf +++ b/opentofu/versions.tf @@ -6,9 +6,5 @@ terraform { source = "telmate/proxmox" version = "3.0.1-rc6" } - null = { - source = "hashicorp/null" - version = ">= 0.0.0" - } } } -- cgit v1.2.3