-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
122 lines (92 loc) · 4.3 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
locals {
# Generated cluster name in case one is not specificed.
cluster_name = coalesce(var.cluster_name, try("talos-cluster-${random_id.cluster_name[0].hex}", "talos-proxmox"))
# Map containing the full, node group configurations.
# Values can also come from cluster wide values specified as explicit variables,
# but the values in this node_group take priority.
node_groups = merge({
# controlplane is a special case and used for the control plane nodes and as such
# it must always exist. These values will get overwritten from the controlplane input
# map variable as needed.
controlplane = merge({
name_prefix = "talos-ctrl-"
machine_type = "controlplane"
node_count = 3
}
, var.controlplane)
}, var.workers)
controlplane_ip_addresses = module.talos["controlplane"].ip_addresses
node_ip_addresses = flatten([for k, v in module.node_groups : v.ipv4_addresses])
machine_secrets = var.talos_machine_secrets != null ? var.talos_machine_secrets : talos_machine_secrets.this[0]
}
# Random ID for use where a cluster_name input variable has not been
# specified.
resource "random_id" "cluster_name" {
count = var.cluster_name == null ? 1 : 0
byte_length = 4
}
resource "talos_machine_secrets" "this" {
count = var.talos_machine_secrets == null ? 1 : 0
talos_version = var.talos_version
}
data "talos_client_configuration" "this" {
cluster_name = local.cluster_name
client_configuration = local.machine_secrets.client_configuration
endpoints = coalesce(var.talos_endpoint_hosts, local.controlplane_ip_addresses)
nodes = local.node_ip_addresses
}
module "image" {
count = var.iso_file_id == null ? 1 : 0
source = "./modules/image"
talos_version = var.talos_version
factory_host = var.factory_host
extensions = var.extensions
id = local.cluster_name
pve_node_name = var.image_pve_node_name
datastore_id = var.image_datastore_id
}
module "node_groups" {
source = "./modules/node_group"
for_each = local.node_groups
name_prefix = try(each.value.name_prefix, "talos-wrkr-${each.key}-")
machine_type = try(each.value.machine_type, "worker")
node_count = try(each.value.node_count, null)
pve_node_names = try(each.value.pve_node_names, var.pve_node_names, null)
pool_id = try(each.value.pool_id, null)
cpu_count = try(each.value.cpu_count, var.cpu_count, null)
cpu_flags = try(each.value.cpu_flags, var.cpu_flags, null)
cpu_type = try(each.value.cpu_type, var.cpu_type, null)
memory_size_in_mb = try(each.value.memory_size_in_mb, var.memory_size_in_mb, null)
datastore_id = try(each.value.datastore_id, var.datastore_id)
iso_file_id = coalesce(var.iso_file_id, module.image[0].iso_file_id)
disks = try(each.value.disks, var.disks, null)
network_devices = lookup(each.value, "network_devices", null)
ipconfig_ipv4 = try(each.value.ipconfig_ipv4, null)
ipconfig_ipv6 = try(each.value.ipconfig_ipv6, null)
tags = concat(try(each.value.tags, []), var.tags)
}
module "talos" {
for_each = module.node_groups
source = "./modules/talos"
node_count = each.value.node_count
installer_image = coalesce(var.installer_image, module.image[0].installer_image)
# yamlencode as list of mixed elements can't be concatenated. https://github.com/hashicorp/terraform/issues/33259
config_patches = concat(
[for p in var.config_patches : yamlencode(p)],
[for p in try(local.node_groups[each.key].config_patches, []) : yamlencode(p)]
)
machine_secrets = local.machine_secrets
machine_type = each.value.machine_type
cluster_name = local.cluster_name
cluster_endpoint = coalesce(var.cluster_endpoint, format("https://%s:6443", coalesce(var.vip_address, module.node_groups["controlplane"].ipv4_addresses[0])))
ip_addresses = each.value.ipv4_addresses
vip_address = var.vip_address
registry_mirrors = var.registry_mirrors
registry_mirrors_override_path = var.registry_mirrors_override_path
metrics_server = var.metrics_server
cilium = var.cilium
cilium_cli_version = var.cilium_cli_version
cilium_version = var.cilium_version
node_labels = merge(var.node_labels, try(each.value.node_labels, {}))
node_taints = merge(var.node_taints, try(each.value.node_taints, {}))
}