-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
333 lines (281 loc) · 9.42 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
locals {
local_host = "127.0.0.1"
template_path = "${path.module}/templates"
cluster_fqdn = "${var.cluster_name}.${var.cluster_domainname}"
boot_token = "${random_string.token_prefix.result}.${random_string.token_suffix.result}"
public_ip = brightbox_cloudip.k8s_master.public_ip
public_rdns = brightbox_cloudip.k8s_master.reverse_dns
public_fqdn = brightbox_cloudip.k8s_master.fqdn
lb_count = var.master_count > 1 ? 1 : 0
bastion = local.lb_count == 1 ? brightbox_cloudip.bastion[0].fqdn : local.public_fqdn
bastion_user = brightbox_server.k8s_master[0].username
api_target = local.lb_count == 1 ? brightbox_load_balancer.k8s_master[0].id : brightbox_server.k8s_master[0].interface
install_script = templatefile(
"${local.template_path}/install-kube",
{ kubernetes_release = var.kubernetes_release }
)
cloud_config = file("${local.template_path}/cloud-config.yml")
}
resource "brightbox_cloudip" "k8s_master" {
# The firewall rules have to be there before the bastion will work.
# This is used as the bastion ip if there is only one master.
depends_on = [
var.cluster_ready,
brightbox_load_balancer.k8s_master,
brightbox_firewall_rule.k8s_lb,
]
name = "k8s-master.${var.cluster_name}"
target = local.api_target
provisioner "local-exec" {
when = destroy
command = "ssh-keygen -R ${self.fqdn}; ssh-keygen -R ${self.public_ip}"
}
}
resource "brightbox_load_balancer" "k8s_master" {
count = local.lb_count
name = "k8s-master.${var.cluster_name}"
listener {
protocol = "tcp"
in = var.apiserver_service_port
out = var.apiserver_service_port
timeout = 86400000
}
healthcheck {
type = "tcp"
port = var.apiserver_service_port
}
nodes = brightbox_server.k8s_master[*].id
}
resource "brightbox_cloudip" "bastion" {
# The firewall rules have to be there before the bastion will work
depends_on = [
var.cluster_ready,
brightbox_load_balancer.k8s_master,
brightbox_cloudip.k8s_master,
brightbox_firewall_rule.k8s_lb,
]
count = local.lb_count
name = "bastion.${var.cluster_name}"
target = brightbox_server.k8s_master[0].interface
provisioner "local-exec" {
when = destroy
command = "ssh-keygen -R ${self.fqdn}; ssh-keygen -R ${self.public_ip}"
}
}
resource "brightbox_firewall_rule" "k8s_lb" {
count = local.lb_count
destination_port = var.apiserver_service_port
protocol = "tcp"
source = brightbox_load_balancer.k8s_master[count.index].id
description = "${brightbox_load_balancer.k8s_master[count.index].id} API access"
firewall_policy = var.cluster_firewall_policy
}
resource "brightbox_server" "k8s_master" {
depends_on = [
var.cluster_ready
]
count = var.master_count
name = "k8s-master-${count.index}.${local.cluster_fqdn}"
image = data.brightbox_image.k8s_master.id
type = var.master_type
user_data = local.cloud_config
zone = "${var.region}-${var.master_zone == "" ? (count.index % 2 == 0 ? "a" : "b") : var.master_zone}"
server_groups = [var.cluster_server_group]
lifecycle {
ignore_changes = [
image,
type,
server_groups,
]
create_before_destroy = true
}
}
resource "null_resource" "k8s_master" {
triggers = {
master_id = brightbox_server.k8s_master[0].id
cert_change = var.ca_cert_pem
}
connection {
user = local.bastion_user
host = local.bastion
}
provisioner "file" {
content = var.ca_cert_pem
destination = "ca.crt"
}
provisioner "file" {
content = var.ca_private_key_pem
destination = "ca.key"
}
# Generic provisioner
provisioner "remote-exec" {
inline = [
local.install_script
]
}
provisioner "remote-exec" {
when = destroy
# The sleep 10 is a hack to workaround the lack of wait on the delete
# command
inline = [
"kubectl get services -o=jsonpath='{range .items[?(.spec.type==\"LoadBalancer\")]}{\"service/\"}{.metadata.name}{\" \"}{end}' | xargs -r kubectl delete",
"sleep 10",
]
}
}
resource "null_resource" "k8s_master_configure" {
depends_on = [
null_resource.k8s_master,
]
triggers = {
cert_key = random_id.master_certificate_key.hex
master_id = brightbox_server.k8s_master[0].id
k8s_release = var.kubernetes_release
cert_change = var.ca_cert_pem
master_script = local.master_provisioner_script
kubeadm_script = local.kubeadm_config_script
cert_change = var.ca_cert_pem
}
connection {
user = local.bastion_user
host = local.bastion
}
provisioner "remote-exec" {
inline = [
local.kubeadm_config_script,
local.master_provisioner_script,
]
}
}
resource "null_resource" "k8s_master_mirrors" {
depends_on = [
null_resource.k8s_master_configure,
]
count = max(0, length(brightbox_server.k8s_master) - 1)
triggers = {
mirror_id = brightbox_server.k8s_master[count.index + 1].id
cert_change = var.ca_cert_pem
}
connection {
host = brightbox_server.k8s_master[count.index + 1].hostname
user = brightbox_server.k8s_master[count.index + 1].username
type = "ssh"
bastion_host = local.bastion
bastion_user = local.bastion_user
}
provisioner "file" {
content = var.ca_cert_pem
destination = "ca.crt"
}
provisioner "file" {
content = var.ca_private_key_pem
destination = "ca.key"
}
# Generic provisioner
provisioner "remote-exec" {
inline = [
local.install_script
]
}
}
resource "null_resource" "k8s_master_mirrors_configure" {
depends_on = [
null_resource.k8s_master_configure,
null_resource.k8s_master_mirrors,
]
count = max(0, length(brightbox_server.k8s_master) - 1)
triggers = {
cert_key = random_id.master_certificate_key.hex
mirror_id = brightbox_server.k8s_master[count.index + 1].id
k8s_release = var.kubernetes_release
cert_change = var.ca_cert_pem
master_script = local.master_mirror_provisioner_script
kubeadm_script = local.kubeadm_config_script
}
connection {
host = brightbox_server.k8s_master[count.index + 1].hostname
user = brightbox_server.k8s_master[count.index + 1].username
type = "ssh"
bastion_host = local.bastion
bastion_user = local.bastion_user
}
provisioner "remote-exec" {
inline = [
local.kubeadm_config_script,
local.master_mirror_provisioner_script,
]
}
}
data "brightbox_image" "k8s_master" {
name = var.image_desc
arch = "x86_64"
official = true
most_recent = true
}
resource "brightbox_api_client" "controller_client" {
name = "Cloud Controller ${var.cluster_name}"
permissions_group = "full"
}
locals {
master_provisioner_script = templatefile("${local.template_path}/install-master", {
kubernetes_release = var.kubernetes_release,
calico_release = var.calico_release,
cluster_name = var.cluster_name,
public_ip = local.public_ip,
public_fqdn = local.public_fqdn,
boot_token = local.boot_token,
service_cluster_ip_range = var.service_cidr,
controller_client = brightbox_api_client.controller_client.id,
controller_client_secret = brightbox_api_client.controller_client.secret,
apiurl = "https://api.${var.region}.brightbox.com",
service_port = var.apiserver_service_port,
local_host = local.local_host
}
)
master_mirror_provisioner_script = templatefile("${local.template_path}/install-master-mirror", {
kubernetes_release = var.kubernetes_release,
calico_release = var.calico_release,
cluster_name = var.cluster_name,
public_fqdn = local.public_fqdn,
service_cluster_ip_range = var.service_cidr,
controller_client = brightbox_api_client.controller_client.id,
controller_client_secret = brightbox_api_client.controller_client.secret,
apiurl = "https://api.${var.region}.brightbox.com",
boot_token = local.boot_token
fqdn = local.public_fqdn
master_certificate_key = random_id.master_certificate_key.hex,
service_port = var.apiserver_service_port
}
)
kubeadm_config_script = templatefile(
"${local.template_path}/kubeadm-config",
{
kubernetes_release = var.kubernetes_release,
cluster_name = var.cluster_name,
cluster_domainname = var.cluster_domainname,
service_cidr = var.service_cidr,
cluster_cidr = var.cluster_cidr,
public_ip = local.public_ip,
public_rdns = local.public_rdns,
public_fqdn = local.public_fqdn,
boot_token = local.boot_token,
cluster_domainname = var.cluster_domainname,
master_certificate_key = random_id.master_certificate_key.hex,
service_port = var.apiserver_service_port,
}
)
masters_configured = concat([null_resource.k8s_master_configure.id], null_resource.k8s_master_mirrors_configure[*].id)
}
resource "random_id" "master_certificate_key" {
byte_length = 32
}
resource "random_string" "token_suffix" {
length = 16
special = false
upper = false
}
resource "random_string" "token_prefix" {
length = 6
special = false
upper = false
}