forked from MusicDin/kubitect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
247 lines (207 loc) · 8.12 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#=====================================================================================
# Provider specific
#=====================================================================================
# Sets libvirt provider's uri #
provider "libvirt" {
uri = var.libvirt_provider_uri
}
#======================================================================================
# Modules
#======================================================================================
# Creates network #
module "network_module" {
source = "./modules/network/"
libvirt_provider_uri = var.libvirt_provider_uri
network_name = var.network_name
network_forward_mode = var.network_forward_mode
network_virtual_bridge = var.network_virtual_bridge
network_mac = var.network_mac
network_gateway = var.network_gateway
network_mask_bits = var.network_mask_bits
network_dhcp_ip_start = var.network_dhcp_ip_start
network_dhcp_ip_end = var.network_dhcp_ip_end
vm_lb_macs_ips = var.vm_lb_macs_ips
vm_master_macs_ips = var.vm_master_macs_ips
vm_worker_macs_ips = var.vm_worker_macs_ips
}
# Create HAProxy load balancer #
module "lb_module" {
source = "./modules/vm"
count = length(var.vm_lb_macs_ips)
# Variables from general resources #
resource_pool_name = libvirt_pool.resource_pool.name
base_volume_id = libvirt_volume.base_volume.id
cloud_init_id = libvirt_cloudinit_disk.cloud_init.id
# Load balancer specific variables #
vm_index = count.index
vm_type = "lb"
vm_user = var.vm_user
vm_ssh_private_key = var.vm_ssh_private_key
vm_ssh_known_hosts = var.vm_ssh_known_hosts
vm_network_name = var.network_name
vm_name_prefix = var.vm_name_prefix
vm_cpu = var.vm_lb_cpu
vm_ram = var.vm_lb_ram
vm_storage = var.vm_lb_storage
vm_mac = keys(var.vm_lb_macs_ips)[count.index]
vm_ip = values(var.vm_lb_macs_ips)[count.index]
# Dependancy takes care that resource pool is not removed before volumes are #
depends_on = [
libvirt_pool.resource_pool,
libvirt_volume.base_volume
]
}
# Creates master nodes #
module "master_module" {
source = "./modules/vm"
count = length(var.vm_master_macs_ips)
# Variables from general resources #
resource_pool_name = libvirt_pool.resource_pool.name
base_volume_id = libvirt_volume.base_volume.id
cloud_init_id = libvirt_cloudinit_disk.cloud_init.id
# Master node specific variables #
vm_index = count.index
vm_type = "master"
vm_user = var.vm_user
vm_ssh_private_key = var.vm_ssh_private_key
vm_ssh_known_hosts = var.vm_ssh_known_hosts
vm_network_name = var.network_name
vm_name_prefix = var.vm_name_prefix
vm_cpu = var.vm_master_cpu
vm_ram = var.vm_master_ram
vm_storage = var.vm_master_storage
vm_mac = keys(var.vm_master_macs_ips)[count.index]
vm_ip = values(var.vm_master_macs_ips)[count.index]
# Dependancy takes care that resource pool is not removed before volumes are #
depends_on = [
libvirt_pool.resource_pool,
libvirt_volume.base_volume
]
}
# Creates worker nodes #
module "worker_module" {
source = "./modules/vm"
count = length(var.vm_worker_macs_ips)
# Variables from general resources #
resource_pool_name = libvirt_pool.resource_pool.name
base_volume_id = libvirt_volume.base_volume.id
cloud_init_id = libvirt_cloudinit_disk.cloud_init.id
# Worker node specific variables #
vm_index = count.index
vm_type = "worker"
vm_user = var.vm_user
vm_ssh_private_key = var.vm_ssh_private_key
vm_ssh_known_hosts = var.vm_ssh_known_hosts
vm_network_name = var.network_name
vm_name_prefix = var.vm_name_prefix
vm_cpu = var.vm_worker_cpu
vm_ram = var.vm_worker_ram
vm_storage = var.vm_worker_storage
vm_mac = keys(var.vm_worker_macs_ips)[count.index]
vm_ip = values(var.vm_worker_macs_ips)[count.index]
# Dependancy takes care that resource pool is not removed before volumes are #
depends_on = [
libvirt_pool.resource_pool,
libvirt_volume.base_volume
]
}
# Configures k8s cluster using Kubespray #
module "k8s_cluster" {
source = "./modules/cluster"
action = var.action
# VM variables
vm_distro = var.vm_distro
vm_user = var.vm_user
vm_ssh_private_key = var.vm_ssh_private_key
vm_name_prefix = var.vm_name_prefix
vm_worker_ips = values(var.vm_worker_macs_ips)
vm_worker_node_label = var.vm_worker_node_label
vm_master_ips = values(var.vm_master_macs_ips)
vm_lb_ips = values(var.vm_lb_macs_ips)
vm_lb_vip = var.vm_lb_vip
network_interface = var.network_interface
# K8s cluster variables
k8s_kubespray_url = var.k8s_kubespray_url
k8s_kubespray_version = var.k8s_kubespray_version
k8s_version = var.k8s_version
k8s_network_plugin = var.k8s_network_plugin
k8s_dns_mode = var.k8s_dns_mode
# Kubespray addons
kubespray_custom_addons_enabled = var.kubespray_custom_addons_enabled
kubespray_custom_addons_path = var.kubespray_custom_addons_path
k8s_dashboard_enabled = var.k8s_dashboard_enabled
helm_enabled = var.helm_enabled
metallb_enabled = var.metallb_enabled
metallb_version = var.metallb_version
metallb_port = var.metallb_port
metallb_cpu_limit = var.metallb_cpu_limit
metallb_mem_limit = var.metallb_mem_limit
metallb_protocol = var.metallb_protocol
metallb_ip_range = var.metallb_ip_range
metallb_peers = var.metallb_peers
# K8s cluster creation depends on network and all VMs
depends_on = [
module.network_module,
module.lb_module,
module.worker_module,
module.master_module
]
}
#======================================================================================
# General Resources
#======================================================================================
#================================
# Resource pool and base volume
#================================
# Creates a resource pool for Kubernetes VM volumes #
resource "libvirt_pool" "resource_pool" {
name = var.libvirt_resource_pool_name
type = "dir"
path = "${var.libvirt_resource_pool_location}${var.libvirt_resource_pool_name}"
}
# Creates base OS image for nodes in a cluster #
resource "libvirt_volume" "base_volume" {
name = "base_volume"
pool = var.libvirt_resource_pool_name
source = var.vm_image_source
depends_on = [libvirt_pool.resource_pool]
}
#================================
# Cloud-init
#================================
# Public ssh key for vm (it is directly injected in cloud-init configuration) #
data "template_file" "public_ssh_key" {
template = file("${var.vm_ssh_private_key}.pub")
}
# Network bridge configuration (for cloud-init) #
data "template_file" "cloud_init_network_tpl" {
template = file("templates/cloud_init_network.tpl")
vars = {
network_interface = var.network_interface
}
}
# Creates network bridge configuration file from template #
resource "local_file" "cloud_init_network_file" {
content = data.template_file.cloud_init_network_tpl.rendered
filename = "config/cloud_init_network.cfg"
}
# Cloud-init configuration template #
data "template_file" "cloud_init_tpl" {
template = file("templates/cloud_init.tpl")
vars = {
user = var.vm_user
ssh_public_key = data.template_file.public_ssh_key.rendered
}
}
# Creates cloud-init configuration file from template #
resource "local_file" "cloud_init_file" {
content = data.template_file.cloud_init_tpl.rendered
filename = "config/cloud_init.cfg"
}
# Initializes cloud-init disk for user data#
resource "libvirt_cloudinit_disk" "cloud_init" {
name = "cloud-init.iso"
pool = libvirt_pool.resource_pool.name
user_data = data.template_file.cloud_init_tpl.rendered
network_config = data.template_file.cloud_init_network_tpl.rendered
}