|
| 1 | +/** |
| 2 | + * Copyright 2025 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 | +locals { |
| 18 | + cluster_type = "simple-autopilot-private-cmek" |
| 19 | + network_name = "simple-autopilot-private-cmek-network" |
| 20 | + subnet_name = "simple-autopilot-private-cmek-subnet" |
| 21 | + master_auth_subnetwork = "simple-autopilot-private-master-subnet" |
| 22 | + pods_range_name = "ip-range-pods-simple-autopilot-private" |
| 23 | + svc_range_name = "ip-range-svc-simple-autopilot-private" |
| 24 | + subnet_names = [for subnet_self_link in module.gcp-network.subnets_self_links : split("/", subnet_self_link)[length(split("/", subnet_self_link)) - 1]] |
| 25 | +} |
| 26 | + |
| 27 | +data "google_client_config" "default" {} |
| 28 | + |
| 29 | +data "google_project" "main" { |
| 30 | + project_id = var.project_id |
| 31 | +} |
| 32 | + |
| 33 | +module "kms" { |
| 34 | + source = "terraform-google-modules/kms/google" |
| 35 | + version = "~> 3.2" |
| 36 | + |
| 37 | + project_id = var.project_id |
| 38 | + key_protection_level = "HSM" |
| 39 | + location = "us-central1" |
| 40 | + keyring = "keyring" |
| 41 | + keys = ["key"] |
| 42 | + prevent_destroy = false |
| 43 | +} |
| 44 | + |
| 45 | +resource "google_kms_crypto_key_iam_member" "main" { |
| 46 | + crypto_key_id = values(module.kms.keys)[0] |
| 47 | + role = "roles/cloudkms.cryptoKeyEncrypterDecrypter" |
| 48 | + member = "serviceAccount:service-${data.google_project.main.number}@compute-system.iam.gserviceaccount.com" |
| 49 | +} |
| 50 | + |
| 51 | +provider "kubernetes" { |
| 52 | + host = "https://${module.gke.endpoint}" |
| 53 | + token = data.google_client_config.default.access_token |
| 54 | + cluster_ca_certificate = base64decode(module.gke.ca_certificate) |
| 55 | +} |
| 56 | + |
| 57 | +module "gke" { |
| 58 | + source = "terraform-google-modules/kubernetes-engine/google//modules/beta-autopilot-private-cluster" |
| 59 | + version = "~> 35.0" |
| 60 | + |
| 61 | + project_id = var.project_id |
| 62 | + name = "${local.cluster_type}-cluster" |
| 63 | + regional = true |
| 64 | + region = "us-central1" |
| 65 | + network = module.gcp-network.network_name |
| 66 | + subnetwork = local.subnet_names[index(module.gcp-network.subnets_names, local.subnet_name)] |
| 67 | + ip_range_pods = local.pods_range_name |
| 68 | + ip_range_services = local.svc_range_name |
| 69 | + release_channel = "REGULAR" |
| 70 | + enable_vertical_pod_autoscaling = true |
| 71 | + enable_private_endpoint = true |
| 72 | + enable_private_nodes = true |
| 73 | + network_tags = [local.cluster_type] |
| 74 | + deletion_protection = false |
| 75 | + boot_disk_kms_key = values(module.kms.keys)[0] |
| 76 | + depends_on = [google_kms_crypto_key_iam_member.main] |
| 77 | +} |
0 commit comments