diff --git a/infra/_modules/nixos/configuration.nix b/infra/_modules/nixos/configuration.nix index da82bbe..8517e90 100644 --- a/infra/_modules/nixos/configuration.nix +++ b/infra/_modules/nixos/configuration.nix @@ -1,5 +1,8 @@ { modulesPath, ... }: +let + hosts = builtins.fromJSON (builtins.readFile ./hosts.json); +in { imports = [ (modulesPath + "/profiles/all-hardware.nix") diff --git a/infra/_modules/nixos/flake.nix b/infra/_modules/nixos/flake.nix index 927529d..6a973fb 100644 --- a/infra/_modules/nixos/flake.nix +++ b/infra/_modules/nixos/flake.nix @@ -25,7 +25,25 @@ ./profiles/devbox.nix ]; }; - k3s = nixpkgs.lib.nixosSystem { + kube-1 = nixpkgs.lib.nixosSystem { + system = "x86_64-linux"; + modules = [ + disko.nixosModules.disko + ./configuration.nix + ./disks.nix + ./profiles/k3s.nix + ]; + }; + kube-2 = nixpkgs.lib.nixosSystem { + system = "x86_64-linux"; + modules = [ + disko.nixosModules.disko + ./configuration.nix + ./disks.nix + ./profiles/k3s.nix + ]; + }; + kube-3 = nixpkgs.lib.nixosSystem { system = "x86_64-linux"; modules = [ disko.nixosModules.disko diff --git a/infra/_modules/nixos/hosts.json b/infra/_modules/nixos/hosts.json new file mode 100644 index 0000000..49cba8a --- /dev/null +++ b/infra/_modules/nixos/hosts.json @@ -0,0 +1 @@ +{"kube-1":{"ipv6_address":"2402:800:63e2:5af5:57e2:ac2e:7bfa:b4ee"},"kube-2":{"ipv6_address":"2402:800:63e2:5af5:2eb2:b07e:ced0:5d7f"},"kube-3":{"ipv6_address":"2402:800:63e2:5af5:a811:9da5:775e:addc"}} \ No newline at end of file diff --git a/infra/_modules/nixos/main.tf b/infra/_modules/nixos/main.tf index 3c9c330..bc85040 100644 --- a/infra/_modules/nixos/main.tf +++ b/infra/_modules/nixos/main.tf @@ -1,3 +1,14 @@ +resource "local_file" "hosts" { + content = jsonencode(var.hosts) # Converts variables to JSON + filename = "${var.flake}/hosts.json" + file_permission = "600" + + provisioner "local-exec" { + interpreter = ["bash", "-c"] + command = "git add -f '${var.flake}/hosts.json'" + } +} + module "nixos" { for_each = var.hosts diff --git a/infra/_modules/proxmox-vm/main.tf b/infra/_modules/proxmox-vm/main.tf index 22ad58a..63a12cc 100644 --- a/infra/_modules/proxmox-vm/main.tf +++ b/infra/_modules/proxmox-vm/main.tf @@ -1,15 +1,17 @@ resource "proxmox_virtual_environment_vm" "main" { - name = var.name + for_each = var.hosts + + name = each.key node_name = var.node_name cpu { - cores = var.cpu.cores + cores = each.value.cpu } memory { - dedicated = 1024 * var.memory.dedicated + dedicated = 1024 * each.value.memory # Set floating to the same value as dedicated to enable ballooning device - floating = 1024 * var.memory.dedicated + floating = 1024 * each.value.memory } cdrom { @@ -18,14 +20,11 @@ resource "proxmox_virtual_environment_vm" "main" { interface = "ide3" } - dynamic "disk" { - for_each = var.disks - content { - datastore_id = "local-lvm" - interface = "scsi0" - size = disk.value.size - file_format = "raw" - } + disk { + datastore_id = "local-lvm" + interface = "scsi0" + size = each.value.disk + file_format = "raw" } boot_order = [ diff --git a/infra/_modules/proxmox-vm/outputs.tf b/infra/_modules/proxmox-vm/outputs.tf index 398e5a1..ec59036 100644 --- a/infra/_modules/proxmox-vm/outputs.tf +++ b/infra/_modules/proxmox-vm/outputs.tf @@ -1,3 +1,7 @@ -output "ipv6_address" { - value = proxmox_virtual_environment_vm.main.ipv6_addresses[1][0] +output "hosts" { + value = { + for node in proxmox_virtual_environment_vm.main : node.name => { + ipv6_address = node.ipv6_addresses[1][0] + } + } } diff --git a/infra/_modules/proxmox-vm/variables.tf b/infra/_modules/proxmox-vm/variables.tf index e293fef..a64840a 100644 --- a/infra/_modules/proxmox-vm/variables.tf +++ b/infra/_modules/proxmox-vm/variables.tf @@ -1,5 +1,9 @@ -variable "name" { - type = string +variable "hosts" { + type = map(object({ + cpu = number + memory = number + disk = number + })) } variable "node_name" { @@ -7,18 +11,6 @@ variable "node_name" { default = "proxmox" } -variable "cpu" { - type = object({ - cores = number - }) -} - -variable "memory" { - type = object({ - dedicated = number - }) -} - variable "cdrom" { type = object({ file = string @@ -29,12 +21,6 @@ variable "cdrom" { } } -variable "disks" { - type = map(object({ - size = number - })) -} - variable "tags" { type = list(string) default = [] diff --git a/infra/production/nixos/.terraform.lock.hcl b/infra/production/nixos/.terraform.lock.hcl index 5ea81b7..ba8350e 100644 --- a/infra/production/nixos/.terraform.lock.hcl +++ b/infra/production/nixos/.terraform.lock.hcl @@ -18,6 +18,23 @@ provider "registry.opentofu.org/hashicorp/external" { ] } +provider "registry.opentofu.org/hashicorp/local" { + version = "2.5.3" + hashes = [ + "h1:mC9+u1eaUILTjxey6Ivyf/3djm//RNNze9kBVX/trng=", + "zh:32e1d4b0595cea6cda4ca256195c162772ddff25594ab4008731a2ec7be230bf", + "zh:48c390af0c87df994ec9796f04ec2582bcac581fb81ed6bb58e0671da1c17991", + "zh:4be7289c969218a57b40902e2f359914f8d35a7f97b439140cb711aa21e494bd", + "zh:4cf958e631e99ed6c8b522c9b22e1f1b568c0bdadb01dd002ca7dffb1c927764", + "zh:7a0132c0faca4c4c96aa70808effd6817e28712bf5a39881666ac377b4250acf", + "zh:7d60de08fac427fb045e4590d1b921b6778498eee9eb16f78c64d4c577bde096", + "zh:91003bee5981e99ec3925ce2f452a5f743827f9d0e131a86613549c1464796f0", + "zh:9fe2fe75977c8149e2515fb30c6cc6cfd57b225d4ce592c570d81a3831d7ffa3", + "zh:e210e6be54933ce93e03d0994e520ba289aa01b2c1f70e77afb8f2ee796b0fe3", + "zh:e8793e5f9422f2b31a804e51806595f335b827c9a38db18766960464566f21d5", + ] +} + provider "registry.opentofu.org/hashicorp/null" { version = "3.2.4" hashes = [ diff --git a/infra/production/nixos/terragrunt.hcl b/infra/production/nixos/terragrunt.hcl index 7c76707..c36e846 100644 --- a/infra/production/nixos/terragrunt.hcl +++ b/infra/production/nixos/terragrunt.hcl @@ -13,9 +13,5 @@ dependency "proxmox" { inputs = { flake = "${find_in_parent_folders("_modules")}//nixos" - hosts = { - k3s = { - ipv6_address = dependency.proxmox.outputs.ipv6_address - } - } + hosts = dependency.proxmox.outputs.hosts } diff --git a/infra/production/proxmox/compute/terragrunt.hcl b/infra/production/proxmox/compute/terragrunt.hcl index 274b406..a628ee8 100644 --- a/infra/production/proxmox/compute/terragrunt.hcl +++ b/infra/production/proxmox/compute/terragrunt.hcl @@ -8,16 +8,21 @@ terraform { } inputs = { - name = "k3s" - cpu = { - cores = 8 - } - memory = { - dedicated = 16 - } - disks = { - os = { - size = 256 + hosts = { + "kube-1" = { + cpu = 2 + memory = 4 + disk = 128 + } + "kube-2" = { + cpu = 2 + memory = 4 + disk = 128 + } + "kube-3" = { + cpu = 8 + memory = 16 + disk = 128 } } }