Skip to content

Commit

Permalink
feat: dhcp support (#1)
Browse files Browse the repository at this point in the history
* feat: dhcp support

* fix: restore install_disk in vms variable

* feat: dhcp support

---------

Co-authored-by: Vincent Dupain <vincent.dupain@protonmail.com>
  • Loading branch information
vdupain and Vincent Dupain authored Nov 6, 2024
1 parent de5293d commit 927103d
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 81 deletions.
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ module "talos-k8s-cluster" {
os_disk_size = 10
data_disk_size = 10
datastore_id = "local-lvm"
install_disk = "/dev/sda"
hostname = "cp-0"
}
"k8s-cp-1" = {
host_node = "pve1"
Expand All @@ -55,8 +53,6 @@ module "talos-k8s-cluster" {
os_disk_size = 10
data_disk_size = 10
datastore_id = "local-lvm"
install_disk = "/dev/sda"
hostname = "cp-1"
}
"k8s-cp-2" = {
host_node = "pve1"
Expand All @@ -67,8 +63,6 @@ module "talos-k8s-cluster" {
os_disk_size = 10
data_disk_size = 10
datastore_id = "local-lvm"
install_disk = "/dev/sda"
hostname = "cp-2"
}
}

Expand Down Expand Up @@ -207,11 +201,11 @@ flux-system flux-system main@sha1:5902d505 False True Applied revision: mai

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_cluster"></a> [cluster](#input\_cluster) | Cluster configuration | <pre>object({<br/> gateway = string<br/> cidr = number<br/> vlan_id = optional(number, null)<br/> name = string<br/> endpoint = string<br/> talos_version = optional(string, "v1.8.2")<br/> })</pre> | n/a | yes |
| <a name="input_cluster"></a> [cluster](#input\_cluster) | Cluster configuration | <pre>object({<br/> name = string<br/> talos_version = optional(string, "v1.8.2")<br/> network_dhcp = optional(bool, false)<br/> gateway = optional(string)<br/> cidr = optional(number)<br/> vlan_id = optional(number, null)<br/> endpoint = optional(string)<br/> })</pre> | n/a | yes |
| <a name="input_github"></a> [github](#input\_github) | Github Flux GitOps configuration | <pre>object({<br/> token = string<br/> org = string<br/> repository = string<br/> })</pre> | `null` | no |
| <a name="input_pci"></a> [pci](#input\_pci) | Mapping PCI configuration | <pre>map(object({<br/> name = string<br/> id = string<br/> iommu_group = number<br/> node = string<br/> path = string<br/> subsystem_id = string<br/> }))</pre> | `null` | no |
| <a name="input_proxmox"></a> [proxmox](#input\_proxmox) | Proxmox configuration | <pre>object({<br/> endpoint = string<br/> insecure = bool<br/> username = string<br/> password = optional(string)<br/> api_token = optional(string)<br/> ssh_agent = optional(string, false)<br/> })</pre> | n/a | yes |
| <a name="input_vms"></a> [vms](#input\_vms) | VMs configuration | <pre>map(object({<br/> host_node = string<br/> machine_type = string<br/> datastore_id = optional(string, "local-lvm")<br/> ip = string<br/> cpu = number<br/> ram_dedicated = number<br/> os_disk_size = number<br/> data_disk_size = number<br/> gpu = optional(bool, false)<br/> install_disk = string<br/> hostname = optional(string)<br/> }))</pre> | n/a | yes |
| <a name="input_vms"></a> [vms](#input\_vms) | VMs configuration | <pre>map(object({<br/> host_node = string<br/> machine_type = string<br/> datastore_id = optional(string, "local-lvm")<br/> ip = optional(string)<br/> cpu = number<br/> ram_dedicated = number<br/> os_disk_size = number<br/> data_disk_size = number<br/> install_disk = optional(string, "/dev/sda")<br/> gpu = optional(bool, false)<br/> }))</pre> | n/a | yes |

## Outputs

Expand All @@ -220,6 +214,7 @@ flux-system flux-system main@sha1:5902d505 False True Applied revision: mai
| <a name="output_cluster_name"></a> [cluster\_name](#output\_cluster\_name) | Retrieves the name for a k8s Talos cluster |
| <a name="output_config_ipv4_addresses"></a> [config\_ipv4\_addresses](#output\_config\_ipv4\_addresses) | Retrieves VM names with IPv4 address for a k8s Talos cluster |
| <a name="output_kube_config"></a> [kube\_config](#output\_kube\_config) | Retrieves the kubeconfig for a k8s Talos cluster |
| <a name="output_qemu_ipv4_addresses"></a> [qemu\_ipv4\_addresses](#output\_qemu\_ipv4\_addresses) | Retrieves VM names with IPv4 address for a k8s Talos cluster |
| <a name="output_talos_config"></a> [talos\_config](#output\_talos\_config) | Retrieves the talosconfig for a k8s Talos cluster |
| <a name="output_vm_ipv4_address_vms"></a> [vm\_ipv4\_address\_vms](#output\_vm\_ipv4\_address\_vms) | Retrieves IPv4 address for a k8s Talos cluster |
<!-- END_TF_DOCS -->
6 changes: 0 additions & 6 deletions examples/0-demo-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ module "talos-k8s-cluster" {
os_disk_size = 10
data_disk_size = 10
datastore_id = "local-lvm"
install_disk = "/dev/sda"
hostname = "cp-0"
}
"k8s-cp-1" = {
host_node = "pve"
Expand All @@ -31,8 +29,6 @@ module "talos-k8s-cluster" {
os_disk_size = 10
data_disk_size = 10
datastore_id = "local-lvm"
install_disk = "/dev/sda"
hostname = "cp-1"
}
"k8s-cp-2" = {
host_node = "pve"
Expand All @@ -43,8 +39,6 @@ module "talos-k8s-cluster" {
os_disk_size = 10
data_disk_size = 10
datastore_id = "local-lvm"
install_disk = "/dev/sda"
hostname = "cp-2"
}
}

Expand Down
35 changes: 11 additions & 24 deletions examples/1-gpu-cluster/main.tf
Original file line number Diff line number Diff line change
@@ -1,62 +1,49 @@
module "talos-k8s-cluster" {
source = "vdupain/talos-k8s-cluster/proxmox"
version = "1.0.0-rc6"
#source = "vdupain/talos-k8s-cluster/proxmox"
#version = "1.0.0-rc6"
source = "../.."

cluster = {
name = "gpu-cluster"
gateway = "192.168.10.1"
cidr = 24
endpoint = "192.168.10.220"
name = "gpu-cluster"
network_dhcp = true
}

vms = {
"k8s-cp-0" = {
host_node = "pve3"
machine_type = "controlplane"
ip = "192.168.10.220"
cpu = 4
ram_dedicated = 8196
os_disk_size = 10
data_disk_size = 10
os_disk_size = 20
data_disk_size = 20
datastore_id = "local-lvm"
install_disk = "/dev/sda"
hostname = "cp-0"
}
"k8s-cp-1" = {
host_node = "pve3"
machine_type = "controlplane"
ip = "192.168.10.221"
cpu = 4
ram_dedicated = 8196
os_disk_size = 10
data_disk_size = 10
os_disk_size = 20
data_disk_size = 20
datastore_id = "local-lvm"
install_disk = "/dev/sda"
hostname = "cp-1"
}
"k8s-cp-2" = {
host_node = "pve3"
machine_type = "controlplane"
ip = "192.168.10.222"
cpu = 4
ram_dedicated = 8196
os_disk_size = 10
data_disk_size = 10
os_disk_size = 20
data_disk_size = 20
datastore_id = "local-lvm"
install_disk = "/dev/sda"
hostname = "cp-2"
}
"k8s-worker-gpu" = {
host_node = "pve3"
machine_type = "worker"
ip = "192.168.10.223"
cpu = 4
ram_dedicated = 8196
os_disk_size = 20
data_disk_size = 20
datastore_id = "local-lvm"
install_disk = "/dev/sda"
hostname = "worker-gpu"
gpu = true
}

Expand Down
5 changes: 5 additions & 0 deletions examples/1-gpu-cluster/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ output "cluster_name" {
output "config_ipv4_addresses" {
value = module.talos-k8s-cluster.config_ipv4_addresses
}


output "qemu_ipv4_addresses" {
value = module.talos-k8s-cluster.qemu_ipv4_addresses
}
6 changes: 0 additions & 6 deletions examples/2-bare-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ module "talos-k8s-cluster" {
os_disk_size = 10
data_disk_size = 10
datastore_id = "local-lvm"
install_disk = "/dev/sda"
hostname = "cp-0"
}
"k8s-cp-1" = {
host_node = "pve1"
Expand All @@ -31,8 +29,6 @@ module "talos-k8s-cluster" {
os_disk_size = 10
data_disk_size = 10
datastore_id = "local-lvm"
install_disk = "/dev/sda"
hostname = "cp-1"
}
"k8s-cp-2" = {
host_node = "pve1"
Expand All @@ -43,8 +39,6 @@ module "talos-k8s-cluster" {
os_disk_size = 10
data_disk_size = 10
datastore_id = "local-lvm"
install_disk = "/dev/sda"
hostname = "cp-2"
}
}

Expand Down
2 changes: 0 additions & 2 deletions examples/3-mini-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ module "talos-k8s-cluster" {
os_disk_size = 10
data_disk_size = 10
datastore_id = "local-lvm"
install_disk = "/dev/sda"
hostname = "cp-0"
}
}

Expand Down
12 changes: 9 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module "vms" {
cidr = var.cluster.cidr
vlan_id = var.cluster.vlan_id
talos_version = var.cluster.talos_version
network_dhcp = var.cluster.network_dhcp
}

vms = var.vms
Expand All @@ -20,11 +21,16 @@ module "talos_k8s" {
source = "./modules/talos_k8s"

cluster = {
name = var.cluster.name
endpoint = var.cluster.endpoint
name = var.cluster.name
endpoint = var.cluster.endpoint
network_dhcp = var.cluster.network_dhcp
}

nodes = var.vms
nodes = { for k, vm in var.vms : k => merge(vm, {
ip = lookup(module.vms.qemu_ipv4_addresses, k, vm.ip)
}) }


}

module "fluxcd" {
Expand Down
4 changes: 2 additions & 2 deletions modules/talos_k8s/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ No modules.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_cluster"></a> [cluster](#input\_cluster) | Cluster configuration | <pre>object({<br/> name = string<br/> endpoint = string<br/> })</pre> | n/a | yes |
| <a name="input_nodes"></a> [nodes](#input\_nodes) | Configuration for worker nodes | <pre>map(object({<br/> install_disk = string<br/> hostname = optional(string)<br/> machine_type = string<br/> ip = string<br/> gpu = optional(bool, false)<br/> }))</pre> | n/a | yes |
| <a name="input_cluster"></a> [cluster](#input\_cluster) | Cluster configuration | <pre>object({<br/> name = string<br/> endpoint = string<br/> network_dhcp = optional(bool, false)<br/> })</pre> | n/a | yes |
| <a name="input_nodes"></a> [nodes](#input\_nodes) | Configuration for worker nodes | <pre>map(object({<br/> machine_type = string<br/> ip = string<br/> install_disk = optional(string, "/dev/sda")<br/> gpu = optional(bool, false)<br/> }))</pre> | n/a | yes |

## Outputs

Expand Down
14 changes: 9 additions & 5 deletions modules/talos_k8s/main.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
locals {
first_control_plane = [for k, v in var.nodes : v.ip if v.machine_type == "controlplane"][0]
}

resource "talos_machine_secrets" "this" {}

data "talos_machine_configuration" "controlplane" {
cluster_name = var.cluster.name
cluster_endpoint = "https://${var.cluster.endpoint}:6443"
cluster_endpoint = var.cluster.network_dhcp ? "https://${local.first_control_plane}:6443" : "https://${var.cluster.endpoint}:6443"
machine_type = "controlplane"
machine_secrets = talos_machine_secrets.this.machine_secrets
}

data "talos_machine_configuration" "worker" {
cluster_name = var.cluster.name
cluster_endpoint = "https://${var.cluster.endpoint}:6443"
cluster_endpoint = var.cluster.network_dhcp ? "https://${local.first_control_plane}:6443" : "https://${var.cluster.endpoint}:6443"
machine_type = "worker"
machine_secrets = talos_machine_secrets.this.machine_secrets
}
Expand All @@ -31,7 +35,7 @@ resource "talos_machine_configuration_apply" "controlplane" {
node = each.value.ip
config_patches = [
templatefile("${path.module}/config/control-plane.yaml.tmpl", {
hostname = each.value.hostname
hostname = "${var.cluster.name}-${each.key}"
install_disk = each.value.install_disk
cilium_values = file("${path.module}/kubernetes/cilium-values.yaml")
cilium_install = file("${path.module}/kubernetes/cilium-install.yaml")
Expand All @@ -51,7 +55,7 @@ resource "talos_machine_configuration_apply" "worker" {
node = each.value.ip
config_patches = [
templatefile("${path.module}/config/worker.yaml.tmpl", {
hostname = each.value.hostname
hostname = "${var.cluster.name}-${each.key}"
install_disk = each.value.install_disk
}),
file("${path.module}/config/storage-patch.yaml"),
Expand All @@ -68,7 +72,7 @@ resource "talos_machine_configuration_apply" "worker-gpu" {
node = each.value.ip
config_patches = [
templatefile("${path.module}/config/worker.yaml.tmpl", {
hostname = each.value.hostname
hostname = "${var.cluster.name}-${each.key}"
install_disk = each.value.install_disk
}),
file("${path.module}/config/storage-patch.yaml"),
Expand Down
8 changes: 4 additions & 4 deletions modules/talos_k8s/variables.tf
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
variable "cluster" {
description = "Cluster configuration"
type = object({
name = string
endpoint = string
name = string
endpoint = string
network_dhcp = optional(bool, false)
})
}

variable "nodes" {
description = "Configuration for worker nodes"
type = map(object({
install_disk = string
hostname = optional(string)
machine_type = string
ip = string
install_disk = optional(string, "/dev/sda")
gpu = optional(bool, false)
}))
}
5 changes: 4 additions & 1 deletion modules/vms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
|------|---------|
| <a name="provider_http"></a> [http](#provider\_http) | n/a |
| <a name="provider_proxmox"></a> [proxmox](#provider\_proxmox) | >=0.66.3 |
| <a name="provider_time"></a> [time](#provider\_time) | n/a |

## Modules

Expand All @@ -23,14 +24,15 @@ No modules.
| [proxmox_virtual_environment_download_file.this](https://registry.terraform.io/providers/bpg/proxmox/latest/docs/resources/virtual_environment_download_file) | resource |
| [proxmox_virtual_environment_hardware_mapping_pci.pci](https://registry.terraform.io/providers/bpg/proxmox/latest/docs/resources/virtual_environment_hardware_mapping_pci) | resource |
| [proxmox_virtual_environment_vm.vms](https://registry.terraform.io/providers/bpg/proxmox/latest/docs/resources/virtual_environment_vm) | resource |
| [time_sleep.waiting_if_dhcp](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep) | resource |
| [http_http.schematic_id](https://registry.terraform.io/providers/hashicorp/http/latest/docs/data-sources/http) | data source |
| [http_http.schematic_nvidia_id](https://registry.terraform.io/providers/hashicorp/http/latest/docs/data-sources/http) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_cluster"></a> [cluster](#input\_cluster) | Cluster configuration | <pre>object({<br/> name = string<br/> gateway = string<br/> cidr = number<br/> vlan_id = optional(number, null)<br/> talos_version = string<br/> })</pre> | n/a | yes |
| <a name="input_cluster"></a> [cluster](#input\_cluster) | Cluster configuration | <pre>object({<br/> network_dhcp = optional(bool, false)<br/> gateway = string<br/> cidr = number<br/> vlan_id = optional(number, null)<br/> name = string<br/> talos_version = optional(string, "v1.8.2")<br/> })</pre> | n/a | yes |
| <a name="input_pci"></a> [pci](#input\_pci) | Configuration mapping PCI | <pre>map(object({<br/> name = string<br/> id = string<br/> iommu_group = number<br/> node = string<br/> path = string<br/> subsystem_id = string<br/> }))</pre> | `null` | no |
| <a name="input_proxmox"></a> [proxmox](#input\_proxmox) | Proxmox configuration | <pre>object({<br/> endpoint = string<br/> insecure = bool<br/> username = string<br/> password = optional(string)<br/> api_token = optional(string)<br/> ssh_agent = optional(string, false)<br/> })</pre> | n/a | yes |
| <a name="input_vms"></a> [vms](#input\_vms) | Configuration for cluster nodes | <pre>map(object({<br/> host_node = string<br/> machine_type = string<br/> datastore_id = optional(string, "local-lvm")<br/> ip = string<br/> cpu = number<br/> ram_dedicated = number<br/> os_disk_size = number<br/> data_disk_size = number<br/> gpu = optional(bool, false)<br/> }))</pre> | n/a | yes |
Expand All @@ -40,5 +42,6 @@ No modules.
| Name | Description |
|------|-------------|
| <a name="output_config_ipv4_addresses"></a> [config\_ipv4\_addresses](#output\_config\_ipv4\_addresses) | n/a |
| <a name="output_qemu_ipv4_addresses"></a> [qemu\_ipv4\_addresses](#output\_qemu\_ipv4\_addresses) | n/a |
| <a name="output_vm_ipv4_address_vms"></a> [vm\_ipv4\_address\_vms](#output\_vm\_ipv4\_address\_vms) | n/a |
<!-- END_TF_DOCS -->
11 changes: 10 additions & 1 deletion modules/vms/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ output "vm_ipv4_address_vms" {

output "config_ipv4_addresses" {
value = {
for vm in proxmox_virtual_environment_vm.vms : vm.name => vm.initialization[0].ip_config[0].ipv4[0].address
for vm in proxmox_virtual_environment_vm.vms : vm.name
=> vm.initialization[0].ip_config[0].ipv4[0].address
}
}

output "qemu_ipv4_addresses" {
depends_on = [time_sleep.waiting_if_dhcp]
value = {
for vm in proxmox_virtual_environment_vm.vms : trimprefix(vm.name, "${var.cluster.name}-")
=> element(vm.ipv4_addresses, index(vm.mac_addresses, vm.network_device[0].mac_address))[0]
}
}
5 changes: 3 additions & 2 deletions modules/vms/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ variable "proxmox" {
variable "cluster" {
description = "Cluster configuration"
type = object({
name = string
network_dhcp = optional(bool, false)
gateway = string
cidr = number
vlan_id = optional(number, null)
talos_version = string
name = string
talos_version = optional(string, "v1.8.2")
})
}

Expand Down
9 changes: 7 additions & 2 deletions modules/vms/vms.tf
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ resource "proxmox_virtual_environment_vm" "vms" {
datastore_id = each.value.datastore_id
ip_config {
ipv4 {
address = "${each.value.ip}/${var.cluster.cidr}"
gateway = var.cluster.gateway
address = var.cluster.network_dhcp == true ? "dhcp" : "${each.value.ip}/${var.cluster.cidr}"
gateway = var.cluster.network_dhcp == true ? null : var.cluster.gateway
}
}
}
Expand All @@ -83,3 +83,8 @@ resource "proxmox_virtual_environment_vm" "vms" {
}
}
}

resource "time_sleep" "waiting_if_dhcp" {
depends_on = [proxmox_virtual_environment_vm.vms]
create_duration = (var.cluster.network_dhcp == true) ? "10s" : "0s"
}
Loading

0 comments on commit 927103d

Please sign in to comment.