Skip to content

Commit ad54226

Browse files
committed
support multiple networks for all nodes w/ inventory output
1 parent c93d9be commit ad54226

File tree

7 files changed

+61
-42
lines changed

7 files changed

+61
-42
lines changed

environments/skeleton/{{cookiecutter.environment}}/tofu/compute.tf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ module "compute" {
77
nodes = each.value.nodes
88
flavor = each.value.flavor
99

10+
# always taken from top-level value:
1011
cluster_name = var.cluster_name
1112
cluster_domain_suffix = var.cluster_domain_suffix
12-
cluster_net_id = data.openstack_networking_network_v2.cluster_net[var.cluster_networks[0].network].id
13-
cluster_subnet_id = data.openstack_networking_subnet_v2.cluster_subnet[var.cluster_networks[0].network].id
14-
13+
key_pair = var.key_pair
14+
environment_root = var.environment_root
15+
1516
# can be set for group, defaults to top-level value:
17+
networks = lookup(each.value, "networks", var.cluster_networks)
1618
image_id = lookup(each.value, "image_id", var.cluster_image_id)
17-
#vnic_type = lookup(each.value, "vnic_type", var.vnic_type)
18-
#vnic_profile = lookup(each.value, "vnic_profile", var.vnic_profile)
19+
vnic_types = lookup(each.value, "vnic_types", var.vnic_types)
20+
vnic_profiles = lookup(each.value, "vnic_profiles", var.vnic_profiles)
1921
volume_backed_instances = lookup(each.value, "volume_backed_instances", var.volume_backed_instances)
2022
root_volume_size = lookup(each.value, "root_volume_size", var.root_volume_size)
2123
extra_volumes = lookup(each.value, "extra_volumes", {})
22-
2324
compute_init_enable = lookup(each.value, "compute_init_enable", [])
2425
ignore_image_changes = lookup(each.value, "ignore_image_changes", false)
2526

26-
key_pair = var.key_pair
27-
environment_root = var.environment_root
27+
# computed
2828
k3s_token = local.k3s_token
2929
control_address = [for n in openstack_compute_instance_v2.control["control"].network: n.fixed_ip_v4 if n.access_network][0]
3030
security_group_ids = [for o in data.openstack_networking_secgroup_v2.nonlogin: o.id]

environments/skeleton/{{cookiecutter.environment}}/tofu/control.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ resource "openstack_compute_instance_v2" "control" {
5555
for_each = {for net in var.cluster_networks: net.network => net}
5656
content {
5757
port = openstack_networking_port_v2.control[network.key].id
58-
access_network = length(var.cluster_networks) == 1 ? true : lookup(each.value, "access_network", false)
58+
access_network = length(var.cluster_networks) == 1 ? true : lookup(network.value, "access_network", false)
5959
}
6060
}
6161

environments/skeleton/{{cookiecutter.environment}}/tofu/inventory.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ control:
99
${ control.name }:
1010
ansible_host: ${[for n in control.network: n.fixed_ip_v4 if n.access_network][0]}
1111
instance_id: ${ control.id }
12+
networks: ${jsonencode({for n in control.network: n.name => {"fixed_ip_v4": n.fixed_ip_v4, "fixed_ip_v6": n.fixed_ip_v6}})}
1213
%{ endfor ~}
1314
vars:
1415
appliances_state_dir: ${state_dir} # NB needs to be set on group not host otherwise it is ignored in packer build!
@@ -22,6 +23,7 @@ ${cluster_name}_${group_name}:
2223
ansible_host: ${node.access_ip_v4}
2324
instance_id: ${ node.id }
2425
image_id: ${ node.image_id }
26+
networks: ${jsonencode({for n in node.network: n.name => {"fixed_ip_v4": n.fixed_ip_v4, "fixed_ip_v6": n.fixed_ip_v6}})}
2527
%{ endfor ~}
2628
%{ endfor ~}
2729

@@ -39,6 +41,7 @@ ${cluster_name}_${group_name}:
3941
ansible_host: ${node.access_ip_v4}
4042
instance_id: ${ node.id }
4143
image_id: ${ node.image_id }
44+
networks: ${jsonencode({for n in node.network: n.name => {"fixed_ip_v4": n.fixed_ip_v4, "fixed_ip_v6": n.fixed_ip_v6}})}
4245
%{ endfor ~}
4346
%{ endfor ~}
4447

environments/skeleton/{{cookiecutter.environment}}/tofu/login.tf

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ module "login" {
99

1010
cluster_name = var.cluster_name
1111
cluster_domain_suffix = var.cluster_domain_suffix
12-
cluster_net_id = data.openstack_networking_network_v2.cluster_net[var.cluster_networks[0].network].id
13-
cluster_subnet_id = data.openstack_networking_subnet_v2.cluster_subnet[var.cluster_networks[0].network].id
14-
12+
1513
# can be set for group, defaults to top-level value:
14+
networks = lookup(each.value, "networks", var.cluster_networks)
1615
image_id = lookup(each.value, "image_id", var.cluster_image_id)
17-
#vnic_type = lookup(each.value, "vnic_type", var.vnic_type)
18-
#vnic_profile = lookup(each.value, "vnic_profile", var.vnic_profile)
16+
vnic_types = lookup(each.value, "vnic_types", var.vnic_types)
17+
vnic_profiles = lookup(each.value, "vnic_profiles", var.vnic_profiles)
1918
volume_backed_instances = lookup(each.value, "volume_backed_instances", var.volume_backed_instances)
2019
root_volume_size = lookup(each.value, "root_volume_size", var.root_volume_size)
2120
extra_volumes = lookup(each.value, "extra_volumes", {})
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
data "openstack_networking_network_v2" "network" {
3+
4+
for_each = {for net in var.networks: net.network => net}
5+
6+
name = each.value.network
7+
}
8+
9+
data "openstack_networking_subnet_v2" "subnet" {
10+
11+
for_each = {for net in var.networks: net.network => net}
12+
13+
name = each.value.subnet
14+
}

environments/skeleton/{{cookiecutter.environment}}/tofu/node_group/nodes.tf

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,23 @@ resource "openstack_compute_volume_attach_v2" "compute" {
3333

3434
resource "openstack_networking_port_v2" "compute" {
3535

36-
for_each = toset(var.nodes)
36+
for_each = {for item in setproduct(var.nodes, var.networks):
37+
"${item[0]}-${item[1].network}" => item[1]
38+
}
3739

3840
name = "${var.cluster_name}-${each.key}"
39-
network_id = var.cluster_net_id
41+
network_id = data.openstack_networking_network_v2.network[each.value.network].id
4042
admin_state_up = "true"
4143

4244
fixed_ip {
43-
subnet_id = var.cluster_subnet_id
45+
subnet_id = data.openstack_networking_subnet_v2.subnet[each.value.network].id
4446
}
4547

4648
security_group_ids = var.security_group_ids
4749

4850
binding {
49-
vnic_type = var.vnic_type
50-
profile = var.vnic_profile
51+
vnic_type = lookup(var.vnic_types, each.value.network, "normal")
52+
profile = lookup(var.vnic_profiles, each.value.network, "{}")
5153
}
5254
}
5355

@@ -72,9 +74,12 @@ resource "openstack_compute_instance_v2" "compute_fixed_image" {
7274
}
7375
}
7476

75-
network {
76-
port = openstack_networking_port_v2.compute[each.key].id
77-
access_network = true
77+
dynamic "network" {
78+
for_each = {for net in var.networks: net.network => net}
79+
content {
80+
port = openstack_networking_port_v2.compute["${each.key}-${network.key}"].id
81+
access_network = length(var.networks) == 1 ? true : lookup(network.value, "access_network", false)
82+
}
7883
}
7984

8085
metadata = merge(
@@ -120,9 +125,12 @@ resource "openstack_compute_instance_v2" "compute" {
120125
}
121126
}
122127

123-
network {
124-
port = openstack_networking_port_v2.compute[each.key].id
125-
access_network = true
128+
dynamic "network" {
129+
for_each = {for net in var.networks: net.network => net}
130+
content {
131+
port = openstack_networking_port_v2.compute["${each.key}-${network.key}"].id
132+
access_network = length(var.networks) == 1 ? true : lookup(network.value, "access_network", false)
133+
}
126134
}
127135

128136
metadata = merge(

environments/skeleton/{{cookiecutter.environment}}/tofu/node_group/variables.tf

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ variable "cluster_domain_suffix" {
1717
default = "invalid"
1818
}
1919

20-
variable "cluster_net_id" {
21-
type = string
22-
}
23-
24-
variable "cluster_subnet_id" {
25-
type = string
26-
}
27-
2820
variable "key_pair" {
2921
type = string
3022
description = "Name of an existing keypair in OpenStack"
@@ -40,16 +32,14 @@ variable "environment_root" {
4032
description = "Path to environment root, automatically set by activate script"
4133
}
4234

43-
variable "vnic_type" {
44-
type = string
45-
description = "VNIC type, see https://registry.terraform.io/providers/terraform-provider-openstack/openstack/latest/docs/resources/networking_port_v2#vnic_type"
46-
default = "normal"
35+
variable "vnic_types" {
36+
type = map(string)
37+
default = {}
4738
}
4839

49-
variable "vnic_profile" {
50-
type = string
51-
description = "VNIC binding profile as json string, see https://registry.terraform.io/providers/terraform-provider-openstack/openstack/latest/docs/resources/networking_port_v2#profile."
52-
default = "{}"
40+
variable "vnic_profiles" {
41+
type = map(string)
42+
default = {}
5343
}
5444

5545
variable "volume_backed_instances" {
@@ -99,4 +89,9 @@ variable "ignore_image_changes" {
9989
type = bool
10090
description = "Whether to ignore changes to the image_id parameter"
10191
default = false
102-
}
92+
}
93+
94+
variable "networks" {
95+
type = list(map(string))
96+
default = []
97+
}

0 commit comments

Comments
 (0)