|
| 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 = "confidential-autopilot-private" |
| 19 | + network_name = "confidential-autopilot-private-network" |
| 20 | + subnet_name = "confidential-autopilot-private-subnet" |
| 21 | + master_auth_subnetwork = "confidential-autopilot-master-subnet" |
| 22 | + pods_range_name = "ip-range-pods-confidential-autopilot" |
| 23 | + svc_range_name = "ip-range-svc-confidential-autopilot" |
| 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_project" "main" { |
| 28 | + project_id = var.project_id |
| 29 | +} |
| 30 | + |
| 31 | +resource "random_string" "suffix" { |
| 32 | + length = 4 |
| 33 | + special = false |
| 34 | + upper = false |
| 35 | +} |
| 36 | + |
| 37 | +module "kms" { |
| 38 | + source = "terraform-google-modules/kms/google" |
| 39 | + version = "~> 4.0" |
| 40 | + |
| 41 | + project_id = var.project_id |
| 42 | + key_protection_level = "HSM" |
| 43 | + location = "us-central1" |
| 44 | + keyring = "keyring-${random_string.suffix.result}" |
| 45 | + keys = ["key"] |
| 46 | + prevent_destroy = false |
| 47 | +} |
| 48 | + |
| 49 | +resource "google_kms_crypto_key_iam_member" "main" { |
| 50 | + crypto_key_id = values(module.kms.keys)[0] |
| 51 | + role = "roles/cloudkms.cryptoKeyEncrypterDecrypter" |
| 52 | + member = "serviceAccount:service-${data.google_project.main.number}@compute-system.iam.gserviceaccount.com" |
| 53 | +} |
| 54 | + |
| 55 | +module "gke" { |
| 56 | + source = "terraform-google-modules/kubernetes-engine/google//modules/beta-autopilot-private-cluster" |
| 57 | + version = "~> 36.0" |
| 58 | + |
| 59 | + project_id = var.project_id |
| 60 | + name = "${local.cluster_type}-cluster" |
| 61 | + regional = true |
| 62 | + region = "us-central1" |
| 63 | + network = module.gcp-network.network_name |
| 64 | + subnetwork = local.subnet_names[index(module.gcp-network.subnets_names, local.subnet_name)] |
| 65 | + ip_range_pods = local.pods_range_name |
| 66 | + ip_range_services = local.svc_range_name |
| 67 | + release_channel = "REGULAR" |
| 68 | + enable_vertical_pod_autoscaling = true |
| 69 | + enable_private_endpoint = true |
| 70 | + enable_private_nodes = true |
| 71 | + network_tags = [local.cluster_type] |
| 72 | + deletion_protection = false |
| 73 | + boot_disk_kms_key = values(module.kms.keys)[0] |
| 74 | + enable_confidential_nodes = true |
| 75 | + |
| 76 | + database_encryption = [ |
| 77 | + { |
| 78 | + "key_name" : values(module.kms.keys)[0], |
| 79 | + "state" : "ENCRYPTED" |
| 80 | + } |
| 81 | + ] |
| 82 | + |
| 83 | + depends_on = [google_kms_crypto_key_iam_member.main] |
| 84 | +} |
0 commit comments