From 1e1a8a1f3f4a8586072d3e31d2bef9c27786d448 Mon Sep 17 00:00:00 2001 From: clyhtsuriva Date: Tue, 18 Feb 2025 21:03:00 +0100 Subject: Replace proxmox provider telmate by bpg + remove k8s cluster (talos on the way) Also changes the id of the packer images, now starting at the "end" (999), while VMs start at 100 --- opentofu/docker-server.tf | 66 ----------- opentofu/inventory.tf | 18 --- opentofu/main.tf | 112 +++++++++--------- opentofu/modules/ansible_provisioner/main.tf | 2 +- opentofu/modules/k8s_control_plane/main.tf | 55 --------- opentofu/modules/k8s_control_plane/outputs.tf | 3 - opentofu/modules/k8s_control_plane/variables.tf | 143 ----------------------- 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 | 149 ------------------------ opentofu/modules/k8s_worker/versions.tf | 10 -- opentofu/outputs.tf | 6 +- opentofu/provider.tf | 7 +- opentofu/terraform.tfvars | 1 + opentofu/variables.tf | 11 +- opentofu/versions.tf | 4 +- 17 files changed, 67 insertions(+), 590 deletions(-) delete mode 100644 opentofu/docker-server.tf delete mode 100644 opentofu/inventory.tf delete mode 100644 opentofu/modules/k8s_control_plane/main.tf delete mode 100644 opentofu/modules/k8s_control_plane/outputs.tf delete mode 100644 opentofu/modules/k8s_control_plane/variables.tf delete mode 100644 opentofu/modules/k8s_control_plane/versions.tf delete mode 100644 opentofu/modules/k8s_worker/main.tf delete mode 100644 opentofu/modules/k8s_worker/outputs.tf delete mode 100644 opentofu/modules/k8s_worker/variables.tf delete mode 100644 opentofu/modules/k8s_worker/versions.tf (limited to 'opentofu') diff --git a/opentofu/docker-server.tf b/opentofu/docker-server.tf deleted file mode 100644 index 04b2cb1..0000000 --- a/opentofu/docker-server.tf +++ /dev/null @@ -1,66 +0,0 @@ -resource "proxmox_vm_qemu" "docker_server" { - - lifecycle { - ignore_changes = [ - bootdisk, - ] - } - - name = "docker-server" - desc = "Debian server with docker installed." - agent = 1 # Qemu Guest Agent - target_node = var.proxmox_node - tags = "debian;docker" - - clone = var.debian_server_bookworm_packer_image_name - full_clone = true - - qemu_os = "other" - cores = 2 - sockets = 1 - cpu_type = "host" - memory = 4096 - - scsihw = "virtio-scsi-pci" - bootdisk = "scsi0" - - disks { - ide { - ide0 { - cloudinit { - storage = "local-lvm" - } - } - } - virtio { - virtio0 { - disk { - storage = "local-lvm" - size = "20G" - iothread = true - replicate = false - } - } - } - } - - network { - id = 0 - model = "virtio" - bridge = "vmbr0" - } - - # Cloud-Init settings - ipconfig0 = "ip=dhcp" - ciuser = "mas" - sshkeys = var.ssh_public_key -} - -# Run Ansible playbook after VM creation to install Docker -module "ansible_provision_docker_server" { - source = "./modules/ansible_provisioner" - vm_ip = proxmox_vm_qemu.docker_server.default_ipv4_address # Pass only the VM's IP - vm_username = var.vm_username - ssh_private_key_path = var.ssh_private_key_path - ansible_playbook_path = var.docker_ansible_playbook_path -} diff --git a/opentofu/inventory.tf b/opentofu/inventory.tf deleted file mode 100644 index 16d183a..0000000 --- a/opentofu/inventory.tf +++ /dev/null @@ -1,18 +0,0 @@ -resource "local_file" "ansible_inventory" { - filename = "${path.module}/inventory.ini" - content = <<-EOT - [kube_control_plane] - ${module.k8s_control_plane.vm.default_ipv4_address} - - [etcd] - ${module.k8s_control_plane.vm.default_ipv4_address} - - [kube_node] - %{for vm in module.k8s_worker.vms}${vm.default_ipv4_address} - %{endfor} - - [k8s_cluster:children] - kube_control_plane - kube_node - EOT -} diff --git a/opentofu/main.tf b/opentofu/main.tf index 3da6045..ff9f945 100644 --- a/opentofu/main.tf +++ b/opentofu/main.tf @@ -1,68 +1,62 @@ -module "k8s_control_plane" { - source = "./modules/k8s_control_plane" +resource "proxmox_virtual_environment_vm" "docker_server" { + name = "docker-server" + description = "Debian server with docker installed." + tags = ["debian", "docker"] + node_name = var.proxmox_node - 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 -} + clone { + vm_id = var.debian_server_bookworm_packer_image_id # Use the VM ID of the template + full = true + } + + agent { + enabled = true # Qemu Guest Agent + } + + cpu { + cores = 2 + sockets = 1 + type = "host" + } + + memory { + dedicated = 4096 + } -module "k8s_worker" { - source = "./modules/k8s_worker" + disk { + interface = "virtio0" + datastore_id = "local-lvm" + size = 20 + discard = "on" + iothread = true + } - 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 + network_device { + bridge = "vmbr0" + model = "virtio" + } + + initialization { + ip_config { + ipv4 { + address = "dhcp" + } + } + + user_account { + username = var.vm_username + keys = [var.ssh_public_key] + } + } } -module "ansible_provision_k8s" { +# Run Ansible playbook after VM creation to install Docker +module "ansible_provision_docker_server" { source = "./modules/ansible_provisioner" - inventory_file_path = local_file.ansible_inventory.filename + vm_ip = proxmox_virtual_environment_vm.docker_server.ipv4_addresses[1][0] # Use first ip & ensure this is a string vm_username = var.vm_username ssh_private_key_path = var.ssh_private_key_path - ansible_playbook_path = var.k8s_ansible_playbook_path + ansible_playbook_path = var.docker_ansible_playbook_path + depends_on = [proxmox_virtual_environment_vm.docker_server] +} } diff --git a/opentofu/modules/ansible_provisioner/main.tf b/opentofu/modules/ansible_provisioner/main.tf index 0de288e..b836213 100644 --- a/opentofu/modules/ansible_provisioner/main.tf +++ b/opentofu/modules/ansible_provisioner/main.tf @@ -1,6 +1,6 @@ resource "null_resource" "ansible_provisioner" { triggers = { - ip_or_inventory = coalesce(var.vm_ip, var.inventory_file_path) # Choose based on what is provided + ip_or_inventory = coalesce(var.vm_ip, var.inventory_file_path) # Choose based on what is provided } provisioner "local-exec" { diff --git a/opentofu/modules/k8s_control_plane/main.tf b/opentofu/modules/k8s_control_plane/main.tf deleted file mode 100644 index d8477af..0000000 --- a/opentofu/modules/k8s_control_plane/main.tf +++ /dev/null @@ -1,55 +0,0 @@ -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 deleted file mode 100644 index a081907..0000000 --- a/opentofu/modules/k8s_control_plane/outputs.tf +++ /dev/null @@ -1,3 +0,0 @@ -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 deleted file mode 100644 index 4ffb3bd..0000000 --- a/opentofu/modules/k8s_control_plane/variables.tf +++ /dev/null @@ -1,143 +0,0 @@ -variable "name" { - type = string - description = "The name of the virtual machine." -} - -variable "desc" { - type = string - description = "A description of the virtual machine." - default = "" -} - -variable "agent" { - type = number - description = "Whether to enable the QEMU guest agent (0 = disabled, 1 = enabled)." - default = 0 -} - -variable "target_node" { - type = string - description = "The name of the Proxmox node where the VM will be created." -} - -variable "tags" { - type = string - description = "Tags to assign to the virtual machine." - default = "" -} - -variable "clone" { - type = string - description = "The name of the VM template to clone." -} - -variable "full_clone" { - type = bool - description = "Whether to create a full clone of the template (true) or a linked clone (false)." - default = true -} - -variable "qemu_os" { - type = string - description = "The type of OS installed on the VM." - default = "l26" # Default is Linux 2.6/3.x/4.x/5.x kernel -} - -variable "cores" { - type = number - description = "The number of CPU cores to allocate to the VM." - default = 1 -} - -variable "sockets" { - type = number - description = "The number of CPU sockets to allocate to the VM." - default = 1 -} - -variable "cpu_type" { - type = string - description = "The type of CPU to emulate (e.g., 'host', 'kvm64')." - default = "host" -} - -variable "memory" { - type = number - description = "The amount of memory (in MB) to allocate to the VM." - default = 1024 -} - -variable "scsihw" { - type = string - description = "The SCSI controller type (e.g., 'virtio-scsi-pci', 'virtio-scsi-single')." - default = "virtio-scsi-pci" -} - -variable "bootdisk" { - type = string - description = "The boot disk interface (e.g., 'scsi0', 'virtio0')." - default = "virtio0" -} - -variable "cloudinit_storage" { - type = string - description = "The storage pool for the cloud-init drive." -} - -variable "disk_storage" { - type = string - description = "The storage pool for the primary disk." -} - -variable "disk_size" { - type = string - description = "The size of the primary disk (e.g., '20G')." -} - -variable "iothread" { - type = bool - description = "Whether to enable IO threading for the disk." - default = false -} - -variable "replicate" { - type = bool - description = "Whether to replicate the disk to other nodes." - default = false -} - -variable "network_id" { - type = number - description = "The ID of the network interface." - default = 0 -} - -variable "network_model" { - type = string - description = "The model of the network interface (e.g., 'virtio')." - default = "virtio" -} - -variable "network_bridge" { - type = string - description = "The bridge to attach the network interface to." - default = "vmbr0" -} - -variable "ipconfig0" { - type = string - description = "The IP configuration for the VM (e.g., 'ip=dhcp')." - default = "ip=dhcp" -} - -variable "ciuser" { - type = string - description = "The username for cloud-init." - default = "" -} - -variable "sshkeys" { - type = string - description = "The SSH public keys to inject into the VM via cloud-init." - default = "" -} diff --git a/opentofu/modules/k8s_control_plane/versions.tf b/opentofu/modules/k8s_control_plane/versions.tf deleted file mode 100644 index 07c0f16..0000000 --- a/opentofu/modules/k8s_control_plane/versions.tf +++ /dev/null @@ -1,11 +0,0 @@ -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 deleted file mode 100644 index ff039b2..0000000 --- a/opentofu/modules/k8s_worker/main.tf +++ /dev/null @@ -1,56 +0,0 @@ -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 deleted file mode 100644 index c72c51e..0000000 --- a/opentofu/modules/k8s_worker/outputs.tf +++ /dev/null @@ -1,3 +0,0 @@ -output "vms" { - value = proxmox_vm_qemu.k8s_worker -} diff --git a/opentofu/modules/k8s_worker/variables.tf b/opentofu/modules/k8s_worker/variables.tf deleted file mode 100644 index e11a5a2..0000000 --- a/opentofu/modules/k8s_worker/variables.tf +++ /dev/null @@ -1,149 +0,0 @@ -variable "vm_count" { - type = number - description = "The number of virtual machines to create." - default = 1 -} - -variable "name_prefix" { - type = string - description = "The prefix for the names of the virtual machines. The VM names will be in the format '-'." -} - -variable "desc" { - type = string - description = "A description of the virtual machines." - default = "" -} - -variable "agent" { - type = number - description = "Whether to enable the QEMU guest agent (0 = disabled, 1 = enabled)." - default = 0 -} - -variable "target_node" { - type = string - description = "The name of the Proxmox node where the VMs will be created." -} - -variable "tags" { - type = string - description = "Tags to assign to the virtual machines." - default = "" -} - -variable "clone" { - type = string - description = "The name of the VM template to clone." -} - -variable "full_clone" { - type = bool - description = "Whether to create a full clone of the template (true) or a linked clone (false)." - default = true -} - -variable "qemu_os" { - type = string - description = "The type of OS installed on the VMs." - default = "l26" # Default is Linux 2.6/3.x/4.x/5.x kernel -} - -variable "cores" { - type = number - description = "The number of CPU cores to allocate to each VM." - default = 1 -} - -variable "sockets" { - type = number - description = "The number of CPU sockets to allocate to each VM." - default = 1 -} - -variable "cpu_type" { - type = string - description = "The type of CPU to emulate (e.g., 'host', 'kvm64')." - default = "host" -} - -variable "memory" { - type = number - description = "The amount of memory (in MB) to allocate to each VM." - default = 1024 -} - -variable "scsihw" { - type = string - description = "The SCSI controller type (e.g., 'virtio-scsi-pci', 'virtio-scsi-pci')." - default = "virtio-scsi-pci" -} - -variable "bootdisk" { - type = string - description = "The boot disk interface (e.g., 'scsi0', 'virtio0')." - default = "virtio0" -} - -variable "cloudinit_storage" { - type = string - description = "The storage pool for the cloud-init drive." -} - -variable "disk_storage" { - type = string - description = "The storage pool for the primary disk." -} - -variable "disk_size" { - type = string - description = "The size of the primary disk (e.g., '20G')." -} - -variable "iothread" { - type = bool - description = "Whether to enable IO threading for the disk." - default = false -} - -variable "replicate" { - type = bool - description = "Whether to replicate the disk to other nodes." - default = false -} - -variable "network_id" { - type = number - description = "The ID of the network interface." - default = 0 -} - -variable "network_model" { - type = string - description = "The model of the network interface (e.g., 'virtio')." - default = "virtio" -} - -variable "network_bridge" { - type = string - description = "The bridge to attach the network interface to." - default = "vmbr0" -} - -variable "ipconfig0" { - type = string - description = "The IP configuration for the VMs (e.g., 'ip=dhcp')." - default = "ip=dhcp" -} - -variable "ciuser" { - type = string - description = "The username for cloud-init." - default = "" -} - -variable "sshkeys" { - type = string - description = "The SSH public keys to inject into the VMs via cloud-init." - default = "" -} diff --git a/opentofu/modules/k8s_worker/versions.tf b/opentofu/modules/k8s_worker/versions.tf deleted file mode 100644 index d9656aa..0000000 --- a/opentofu/modules/k8s_worker/versions.tf +++ /dev/null @@ -1,10 +0,0 @@ -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 f0c8b7a..09a05d8 100644 --- a/opentofu/outputs.tf +++ b/opentofu/outputs.tf @@ -1,12 +1,10 @@ locals { all_vms = flatten([ - [module.k8s_control_plane.vm], - module.k8s_worker.vms, - [proxmox_vm_qemu.docker_server], + [proxmox_virtual_environment_vm.docker_server], ]) } output "vm_ips" { description = "Mapping of VM names to their IP addresses" - value = { for vm in local.all_vms : vm.name => vm.default_ipv4_address if can(vm.default_ipv4_address) } + value = { for vm in local.all_vms : vm.name => vm.ipv4_addresses[1][0] if can(vm.ipv4_addresses[1][0]) } } diff --git a/opentofu/provider.tf b/opentofu/provider.tf index fade80e..cd476cb 100644 --- a/opentofu/provider.tf +++ b/opentofu/provider.tf @@ -1,6 +1,5 @@ provider "proxmox" { - pm_api_url = var.proxmox_api_url - pm_api_token_id = var.proxmox_api_token_id - pm_api_token_secret = var.proxmox_api_token_secret - pm_tls_insecure = true + endpoint = var.proxmox_api_url + api_token = var.proxmox_api_token + insecure = true } diff --git a/opentofu/terraform.tfvars b/opentofu/terraform.tfvars index bbb7b1b..1fef5f5 100644 --- a/opentofu/terraform.tfvars +++ b/opentofu/terraform.tfvars @@ -1,6 +1,7 @@ proxmox_api_url = "https://10.0.0.5:8006/api2/json" proxmox_node = "pve" debian_server_bookworm_packer_image_name = "debian-server-bookworm-12-9-0-amd64" +debian_server_bookworm_packer_image_id = "999" ubuntu_server_noble_packer_image_name = "ubuntu-server-noble-24-04-1-amd64" vm_username = "mas" ssh_private_key_path = "~/.ssh/id_ecdsa" diff --git a/opentofu/variables.tf b/opentofu/variables.tf index c777b21..e679865 100644 --- a/opentofu/variables.tf +++ b/opentofu/variables.tf @@ -3,15 +3,10 @@ variable "proxmox_api_url" { type = string } -variable "proxmox_api_token_id" { +variable "proxmox_api_token" { type = string } -variable "proxmox_api_token_secret" { - type = string -} - - variable "proxmox_node" { description = "Proxmox node to deploy the VM on" type = string @@ -22,6 +17,10 @@ variable "debian_server_bookworm_packer_image_name" { type = string } +variable "debian_server_bookworm_packer_image_id" { + type = string +} + variable "ubuntu_server_noble_packer_image_name" { description = "Name of the Packer image to clone" type = string diff --git a/opentofu/versions.tf b/opentofu/versions.tf index de393ca..a872a44 100644 --- a/opentofu/versions.tf +++ b/opentofu/versions.tf @@ -3,8 +3,8 @@ terraform { required_providers { proxmox = { - source = "telmate/proxmox" - version = "3.0.1-rc6" + source = "bpg/proxmox" + version = "0.72.0" } local = { source = "hashicorp/local" -- cgit v1.2.3