|
| 1 | +/** |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +resource "random_id" "rand" { |
| 18 | + byte_length = 4 |
| 19 | +} |
| 20 | + |
| 21 | +resource "google_service_account" "gke-sa" { |
| 22 | + account_id = "gke-sa-${random_id.rand.hex}" |
| 23 | + project = var.project_id |
| 24 | +} |
| 25 | + |
| 26 | +module "net" { |
| 27 | + source = "terraform-google-modules/network/google" |
| 28 | + version = "~> 9.0" |
| 29 | + |
| 30 | + network_name = "gke-net-${random_id.rand.hex}" |
| 31 | + routing_mode = "GLOBAL" |
| 32 | + project_id = var.project_id |
| 33 | + delete_default_internet_gateway_routes = true |
| 34 | + |
| 35 | + subnets = [ |
| 36 | + { |
| 37 | + subnet_name = "${var.cluster_name}-${var.region}-snet" |
| 38 | + subnet_ip = var.subnet_cidr |
| 39 | + subnet_region = var.region |
| 40 | + subnet_private_access = "true" |
| 41 | + }, |
| 42 | + { |
| 43 | + subnet_name = "${var.cluster_name}-${var.region}-proxy-snet" |
| 44 | + subnet_ip = var.proxy_subnet_cidr |
| 45 | + subnet_region = var.region |
| 46 | + purpose = "REGIONAL_MANAGED_PROXY" |
| 47 | + role = "ACTIVE" |
| 48 | + }, |
| 49 | + { |
| 50 | + subnet_name = "${var.cluster_name}-${var.region}-psc-snet" |
| 51 | + subnet_ip = var.psc_subnet_cidr |
| 52 | + subnet_region = var.region |
| 53 | + subnet_private_access = "true" |
| 54 | + purpose = "PRIVATE_SERVICE_CONNECT" |
| 55 | + } |
| 56 | + ] |
| 57 | + |
| 58 | + secondary_ranges = { |
| 59 | + "${var.cluster_name}-${var.region}-snet" = [ |
| 60 | + { |
| 61 | + range_name = "${var.cluster_name}-${var.region}-snet-pods" |
| 62 | + ip_cidr_range = var.secondary_ranges["pods"] |
| 63 | + }, |
| 64 | + { |
| 65 | + range_name = "${var.cluster_name}-${var.region}-snet-services" |
| 66 | + ip_cidr_range = var.secondary_ranges["services"] |
| 67 | + }, |
| 68 | + ] |
| 69 | + } |
| 70 | + |
| 71 | + routes = flatten([ |
| 72 | + [for k, v in var.primary_net_cidrs : |
| 73 | + { |
| 74 | + name = "${var.cluster_name}-egress-gke-${k}" |
| 75 | + description = "egress through the router for range ${v}" |
| 76 | + destination_range = v |
| 77 | + tags = "gke-${random_id.rand.hex}" |
| 78 | + next_hop_instance = google_compute_instance.vm.self_link |
| 79 | + priority = 100 |
| 80 | + } |
| 81 | + ], |
| 82 | + [ |
| 83 | + { |
| 84 | + name = "${var.cluster_name}-default-igw" |
| 85 | + description = "internet through the router" |
| 86 | + destination_range = "0.0.0.0/0" |
| 87 | + tags = "gke-${random_id.rand.hex}" |
| 88 | + next_hop_instance = google_compute_instance.vm.self_link |
| 89 | + priority = 100 |
| 90 | + } |
| 91 | + ] |
| 92 | + ]) |
| 93 | + |
| 94 | + firewall_rules = [ |
| 95 | + { |
| 96 | + name = "${var.cluster_name}-iap" |
| 97 | + direction = "INGRESS" |
| 98 | + allow = [ |
| 99 | + { |
| 100 | + protocol = "TCP" |
| 101 | + ports = ["22"] |
| 102 | + } |
| 103 | + ] |
| 104 | + ranges = ["35.235.240.0/20"] |
| 105 | + }, |
| 106 | + { |
| 107 | + name = "${var.cluster_name}-tcp-primary" |
| 108 | + direction = "INGRESS" |
| 109 | + allow = [ |
| 110 | + { |
| 111 | + protocol = "TCP" |
| 112 | + } |
| 113 | + ] |
| 114 | + ranges = [ |
| 115 | + var.subnet_cidr, |
| 116 | + var.secondary_ranges["pods"] |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + name = "${var.cluster_name}-allow-psc" |
| 121 | + direction = "INGRESS" |
| 122 | + allow = [ |
| 123 | + { |
| 124 | + protocol = "TCP" |
| 125 | + } |
| 126 | + ] |
| 127 | + ranges = [var.psc_subnet_cidr] |
| 128 | + target_service_accounts = [google_service_account.gke-sa.email] |
| 129 | + }, |
| 130 | + { |
| 131 | + name = "${var.cluster_name}-allow-proxy" |
| 132 | + direction = "INGRESS" |
| 133 | + allow = [ |
| 134 | + { |
| 135 | + protocol = "TCP" |
| 136 | + } |
| 137 | + ] |
| 138 | + ranges = [var.proxy_subnet_cidr] |
| 139 | + target_service_accounts = [google_service_account.gke-sa.email] |
| 140 | + }, |
| 141 | + ] |
| 142 | +} |
| 143 | + |
| 144 | +module "gke" { |
| 145 | + source = "terraform-google-modules/kubernetes-engine/google//modules/beta-private-cluster" |
| 146 | + version = "~> 30.0" |
| 147 | + |
| 148 | + depends_on = [google_compute_instance.vm] |
| 149 | + |
| 150 | + name = var.cluster_name |
| 151 | + project_id = var.project_id |
| 152 | + region = var.region |
| 153 | + release_channel = "RAPID" |
| 154 | + zones = var.node_locations |
| 155 | + network = module.net.network_name |
| 156 | + subnetwork = "${var.cluster_name}-${var.region}-snet" |
| 157 | + ip_range_pods = "${var.cluster_name}-${var.region}-snet-pods" |
| 158 | + ip_range_services = "${var.cluster_name}-${var.region}-snet-services" |
| 159 | + enable_private_endpoint = true |
| 160 | + enable_private_nodes = true |
| 161 | + datapath_provider = "ADVANCED_DATAPATH" |
| 162 | + monitoring_enable_managed_prometheus = false |
| 163 | + enable_shielded_nodes = true |
| 164 | + master_global_access_enabled = false |
| 165 | + master_ipv4_cidr_block = var.secondary_ranges["master_cidr"] |
| 166 | + master_authorized_networks = var.master_authorized_networks |
| 167 | + deletion_protection = false |
| 168 | + remove_default_node_pool = true |
| 169 | + disable_default_snat = true |
| 170 | + gateway_api_channel = "CHANNEL_STANDARD" |
| 171 | + |
| 172 | + node_pools = [ |
| 173 | + { |
| 174 | + name = "default" |
| 175 | + machine_type = "e2-highcpu-2" |
| 176 | + min_count = 1 |
| 177 | + max_count = 100 |
| 178 | + local_ssd_count = 0 |
| 179 | + spot = true |
| 180 | + local_ssd_ephemeral_count = 0 |
| 181 | + disk_size_gb = 100 |
| 182 | + disk_type = "pd-standard" |
| 183 | + image_type = "COS_CONTAINERD" |
| 184 | + logging_variant = "DEFAULT" |
| 185 | + auto_repair = true |
| 186 | + auto_upgrade = true |
| 187 | + service_account = google_service_account.gke-sa.email |
| 188 | + initial_node_count = 1 |
| 189 | + enable_secure_boot = true |
| 190 | + }, |
| 191 | + ] |
| 192 | + |
| 193 | + node_pools_tags = { |
| 194 | + all = ["gke-${random_id.rand.hex}"] |
| 195 | + } |
| 196 | + |
| 197 | + node_pools_oauth_scopes = { |
| 198 | + all = [ |
| 199 | + "https://www.googleapis.com/auth/logging.write", |
| 200 | + "https://www.googleapis.com/auth/monitoring", |
| 201 | + ] |
| 202 | + } |
| 203 | + |
| 204 | + timeouts = { |
| 205 | + create = "15m" |
| 206 | + update = "15m" |
| 207 | + delete = "15m" |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +resource "google_gke_hub_membership" "primary" { |
| 212 | + provider = google-beta |
| 213 | + |
| 214 | + project = var.project_id |
| 215 | + membership_id = "${var.project_id}-${module.gke.name}" |
| 216 | + location = var.region |
| 217 | + |
| 218 | + endpoint { |
| 219 | + gke_cluster { |
| 220 | + resource_link = "//container.googleapis.com/${module.gke.cluster_id}" |
| 221 | + } |
| 222 | + } |
| 223 | + authority { |
| 224 | + issuer = "https://container.googleapis.com/v1/${module.gke.cluster_id}" |
| 225 | + } |
| 226 | +} |
0 commit comments