Skip to content

Commit 0e71f1f

Browse files
committed
oke and oke-node-pool modules clean up
Signed-off-by: junior <junior@users.noreply.github.com>
1 parent d5112e8 commit 0e71f1f

File tree

10 files changed

+91
-216
lines changed

10 files changed

+91
-216
lines changed

modules/oke-node-pool/main.tf

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
resource "oci_containerengine_node_pool" "oke_node_pool" {
66
cluster_id = var.oke_cluster_ocid
77
compartment_id = var.oke_cluster_compartment_ocid
8-
kubernetes_version = (var.k8s_version == "Latest") ? local.node_pool_k8s_latest_version : var.k8s_version
8+
kubernetes_version = local.node_k8s_version
99
name = var.node_pool_name
1010
node_shape = var.node_pool_shape
11-
ssh_public_key = var.generate_public_ssh_key ? tls_private_key.oke_worker_node_ssh_key.public_key_openssh : var.public_ssh_key
12-
freeform_tags = local.freeform_deployment_tags
11+
ssh_public_key = var.public_ssh_key
12+
freeform_tags = var.freeform_deployment_tags
1313

1414
node_config_details {
1515
dynamic "placement_configs" {
@@ -24,9 +24,9 @@ resource "oci_containerengine_node_pool" "oke_node_pool" {
2424
cni_type = "FLANNEL_OVERLAY"
2525
}
2626
# nsg_ids = []
27-
size = var.num_pool_workers
28-
kms_key_id = var.oci_vault_key_id_oke_node_boot_volume ? var.oci_vault_key_id_oke_node_boot_volume : null
29-
freeform_tags = local.freeform_deployment_tags
27+
size = var.node_pool_min_nodes
28+
kms_key_id = var.oci_vault_key_id_oke_node_boot_volume != "" ? var.oci_vault_key_id_oke_node_boot_volume : null
29+
freeform_tags = var.freeform_deployment_tags
3030
}
3131

3232
dynamic "node_shape_config" {
@@ -53,7 +53,7 @@ resource "oci_containerengine_node_pool" "oke_node_pool" {
5353
value = var.node_pool_name
5454
}
5555

56-
# count = var.create_new_oke_cluster ? 1 : 0
56+
count = var.create_new_node_pool ? 1 : 0
5757
}
5858

5959
locals {
@@ -62,10 +62,5 @@ locals {
6262

6363
# Gets the latest Kubernetes version supported by the node pool
6464
node_pool_k8s_latest_version = reverse(sort(data.oci_containerengine_node_pool_option.node_pool.kubernetes_versions))[0]
65+
node_k8s_version = (var.node_k8s_version == "Latest") ? local.node_pool_k8s_latest_version : var.node_k8s_version
6566
}
66-
67-
# Generate ssh keys to access Worker Nodes, if generate_public_ssh_key=true, applies to the pool
68-
resource "tls_private_key" "oke_worker_node_ssh_key" {
69-
algorithm = "RSA"
70-
rsa_bits = 2048
71-
}

modules/oke-node-pool/outputs.tf

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22
# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
33
#
44

5-
### Important Security Notice ###
6-
# The private key generated by this resource will be stored unencrypted in your Terraform state file.
7-
# Use of this resource for production deployments is not recommended.
8-
# Instead, generate a private key file outside of Terraform and distribute it securely to the system where Terraform will be run.
9-
output "generated_private_key_pem" {
10-
value = var.generate_public_ssh_key ? tls_private_key.oke_worker_node_ssh_key.private_key_pem : "No Keys Auto Generated"
11-
sensitive = true
12-
}
5+
output "node_pool_name" {
6+
value = oci_containerengine_node_pool.oke_node_pool.0.name
7+
}
8+
output "node_pool_min_nodes" {
9+
value = var.node_pool_min_nodes
10+
}
11+
output "node_pool_max_nodes" {
12+
value = var.node_pool_max_nodes
13+
}
14+
output "node_pool_id" {
15+
value = oci_containerengine_node_pool.oke_node_pool.0.id
16+
}
17+
output "node_k8s_version" {
18+
value = local.node_k8s_version
19+
}

modules/oke-node-pool/variables.tf

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,12 @@ variable "oke_cluster_compartment_ocid" {
1212
type = string
1313
}
1414

15-
## App Variables
16-
variable "app_name" {
17-
default = "OKE App"
18-
description = "Application name. Will be used as prefix to identify resources, such as OKE, VCN, ATP, and others"
19-
}
20-
variable "app_deployment_environment" {
21-
default = "generic" # e.g.: Development, QA, Stage, ...
22-
description = "Deployment environment for the freeform tags"
23-
}
24-
variable "app_deployment_type" {
25-
default = "generic" # e.g.: App Type 1, App Type 2, Red, Purple, ...
26-
description = "Deployment type for the freeform tags"
27-
}
28-
variable "deploy_id" {
29-
default = ""
30-
description = "Deployment ID"
31-
}
32-
3315
## Node Pool Variables
34-
variable "k8s_version" {
16+
variable "create_new_node_pool" {
17+
default = true
18+
description = "Create a new node pool if true or use an existing one if false"
19+
}
20+
variable "node_k8s_version" {
3521
description = "Kubernetes version installed on your worker nodes"
3622
type = string
3723
default = "Latest"
@@ -40,10 +26,14 @@ variable "node_pool_name" {
4026
default = "pool1"
4127
description = "Name of the node pool"
4228
}
43-
variable "num_pool_workers" {
29+
variable "node_pool_min_nodes" {
4430
default = 3
4531
description = "The number of worker nodes in the node pool. If select Cluster Autoscaler, will assume the minimum number of nodes configured"
4632
}
33+
variable "node_pool_max_nodes" {
34+
default = 3
35+
description = "The max number of worker nodes in the node pool if using Cluster Autoscaler."
36+
}
4737
variable "node_pool_shape" {
4838
default = "VM.Standard.E4.Flex"
4939
description = "A shape is a template that determines the number of OCPUs, amount of memory, and other resources allocated to a newly created instance for the Worker Node"
@@ -68,9 +58,6 @@ variable "node_pool_boot_volume_size_in_gbs" {
6858
default = "50"
6959
description = "Specify a custom boot volume size (in GB)"
7060
}
71-
variable "generate_public_ssh_key" {
72-
default = true
73-
}
7461
variable "public_ssh_key" {
7562
default = ""
7663
description = "In order to access your private nodes with a public SSH key you will need to set up a bastion host (a.k.a. jump box). If using public nodes, bastion is not needed. Left blank to not import keys."
@@ -94,19 +81,10 @@ variable "tenancy_ocid" {}
9481

9582
# App Name Locals
9683
locals {
97-
app_name_normalized = substr(replace(lower(var.app_name), " ", "-"), 0, 6)
84+
app_name_normalized = substr(replace(lower(var.freeform_deployment_tags.AppName), " ", "-"), 0, 6)
9885
}
9986

100-
# Deployment Details
101-
variable "app_details" {
102-
description = "App Details"
103-
}
104-
105-
# Deployment Tags
106-
locals {
107-
freeform_deployment_tags = {
108-
"DeploymentID" = "${var.app_details.app_deployment_id}",
109-
"AppName" = "${var.app_details.app_name}",
110-
"Environment" = "${var.app_details.app_deployment_environment}",
111-
"DeploymentType" = "${var.app_details.app_deployment_type}" }
87+
# Deployment Details + Freeform Tags
88+
variable "freeform_deployment_tags" {
89+
description = "Tags to be added to the resources"
11290
}

modules/oke/main.tf

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
resource "oci_containerengine_cluster" "oke_cluster" {
66
compartment_id = local.oke_compartment_ocid
77
kubernetes_version = (var.k8s_version == "Latest") ? local.cluster_k8s_latest_version : var.k8s_version
8-
name = "${var.app_name} (${random_string.deploy_id.result})"
8+
name = "${local.app_name} (${local.deploy_id})"
99
vcn_id = oci_core_virtual_network.oke_vcn[0].id
10-
kms_key_id = var.oci_vault_key_id_oke_secrets ? var.oci_vault_key_id_oke_secrets : null
11-
freeform_tags = local.freeform_deployment_tags
10+
kms_key_id = var.oci_vault_key_id_oke_secrets != "" ? var.oci_vault_key_id_oke_secrets : null
11+
freeform_tags = var.freeform_deployment_tags
1212

1313
endpoint_config {
1414
is_public_ip_enabled = (var.cluster_endpoint_visibility == "Private") ? false : true
@@ -29,16 +29,16 @@ resource "oci_containerengine_cluster" "oke_cluster" {
2929
pods_cidr = lookup(var.network_cidrs, "PODS-CIDR")
3030
}
3131
persistent_volume_config {
32-
freeform_tags = local.freeform_deployment_tags
32+
freeform_tags = var.freeform_deployment_tags
3333
}
3434
service_lb_config {
35-
freeform_tags = local.freeform_deployment_tags
35+
freeform_tags = var.freeform_deployment_tags
3636
}
3737
}
3838
image_policy_config {
3939
is_policy_enabled = false
4040
# key_details {
41-
# # kms_key_id = var.oci_vault_key_id_oke_image_policy ? var.oci_vault_key_id_oke_image_policy : null
41+
# # kms_key_id = var.oci_vault_key_id_oke_image_policy != "" ? var.oci_vault_key_id_oke_image_policy : null
4242
# }
4343
}
4444
cluster_pod_network_options {
@@ -117,33 +117,12 @@ resource "oci_containerengine_cluster" "oke_cluster" {
117117
# Local kubeconfig for when using Terraform locally. Not used by Oracle Resource Manager
118118
resource "local_file" "oke_kubeconfig" {
119119
content = data.oci_containerengine_cluster_kube_config.oke.content
120-
filename = "${path.module}/generated/kubeconfig"
120+
filename = "${path.root}/generated/kubeconfig"
121121
}
122122

123-
# # Generate ssh keys to access Worker Nodes, if generate_public_ssh_key=true, applies to the pool
124-
# resource "tls_private_key" "oke_worker_node_ssh_key" {
125-
# algorithm = "RSA"
126-
# rsa_bits = 2048
127-
# }
128-
129123
# Get OKE options
130124
locals {
131-
cluster_k8s_latest_version = reverse(sort(data.oci_containerengine_cluster_option.oke.kubernetes_versions))[0]
132-
# node_pool_k8s_latest_version = reverse(sort(data.oci_containerengine_node_pool_option.oke.kubernetes_versions))[0]
125+
cluster_k8s_latest_version = reverse(sort(data.oci_containerengine_cluster_option.oke.kubernetes_versions))[0]
133126
deployed_k8s_version = var.create_new_oke_cluster ? ((var.k8s_version == "Latest") ? local.cluster_k8s_latest_version : var.k8s_version) : [
134127
for x in data.oci_containerengine_clusters.oke.clusters : x.kubernetes_version if x.id == var.existent_oke_cluster_id][0]
135128
}
136-
137-
# # Checks if is using Flexible Compute Shapes
138-
# locals {
139-
# # is_flexible_node_shape = contains(local.compute_flexible_shapes, var.node_pool_shape)
140-
# is_flexible_node_shape = contains(split(".", var.node_pool_shape), "Flex")
141-
# }
142-
143-
output "debug_k8s_version_calculated" {
144-
value = ((var.k8s_version == "Latest") ? local.cluster_k8s_latest_version : var.k8s_version)
145-
}
146-
147-
output "debug_k8s_version_var" {
148-
value = var.k8s_version
149-
}

modules/oke/network.tf

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,52 @@
55
resource "oci_core_virtual_network" "oke_vcn" {
66
cidr_block = lookup(var.network_cidrs, "VCN-CIDR")
77
compartment_id = local.oke_compartment_ocid
8-
display_name = "OKE ${var.app_name} VCN - ${random_string.deploy_id.result}"
9-
dns_label = "oke${random_string.deploy_id.result}"
10-
freeform_tags = local.freeform_deployment_tags
8+
display_name = "OKE ${local.app_name} VCN - ${local.deploy_id}"
9+
dns_label = "oke${local.deploy_id}"
10+
freeform_tags = var.freeform_deployment_tags
1111

1212
count = var.create_new_oke_cluster ? 1 : 0
1313
}
1414

1515
resource "oci_core_subnet" "oke_k8s_endpoint_subnet" {
1616
cidr_block = lookup(var.network_cidrs, "ENDPOINT-SUBNET-REGIONAL-CIDR")
1717
compartment_id = local.oke_compartment_ocid
18-
display_name = "oke-k8s-endpoint-subnet-${local.app_name_normalized}-${random_string.deploy_id.result}"
19-
dns_label = "okek8sn${random_string.deploy_id.result}"
18+
display_name = "oke-k8s-endpoint-subnet-${local.app_name_normalized}-${local.deploy_id}"
19+
dns_label = "okek8sn${local.deploy_id}"
2020
vcn_id = oci_core_virtual_network.oke_vcn[0].id
2121
prohibit_public_ip_on_vnic = (var.cluster_endpoint_visibility == "Private") ? true : false
2222
route_table_id = (var.cluster_endpoint_visibility == "Private") ? oci_core_route_table.oke_private_route_table[0].id : oci_core_route_table.oke_public_route_table[0].id
2323
dhcp_options_id = oci_core_virtual_network.oke_vcn[0].default_dhcp_options_id
2424
security_list_ids = [oci_core_security_list.oke_endpoint_security_list[0].id]
25-
freeform_tags = local.freeform_deployment_tags
25+
freeform_tags = var.freeform_deployment_tags
2626

2727
count = var.create_new_oke_cluster ? 1 : 0
2828
}
2929
resource "oci_core_subnet" "oke_nodes_subnet" {
3030
cidr_block = lookup(var.network_cidrs, "SUBNET-REGIONAL-CIDR")
3131
compartment_id = local.oke_compartment_ocid
32-
display_name = "oke-nodes-subnet-${local.app_name_normalized}-${random_string.deploy_id.result}"
33-
dns_label = "okenodesn${random_string.deploy_id.result}"
32+
display_name = "oke-nodes-subnet-${local.app_name_normalized}-${local.deploy_id}"
33+
dns_label = "okenodesn${local.deploy_id}"
3434
vcn_id = oci_core_virtual_network.oke_vcn[0].id
3535
prohibit_public_ip_on_vnic = (var.cluster_workers_visibility == "Private") ? true : false
3636
route_table_id = (var.cluster_workers_visibility == "Private") ? oci_core_route_table.oke_private_route_table[0].id : oci_core_route_table.oke_public_route_table[0].id
3737
dhcp_options_id = oci_core_virtual_network.oke_vcn[0].default_dhcp_options_id
3838
security_list_ids = [oci_core_security_list.oke_nodes_security_list[0].id]
39-
freeform_tags = local.freeform_deployment_tags
39+
freeform_tags = var.freeform_deployment_tags
4040

4141
count = var.create_new_oke_cluster ? 1 : 0
4242
}
4343
resource "oci_core_subnet" "oke_lb_subnet" {
4444
cidr_block = lookup(var.network_cidrs, "LB-SUBNET-REGIONAL-CIDR")
4545
compartment_id = local.oke_compartment_ocid
46-
display_name = "oke-lb-subnet-${local.app_name_normalized}-${random_string.deploy_id.result}"
47-
dns_label = "okelbsn${random_string.deploy_id.result}"
46+
display_name = "oke-lb-subnet-${local.app_name_normalized}-${local.deploy_id}"
47+
dns_label = "okelbsn${local.deploy_id}"
4848
vcn_id = oci_core_virtual_network.oke_vcn[0].id
4949
prohibit_public_ip_on_vnic = false
5050
route_table_id = oci_core_route_table.oke_public_route_table[0].id
5151
dhcp_options_id = oci_core_virtual_network.oke_vcn[0].default_dhcp_options_id
5252
security_list_ids = [oci_core_security_list.oke_lb_security_list[0].id]
53-
freeform_tags = local.freeform_deployment_tags
53+
freeform_tags = var.freeform_deployment_tags
5454

5555
count = var.create_new_oke_cluster ? 1 : 0
5656
}
@@ -72,8 +72,8 @@ resource "oci_core_subnet" "oke_lb_subnet" {
7272
resource "oci_core_route_table" "oke_private_route_table" {
7373
compartment_id = local.oke_compartment_ocid
7474
vcn_id = oci_core_virtual_network.oke_vcn[0].id
75-
display_name = "oke-private-route-table-${local.app_name_normalized}-${random_string.deploy_id.result}"
76-
freeform_tags = local.freeform_deployment_tags
75+
display_name = "oke-private-route-table-${local.app_name_normalized}-${local.deploy_id}"
76+
freeform_tags = var.freeform_deployment_tags
7777

7878
route_rules {
7979
description = "Traffic to the internet"
@@ -93,8 +93,8 @@ resource "oci_core_route_table" "oke_private_route_table" {
9393
resource "oci_core_route_table" "oke_public_route_table" {
9494
compartment_id = local.oke_compartment_ocid
9595
vcn_id = oci_core_virtual_network.oke_vcn[0].id
96-
display_name = "oke-public-route-table-${local.app_name_normalized}-${random_string.deploy_id.result}"
97-
freeform_tags = local.freeform_deployment_tags
96+
display_name = "oke-public-route-table-${local.app_name_normalized}-${local.deploy_id}"
97+
freeform_tags = var.freeform_deployment_tags
9898

9999
route_rules {
100100
description = "Traffic to/from internet"
@@ -124,28 +124,28 @@ resource "oci_core_route_table" "oke_public_route_table" {
124124
resource "oci_core_nat_gateway" "oke_nat_gateway" {
125125
block_traffic = "false"
126126
compartment_id = local.oke_compartment_ocid
127-
display_name = "oke-nat-gateway-${local.app_name_normalized}-${random_string.deploy_id.result}"
127+
display_name = "oke-nat-gateway-${local.app_name_normalized}-${local.deploy_id}"
128128
vcn_id = oci_core_virtual_network.oke_vcn[0].id
129-
freeform_tags = local.freeform_deployment_tags
129+
freeform_tags = var.freeform_deployment_tags
130130

131131
count = var.create_new_oke_cluster ? 1 : 0
132132
}
133133

134134
resource "oci_core_internet_gateway" "oke_internet_gateway" {
135135
compartment_id = local.oke_compartment_ocid
136-
display_name = "oke-internet-gateway-${local.app_name_normalized}-${random_string.deploy_id.result}"
136+
display_name = "oke-internet-gateway-${local.app_name_normalized}-${local.deploy_id}"
137137
enabled = true
138138
vcn_id = oci_core_virtual_network.oke_vcn[0].id
139-
freeform_tags = local.freeform_deployment_tags
139+
freeform_tags = var.freeform_deployment_tags
140140

141141
count = var.create_new_oke_cluster ? 1 : 0
142142
}
143143

144144
resource "oci_core_service_gateway" "oke_service_gateway" {
145145
compartment_id = local.oke_compartment_ocid
146-
display_name = "oke-service-gateway-${local.app_name_normalized}-${random_string.deploy_id.result}"
146+
display_name = "oke-service-gateway-${local.app_name_normalized}-${local.deploy_id}"
147147
vcn_id = oci_core_virtual_network.oke_vcn[0].id
148-
freeform_tags = local.freeform_deployment_tags
148+
freeform_tags = var.freeform_deployment_tags
149149

150150
services {
151151
service_id = lookup(data.oci_core_services.all_services.services[0], "id")

modules/oke/oke-orm-private-endpoint.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
resource "oci_resourcemanager_private_endpoint" "private_kubernetes_endpoint" {
1111
compartment_id = local.oke_compartment_ocid
12-
display_name = "Private Endpoint for OKE ${var.app_name} - ${random_string.deploy_id.result}"
13-
description = "Resource Manager Private Endpoint for OKE for the ${var.app_name} - ${random_string.deploy_id.result}"
12+
display_name = "Private Endpoint for OKE ${local.app_name} - ${local.deploy_id}"
13+
description = "Resource Manager Private Endpoint for OKE for the ${local.app_name} - ${local.deploy_id}"
1414
vcn_id = oci_core_virtual_network.oke_vcn[0].id
1515
subnet_id = oci_core_subnet.oke_k8s_endpoint_subnet[0].id
16-
freeform_tags = local.freeform_deployment_tags
16+
freeform_tags = var.freeform_deployment_tags
1717

1818
count = var.create_new_oke_cluster ? ((var.cluster_endpoint_visibility == "Private") ? 1 : 0) : 0
1919
}

modules/oke/outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
output "comments" {
66
value = "The application URL will be unavailable for a few minutes after provisioning while the application is configured and deployed to Kubernetes"
77
}
8-
output "deploy_id" {
9-
value = random_string.deploy_id.result
10-
}
8+
# output "deploy_id" {
9+
# value = random_string.deploy_id.result
10+
# }
1111
output "deployed_oke_kubernetes_version" {
1212
value = local.deployed_k8s_version
1313
}

0 commit comments

Comments
 (0)