-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.tf
228 lines (202 loc) · 8.47 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# Copyright © 2022-2024, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
data "vsphere_datacenter" "dc" {
name = var.vsphere_datacenter
}
data "vsphere_resource_pool" "pool" {
name = var.vsphere_resource_pool
datacenter_id = data.vsphere_datacenter.dc.id
}
# Kubernetes - Node setup
## Control Plane Nodes
module "control_plane" {
source = "./modules/vm"
for_each = local.control_plane_nodes
name = replace(lower(each.key), "_", "-")
datacenter_id = data.vsphere_datacenter.dc.id
resource_pool_id = data.vsphere_resource_pool.pool.id
folder = var.vsphere_folder
datastore = var.vsphere_datastore
network = var.vsphere_network
template = var.vsphere_template
cluster_domain = var.cluster_domain
cluster_name = local.cluster_name
netmask = var.netmask
gateway = var.gateway
dns_servers = var.dns_servers
instance_count = length(each.value.ip_addresses) != 0 ? length(each.value.ip_addresses) : each.value.count
num_cpu = each.value.cpus
memory = each.value.memory
disk_size = each.value.os_disk
misc_disks = each.value.misc_disks
ip_addresses = length(each.value.ip_addresses) != 0 ? each.value.ip_addresses : []
}
## System Nodes
module "system" {
source = "./modules/vm"
for_each = local.system_nodes
name = replace(lower(each.key), "_", "-")
datacenter_id = data.vsphere_datacenter.dc.id
resource_pool_id = data.vsphere_resource_pool.pool.id
folder = var.vsphere_folder
datastore = var.vsphere_datastore
network = var.vsphere_network
template = var.vsphere_template
cluster_domain = var.cluster_domain
cluster_name = local.cluster_name
netmask = var.netmask
gateway = var.gateway
dns_servers = var.dns_servers
instance_count = length(each.value.ip_addresses) != 0 ? length(each.value.ip_addresses) : each.value.count
num_cpu = each.value.cpus
memory = each.value.memory
disk_size = each.value.os_disk
misc_disks = each.value.misc_disks
ip_addresses = length(each.value.ip_addresses) != 0 ? each.value.ip_addresses : []
}
## Nodes
module "node" {
source = "./modules/vm"
for_each = local.nodes
name = replace(lower(each.key), "_", "-")
datacenter_id = data.vsphere_datacenter.dc.id
resource_pool_id = data.vsphere_resource_pool.pool.id
folder = var.vsphere_folder
datastore = var.vsphere_datastore
network = var.vsphere_network
template = var.vsphere_template
cluster_domain = var.cluster_domain
cluster_name = local.cluster_name
netmask = var.netmask
gateway = var.gateway
dns_servers = var.dns_servers
instance_count = length(each.value.ip_addresses) != 0 ? length(each.value.ip_addresses) : each.value.count
num_cpu = each.value.cpus
memory = each.value.memory
disk_size = each.value.os_disk
misc_disks = each.value.misc_disks
ip_addresses = length(each.value.ip_addresses) != 0 ? each.value.ip_addresses : []
}
module "jump" {
source = "./modules/vm"
name = "jump"
instance_count = var.create_jump ? 1 : 0
resource_pool_id = data.vsphere_resource_pool.pool.id
folder = var.vsphere_folder
datastore = var.vsphere_datastore
network = var.vsphere_network
datacenter_id = data.vsphere_datacenter.dc.id
template = var.vsphere_template
cluster_domain = var.cluster_domain
cluster_name = local.cluster_name
ip_addresses = [var.jump_ip]
memory = var.jump_memory
num_cpu = var.jump_num_cpu
disk_size = var.jump_disk_size
netmask = var.netmask
gateway = var.gateway
dns_servers = var.dns_servers
}
module "nfs" {
source = "./modules/vm"
name = "nfs"
instance_count = var.create_nfs ? 1 : 0
resource_pool_id = data.vsphere_resource_pool.pool.id
folder = var.vsphere_folder
datastore = var.vsphere_datastore
network = var.vsphere_network
datacenter_id = data.vsphere_datacenter.dc.id
template = var.vsphere_template
cluster_domain = var.cluster_domain
cluster_name = local.cluster_name
ip_addresses = [var.nfs_ip]
memory = var.nfs_memory
num_cpu = var.nfs_num_cpu
disk_size = var.nfs_disk_size
netmask = var.netmask
gateway = var.gateway
dns_servers = var.dns_servers
}
module "cr" {
source = "./modules/vm"
name = "cr"
instance_count = var.create_cr ? 1 : 0
resource_pool_id = data.vsphere_resource_pool.pool.id
folder = var.vsphere_folder
datastore = var.vsphere_datastore
network = var.vsphere_network
datacenter_id = data.vsphere_datacenter.dc.id
template = var.vsphere_template
cluster_domain = var.cluster_domain
cluster_name = local.cluster_name
ip_addresses = [var.cr_ip]
memory = var.cr_memory
num_cpu = var.cr_num_cpu
disk_size = var.cr_disk_size
netmask = var.netmask
gateway = var.gateway
dns_servers = var.dns_servers
}
module "postgresql" {
source = "./modules/server"
for_each = local.postgres_servers != null ? length(local.postgres_servers) != 0 ? local.postgres_servers : {} : {}
name = lower("${local.cluster_name}-${each.key}-pgsql")
resource_pool_id = data.vsphere_resource_pool.pool.id
folder = var.vsphere_folder
datastore = var.vsphere_datastore
network = var.vsphere_network
datacenter_id = data.vsphere_datacenter.dc.id
template = var.vsphere_template
cluster_domain = var.cluster_domain
dns_servers = var.dns_servers
netmask = var.netmask
gateway = var.gateway
num_cpu = each.value.server_num_cpu
memory = each.value.server_memory
disk_size = each.value.server_disk_size
ip_address = each.value.server_ip
}
resource "local_file" "inventory" {
filename = var.inventory
content = templatefile("${path.module}/templates/ansible/inventory.tmpl", {
prefix = replace(var.prefix, "-", "_") # NOTE: Conversion needed in taking a URL value and using it as an Ansible Inventory value
control_plane_ips = length(local.control_plane_ips) > 0 ? local.control_plane_ips : []
node_ips = length(local.node_ips) > 0 ? local.node_ips : []
nfs_ip = var.create_nfs ? var.nfs_ip : null
jump_ip = var.create_jump ? var.jump_ip : null
cr_ip = var.create_cr ? var.cr_ip : null
postgres_servers = local.postgres_servers
}
)
}
resource "local_file" "ansible_vars" {
filename = var.ansible_vars
content = templatefile("${path.module}/templates/ansible/ansible-vars.yaml.tmpl", {
ansible_user = var.ansible_user
ansible_password = var.ansible_password
deployment_type = var.deployment_type
iac_tooling = var.iac_tooling
prefix = var.prefix
cluster_name = local.cluster_name
cluster_version = var.cluster_version
cluster_cni = var.cluster_cni
cluster_cni_version = var.cluster_cni_version
cluster_cri = var.cluster_cri
cluster_cri_version = var.cluster_cri_version
cluster_service_subnet = var.cluster_service_subnet
cluster_pod_subnet = var.cluster_pod_subnet
control_plane_ssh_key_name = var.control_plane_ssh_key_name
cluster_vip_version = var.cluster_vip_version
cluster_vip_ip = var.cluster_vip_ip
cluster_vip_fqdn = var.cluster_vip_fqdn == null ? "${local.cluster_name}-vip.${var.cluster_domain}" : length(var.cluster_vip_fqdn) > 0 ? var.cluster_vip_fqdn : "${local.cluster_name}-vip.${var.cluster_domain}"
cluster_lb_type = var.cluster_lb_type
cluster_lb_addresses = local.loadbalancer_addresses
nfs_ip = var.create_nfs ? var.nfs_ip : null
jump_ip = var.create_jump ? var.jump_ip : null
cr_ip = var.create_cr ? var.cr_ip : null
system_ssh_keys_dir = var.system_ssh_keys_dir
node_labels = local.node_labels
node_taints = local.node_taints
}
)
}