-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbootstrap.tf
348 lines (305 loc) · 9.82 KB
/
bootstrap.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
resource "rke_cluster" "cluster" {
depends_on = [null_resource.node_cleanup]
cluster_name = local.cluster_name
ignore_docker_version = var.ignore_docker_version
kubernetes_version = local.kubernetes_version
enable_cri_dockerd = var.enable_cri_dockerd
authentication {
strategy = "x509"
sans = local.sans
}
authorization {
mode = var.rke_authorization
}
network {
plugin = local.network_plugin
}
ingress {
provider = var.ingress_provider
http_port = 80
https_port = 443
network_mode = "hostPort"
}
dns {
provider = var.dns_provider
}
monitoring {
provider = var.monitoring
}
addon_job_timeout = var.addon_job_timeout
addons_include = var.addons_include
private_registries {
url = var.registry_url
user = var.registry_username
password = var.registry_password
is_default = var.registry_activate
}
upgrade_strategy {
drain = var.drain_on_upgrade
max_unavailable_controlplane = var.upgrade_max_unavailable_controlplane
max_unavailable_worker = var.upgrade_max_unavailable_worker
drain_input {
delete_local_data = var.delete_local_data_on_drain
force = var.force_drain
grace_period = var.drain_grace_period
ignore_daemon_sets = var.ignore_daemon_sets_on_drain
timeout = var.drain_timeout
}
}
dynamic "nodes" {
for_each = flatten(
[
for name, node in var.nodes : [
for n in node : {
ip = n.ip
name = name
role = n.type
labels = can(n.labels) ? n.labels : {}
taints = can(n.taints) ? n.taints : []
}
]
]
)
content {
address = nodes.value.ip
hostname_override = lower(nodes.value.name)
internal_address = nodes.value.ip
labels = nodes.value.labels
node_name = nodes.value.name
role = nodes.value.role
ssh_key = var.private_key
user = var.node_user
dynamic "taints" {
for_each = flatten(
[
for taint in nodes.value.taints : {
key = lookup(taint, "key", "")
value = lookup(taint, "value", "")
effect = lookup(taint, "effect", "")
}
]
)
content {
key = taints.value.key
value = taints.value.value
effect = taints.value.effect
}
}
}
}
# Cloud Provider vSphere In-tree
dynamic "cloud_provider" {
for_each = var.cloud_provider_vsphere_in_tree
content {
name = lookup(cloud_provider.value, "type", null)
vsphere_cloud_provider {
virtual_center {
name = lookup(cloud_provider.value, "server_name", null)
user = lookup(cloud_provider.value, "user", null)
password = lookup(cloud_provider.value, "password", null)
port = lookup(cloud_provider.value, "port", null)
datacenters = lookup(cloud_provider.value, "datacenters", null)
}
workspace {
datacenter = lookup(cloud_provider.value, "datacenters", null)
server = lookup(cloud_provider.value, "server_name", null)
default_datastore = lookup(cloud_provider.value, "default_datastore", null)
folder = lookup(cloud_provider.value, "folder", null)
resourcepool_path = lookup(cloud_provider.value, "resourcepool_path", null)
}
global {
insecure_flag = lookup(cloud_provider.value, "insecure_flag", null)
}
}
}
}
# kubernetes services
services {
## etcd
dynamic "etcd" {
for_each = var.external_etcd ? [1] : []
content {
extra_args = local.etcd_extra_args
extra_binds = var.etcd_extra_binds
extra_env = var.etcd_extra_env
backup_config {
interval_hours = var.etcd_backup_interval_hours
retention = var.etcd_backup_retention
s3_backup_config {
access_key = var.etcd_s3_access_key
bucket_name = var.etcd_s3_bucket_name
endpoint = var.etcd_s3_endpoint
folder = var.etcd_s3_folder
region = var.etcd_s3_region
secret_key = var.etcd_s3_secret_key
}
}
}
}
## api-server
kube_api {
always_pull_images = var.always_pull_images
extra_args = var.kube_api_extra_args
extra_binds = var.kube_api_extra_binds
extra_env = var.kube_api_extra_env
pod_security_policy = var.pod_security_policy
service_cluster_ip_range = var.service_cluster_ip_range
service_node_port_range = var.service_node_port_range
audit_log {
enabled = true
configuration {
format = "json"
max_age = 30
max_backup = 5
max_size = 10
path = "/var/log/kube-audit/audit-log.json"
policy = jsonencode(
{
apiVersion = "audit.k8s.io/v1"
kind = "Policy"
rules = [
{
level = "Metadata"
},
]
}
)
}
}
secrets_encryption_config {
enabled = true
}
}
## controller
kube_controller {
cluster_cidr = var.cluster_cidr
extra_args = local.kube_controller_extra_args
extra_binds = var.kube_controller_extra_binds
extra_env = var.kube_controller_extra_env
service_cluster_ip_range = var.service_cluster_ip_range
}
## kubelet
kubelet {
cluster_dns_server = cidrhost(var.service_cluster_ip_range, 10)
cluster_domain = var.cluster_domain
extra_args = local.kubelet_extra_args
extra_binds = var.kubelet_extra_binds
extra_env = var.kubelet_extra_env
fail_swap_on = var.fail_swap_on
generate_serving_certificate = var.generate_serving_certificate
}
## kube-proxy
kubeproxy {
extra_args = local.kubeproxy_extra_args
extra_binds = var.kubeproxy_extra_binds
extra_env = var.kubeproxy_extra_env
}
## scheduler
scheduler {
extra_args = var.scheduler_extra_args
extra_binds = var.scheduler_extra_binds
extra_env = var.scheduler_extra_env
}
}
lifecycle {
ignore_changes = [services]
}
}
resource "local_sensitive_file" "kube_cluster_yaml" {
# Workaround: https://github.com/rancher/rke/issues/705
count = var.write_kubeconfig ? 1 : 0
file_permission = "0600"
filename = format("%s/%s", path.root, "kube_config_cluster.yaml")
content = replace(rke_cluster.cluster.kube_config_yaml, local.api_access_regex, local.api_access)
}
resource "local_sensitive_file" "cluster_yaml" {
count = var.write_cluster_yaml ? 1 : 0
file_permission = "0644"
filename = format("%s/%s", path.root, "cluster.yaml")
content = rke_cluster.cluster.rke_cluster_yaml
}
resource "helm_release" "calico" {
count = var.install_calico ? 1 : 0
depends_on = [local_sensitive_file.kube_cluster_yaml, rke_cluster.cluster]
name = "calico"
repository = "https://docs.tigera.io/calico/charts"
chart = "tigera-operator"
version = local.calico_version
namespace = coalesce(var.namespace_calico, local.namespace_calico)
create_namespace = true
timeout = 300
atomic = true
replace = local.check_rancher1_19 ? true : false
values = [
yamlencode({
installation = {
cni = {
type = "Calico"
ipam = {
type = "Calico"
}
}
calicoNetwork = {
bgp = "Disabled"
ipPools = [{
cidr = var.cluster_cidr
encapsulation = "VXLAN"
natOutgoing = "Enabled"
nodeSelector = "all()"
blockSize = var.node_cidr_mask_size
}]
}
}
})
]
}
resource "helm_release" "argocd" {
count = var.install_argocd ? 1 : 0
depends_on = [helm_release.calico, local_sensitive_file.kube_cluster_yaml]
name = "argocd"
repository = "https://argoproj.github.io/argo-helm"
chart = "argo-cd"
version = local.argocd_version
namespace = "argo-cd"
create_namespace = true
timeout = 240
set {
name = "server.extraArgs"
value = "{--insecure,--request-timeout='5m'}"
}
}
resource "null_resource" "node_cleanup" {
for_each = var.nodes
triggers = {
node_user = var.node_user
private_key = var.private_key
node_name = each.key
node_ip = each.value[0].ip
}
connection {
user = self.triggers.node_user
host = self.triggers.node_ip
private_key = self.triggers.private_key
}
provisioner "file" {
when = destroy
source = "${path.module}/files/rkecleanup.bash"
destination = "/tmp/cleanup.bash"
}
provisioner "remote-exec" {
when = destroy
inline = [
"chmod +x /tmp/cleanup.bash && /tmp/cleanup.bash -f -i", "shutdown -r"
]
}
}
resource "null_resource" "nodes_taint" {
count = local.install_cp_vsphere
provisioner "local-exec" {
command = "kubectl --kubeconfig $KUBECONFIG taint nodes --all=true node.cloudprovider.kubernetes.io/uninitialized=true:NoSchedule"
environment = {
KUBECONFIG = local_sensitive_file.kube_cluster_yaml.0.filename
}
}
depends_on = [helm_release.calico, local_sensitive_file.kube_cluster_yaml]
}