@@ -11,10 +11,10 @@ module "vault" {
11
11
}
12
12
13
13
# Oracle Cloud Infrastructure Tenancy and Compartment OCID
14
- tenancy_ocid = var. tenancy_ocid
14
+ tenancy_ocid = var. tenancy_ocid
15
15
16
- # App Details
17
- app_details = local. app_details
16
+ # Deployment Tags + Freeform Tags
17
+ freeform_deployment_tags = local. freeform_deployment_tags
18
18
19
19
# Encryption (OCI Vault/Key Management/KMS)
20
20
use_encryption_from_oci_vault = var. use_encryption_from_oci_vault
@@ -43,15 +43,13 @@ module "oke" {
43
43
compartment_ocid = local. oke_compartment_ocid
44
44
region = var. region
45
45
46
- # App Details
47
- app_details = local. app_details
46
+ # Deployment Tags + Freeform Tags
47
+ freeform_deployment_tags = local. freeform_deployment_tags
48
48
49
49
# OKE Cluster
50
50
# # create_new_oke_cluster
51
- create_new_oke_cluster = var. create_new_oke_cluster
52
- existent_oke_cluster_id = var. existent_oke_cluster_id
53
- # create_new_compartment_for_oke = var.create_new_compartment_for_oke
54
- # oke_compartment_description = var.oke_compartment_description
51
+ create_new_oke_cluster = var. create_new_oke_cluster
52
+ existent_oke_cluster_id = var. existent_oke_cluster_id
55
53
56
54
# # Cluster Workers visibility
57
55
cluster_workers_visibility = var. cluster_workers_visibility
@@ -67,72 +65,65 @@ module "oke" {
67
65
create_compartment_policies = var. create_compartment_policies
68
66
69
67
# # Encryption (OCI Vault/Key Management/KMS)
70
- oci_vault_key_id_oke_secrets = module. vault . oci_vault_key_id
68
+ oci_vault_key_id_oke_secrets = module. vault . oci_vault_key_id
71
69
oci_vault_key_id_oke_image_policy = module. vault . oci_vault_key_id
72
- # use_encryption_from_oci_vault = var.use_encryption_from_oci_vault
73
- # create_new_encryption_key = var.create_new_encryption_key
74
- # existent_encryption_key_id = var.existent_encryption_key_id
75
-
76
- # # Enable Cluster Autoscaler
77
- cluster_autoscaler_enabled = var. cluster_autoscaler_enabled
78
- # cluster_autoscaler_min_nodes = var.cluster_autoscaler_min_nodes
79
- # cluster_autoscaler_max_nodes = var.cluster_autoscaler_max_nodes
80
- # existent_oke_nodepool_id_for_autoscaler = var.existent_oke_nodepool_id_for_autoscaler
81
-
82
- # ## OKE Worker Nodes (Compute)
83
- # num_pool_workers = var.cluster_autoscaler_enabled ? var.cluster_autoscaler_min_nodes : var.num_pool_workers
84
- # node_pool_shape = var.node_pool_instance_shape.instanceShape
85
- # node_pool_node_shape_config_ocpus = var.node_pool_instance_shape.ocpus
86
- # node_pool_node_shape_config_memory_in_gbs = var.node_pool_instance_shape.memory
87
- # generate_public_ssh_key = var.generate_public_ssh_key
88
- # public_ssh_key = var.public_ssh_key
89
70
}
90
71
91
72
module "oke_node_pool" {
92
- source = " ./modules/oke-node-pool"
73
+ for_each = { for map in local . node_pools : map . node_pool_name => map }
74
+ source = " ./modules/oke-node-pool"
93
75
94
- # App Details
95
- app_details = local. app_details
76
+ # Deployment Tags + Freeform Tags
77
+ freeform_deployment_tags = local. freeform_deployment_tags
96
78
97
79
# Oracle Cloud Infrastructure Tenancy and Compartment OCID
98
- tenancy_ocid = var. tenancy_ocid
80
+ tenancy_ocid = var. tenancy_ocid
99
81
100
82
# OKE Cluster Details
101
- oke_cluster_ocid = module. oke . oke_cluster_ocid
83
+ oke_cluster_ocid = module. oke . oke_cluster_ocid
102
84
oke_cluster_compartment_ocid = local. oke_compartment_ocid
85
+ create_new_node_pool = var. create_new_oke_cluster
103
86
104
87
# OKE Worker Nodes (Compute)
105
- num_pool_workers = var. num_pool_workers
106
- node_pool_shape = var. node_pool_instance_shape . instanceShape
107
- node_pool_node_shape_config_ocpus = var. node_pool_instance_shape . ocpus
108
- node_pool_node_shape_config_memory_in_gbs = var. node_pool_instance_shape . memory
109
- generate_public_ssh_key = var. generate_public_ssh_key
110
- public_ssh_key = var. public_ssh_key
88
+ node_pool_name = each. value . node_pool_name
89
+ node_pool_min_nodes = each. value . node_pool_min_nodes
90
+ node_pool_max_nodes = each. value . node_pool_max_nodes
91
+ node_k8s_version = each. value . node_k8s_version
92
+ node_pool_shape = each. value . node_pool_shape
93
+ node_pool_node_shape_config_ocpus = each. value . node_pool_node_shape_config_ocpus
94
+ node_pool_node_shape_config_memory_in_gbs = each. value . node_pool_node_shape_config_memory_in_gbs
95
+ public_ssh_key = local. workers_public_ssh_key
111
96
112
97
# OKE Network Details
113
98
oke_vcn_nodes_subnet_ocid = module. oke . oke_vcn_nodes_subnet_ocid
114
99
115
100
# Encryption (OCI Vault/Key Management/KMS)
116
101
oci_vault_key_id_oke_node_boot_volume = module. vault . oci_vault_key_id
117
102
}
103
+ locals {
104
+ node_pools = [
105
+ {
106
+ node_pool_name = var.node_pool_name != " " ? var.node_pool_name : " pool1" # Must be unique
107
+ node_pool_min_nodes = var.cluster_autoscaler_enabled ? var.cluster_autoscaler_min_nodes_1 : var.num_pool_workers
108
+ node_pool_max_nodes = var.cluster_autoscaler_max_nodes_1
109
+ node_k8s_version = var.k8s_version # TODO: Allow to set different version for each node pool
110
+ node_pool_shape = var.node_pool_instance_shape.instanceShape
111
+ node_pool_node_shape_config_ocpus = var.node_pool_instance_shape.ocpus
112
+ node_pool_node_shape_config_memory_in_gbs = var.node_pool_instance_shape.memory
113
+ node_pool_boot_volume_size_in_gbs = var.node_pool_boot_volume_size_in_gbs
114
+ },
115
+ ]
116
+ }
118
117
119
118
module "oke_cluster_autoscaler" {
120
119
source = " ./modules/oke-cluster-autoscaler"
121
120
122
121
# Oracle Cloud Infrastructure Tenancy and Compartment OCID
123
- # tenancy_ocid = var.tenancy_ocid
124
- # compartment_ocid = var.compartment_ocid
125
122
region = var. region
126
123
127
124
# # Enable Cluster Autoscaler
128
125
cluster_autoscaler_enabled = var. cluster_autoscaler_enabled
129
- # cluster_autoscaler_min_nodes = var.cluster_autoscaler_min_nodes
130
- # cluster_autoscaler_max_nodes = var.cluster_autoscaler_max_nodes
131
- # existent_oke_nodepool_id_for_autoscaler = var.existent_oke_nodepool_id_for_autoscaler
132
- oke_node_pools = var. oke_node_pools
133
-
134
- # # Nodes Kubernetes Version
135
- k8s_version = var. k8s_version
126
+ oke_node_pools = values (module. oke_node_pool )
136
127
137
128
depends_on = [module . oke , module . oke_node_pool ]
138
129
}
@@ -142,14 +133,6 @@ variable "app_name" {
142
133
default = " K8s App"
143
134
description = " Application name. Will be used as prefix to identify resources, such as OKE, VCN, ATP, and others"
144
135
}
145
- variable "app_deployment_environment" {
146
- default = " generic" # e.g.: Development, QA, Stage, ...
147
- description = " Deployment environment for the freeform tags"
148
- }
149
- variable "app_deployment_type" {
150
- default = " generic" # e.g.: App Type 1, App Type 2, Red, Purple, ...
151
- description = " Deployment type for the freeform tags"
152
- }
153
136
variable "create_new_oke_cluster" {
154
137
default = true
155
138
description = " Creates a new OKE cluster, node pool and network resources"
@@ -216,30 +199,14 @@ variable "cluster_autoscaler_enabled" {
216
199
default = true
217
200
description = " Enables OKE cluster autoscaler. Node pools will auto scale based on the resources usage"
218
201
}
219
- variable "oke_node_pools" {
220
- type = list (any )
221
-
222
- default = [
223
- {
224
- node_pool_id = " " # TODO: node pool Id from module
225
- node_pool_min_nodes = 3
226
- node_pool__max_nodes = 10
227
- }
228
- ]
229
- description = " Node pools (id, min_nodes, max_nodes) to use with Cluster Autoscaler"
202
+ variable "cluster_autoscaler_min_nodes_1" {
203
+ default = 3
204
+ description = " Minimum number of nodes on the node pool to be scheduled by the Kubernetes"
205
+ }
206
+ variable "cluster_autoscaler_max_nodes_1" {
207
+ default = 10
208
+ description = " Maximum number of nodes on the node pool to be scheduled by the Kubernetes"
230
209
}
231
- # variable "cluster_autoscaler_min_nodes" {
232
- # default = 3
233
- # description = "Minimum number of nodes on the node pool to be scheduled by the Kubernetes"
234
- # }
235
- # variable "cluster_autoscaler_max_nodes" {
236
- # default = 10
237
- # description = "Maximum number of nodes on the node pool to be scheduled by the Kubernetes"
238
- # }
239
- # variable "existent_oke_nodepool_id_for_autoscaler" {
240
- # default = ""
241
- # description = "Nodepool Id of the existent OKE to use with Cluster Autoscaler"
242
- # }
243
210
244
211
# # OKE Node Pool Details
245
212
variable "node_pool_name" {
@@ -248,7 +215,7 @@ variable "node_pool_name" {
248
215
}
249
216
variable "k8s_version" {
250
217
default = " Latest"
251
- description = " Kubernetes version installed on your master and worker nodes. If not version select, will use the latest available."
218
+ description = " Kubernetes version installed on your Control Plane and worker nodes. If not version select, will use the latest available."
252
219
}
253
220
variable "num_pool_workers" {
254
221
default = 3
@@ -299,31 +266,45 @@ variable "create_compartment_policies" {
299
266
description = " Creates policies that will reside on the compartment. e.g.: Policies to support Cluster Autoscaler, OCI Logging datasource on Grafana"
300
267
}
301
268
269
+ variable "tag_values" {
270
+ type = map (any )
271
+ default = { " freeformTags" = {
272
+ " Environment" = " Development" , # e.g.: Demo, Sandbox, Development, QA, Stage, ...
273
+ " DeploymentType" = " generic" } } # e.g.: App Type 1, App Type 2, Red, Purple, ...
274
+ description = " Use Tagging to add metadata to resources. All resources created by this stack will be tagged with the selected tag values."
275
+ }
276
+
302
277
resource "random_string" "deploy_id" {
303
278
length = 4
304
279
special = false
305
280
}
306
281
307
282
resource "oci_identity_compartment" "oke_compartment" {
308
283
compartment_id = var. compartment_ocid
309
- name = " ${ local . app_details . app_name_normalized } -${ local . deploy_id } "
284
+ name = " ${ local . app_name_normalized } -${ local . deploy_id } "
310
285
description = " ${ var . app_name } ${ var . oke_compartment_description } (Deployment ${ local . deploy_id } )"
311
286
enable_delete = true
312
287
313
288
count = var. create_new_compartment_for_oke ? 1 : 0
314
289
}
315
290
291
+ # Generate ssh keys to access Worker Nodes, if generate_public_ssh_key=true, applies to the pool
292
+ resource "tls_private_key" "oke_worker_node_ssh_key" {
293
+ algorithm = " RSA"
294
+ rsa_bits = 2048
295
+ }
296
+
316
297
# Locals
317
298
locals {
318
- deploy_id = random_string. deploy_id . result
299
+ deploy_id = random_string. deploy_id . result
319
300
oke_compartment_ocid = var. create_new_compartment_for_oke ? oci_identity_compartment. oke_compartment . 0 . id : var. compartment_ocid
320
- app_details = {
321
- " app_name " = var.app_name
322
- " app_name_normalized " = substr ( replace ( lower (var . app_name ), " " , " - " ), 0 , 6 )
323
- " app_deployment_environment " = var.app_deployment_environment
324
- " app_deployment_type " = var.app_deployment_type
325
- " app_deployment_id " = local.deploy_id
326
- }
301
+ deploy_tags = {
302
+ " DeploymentID " = local.deploy_id,
303
+ " AppName " = var.app_name,
304
+ " Quickstart " = " oke_base " }
305
+ freeform_deployment_tags = merge ( var. tag_values . freeformTags , local . deploy_tags )
306
+ workers_public_ssh_key = var . generate_public_ssh_key ? tls_private_key . oke_worker_node_ssh_key . public_key_openssh : var . public_ssh_key
307
+ app_name_normalized = substr ( replace ( lower (var . app_name ), " " , " - " ), 0 , 6 )
327
308
}
328
309
329
310
# OKE Outputs
@@ -341,7 +322,8 @@ output "deployed_to_region" {
341
322
value = module. oke . deployed_to_region
342
323
}
343
324
output "kubeconfig" {
344
- value = module. oke . kubeconfig
325
+ value = module. oke . kubeconfig
326
+ sensitive = true
345
327
}
346
328
output "kubeconfig_for_kubectl" {
347
329
value = module. oke . kubeconfig_for_kubectl
@@ -355,7 +337,7 @@ output "dev" {
355
337
# Use of this resource for production deployments is not recommended.
356
338
# Instead, generate a private key file outside of Terraform and distribute it securely to the system where Terraform will be run.
357
339
output "generated_private_key_pem" {
358
- value = module . oke_node_pool . generated_private_key_pem
340
+ value = var . generate_public_ssh_key ? tls_private_key . oke_worker_node_ssh_key . private_key_pem : " No Keys Auto Generated "
359
341
sensitive = true
360
342
}
361
343
@@ -368,11 +350,3 @@ output "generated_private_key_pem" {
368
350
# output "oke_debug_oke_endpoints" {
369
351
# value = module.oke.oke_debug_oke_endpoints
370
352
# }
371
-
372
- output "debug_k8s_version_calculated" {
373
- value = module. oke . debug_k8s_version_calculated
374
- }
375
-
376
- output "debug_k8s_version_var" {
377
- value = module. oke . debug_k8s_version_var
378
- }
0 commit comments