-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.tf
185 lines (158 loc) · 5.94 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
locals {
create_data_volume = var.data_volume_size > 0
data_volume_name = coalesce(
var.ephemeral_data_volume ? "ephemeral0" : null,
var.image_scsi_bus ? "/dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_${openstack_blockstorage_volume_v3.data.0.id}" : null,
"/dev/disk/by-id/virtio-${substr(openstack_blockstorage_volume_v3.data.0.id, 0, 20)}",
)
}
data "openstack_compute_flavor_v2" "k3s" {
count = var.flavor_id == null ? 1 : 0
name = var.flavor_name
}
data "openstack_images_image_v2" "k3s" {
count = var.image_id == null ? 1 : 0
name = var.image_name
most_recent = true
}
resource "openstack_blockstorage_volume_v3" "data" {
count = local.create_data_volume && !var.ephemeral_data_volume ? 1 : 0
name = "${var.name}-data"
availability_zone = var.availability_zone
volume_type = var.data_volume_type
size = var.data_volume_size
enable_online_resize = var.data_volume_enable_online_resize
}
module "k3s" {
source = "../k3s"
name = var.name
k3s_join_existing = var.k3s_join_existing
cluster_token = var.cluster_token
k3s_version = var.k3s_version
k3s_channel = var.k3s_channel
k3s_install_url = var.k3s_install_url
k3s_ips = local.mgmt_port.all_fixed_ips
k3s_url = var.k3s_url
k3s_external_ip = var.k3s_external_ip != null ? var.k3s_external_ip : local.node_external_ip
k3s_args = var.k3s_args
custom_cloud_config_write_files = var.custom_cloud_config_write_files
custom_cloud_config_runcmd = var.custom_cloud_config_runcmd
bootstrap_token_id = var.bootstrap_token_id
bootstrap_token_secret = var.bootstrap_token_secret
persistent_volume_dev = local.create_data_volume ? local.data_volume_name : ""
persistent_volume_label = var.ephemeral_data_volume ? "ephemeral0" : "k3s-data"
}
resource "openstack_compute_instance_v2" "node" {
name = var.name
image_id = var.image_id == null ? data.openstack_images_image_v2.k3s.0.id : var.image_id
flavor_id = var.flavor_id == null ? data.openstack_compute_flavor_v2.k3s.0.id : var.flavor_id
key_pair = var.keypair_name
metadata = var.server_properties
config_drive = var.config_drive
availability_zone = var.availability_zone
user_data = module.k3s.user_data
stop_before_destroy = var.server_stop_before_destroy
scheduler_hints {
group = var.server_group_id
}
network {
port = local.mgmt_port.id
access_network = true
}
dynamic "network" {
for_each = var.additional_port_ids
content {
port = network["value"]
}
}
block_device {
boot_index = 0
uuid = var.image_id == null ? data.openstack_images_image_v2.k3s.0.id : var.image_id
delete_on_termination = true
destination_type = "local"
source_type = "image"
}
dynamic "block_device" {
for_each = local.create_data_volume && var.ephemeral_data_volume ? { "data" = { "size" = var.data_volume_size } } : {}
content {
boot_index = -1
source_type = "blank"
destination_type = "local"
delete_on_termination = true
volume_size = block_device.value["size"]
}
}
dynamic "block_device" {
for_each = openstack_blockstorage_volume_v3.data
content {
boot_index = -1
uuid = block_device.value["id"]
source_type = "volume"
destination_type = "volume"
delete_on_termination = false
}
}
lifecycle {
ignore_changes = [
block_device.0.uuid
]
}
}
resource "openstack_networking_port_v2" "mgmt" {
count = length(var.allowed_address_cidrs) == 0 ? 1 : 0
name = var.name
network_id = var.network_id
admin_state_up = true
security_group_ids = var.security_group_ids
port_security_enabled = true
dynamic "fixed_ip" {
for_each = toset(var.k3s_ips)
content {
subnet_id = var.subnet_id
ip_address = fixed_ip.value
}
}
lifecycle {
ignore_changes = [
allowed_address_pairs,
]
}
}
resource "openstack_networking_port_v2" "mgmt_with_allowed_address_pairs" {
count = length(var.allowed_address_cidrs) > 0 ? 1 : 0
name = var.name
network_id = var.network_id
admin_state_up = true
security_group_ids = var.security_group_ids
port_security_enabled = true
dynamic "fixed_ip" {
for_each = toset(var.k3s_ips)
content {
subnet_id = var.subnet_id
ip_address = fixed_ip.value
}
}
dynamic "allowed_address_pairs" {
for_each = var.allowed_address_cidrs
content {
ip_address = allowed_address_pairs.value
}
}
}
resource "openstack_networking_floatingip_v2" "node" {
count = var.floating_ip_pool == null ? 0 : 1
pool = var.floating_ip_pool
}
resource "openstack_compute_floatingip_associate_v2" "node" {
count = length(openstack_networking_floatingip_v2.node) > 0 ? 1 : 0
floating_ip = openstack_networking_floatingip_v2.node[0].address
instance_id = openstack_compute_instance_v2.node.id
}
locals {
node_ip = openstack_compute_instance_v2.node.network.0.fixed_ip_v4
node_ipv6 = openstack_compute_instance_v2.node.network.0.fixed_ip_v6
node_external_ip = length(openstack_networking_floatingip_v2.node) > 0 ? openstack_networking_floatingip_v2.node[0].address : null
k3s_url = var.k3s_join_existing ? var.k3s_url : "https://${local.node_ip}:6443"
k3s_external_url = (var.k3s_join_existing || local.node_external_ip == null) ? "" : "https://${local.node_external_ip}:6443"
mgmt_port = try(openstack_networking_port_v2.mgmt.0, openstack_networking_port_v2.mgmt_with_allowed_address_pairs.0)
}