|
| 1 | +# Copyright (c) 2024 Oracle Corporation and/or its affiliates. |
| 2 | +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl |
| 3 | + |
| 4 | + |
| 5 | +data "oci_containerengine_cluster_kube_config" "public" { |
| 6 | + count = local.cluster_enabled && local.public_endpoint_available ? 1 : 0 |
| 7 | + |
| 8 | + cluster_id = local.cluster_id |
| 9 | + endpoint = "PUBLIC_ENDPOINT" |
| 10 | +} |
| 11 | + |
| 12 | + |
| 13 | +data "oci_containerengine_cluster_kube_config" "private" { |
| 14 | + count = local.cluster_enabled && local.private_endpoint_available ? 1 : 0 |
| 15 | + |
| 16 | + cluster_id = local.cluster_id |
| 17 | + endpoint = "PRIVATE_ENDPOINT" |
| 18 | +} |
| 19 | + |
| 20 | +data "oci_containerengine_clusters" "existing_cluster" { |
| 21 | + count = var.cluster_id != null ? 1 : 0 |
| 22 | + compartment_id = var.compartment_id |
| 23 | + |
| 24 | + state = ["ACTIVE","UPDATING"] |
| 25 | + filter { |
| 26 | + name = "id" |
| 27 | + values = [var.cluster_id] |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +# Obtain cluster Kubeconfig. |
| 32 | +data "oci_containerengine_cluster_kube_config" "kube_config" { |
| 33 | + #cluster_id = oci_containerengine_cluster.k8s_cluster.id |
| 34 | + cluster_id = one(module.c1[*].cluster_id) |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +output "cluster_id" { |
| 39 | + description = "ID of the OKE cluster" |
| 40 | + value = one(module.c1[*].cluster_id) |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +output "cluster_kubeconfig" { |
| 45 | + description = "OKE kubeconfig" |
| 46 | + value = var.output_detail ? ( |
| 47 | + local.public_endpoint_available ? local.kubeconfig_public : local.kubeconfig_private |
| 48 | + ) : null |
| 49 | +} |
| 50 | + |
| 51 | +variable "cluster_kube_config_expiration" { |
| 52 | + default = 2592000 |
| 53 | +} |
| 54 | + |
| 55 | +variable "cluster_kube_config_token_version" { |
| 56 | + default = "2.0.0" |
| 57 | +} |
| 58 | + |
| 59 | +output "cluster_ca_cert" { |
| 60 | + description = "OKE cluster CA certificate" |
| 61 | + value = var.output_detail && length(local.cluster_ca_cert) > 0 ? local.cluster_ca_cert : null |
| 62 | +} |
| 63 | + |
| 64 | +output "apiserver_private_host" { |
| 65 | + description = "Private OKE cluster endpoint address" |
| 66 | + value = local.apiserver_private_host |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +locals { |
| 71 | + cluster_enabled = var.create_cluster || coalesce(var.cluster_id, "none") != "none" |
| 72 | + cluster_id = var.create_cluster ? one(module.c1[*].cluster_id) : var.cluster_id |
| 73 | + cluster_name = var.cluster_name |
| 74 | + |
| 75 | + cluster-context = try(format("context-%s", substr(local.cluster_id, -11, -1)), "") |
| 76 | + |
| 77 | + existing_cluster_endpoints = coalesce(one(flatten(data.oci_containerengine_clusters.existing_cluster[*].clusters[*].endpoints)), tomap({})) |
| 78 | + public_endpoint_available = var.cluster_id != null ? length(lookup(local.existing_cluster_endpoints, "public_endpoint", "")) > 0 : var.control_plane_is_public && var.assign_public_ip_to_control_plane |
| 79 | + private_endpoint_available = var.cluster_id != null ? length(lookup(local.existing_cluster_endpoints, "private_endpoint", "")) > 0 : true |
| 80 | + kubeconfig_public = var.control_plane_is_public ? try(yamldecode(replace(lookup(one(data.oci_containerengine_cluster_kube_config.public), "content", ""), local.cluster-context, var.cluster_name)), tomap({})) : null |
| 81 | + kubeconfig_private = try(yamldecode(replace(lookup(one(data.oci_containerengine_cluster_kube_config.private), "content", ""), local.cluster-context, var.cluster_name)), tomap({})) |
| 82 | + |
| 83 | + kubeconfig_clusters = try(lookup(local.kubeconfig_private, "clusters", []), []) |
| 84 | + apiserver_private_host = (var.create_cluster |
| 85 | + ? try(split(":", one(module.c1[*].endpoints.private_endpoint))[0], "") |
| 86 | + : split(":", replace(try(lookup(lookup(local.kubeconfig_clusters[0], "cluster", {}), "server", ""), "none"), "https://", ""))[0]) |
| 87 | + |
| 88 | + kubeconfig_ca_cert = try(lookup(lookup(local.kubeconfig_clusters[0], "cluster", {}), "certificate-authority-data", ""), "none") |
| 89 | + cluster_ca_cert = coalesce(var.cluster_ca_cert, local.kubeconfig_ca_cert) |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +module "c1" { |
| 94 | + |
| 95 | + source = "oracle-terraform-modules/oke/oci" |
| 96 | + version = "5.1.1" |
| 97 | + |
| 98 | + count = lookup(lookup(var.clusters, "c1"), "enabled") ? 1 : 0 |
| 99 | + |
| 100 | + home_region = lookup(local.regions, var.home_region) |
| 101 | + |
| 102 | + #region = lookup(local.regions, lookup(lookup(var.clusters, "c1"), "region")) |
| 103 | + region = lookup(local.regions, var.home_region) |
| 104 | + |
| 105 | + tenancy_id = var.tenancy_id |
| 106 | + |
| 107 | + # general oci parameters |
| 108 | + compartment_id = var.compartment_id |
| 109 | + |
| 110 | + # ssh keys |
| 111 | + ssh_private_key_path = var.ssh_private_key_path |
| 112 | + ssh_public_key_path = var.ssh_public_key_path |
| 113 | + |
| 114 | +# Network |
| 115 | + create_vcn = false |
| 116 | + vcn_id = var.vcn_id |
| 117 | + |
| 118 | + |
| 119 | + # networking |
| 120 | + create_drg = var.oke_control_plane == "private" ? true : false |
| 121 | + drg_display_name = var.cluster_name |
| 122 | + |
| 123 | + |
| 124 | + vcn_cidrs = [lookup(lookup(var.clusters, "c1"), "vcn")] |
| 125 | + vcn_name = "VCN-wktiso1" |
| 126 | + |
| 127 | + #subnets |
| 128 | + subnets = { |
| 129 | + pub_lb = { id = var.pub_lb_id } |
| 130 | + operator = { id = var.pub_lb_id } |
| 131 | + bastion = { id = var.pub_lb_id } |
| 132 | + workers = { id = var.worker_subnet_id } |
| 133 | + cp = {id = var.control_plane_subnet_id } |
| 134 | + } |
| 135 | + |
| 136 | + # bastion host |
| 137 | + create_bastion = true # *true/false |
| 138 | + bastion_allowed_cidrs = [] # e.g. ["0.0.0.0/0"] to allow traffic from all sources |
| 139 | + bastion_availability_domain = null # Defaults to first available |
| 140 | + bastion_image_id = null # Ignored when |
| 141 | + bastion_image_os = "Oracle Linux" # Ignored when bastion_image_type = "custom" |
| 142 | + bastion_image_os_version = "8" # Ignored when bastion_image_type = "custom" |
| 143 | + bastion_image_type = "platform" # platform/custom |
| 144 | + bastion_nsg_ids = [] # Combined with created NSG when enabled in var.nsgs |
| 145 | + bastion_public_ip = null # Ignored when create_bastion = true |
| 146 | + #bastion_type = "public" # *public/private |
| 147 | + bastion_upgrade = false # true/*false |
| 148 | + bastion_user = "opc" |
| 149 | + |
| 150 | + bastion_shape = {shape = var.node_shape,ocpus = 1,memory = 4,boot_volume_size = 50} |
| 151 | + #create_bastion = true |
| 152 | + allow_bastion_cluster_access = true |
| 153 | + bastion_is_public = true |
| 154 | + #bastion_allowed_cidrs = ["0.0.0.0/0"] |
| 155 | + #bastion_upgrade = false |
| 156 | + |
| 157 | + # operator host |
| 158 | + create_operator = false |
| 159 | + operator_upgrade = false |
| 160 | + operator_install_helm = true |
| 161 | + #operator_install_kubectl_from_repo = true |
| 162 | + operator_cloud_init = [] |
| 163 | + create_iam_resources = false |
| 164 | + create_iam_operator_policy = "never" |
| 165 | + operator_install_k9s = false |
| 166 | + |
| 167 | + # oke cluster options |
| 168 | + cluster_name = var.cluster_name |
| 169 | + cluster_type = var.cluster_type |
| 170 | + cni_type = var.preferred_cni |
| 171 | + control_plane_is_public = true |
| 172 | + control_plane_allowed_cidrs = [local.anywhere] |
| 173 | + kubernetes_version = var.kubernetes_version |
| 174 | + services_cidr = lookup(lookup(var.clusters, "c1"), "services") |
| 175 | + |
| 176 | + |
| 177 | + # node pools |
| 178 | + allow_worker_ssh_access = true |
| 179 | + kubeproxy_mode = "iptables" |
| 180 | + worker_pool_mode = "node-pool" |
| 181 | + worker_pools = var.nodepools |
| 182 | + worker_cloud_init = local.worker_cloud_init |
| 183 | + worker_image_type = "oke" |
| 184 | + |
| 185 | + # oke load balancers |
| 186 | + load_balancers = "both" |
| 187 | + preferred_load_balancer = "public" |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | + user_id = var.user_id |
| 192 | + providers = { |
| 193 | + oci = oci.c1 |
| 194 | + oci.home = oci.home |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | + |
| 199 | +resource "local_file" "test_kube_config_file" { |
| 200 | + content = data.oci_containerengine_cluster_kube_config.kube_config.content |
| 201 | + filename = "${path.module}/${var.cluster_name}_kubeconfig" |
| 202 | +} |
0 commit comments