-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmain.tf
More file actions
812 lines (691 loc) · 39.2 KB
/
main.tf
File metadata and controls
812 lines (691 loc) · 39.2 KB
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
##############################################################################
# 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)
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}_openshift"
valid_versions_list = data.ibm_container_cluster_versions.cluster_versions.valid_openshift_versions
valid_ocp_versions = [for version in local.valid_versions_list : regex("^([0-9]+\\.[0-9]+)", version)[0]]
cos_name = var.use_existing_cos == true || (var.use_existing_cos == false && var.cos_name != null) ? var.cos_name : "${var.cluster_name}_cos"
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
# tflint-ignore: terraform_unused_declarations
cluster_with_upgrade_id = var.ignore_worker_pool_size_changes ? try(ibm_container_vpc_cluster.autoscaling_cluster_with_upgrade[0].id, null) : try(ibm_container_vpc_cluster.cluster_with_upgrade[0].id, null)
# tflint-ignore: terraform_unused_declarations
cluster_without_upgrade_id = var.ignore_worker_pool_size_changes ? try(ibm_container_vpc_cluster.autoscaling_cluster[0].id, null) : try(ibm_container_vpc_cluster.cluster[0].id, null)
cluster_id = var.enable_openshift_version_upgrade ? local.cluster_with_upgrade_id : local.cluster_without_upgrade_id
# tflint-ignore: terraform_unused_declarations
cluster_with_upgrade_crn = var.ignore_worker_pool_size_changes ? try(ibm_container_vpc_cluster.autoscaling_cluster_with_upgrade[0].crn, null) : try(ibm_container_vpc_cluster.cluster_with_upgrade[0].crn, null)
# tflint-ignore: terraform_unused_declarations
cluster_without_upgrade_crn = var.ignore_worker_pool_size_changes ? try(ibm_container_vpc_cluster.autoscaling_cluster[0].crn, null) : try(ibm_container_vpc_cluster.cluster[0].crn, null)
cluster_crn = var.enable_openshift_version_upgrade ? local.cluster_with_upgrade_crn : local.cluster_without_upgrade_crn
# security group attached to worker pool
# the terraform provider / iks api take a security group id hardcoded to "cluster", so this pseudo-value is injected into the array based on attach_default_cluster_security_group
# see https://cloud.ibm.com/docs/openshift?topic=openshift-vpc-security-group&interface=ui#vpc-sg-cluster
# attach_ibm_managed_security_group is true and custom_security_group_ids is not set => default behavior, so set to null
# attach_ibm_managed_security_group is true and custom_security_group_ids is set => add "cluster" to the list of custom security group ids
# attach_ibm_managed_security_group is false and custom_security_group_ids is not set => default behavior, so set to null
# attach_ibm_managed_security_group is false and custom_security_group_ids is set => only use the custom security group ids
cluster_security_groups = var.attach_ibm_managed_security_group == true ? (var.custom_security_group_ids == null ? null : concat(["cluster"], var.custom_security_group_ids)) : (var.custom_security_group_ids == null ? null : var.custom_security_group_ids)
# for versions older than 4.15, this value must be null, or provider gives error
disable_outbound_traffic_protection = startswith(local.ocp_version, "4.14") ? null : var.disable_outbound_traffic_protection
binaries_path = "/tmp"
}
########################################################################################################################
# Get OCP addon versions
########################################################################################################################
data "ibm_iam_auth_token" "tokendata" {}
data "external" "ocp_addon_versions" {
program = ["python3", "${path.module}/scripts/get_ocp_addon_versions.py"]
query = {
IAM_TOKEN = sensitive(data.ibm_iam_auth_token.tokendata.iam_access_token)
REGION = var.region
}
}
# Local block to decode the json strings returned by the external data source
locals {
ocp_all_addon_versions = {
for addon, value in data.external.ocp_addon_versions.result :
addon => jsondecode(value)
}
}
# Local block to verify validations for OCP AI Addon.
locals {
# retrieve worker specs (CPU & RAM) for all worker pools
worker_specs = {
for pool in var.worker_pools :
pool.pool_name => {
cpu_count = tonumber(regex("^.*?(\\d+)x(\\d+)", pool.machine_type)[0])
ram_count = tonumber(regex("^.*?(\\d+)x(\\d+)", pool.machine_type)[1])
is_gpu = contains(["gx2", "gx3", "gx4"], split(".", pool.machine_type)[0])
}
}
}
# Separate local block to handle os validations
locals {
os_rhel = "REDHAT_8_64"
os_rhcos = "RHCOS"
os_rhel9 = "RHEL_9_64"
# Strip OCP VERSION and use this ocp version in logic
ocp_version_num = regex("^([0-9]+\\.[0-9]+)", local.ocp_version)[0]
is_valid_version = local.ocp_version_num != null ? tonumber(local.ocp_version_num) >= 4.15 : false
rhcos_allowed_ocp_version = local.default_pool.operating_system == local.os_rhcos && local.is_valid_version
worker_pool_rhcos_entry = [for worker in var.worker_pools : contains([local.os_rhel, local.os_rhel9], worker.operating_system) || (worker.operating_system == local.os_rhcos && local.is_valid_version) ? true : false]
# To verify rhcos operating system exists only for OCP versions >=4.15
# tflint-ignore: terraform_unused_declarations
cluster_rhcos_validation = contains([local.os_rhel9, local.os_rhel], local.default_pool.operating_system) || local.rhcos_allowed_ocp_version ? true : tobool("RHCOS requires VPC clusters created from 4.15 onwards. Upgraded clusters from 4.14 cannot use RHCOS")
# tflint-ignore: terraform_unused_declarations
worker_pool_rhcos_validation = alltrue(local.worker_pool_rhcos_entry) ? true : tobool("RHCOS requires VPC clusters created from 4.15 onwards. Upgraded clusters from 4.14 cannot use RHCOS")
# Validate if default worker pool's operating system is RHEL, all pools' operating system must be RHEL
rhel_check_for_all_standalone_pools = [for pool in var.worker_pools : contains([local.os_rhel, local.os_rhel9], pool.operating_system) if pool.pool_name != "default"]
# tflint-ignore: terraform_unused_declarations
valid_rhel_worker_pools = tonumber(local.ocp_version_num) < 4.18 ? local.default_pool.operating_system == local.os_rhcos || (contains([local.os_rhel, local.os_rhel9], local.default_pool.operating_system) && alltrue(local.rhel_check_for_all_standalone_pools)) ? true : tobool("Choosing RHEL for the default worker pool will limit all additional worker pools to RHEL.") : true
# Validate if RHCOS is used as operating system for the cluster then the default worker pool must be created with RHCOS
rhcos_check = contains([local.os_rhel, local.os_rhel9], local.default_pool.operating_system) || (local.default_pool.operating_system == local.os_rhcos && local.default_pool.operating_system == local.os_rhcos)
# tflint-ignore: terraform_unused_declarations
default_wp_validation = local.rhcos_check ? true : tobool("If RHCOS is used with this cluster, the default worker pool should be created with RHCOS.")
}
resource "terraform_data" "install_required_binaries" {
count = var.install_required_binaries && (var.verify_worker_network_readiness || var.enable_ocp_console != null || lookup(var.addons, "cluster-autoscaler", null) != null) ? 1 : 0
triggers_replace = {
verify_worker_network_readiness = var.verify_worker_network_readiness
cluster_autoscaler = lookup(var.addons, "cluster-autoscaler", null) != null
enable_ocp_console = var.enable_ocp_console
}
provisioner "local-exec" {
# Using the script from the kube-audit module to avoid code duplication.
command = "${path.module}/modules/kube-audit/scripts/install-binaries.sh ${local.binaries_path}"
interpreter = ["/bin/bash", "-c"]
}
}
# Lookup the current default kube version
data "ibm_container_cluster_versions" "cluster_versions" {}
module "cos_instance" {
count = var.enable_registry_storage && !var.use_existing_cos ? 1 : 0
source = "terraform-ibm-modules/cos/ibm"
version = "10.14.9"
cos_instance_name = local.cos_name
resource_group_id = var.resource_group_id
cos_plan = local.cos_plan
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" {
count = var.enable_openshift_version_upgrade ? 0 : (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
secondary_storage = local.default_pool.secondary_storage
pod_subnet = var.pod_subnet_cidr
service_subnet = var.service_subnet_cidr
operating_system = local.default_pool.operating_system
disable_public_service_endpoint = var.disable_public_endpoint
worker_labels = local.default_pool.labels
disable_outbound_traffic_protection = local.disable_outbound_traffic_protection
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
security_groups = local.cluster_security_groups
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
wait_for_apply = var.kms_config.wait_for_apply
}
}
timeouts {
# Extend create, update and delete timeout to static values.
delete = var.cluster_delete_timeout
create = var.cluster_create_timeout
update = var.cluster_update_timeout
}
}
# copy of the cluster resource above which allows major openshift version upgrade
resource "ibm_container_vpc_cluster" "cluster_with_upgrade" {
count = var.enable_openshift_version_upgrade ? (var.ignore_worker_pool_size_changes ? 0 : 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
secondary_storage = local.default_pool.secondary_storage
pod_subnet = var.pod_subnet_cidr
service_subnet = var.service_subnet_cidr
operating_system = local.default_pool.operating_system
disable_public_service_endpoint = var.disable_public_endpoint
worker_labels = local.default_pool.labels
disable_outbound_traffic_protection = local.disable_outbound_traffic_protection
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
security_groups = local.cluster_security_groups
# This resource intentionally omits ignore_changes for kube_version to allow major version upgrades
# 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
wait_for_apply = var.kms_config.wait_for_apply
}
}
timeouts {
# Extend create, update and delete timeout to static values.
delete = var.cluster_delete_timeout
create = var.cluster_create_timeout
update = var.cluster_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.enable_openshift_version_upgrade ? 0 : (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
operating_system = local.default_pool.operating_system
secondary_storage = local.default_pool.secondary_storage
pod_subnet = var.pod_subnet_cidr
service_subnet = var.service_subnet_cidr
disable_public_service_endpoint = var.disable_public_endpoint
worker_labels = local.default_pool.labels
disable_outbound_traffic_protection = local.disable_outbound_traffic_protection
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
security_groups = local.cluster_security_groups
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
wait_for_apply = var.kms_config.wait_for_apply
}
}
timeouts {
# Extend create, update and delete timeout to static values.
delete = var.cluster_delete_timeout
create = var.cluster_create_timeout
update = var.cluster_update_timeout
}
}
# copy of the cluster resource above which allows major openshift version upgrade
resource "ibm_container_vpc_cluster" "autoscaling_cluster_with_upgrade" {
count = var.enable_openshift_version_upgrade ? (var.ignore_worker_pool_size_changes ? 1 : 0) : 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
operating_system = local.default_pool.operating_system
secondary_storage = local.default_pool.secondary_storage
pod_subnet = var.pod_subnet_cidr
service_subnet = var.service_subnet_cidr
disable_public_service_endpoint = var.disable_public_endpoint
worker_labels = local.default_pool.labels
disable_outbound_traffic_protection = local.disable_outbound_traffic_protection
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
security_groups = local.cluster_security_groups
# This resource intentionally omits ignore_changes for kube_version to allow major version upgrades
lifecycle {
ignore_changes = [worker_count]
}
# 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
wait_for_apply = var.kms_config.wait_for_apply
}
}
timeouts {
# Extend create, update and delete timeout to static values.
delete = var.cluster_delete_timeout
create = var.cluster_create_timeout
update = var.cluster_update_timeout
}
}
##############################################################################
# Cluster Access Tag
##############################################################################
resource "ibm_resource_tag" "cluster_access_tag" {
count = length(var.access_tags) == 0 ? 0 : 1
resource_id = local.cluster_crn
tags = var.access_tags
tag_type = "access"
}
##############################################################################
# Access cluster to kick off RBAC synchronisation
##############################################################################
data "ibm_container_cluster_config" "cluster_config" {
count = var.enable_ocp_console != null || var.verify_worker_network_readiness || lookup(var.addons, "cluster-autoscaler", null) != null ? 1 : 0
cluster_name_id = local.cluster_id
config_dir = "${path.module}/kubeconfig"
admin = true # workaround for https://github.com/terraform-ibm-modules/terraform-ibm-base-ocp-vpc/issues/374
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
}
module "worker_pools" {
source = "./modules/worker-pool"
vpc_id = var.vpc_id
resource_group_id = var.resource_group_id
cluster_id = local.cluster_id
vpc_subnets = var.vpc_subnets
worker_pools = var.worker_pools
ignore_worker_pool_size_changes = var.ignore_worker_pool_size_changes
allow_default_worker_pool_replacement = var.allow_default_worker_pool_replacement
worker_pools_taints = var.worker_pools_taints
}
##############################################################################
# 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 = [terraform_data.install_required_binaries, ibm_container_vpc_cluster.cluster, ibm_container_vpc_cluster.cluster_with_upgrade, ibm_container_vpc_cluster.autoscaling_cluster, ibm_container_vpc_cluster.autoscaling_cluster_with_upgrade, module.worker_pools]
triggers = {
verify_worker_network_readiness = var.verify_worker_network_readiness
}
provisioner "local-exec" {
command = "${path.module}/scripts/confirm_network_healthy.sh ${local.binaries_path}"
interpreter = ["/bin/bash", "-c"]
environment = {
KUBECONFIG = data.ibm_container_cluster_config.cluster_config[0].config_file_path
}
}
}
##############################################################################
# Enable or Disable OCP Console Patch
##############################################################################
resource "null_resource" "ocp_console_management" {
count = var.enable_ocp_console != null ? 1 : 0
depends_on = [terraform_data.install_required_binaries, null_resource.confirm_network_healthy]
triggers = {
enable_ocp_console = var.enable_ocp_console
}
provisioner "local-exec" {
command = "${path.module}/scripts/enable_disable_ocp_console.sh ${local.binaries_path}"
interpreter = ["/bin/bash", "-c"]
environment = {
KUBECONFIG = data.ibm_container_cluster_config.cluster_config[0].config_file_path
ENABLE_OCP_CONSOLE = var.enable_ocp_console
}
}
}
##############################################################################
# Addons
##############################################################################
# Lookup the current default csi-driver version
data "ibm_container_addons" "existing_addons" {
cluster = local.cluster_id
}
locals {
# for each cluster, look for installed csi driver to get version. If array is empty (no csi driver) then null is returned
csi_driver_version = anytrue([for key, value in var.addons : true if key == "vpc-block-csi-driver" && value != null]) ? [var.addons["vpc-block-csi-driver"].version] : [
for addon in data.ibm_container_addons.existing_addons.addons :
addon.version if addon.name == "vpc-block-csi-driver"
]
# get the addons and their versions and create an addons map including the corresponding csi_driver_version
addons = merge(
{ for addon_name, addon_version in(var.addons != null ? var.addons : {}) : addon_name => addon_version if addon_version != null },
length(local.csi_driver_version) > 0 ? { vpc-block-csi-driver = { version = local.csi_driver_version[0] } } : {}
)
}
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_cluster.cluster_with_upgrade, ibm_container_vpc_cluster.autoscaling_cluster, ibm_container_vpc_cluster.autoscaling_cluster_with_upgrade, module.worker_pools, 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 = lookup(addons.value, "version", null)
parameters_json = lookup(addons.value, "parameters_json", null)
}
}
timeouts {
create = "1h"
}
}
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 "null_resource" "config_map_status" {
count = lookup(var.addons, "cluster-autoscaler", null) != null ? 1 : 0
depends_on = [terraform_data.install_required_binaries, ibm_container_addons.addons]
triggers = {
cluster_autoscaler = lookup(var.addons, "cluster-autoscaler", null) != null
}
provisioner "local-exec" {
command = "${path.module}/scripts/get_config_map_status.sh ${local.binaries_path}"
interpreter = ["/bin/bash", "-c"]
environment = {
KUBECONFIG = data.ibm_container_cluster_config.cluster_config[0].config_file_path
}
}
}
resource "kubernetes_config_map_v1_data" "set_autoscaling" {
count = lookup(var.addons, "cluster-autoscaler", null) != null ? 1 : 0
depends_on = [null_resource.config_map_status]
metadata {
name = "iks-ca-configmap"
namespace = "kube-system"
}
data = {
"workerPoolsConfig.json" = jsonencode(local.worker_pool_config)
}
force = true
}
##############################################################################
# Attach additional security groups to the load balancers managed by this
# cluster. Note that the module attaches security group to existing loadbalancer
# only. Re-run the module to attach security groups to new load balancers created
# after the initial run of this module. The module detects new load balancers.
# https://cloud.ibm.com/docs/openshift?topic=openshift-vpc-security-group&interface=ui#vpc-sg-vpe-alb
##############################################################################
data "ibm_is_lbs" "all_lbs" {
depends_on = [ibm_container_vpc_cluster.cluster, ibm_container_vpc_cluster.cluster_with_upgrade, ibm_container_vpc_cluster.autoscaling_cluster, ibm_container_vpc_cluster.autoscaling_cluster_with_upgrade, module.worker_pools, null_resource.confirm_network_healthy]
count = length(var.additional_lb_security_group_ids) > 0 ? 1 : 0
}
locals {
lbs_associated_with_cluster = length(var.additional_lb_security_group_ids) > 0 ? [for lb in data.ibm_is_lbs.all_lbs[0].load_balancers : lb.id if strcontains(lb.name, local.cluster_id)] : []
}
module "attach_sg_to_lb" {
count = length(var.additional_lb_security_group_ids)
source = "terraform-ibm-modules/security-group/ibm"
version = "2.8.9"
existing_security_group_id = var.additional_lb_security_group_ids[count.index]
use_existing_security_group_id = true
target_ids = [for index in range(var.number_of_lbs) : local.lbs_associated_with_cluster[index]] # number_of_lbs is necessary to give a static number of elements to tf to accomplish the apply when the cluster does not initially exists
}
##############################################################################
# Attach additional security groups to the load balancers managed by this
# cluster. Note that the module attaches security group to existing loadbalancer
# only. Re-run the module to attach security groups to new load balancers created
# after the initial run of this module. The module detects new load balancers.
# https://cloud.ibm.com/docs/openshift?topic=openshift-vpc-security-group&interface=ui#vpc-sg-vpe-alb
##############################################################################
locals {
vpes_to_attach_to_sg = {
"master" : "iks-${local.cluster_id}",
"api" : "iks-api-${var.vpc_id}",
"registry" : "iks-registry-${var.vpc_id}"
}
}
data "ibm_is_virtual_endpoint_gateway" "master_vpe" {
count = length(var.additional_vpe_security_group_ids["master"])
depends_on = [ibm_container_vpc_cluster.cluster, ibm_container_vpc_cluster.cluster_with_upgrade, ibm_container_vpc_cluster.autoscaling_cluster, ibm_container_vpc_cluster.autoscaling_cluster_with_upgrade, module.worker_pools, null_resource.confirm_network_healthy]
name = local.vpes_to_attach_to_sg["master"]
}
data "ibm_is_virtual_endpoint_gateway" "api_vpe" {
count = length(var.additional_vpe_security_group_ids["api"])
depends_on = [ibm_container_vpc_cluster.cluster, ibm_container_vpc_cluster.cluster_with_upgrade, ibm_container_vpc_cluster.autoscaling_cluster, ibm_container_vpc_cluster.autoscaling_cluster_with_upgrade, module.worker_pools, null_resource.confirm_network_healthy]
name = local.vpes_to_attach_to_sg["api"]
}
data "ibm_is_virtual_endpoint_gateway" "registry_vpe" {
count = length(var.additional_vpe_security_group_ids["registry"])
depends_on = [ibm_container_vpc_cluster.cluster, ibm_container_vpc_cluster.cluster_with_upgrade, ibm_container_vpc_cluster.autoscaling_cluster, ibm_container_vpc_cluster.autoscaling_cluster_with_upgrade, module.worker_pools, null_resource.confirm_network_healthy]
name = local.vpes_to_attach_to_sg["registry"]
}
locals {
# loading cluster master, cluster API and registry VPE IDs to attach related SGs
master_vpe_id = length(var.additional_vpe_security_group_ids["master"]) > 0 ? data.ibm_is_virtual_endpoint_gateway.master_vpe[0].id : null
api_vpe_id = length(var.additional_vpe_security_group_ids["api"]) > 0 ? data.ibm_is_virtual_endpoint_gateway.api_vpe[0].id : null
registry_vpe_id = length(var.additional_vpe_security_group_ids["registry"]) > 0 ? data.ibm_is_virtual_endpoint_gateway.registry_vpe[0].id : null
}
module "attach_sg_to_master_vpe" {
count = length(var.additional_vpe_security_group_ids["master"])
source = "terraform-ibm-modules/security-group/ibm"
version = "2.8.9"
existing_security_group_id = var.additional_vpe_security_group_ids["master"][count.index]
use_existing_security_group_id = true
target_ids = [local.master_vpe_id]
}
module "attach_sg_to_api_vpe" {
count = length(var.additional_vpe_security_group_ids["api"])
source = "terraform-ibm-modules/security-group/ibm"
version = "2.8.9"
existing_security_group_id = var.additional_vpe_security_group_ids["api"][count.index]
use_existing_security_group_id = true
target_ids = [local.api_vpe_id]
}
module "attach_sg_to_registry_vpe" {
count = length(var.additional_vpe_security_group_ids["registry"])
source = "terraform-ibm-modules/security-group/ibm"
version = "2.8.9"
existing_security_group_id = var.additional_vpe_security_group_ids["registry"][count.index]
use_existing_security_group_id = true
target_ids = [local.registry_vpe_id]
}
##############################################################################
# Context Based Restrictions
##############################################################################
locals {
default_operations = [{
api_types = [
{
"api_type_id" : "crn:v1:bluemix:public:context-based-restrictions::::api-type:"
}
]
}]
}
module "cbr_rule" {
count = length(var.cbr_rules) > 0 ? length(var.cbr_rules) : 0
source = "terraform-ibm-modules/cbr/ibm//modules/cbr-rule-module"
version = "1.35.19"
rule_description = var.cbr_rules[count.index].description
enforcement_mode = var.cbr_rules[count.index].enforcement_mode
rule_contexts = var.cbr_rules[count.index].rule_contexts
resources = [{
attributes = [
{
name = "accountId"
value = var.cbr_rules[count.index].account_id
operator = "stringEquals"
},
{
name = "serviceInstance"
value = local.cluster_id
operator = "stringEquals"
},
{
name = "serviceName"
value = "containers-kubernetes"
operator = "stringEquals"
}
],
}]
operations = var.cbr_rules[count.index].operations == null ? local.default_operations : var.cbr_rules[count.index].operations
}
##############################################################
# Ingress Secrets Manager Integration
##############################################################
module "existing_secrets_manager_instance_parser" {
count = var.enable_secrets_manager_integration ? 1 : 0
source = "terraform-ibm-modules/common-utilities/ibm//modules/crn-parser"
version = "1.4.2"
crn = var.existing_secrets_manager_instance_crn
}
resource "ibm_iam_authorization_policy" "ocp_secrets_manager_iam_auth_policy" {
count = var.enable_secrets_manager_integration && !var.skip_ocp_secrets_manager_iam_auth_policy ? 1 : 0
depends_on = [ibm_container_vpc_cluster.cluster, ibm_container_vpc_cluster.autoscaling_cluster, ibm_container_vpc_cluster.cluster_with_upgrade, ibm_container_vpc_cluster.autoscaling_cluster_with_upgrade, module.worker_pools]
source_service_name = "containers-kubernetes"
source_resource_instance_id = local.cluster_id
target_service_name = "secrets-manager"
target_resource_instance_id = module.existing_secrets_manager_instance_parser[0].service_instance
roles = ["Manager"]
}
resource "time_sleep" "wait_for_auth_policy" {
count = var.enable_secrets_manager_integration ? 1 : 0
depends_on = [ibm_iam_authorization_policy.ocp_secrets_manager_iam_auth_policy[0]]
create_duration = "30s"
}
resource "ibm_container_ingress_instance" "instance" {
count = var.enable_secrets_manager_integration ? 1 : 0
depends_on = [time_sleep.wait_for_auth_policy]
cluster = var.cluster_name
instance_crn = var.existing_secrets_manager_instance_crn
is_default = true
secret_group_id = var.secrets_manager_secret_group_id
}