|
| 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_string" "suffix" { |
| 18 | + length = 4 |
| 19 | + special = false |
| 20 | + upper = false |
| 21 | +} |
| 22 | + |
| 23 | +locals { |
| 24 | + cluster_type = "confidential-safer" |
| 25 | + network_name = "confidential-safer-network-${random_string.suffix.result}" |
| 26 | + subnet_name = "confidential-safer-subnet" |
| 27 | + master_auth_subnetwork = "confidential-safer-master-subnet" |
| 28 | + pods_range_name = "ip-range-pods-${random_string.suffix.result}" |
| 29 | + svc_range_name = "ip-range-svc-${random_string.suffix.result}" |
| 30 | + subnet_names = [for subnet_self_link in module.gcp-network.subnets_self_links : split("/", subnet_self_link)[length(split("/", subnet_self_link)) - 1]] |
| 31 | +} |
| 32 | + |
| 33 | +data "google_client_config" "default" {} |
| 34 | + |
| 35 | +provider "kubernetes" { |
| 36 | + host = "https://${module.gke.endpoint}" |
| 37 | + token = data.google_client_config.default.access_token |
| 38 | + cluster_ca_certificate = base64decode(module.gke.ca_certificate) |
| 39 | +} |
| 40 | + |
| 41 | +// A random valid k8s version is retrived |
| 42 | +// to specify as an explicit version. |
| 43 | +data "google_container_engine_versions" "current" { |
| 44 | + project = var.project_id |
| 45 | + location = var.region |
| 46 | +} |
| 47 | + |
| 48 | +resource "random_shuffle" "version" { |
| 49 | + input = data.google_container_engine_versions.current.valid_master_versions |
| 50 | + result_count = 1 |
| 51 | +} |
| 52 | + |
| 53 | +module "gke" { |
| 54 | + source = "terraform-google-modules/kubernetes-engine/google//modules/safer-cluster" |
| 55 | + version = "~> 35.0" |
| 56 | + |
| 57 | + project_id = var.project_id |
| 58 | + name = "${local.cluster_type}-cluster-${random_string.suffix.result}" |
| 59 | + regional = true |
| 60 | + region = var.region |
| 61 | + network = module.gcp-network.network_name |
| 62 | + subnetwork = local.subnet_names[index(module.gcp-network.subnets_names, local.subnet_name)] |
| 63 | + ip_range_pods = local.pods_range_name |
| 64 | + ip_range_services = local.svc_range_name |
| 65 | + master_ipv4_cidr_block = "172.16.0.0/28" |
| 66 | + add_cluster_firewall_rules = true |
| 67 | + firewall_inbound_ports = ["9443", "15017"] |
| 68 | + kubernetes_version = random_shuffle.version.result[0] |
| 69 | + release_channel = "UNSPECIFIED" |
| 70 | + deletion_protection = false |
| 71 | + enable_private_endpoint = true |
| 72 | + enable_confidential_nodes = true |
| 73 | + |
| 74 | + master_authorized_networks = [ |
| 75 | + { |
| 76 | + cidr_block = "10.60.0.0/17" |
| 77 | + display_name = "VPC" |
| 78 | + }, |
| 79 | + ] |
| 80 | + |
| 81 | + database_encryption = [ |
| 82 | + { |
| 83 | + "key_name" : module.kms.keys[local.key_name], |
| 84 | + "state" : "ENCRYPTED" |
| 85 | + } |
| 86 | + ] |
| 87 | + |
| 88 | + node_pools = [ |
| 89 | + { |
| 90 | + name = "default" |
| 91 | + machine_type = "n2d-standard-2" |
| 92 | + enable_secure_boot = true |
| 93 | + }, |
| 94 | + ] |
| 95 | + |
| 96 | + notification_config_topic = google_pubsub_topic.updates.id |
| 97 | +} |
| 98 | + |
| 99 | +resource "google_pubsub_topic" "updates" { |
| 100 | + name = "cluster-updates-${random_string.suffix.result}" |
| 101 | + project = var.project_id |
| 102 | +} |
0 commit comments