-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
434 lines (368 loc) · 18.9 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
##############################################################################
# base-ocp-vpc-module
# Deploy Openshift cluster in IBM Cloud on VPC Gen 2
##############################################################################
# Segregate pools, as we need default pool for cluster creation
locals {
# ibm_container_vpc_cluster automatically names default pool "default" (See https://github.com/IBM-Cloud/terraform-provider-ibm/issues/2849)
default_pool = element([for pool in var.worker_pools : pool if pool.pool_name == "default"], 0)
other_pools = [for pool in var.worker_pools : pool if pool.pool_name != "default" && !var.ignore_worker_pool_size_changes]
other_autoscaling_pools = [for pool in var.worker_pools : pool if pool.pool_name != "default" && var.ignore_worker_pool_size_changes]
latest_ocp_version = "${data.ibm_container_cluster_versions.cluster_versions.valid_openshift_versions[length(data.ibm_container_cluster_versions.cluster_versions.valid_openshift_versions) - 1]}_openshift"
default_ocp_version = "${data.ibm_container_cluster_versions.cluster_versions.default_openshift_version}_openshift"
ocp_version = var.ocp_version == null || var.ocp_version == "default" ? local.default_ocp_version : (var.ocp_version == "latest" ? local.latest_ocp_version : "${var.ocp_version}_openshift")
cos_name = var.use_existing_cos == true || (var.use_existing_cos == false && var.cos_name != null) ? var.cos_name : "${var.cluster_name}_cos"
cos_location = "global"
cos_plan = "standard"
# if not enable_registry_storage then set cos to 'null', otherwise use existing or new CRN
cos_instance_crn = var.enable_registry_storage == true ? (var.use_existing_cos != false ? var.existing_cos_id : module.cos_instance[0].cos_instance_id) : null
# Validation approach based on https://stackoverflow.com/a/66682419
validate_condition = var.enable_registry_storage == true && var.use_existing_cos == true && var.existing_cos_id == null
validate_msg = "A value for 'existing_cos_id' variable must be passed when 'use_existing_cos = true'"
# tflint-ignore: terraform_unused_declarations
validate_check = regex("^${local.validate_msg}$", (!local.validate_condition ? local.validate_msg : ""))
csi_driver_version = [
for addon in data.ibm_container_addons.existing_addons.addons :
addon.version if addon.name == "vpc-block-csi-driver"
]
addons_list = var.addons != null ? { for k, v in var.addons : k => v if v != null } : {}
addons = lookup(local.addons_list, "vpc-block-csi-driver", null) == null ? merge(local.addons_list, { vpc-block-csi-driver = local.csi_driver_version[0] }) : local.addons_list
delete_timeout = "2h"
create_timeout = "3h"
update_timeout = "3h"
cluster_id = var.ignore_worker_pool_size_changes ? ibm_container_vpc_cluster.autoscaling_cluster[0].id : ibm_container_vpc_cluster.cluster[0].id
}
# Lookup the current default kube version
data "ibm_container_cluster_versions" "cluster_versions" {
resource_group_id = var.resource_group_id
}
module "cos_instance" {
count = var.enable_registry_storage && !var.use_existing_cos ? 1 : 0
source = "terraform-ibm-modules/cos/ibm"
version = "7.1.4"
cos_instance_name = local.cos_name
resource_group_id = var.resource_group_id
cos_plan = local.cos_plan
cos_location = local.cos_location
kms_encryption_enabled = false
create_cos_bucket = false
}
moved {
from = ibm_resource_instance.cos_instance[0]
to = module.cos_instance[0].ibm_resource_instance.cos_instance[0]
}
resource "ibm_resource_tag" "cos_access_tag" {
count = var.enable_registry_storage && !var.use_existing_cos && length(var.access_tags) > 0 ? 1 : 0
resource_id = module.cos_instance[0].cos_instance_id
tags = var.access_tags
tag_type = "access"
}
##############################################################################
# Create a OCP Cluster
##############################################################################
resource "ibm_container_vpc_cluster" "cluster" {
depends_on = [null_resource.reset_api_key]
count = var.ignore_worker_pool_size_changes ? 0 : 1
name = var.cluster_name
vpc_id = var.vpc_id
tags = var.tags
kube_version = local.ocp_version
flavor = local.default_pool.machine_type
entitlement = var.ocp_entitlement
cos_instance_crn = local.cos_instance_crn
worker_count = local.default_pool.workers_per_zone
resource_group_id = var.resource_group_id
wait_till = var.cluster_ready_when
force_delete_storage = var.force_delete_storage
disable_public_service_endpoint = var.disable_public_endpoint
worker_labels = local.default_pool.labels
crk = local.default_pool.boot_volume_encryption_kms_config == null ? null : local.default_pool.boot_volume_encryption_kms_config.crk
kms_instance_id = local.default_pool.boot_volume_encryption_kms_config == null ? null : local.default_pool.boot_volume_encryption_kms_config.kms_instance_id
kms_account_id = local.default_pool.boot_volume_encryption_kms_config == null ? null : local.default_pool.boot_volume_encryption_kms_config.kms_account_id
lifecycle {
ignore_changes = [kube_version]
}
# default workers are mapped to the subnets that are "private"
dynamic "zones" {
for_each = local.default_pool.subnet_prefix != null ? var.vpc_subnets[local.default_pool.subnet_prefix] : local.default_pool.vpc_subnets
content {
subnet_id = zones.value.id
name = zones.value.zone
}
}
# Apply taints to the default worker pools i.e private
dynamic "taints" {
for_each = var.worker_pools_taints == null ? [] : concat(var.worker_pools_taints["all"], var.worker_pools_taints["default"])
content {
effect = taints.value.effect
key = taints.value.key
value = taints.value.value
}
}
dynamic "kms_config" {
for_each = var.kms_config != null ? [1] : []
content {
crk_id = var.kms_config.crk_id
instance_id = var.kms_config.instance_id
private_endpoint = var.kms_config.private_endpoint == null ? true : var.kms_config.private_endpoint
account_id = var.kms_config.account_id
}
}
timeouts {
# Extend create, update and delete timeout to static values.
delete = local.delete_timeout
create = local.create_timeout
update = local.update_timeout
}
}
# copy of the cluster resource above which ignores changes to the worker pool for use in autoscaling scenarios
resource "ibm_container_vpc_cluster" "autoscaling_cluster" {
count = var.ignore_worker_pool_size_changes ? 1 : 0
name = var.cluster_name
vpc_id = var.vpc_id
tags = var.tags
kube_version = local.ocp_version
flavor = local.default_pool.machine_type
entitlement = var.ocp_entitlement
cos_instance_crn = local.cos_instance_crn
worker_count = local.default_pool.workers_per_zone
resource_group_id = var.resource_group_id
wait_till = var.cluster_ready_when
force_delete_storage = var.force_delete_storage
disable_public_service_endpoint = var.disable_public_endpoint
worker_labels = local.default_pool.labels
crk = local.default_pool.boot_volume_encryption_kms_config == null ? null : local.default_pool.boot_volume_encryption_kms_config.crk
kms_instance_id = local.default_pool.boot_volume_encryption_kms_config == null ? null : local.default_pool.boot_volume_encryption_kms_config.kms_instance_id
kms_account_id = local.default_pool.boot_volume_encryption_kms_config == null ? null : local.default_pool.boot_volume_encryption_kms_config.kms_account_id
lifecycle {
ignore_changes = [worker_count, kube_version]
}
# default workers are mapped to the subnets that are "private"
dynamic "zones" {
for_each = local.default_pool.subnet_prefix != null ? var.vpc_subnets[local.default_pool.subnet_prefix] : local.default_pool.vpc_subnets
content {
subnet_id = zones.value.id
name = zones.value.zone
}
}
# Apply taints to the default worker pools i.e private
dynamic "taints" {
for_each = var.worker_pools_taints == null ? [] : concat(var.worker_pools_taints["all"], var.worker_pools_taints["default"])
content {
effect = taints.value.effect
key = taints.value.key
value = taints.value.value
}
}
dynamic "kms_config" {
for_each = var.kms_config != null ? [1] : []
content {
crk_id = var.kms_config.crk_id
instance_id = var.kms_config.instance_id
private_endpoint = var.kms_config.private_endpoint
account_id = var.kms_config.account_id
}
}
timeouts {
# Extend create, update and delete timeout to static values.
delete = local.delete_timeout
create = local.create_timeout
update = local.update_timeout
}
}
##############################################################################
# Cluster Access Tag
##############################################################################
resource "ibm_resource_tag" "cluster_access_tag" {
count = length(var.access_tags) == 0 ? 0 : 1
resource_id = var.ignore_worker_pool_size_changes ? ibm_container_vpc_cluster.autoscaling_cluster[0].crn : ibm_container_vpc_cluster.cluster[0].crn
tags = var.access_tags
tag_type = "access"
}
# Cluster provisioning will automatically create an IAM API key called "containers-kubernetes-key" if one does not exist
# for the given region and resource group. The API key is used to access several services, such as the IBM Cloud classic
# infrastructure portfolio, and is required to manage the cluster. Immediately after the IAM API key is created and
# added to the new resource group, it is replicated across IAM Cloudant instances. There is a small period of time from
# when the IAM API key is initially created and when it is fully replicated across Cloudant instances where the API key
# does not work because it is not fully replicated, so commands that require the API key may fail with 404.
#
# WORKAROUND:
# Run a script that checks if an IAM API key already exists for the given region and resource group, and if it does not,
# run the ibmcloud ks api-key reset command to create one. The script will then pause for some time to allow any IAM
# Cloudant replication to occur. By doing this, it means the cluster provisioning process will not attempt to create a
# new key, and simply use the key created by this script. So hence should not face 404s anymore.
# The IKS team are tracking internally https://github.ibm.com/alchemy-containers/armada-ironsides/issues/5023
resource "null_resource" "reset_api_key" {
provisioner "local-exec" {
command = "${path.module}/scripts/reset_iks_api_key.sh ${var.region} ${var.resource_group_id}"
interpreter = ["/bin/bash", "-c"]
environment = {
IBMCLOUD_API_KEY = var.ibmcloud_api_key
}
}
}
##############################################################################
# Access cluster to kick off RBAC synchronisation
##############################################################################
data "ibm_container_cluster_config" "cluster_config" {
count = var.verify_worker_network_readiness ? 1 : 0
cluster_name_id = local.cluster_id
config_dir = "${path.module}/kubeconfig"
resource_group_id = var.resource_group_id
endpoint_type = var.cluster_config_endpoint_type != "default" ? var.cluster_config_endpoint_type : null # null value represents default
}
##############################################################################
# Worker Pools
##############################################################################
resource "ibm_container_vpc_worker_pool" "pool" {
for_each = { for pool in local.other_pools : pool.pool_name => pool }
vpc_id = var.vpc_id
resource_group_id = var.resource_group_id
cluster = local.cluster_id
worker_pool_name = each.value.pool_name
flavor = each.value.machine_type
worker_count = each.value.workers_per_zone
labels = each.value.labels
crk = each.value.boot_volume_encryption_kms_config == null ? null : each.value.boot_volume_encryption_kms_config.crk
kms_instance_id = each.value.boot_volume_encryption_kms_config == null ? null : each.value.boot_volume_encryption_kms_config.kms_instance_id
kms_account_id = each.value.boot_volume_encryption_kms_config == null ? null : each.value.boot_volume_encryption_kms_config.kms_account_id
dynamic "zones" {
for_each = each.value.subnet_prefix != null ? var.vpc_subnets[each.value.subnet_prefix] : each.value.vpc_subnets
content {
subnet_id = zones.value.id
name = zones.value.zone
}
}
# Apply taints to worker pools i.e. other_pools
dynamic "taints" {
for_each = var.worker_pools_taints == null ? [] : concat(var.worker_pools_taints["all"], lookup(var.worker_pools_taints, each.value["pool_name"], []))
content {
effect = taints.value.effect
key = taints.value.key
value = taints.value.value
}
}
timeouts {
# Extend create and delete timeout to 2h
delete = "2h"
create = "2h"
}
}
# copy of the pool resource above which ignores changes to the worker pool for use in autoscaling scenarios
resource "ibm_container_vpc_worker_pool" "autoscaling_pool" {
for_each = { for pool in local.other_autoscaling_pools : pool.pool_name => pool }
vpc_id = var.vpc_id
resource_group_id = var.resource_group_id
cluster = local.cluster_id
worker_pool_name = each.value.pool_name
flavor = each.value.machine_type
worker_count = each.value.workers_per_zone
labels = each.value.labels
crk = each.value.boot_volume_encryption_kms_config == null ? null : each.value.boot_volume_encryption_kms_config.crk
kms_instance_id = each.value.boot_volume_encryption_kms_config == null ? null : each.value.boot_volume_encryption_kms_config.kms_instance_id
kms_account_id = each.value.boot_volume_encryption_kms_config == null ? null : each.value.boot_volume_encryption_kms_config.kms_account_id
lifecycle {
ignore_changes = [worker_count]
}
dynamic "zones" {
for_each = each.value.subnet_prefix != null ? var.vpc_subnets[each.value.subnet_prefix] : each.value.vpc_subnets
content {
subnet_id = zones.value.id
name = zones.value.zone
}
}
# Apply taints to worker pools i.e. other_pools
dynamic "taints" {
for_each = var.worker_pools_taints == null ? [] : concat(var.worker_pools_taints["all"], lookup(var.worker_pools_taints, each.value["pool_name"], []))
content {
effect = taints.value.effect
key = taints.value.key
value = taints.value.value
}
}
}
##############################################################################
# Confirm network healthy by ensuring master can communicate with all workers.
#
# Please note:
# The network health check is applicable only if the cluster is accessible.
#
# To do this, we run a script to execute "kubectl logs" against each calico
# daemonset pod (as there will be one pod per node) and ensure it passes.
#
# Why?
# There can be a delay in getting the routes set up for the VPN that lets
# the master connect across accounts down to the workers, and that VPN
# connection is what is used by "kubectl logs".
#
# Why is there a delay?
# The network microservice has to trigger on new workers being created and
# push down an updated vpn config, and then the vpn server and client need
# to pick up this updated config. Depending on how busy the network
# microservice is handling requests, there might be a delay.
##############################################################################
resource "null_resource" "confirm_network_healthy" {
count = var.verify_worker_network_readiness ? 1 : 0
# Worker pool creation can start before the 'ibm_container_vpc_cluster' completes since there is no explicit
# depends_on in 'ibm_container_vpc_worker_pool', just an implicit depends_on on the cluster ID. Cluster ID can exist before
# 'ibm_container_vpc_cluster' completes, so hence need to add explicit depends on against 'ibm_container_vpc_cluster' here.
depends_on = [ibm_container_vpc_cluster.cluster, ibm_container_vpc_worker_pool.pool, ibm_container_vpc_worker_pool.autoscaling_pool]
provisioner "local-exec" {
command = "${path.module}/scripts/confirm_network_healthy.sh"
interpreter = ["/bin/bash", "-c"]
environment = {
KUBECONFIG = data.ibm_container_cluster_config.cluster_config[0].config_file_path
}
}
}
# Lookup the current default csi-driver version
data "ibm_container_addons" "existing_addons" {
cluster = local.cluster_id
}
resource "ibm_container_addons" "addons" {
# Worker pool creation can start before the 'ibm_container_vpc_cluster' completes since there is no explicit
# depends_on in 'ibm_container_vpc_worker_pool', just an implicit depends_on on the cluster ID. Cluster ID can exist before
# 'ibm_container_vpc_cluster' completes, so hence need to add explicit depends on against 'ibm_container_vpc_cluster' here.
depends_on = [ibm_container_vpc_cluster.cluster, ibm_container_vpc_worker_pool.pool, ibm_container_vpc_worker_pool.autoscaling_pool, null_resource.confirm_network_healthy]
cluster = local.cluster_id
resource_group_id = var.resource_group_id
# setting to false means we do not want Terraform to manage addons that are managed elsewhere
manage_all_addons = var.manage_all_addons
dynamic "addons" {
for_each = local.addons
content {
name = addons.key
version = addons.value
}
}
timeouts {
create = "1h"
}
}
resource "time_sleep" "wait_operators" {
depends_on = [ibm_container_addons.addons]
create_duration = "5s"
}
locals {
worker_pool_config = [
for worker in var.worker_pools :
{
name = worker.pool_name
minSize = worker.minSize
maxSize = worker.maxSize
enabled = worker.enableAutoscaling
} if worker.enableAutoscaling != null && worker.minSize != null && worker.maxSize != null
]
}
resource "kubernetes_config_map_v1_data" "set_autoscaling" {
count = !(var.disable_public_endpoint) && lookup(local.addons_list, "cluster-autoscaler", null) != null ? 1 : 0
depends_on = [time_sleep.wait_operators]
metadata {
name = "iks-ca-configmap"
namespace = "kube-system"
}
data = {
"workerPoolsConfig.json" = jsonencode(local.worker_pool_config)
}
force = true
}