Skip to content

Commit cafde89

Browse files
Feature/improve vm ids and ip logic (#93)
* feat: enhance controlplane and worker node configurations with optional VM ID and IP offsets * fix: update worker node VM ID calculation to simplify offset logic * fix: update controlplane and worker node VM ID references to use vm_id instead of vm_base_id * fix: update image version to v1.10.4 and add vm_id to controlplane overrides * fix: update image version to v1.10.4 in k8s_cluster_override module * terraform-docs: automated action --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent f0b10a9 commit cafde89

File tree

4 files changed

+109
-69
lines changed

4 files changed

+109
-69
lines changed

README.md

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@ The preserve option is only needed when wanting to keep files/directories on Tal
2121

2222
```hcl
2323
module "k8s_cluster" {
24-
source = "git::https://github.com/pascalinthecloud/terraform-proxmox-talos-cluster.git?ref=v1.0.0"
25-
24+
source = "git::https://github.com/pascalinthecloud/terraform-proxmox-talos-cluster.git?ref=v1.0.0"
25+
2626
cluster = {
27-
name = "homelab.cluster"
28-
vm_base_id = 700
29-
datastore = "local-lvm"
30-
node = "pve01"
27+
name = "homelab.cluster"
28+
vm_base_id = 700
29+
ip_base_offset = 10 # Offset for IP addresses of controlplane and worker nodes
30+
datastore = "local-lvm"
31+
node = "pve01"
3132
config_patches = [file("${path.module}/config_patch.yaml")]
3233
}
33-
34+
3435
image = {
35-
version = "v1.9.1"
36+
version = "v1.10.4"
3637
extensions = ["qemu-guest-agent", "iscsi-tools", "util-linux-tools"]
3738
}
3839
@@ -56,27 +57,28 @@ module "k8s_cluster" {
5657
worker = {
5758
count = 2
5859
specs = {
59-
cpu = 2
60-
memory = 6192
61-
disk = 50
60+
ip_offset = 10 # Offset for IP addresses of worker nodes (from the controlplane IPs)
61+
cpu = 2
62+
memory = 6192
63+
disk = 50
6264
}
6365
}
6466
}
6567
6668
# Override example
6769
module "k8s_cluster_override" {
68-
source = "git::https://github.com/pascalinthecloud/terraform-proxmox-talos-cluster.git?ref=v1.0.0"
69-
70+
source = "git::https://github.com/pascalinthecloud/terraform-proxmox-talos-cluster.git?ref=v1.0.1"
71+
7072
cluster = {
71-
name = "homelab.cluster"
72-
vm_base_id = 700
73-
datastore = "local-lvm"
74-
node = "pve01"
73+
name = "homelab.cluster"
74+
vm_base_id = 700
75+
datastore = "local-lvm"
76+
node = "pve01"
7577
config_patches = [file("${path.module}/config_patch.yaml")]
7678
}
77-
79+
7880
image = {
79-
version = "v1.9.1"
81+
version = "v1.10.4"
8082
extensions = ["qemu-guest-agent", "iscsi-tools", "util-linux-tools"]
8183
}
8284
@@ -94,6 +96,19 @@ module "k8s_cluster_override" {
9496
memory = 4096
9597
disk = 50
9698
}
99+
overrides = {
100+
"controlplane-1" = {
101+
node = "pve01"
102+
vm_id = 720
103+
network = {
104+
cidr = "10.10.101.0/24"
105+
ip_address = "10.10.101.150"
106+
gateway = "10.10.101.1"
107+
dns_servers = ["10.0.10.1", "1.1.1.1"]
108+
vlan_id = 1101
109+
}
110+
}
111+
}
97112
}
98113
99114
worker = {
@@ -103,19 +118,20 @@ module "k8s_cluster_override" {
103118
memory = 6192
104119
disk = 50
105120
}
106-
}
107-
overrides = {
108-
"controlplane-1" = {
109-
node = "pve01"
110-
network = {
111-
cidr = "10.10.101.0/24"
112-
ip_address = "10.10.101.150"
113-
gateway = "10.10.101.1"
114-
dns_servers = ["10.0.10.1", "1.1.1.1"]
115-
vlan_id = 1101
121+
overrides = {
122+
"worker-1" = {
123+
node = "pve01"
124+
network = {
125+
cidr = "10.10.101.0/24"
126+
ip_address = "10.10.101.156"
127+
gateway = "10.10.101.1"
128+
dns_servers = ["10.0.10.1", "1.1.1.1"]
129+
vlan_id = 1101
130+
}
116131
}
117132
}
118133
}
134+
119135
}
120136
```
121137

@@ -138,11 +154,11 @@ module "k8s_cluster_override" {
138154

139155
| Name | Description | Type | Default | Required |
140156
|------|-------------|------|---------|:--------:|
141-
| <a name="input_cluster"></a> [cluster](#input\_cluster) | Cluster configuration | <pre>object({<br/> name = string # The name of the cluster<br/> config_patches = optional(list(string), []) # List of configuration patches to apply to the Talos machine configuration<br/> node = string # Default node to deploy the vms on<br/> datastore = string # Default datastore to deploy the vms on<br/> vm_base_id = number # The first VM ID for Proxmox VMs, with subsequent IDs counted up from it<br/> install_disk = optional(string, "/dev/sda") # The disk to install Talos on<br/> })</pre> | n/a | yes |
142-
| <a name="input_controlplane"></a> [controlplane](#input\_controlplane) | Specification of controlplane nodes | <pre>object({<br/> count = number<br/> specs = object({<br/> cpu = number<br/> memory = number<br/> disk = number<br/> })<br/> overrides = optional(map(object({<br/> datastore = optional(string, null)<br/> node = optional(string, null)<br/> cpu = optional(number, null)<br/> memory = optional(number, null)<br/> disk = optional(number, null)<br/> install_disk = optional(string, null)<br/> network = optional(object({<br/> ip_address = string<br/> cidr = string<br/> gateway = string<br/> vlan_id = optional(number, null)<br/> }), null)<br/> })), {})<br/> })</pre> | n/a | yes |
157+
| <a name="input_cluster"></a> [cluster](#input\_cluster) | Cluster configuration | <pre>object({<br/> name = string # The name of the cluster<br/> config_patches = optional(list(string), []) # List of configuration patches to apply to the Talos machine configuration<br/> node = string # Default node to deploy the vms on<br/> datastore = string # Default datastore to deploy the vms on<br/> vm_base_id = number # The first VM ID for Proxmox VMs, with subsequent IDs counted up from it<br/> install_disk = optional(string, "/dev/sda") # The disk to install Talos on<br/> ip_base_offset = optional(number, 10) # Offset for IP addresses of the cluster nodes<br/> })</pre> | n/a | yes |
158+
| <a name="input_controlplane"></a> [controlplane](#input\_controlplane) | Specification of controlplane nodes | <pre>object({<br/> count = number<br/> specs = object({<br/> cpu = number<br/> memory = number<br/> disk = number<br/> })<br/> overrides = optional(map(object({<br/> datastore = optional(string, null)<br/> vm_id = optional(number, null)<br/> node = optional(string, null)<br/> cpu = optional(number, null)<br/> memory = optional(number, null)<br/> disk = optional(number, null)<br/> install_disk = optional(string, null)<br/> network = optional(object({<br/> ip_address = string<br/> cidr = string<br/> gateway = string<br/> vlan_id = optional(number, null)<br/> }), null)<br/> })), {})<br/> })</pre> | n/a | yes |
143159
| <a name="input_image"></a> [image](#input\_image) | Variable to define the image configuration for Talos machines | <pre>object({<br/> version = string<br/> extensions = list(string)<br/> factory_url = optional(string, "https://factory.talos.dev")<br/> arch = optional(string, "amd64")<br/> platform = optional(string, "nocloud")<br/> proxmox_datastore = optional(string, "local")<br/> })</pre> | n/a | yes |
144160
| <a name="input_network"></a> [network](#input\_network) | Network configuration for nodes | <pre>object({<br/> bridge = optional(string, "vmbr0") # The bridge to use for the network interface<br/> cidr = string<br/> gateway = string<br/> dns_servers = list(string)<br/> vlan_id = optional(number, null)<br/> })</pre> | n/a | yes |
145-
| <a name="input_worker"></a> [worker](#input\_worker) | Specification of worker nodes | <pre>object({<br/> count = number<br/> specs = object({<br/> cpu = number<br/> memory = number<br/> disk = number<br/> })<br/> overrides = optional(map(object({<br/> datastore = optional(string, null)<br/> node = optional(string, null)<br/> cpu = optional(number, null)<br/> memory = optional(number, null)<br/> disk = optional(number, null)<br/> install_disk = optional(string, null)<br/> network = optional(object({<br/> ip_address = string<br/> cidr = string<br/> gateway = string<br/> vlan_id = optional(number, null)<br/> }), null)<br/> })), {})<br/> })</pre> | n/a | yes |
161+
| <a name="input_worker"></a> [worker](#input\_worker) | Specification of worker nodes | <pre>object({<br/> count = number<br/> specs = object({<br/> ip_offset = optional(number, 10) # Offset for IP addresses of worker nodes<br/> cpu = number<br/> memory = number<br/> disk = number<br/> })<br/> overrides = optional(map(object({<br/> datastore = optional(string, null)<br/> vm_id = optional(number, null)<br/> node = optional(string, null)<br/> cpu = optional(number, null)<br/> memory = optional(number, null)<br/> disk = optional(number, null)<br/> install_disk = optional(string, null)<br/> network = optional(object({<br/> ip_address = string<br/> cidr = string<br/> gateway = string<br/> vlan_id = optional(number, null)<br/> }), null)<br/> })), {})<br/> })</pre> | n/a | yes |
146162

147163
## Outputs
148164

examples/main.example

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
module "k8s_cluster" {
2-
source = "git::https://github.com/pascalinthecloud/terraform-proxmox-talos-cluster.git?ref=v1.0.0"
3-
2+
source = "git::https://github.com/pascalinthecloud/terraform-proxmox-talos-cluster.git?ref=v1.0.0"
3+
44
cluster = {
5-
name = "homelab.cluster"
6-
vm_base_id = 700
7-
datastore = "local-lvm"
8-
node = "pve01"
5+
name = "homelab.cluster"
6+
vm_base_id = 700
7+
ip_base_offset = 10 # Offset for IP addresses of controlplane and worker nodes
8+
datastore = "local-lvm"
9+
node = "pve01"
910
config_patches = [file("${path.module}/config_patch.yaml")]
1011
}
11-
12+
1213
image = {
13-
version = "v1.9.1"
14+
version = "v1.10.4"
1415
extensions = ["qemu-guest-agent", "iscsi-tools", "util-linux-tools"]
1516
}
1617

@@ -34,27 +35,28 @@ module "k8s_cluster" {
3435
worker = {
3536
count = 2
3637
specs = {
37-
cpu = 2
38-
memory = 6192
39-
disk = 50
38+
ip_offset = 10 # Offset for IP addresses of worker nodes (from the controlplane IPs)
39+
cpu = 2
40+
memory = 6192
41+
disk = 50
4042
}
4143
}
4244
}
4345

4446
# Override example
4547
module "k8s_cluster_override" {
46-
source = "git::https://github.com/pascalinthecloud/terraform-proxmox-talos-cluster.git?ref=v1.0.0"
47-
48+
source = "git::https://github.com/pascalinthecloud/terraform-proxmox-talos-cluster.git?ref=v1.0.1"
49+
4850
cluster = {
49-
name = "homelab.cluster"
50-
vm_base_id = 700
51-
datastore = "local-lvm"
52-
node = "pve01"
51+
name = "homelab.cluster"
52+
vm_base_id = 700
53+
datastore = "local-lvm"
54+
node = "pve01"
5355
config_patches = [file("${path.module}/config_patch.yaml")]
5456
}
55-
57+
5658
image = {
57-
version = "v1.9.1"
59+
version = "v1.10.4"
5860
extensions = ["qemu-guest-agent", "iscsi-tools", "util-linux-tools"]
5961
}
6062

@@ -72,6 +74,19 @@ module "k8s_cluster_override" {
7274
memory = 4096
7375
disk = 50
7476
}
77+
overrides = {
78+
"controlplane-1" = {
79+
node = "pve01"
80+
vm_id = 720
81+
network = {
82+
cidr = "10.10.101.0/24"
83+
ip_address = "10.10.101.150"
84+
gateway = "10.10.101.1"
85+
dns_servers = ["10.0.10.1", "1.1.1.1"]
86+
vlan_id = 1101
87+
}
88+
}
89+
}
7590
}
7691

7792
worker = {
@@ -81,17 +96,18 @@ module "k8s_cluster_override" {
8196
memory = 6192
8297
disk = 50
8398
}
84-
}
85-
overrides = {
86-
"controlplane-1" = {
87-
node = "pve01"
88-
network = {
89-
cidr = "10.10.101.0/24"
90-
ip_address = "10.10.101.150"
91-
gateway = "10.10.101.1"
92-
dns_servers = ["10.0.10.1", "1.1.1.1"]
93-
vlan_id = 1101
99+
overrides = {
100+
"worker-1" = {
101+
node = "pve01"
102+
network = {
103+
cidr = "10.10.101.0/24"
104+
ip_address = "10.10.101.156"
105+
gateway = "10.10.101.1"
106+
dns_servers = ["10.0.10.1", "1.1.1.1"]
107+
vlan_id = 1101
108+
}
94109
}
95110
}
96111
}
112+
97113
}

locals.tf

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ locals {
1919
controlplanes = {
2020
for i in range(var.controlplane.count) : format("controlplane-%s", i + 1) => {
2121
hostname = format("%s-controlplane-%02d", var.cluster.name, i + 1)
22-
vm_id = var.cluster.vm_base_id + i
22+
23+
vm_id = coalesce(try(var.controlplane.overrides[format("controlplane-%s", i + 1)].vm_id, null),
24+
var.cluster.vm_base_id + i)
2325

2426
node = coalesce(
2527
try(var.controlplane.overrides[format("controlplane-%s", i + 1)].node, null),
@@ -35,10 +37,10 @@ locals {
3537
# Otherwise, calculate the IP address based on the network CIDR and the node index.
3638
# The `coalesce` function returns the first non-null value from the provided arguments.
3739
# The `try` function attempts to extract the IP address from the override; if it fails, it returns null.
38-
# The `cidrhost` function calculates the IP address based on the network CIDR and the node index.b
40+
# The `cidrhost` function calculates the IP address based on the network CIDR, the node index, and the base offset.
3941
ip_address = coalesce(
4042
try(var.controlplane.overrides[format("controlplane-%s", i + 1)].network.ip_address, null),
41-
cidrhost(var.network.cidr, i + 10)
43+
cidrhost(var.network.cidr, i + var.cluster.ip_base_offset)
4244
)
4345

4446
# Determine the subnet mask for the control plane node.
@@ -82,7 +84,9 @@ locals {
8284
workers = {
8385
for i in range(var.worker.count) : format("worker-%s", i + 1) => {
8486
hostname = format("%s-worker-%02d", var.cluster.name, i + 1)
85-
vm_id = var.cluster.vm_base_id + var.controlplane.count + i
87+
88+
vm_id = coalesce(try(var.worker.overrides[format("worker-%s", i + 1)].vm_id, null),
89+
var.cluster.vm_base_id + 10)
8690

8791
node = coalesce(
8892
try(var.worker.overrides[format("worker-%s", i + 1)].node, null),
@@ -96,7 +100,7 @@ locals {
96100

97101
ip_address = coalesce(
98102
try(var.worker.overrides[format("worker-%s", i + 1)].network.ip_address, null),
99-
cidrhost(var.network.cidr, i + 10 + var.controlplane.count)
103+
cidrhost(var.network.cidr, i + var.cluster.ip_base_offset + var.worker.specs.ip_offset)
100104
)
101105

102106
subnet = coalesce(

variables.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ variable "cluster" {
77
datastore = string # Default datastore to deploy the vms on
88
vm_base_id = number # The first VM ID for Proxmox VMs, with subsequent IDs counted up from it
99
install_disk = optional(string, "/dev/sda") # The disk to install Talos on
10+
ip_base_offset = optional(number, 10) # Offset for IP addresses of the cluster nodes
1011
})
1112
validation {
1213
condition = can(regex("^[a-zA-Z0-9-]+$", var.cluster.name))
@@ -25,6 +26,7 @@ variable "controlplane" {
2526
})
2627
overrides = optional(map(object({
2728
datastore = optional(string, null)
29+
vm_id = optional(number, null)
2830
node = optional(string, null)
2931
cpu = optional(number, null)
3032
memory = optional(number, null)
@@ -68,12 +70,14 @@ variable "worker" {
6870
type = object({
6971
count = number
7072
specs = object({
71-
cpu = number
72-
memory = number
73-
disk = number
73+
ip_offset = optional(number, 10) # Offset for IP addresses of worker nodes
74+
cpu = number
75+
memory = number
76+
disk = number
7477
})
7578
overrides = optional(map(object({
7679
datastore = optional(string, null)
80+
vm_id = optional(number, null)
7781
node = optional(string, null)
7882
cpu = optional(number, null)
7983
memory = optional(number, null)

0 commit comments

Comments
 (0)