|
| 1 | +/** |
| 2 | + * Copyright 2018 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 | +{{ autogeneration_note }} |
| 18 | + |
| 19 | +/****************************************** |
| 20 | + Create regional cluster |
| 21 | + *****************************************/ |
| 22 | +resource "google_container_cluster" "primary" { |
| 23 | + count = "${var.regional ? 1 : 0}" |
| 24 | + name = "${var.name}" |
| 25 | + description = "${var.description}" |
| 26 | + project = "${var.project_id}" |
| 27 | + |
| 28 | + region = "${var.region}" |
| 29 | + additional_zones = ["${coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result))}"] |
| 30 | + |
| 31 | + network = "${replace(data.google_compute_network.gke_network.self_link, "https://www.googleapis.com/compute/v1/", "")}" |
| 32 | + subnetwork = "${replace(data.google_compute_subnetwork.gke_subnetwork.self_link, "https://www.googleapis.com/compute/v1/", "")}" |
| 33 | + min_master_version = "${local.kubernetes_version}" |
| 34 | + |
| 35 | + logging_service = "${var.logging_service}" |
| 36 | + monitoring_service = "${var.monitoring_service}" |
| 37 | + |
| 38 | + master_authorized_networks_config = "${var.master_authorized_networks_config}" |
| 39 | + |
| 40 | + addons_config { |
| 41 | + http_load_balancing { |
| 42 | + disabled = "${var.http_load_balancing ? 0 : 1}" |
| 43 | + } |
| 44 | + |
| 45 | + horizontal_pod_autoscaling { |
| 46 | + disabled = "${var.horizontal_pod_autoscaling ? 0 : 1}" |
| 47 | + } |
| 48 | + |
| 49 | + kubernetes_dashboard { |
| 50 | + disabled = "${var.kubernetes_dashboard ? 0 : 1}" |
| 51 | + } |
| 52 | + |
| 53 | + network_policy_config { |
| 54 | + disabled = "${var.network_policy ? 0 : 1}" |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + ip_allocation_policy { |
| 59 | + cluster_secondary_range_name = "${var.ip_range_pods}" |
| 60 | + services_secondary_range_name = "${var.ip_range_services}" |
| 61 | + } |
| 62 | + |
| 63 | + maintenance_policy { |
| 64 | + daily_maintenance_window { |
| 65 | + start_time = "${var.maintenance_start_time}" |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + lifecycle { |
| 70 | + ignore_changes = ["node_pool"] |
| 71 | + } |
| 72 | + |
| 73 | + timeouts { |
| 74 | + create = "30m" |
| 75 | + update = "30m" |
| 76 | + delete = "30m" |
| 77 | + } |
| 78 | + |
| 79 | + node_pool { |
| 80 | + name = "default-pool" |
| 81 | + |
| 82 | + node_config { |
| 83 | + service_account = "${lookup(var.node_pools[0], "service_account", var.service_account)}" |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + remove_default_node_pool = "${var.remove_default_node_pool}" |
| 88 | +} |
| 89 | + |
| 90 | +/****************************************** |
| 91 | + Create regional node pools |
| 92 | + *****************************************/ |
| 93 | +resource "google_container_node_pool" "pools" { |
| 94 | + count = "${var.regional ? length(var.node_pools) : 0}" |
| 95 | + name = "${lookup(var.node_pools[count.index], "name")}" |
| 96 | + project = "${var.project_id}" |
| 97 | + region = "${var.region}" |
| 98 | + cluster = "${var.name}" |
| 99 | + version = "${lookup(var.node_pools[count.index], "auto_upgrade", false) ? "" : lookup(var.node_pools[count.index], "version", local.node_version)}" |
| 100 | + initial_node_count = "${lookup(var.node_pools[count.index], "initial_node_count", lookup(var.node_pools[count.index], "min_count", 1))}" |
| 101 | + |
| 102 | + autoscaling { |
| 103 | + min_node_count = "${lookup(var.node_pools[count.index], "min_count", 1)}" |
| 104 | + max_node_count = "${lookup(var.node_pools[count.index], "max_count", 100)}" |
| 105 | + } |
| 106 | + |
| 107 | + management { |
| 108 | + auto_repair = "${lookup(var.node_pools[count.index], "auto_repair", true)}" |
| 109 | + auto_upgrade = "${lookup(var.node_pools[count.index], "auto_upgrade", true)}" |
| 110 | + } |
| 111 | + |
| 112 | + node_config { |
| 113 | + image_type = "${lookup(var.node_pools[count.index], "image_type", "COS")}" |
| 114 | + machine_type = "${lookup(var.node_pools[count.index], "machine_type", "n1-standard-2")}" |
| 115 | + labels = "${merge(map("cluster_name", var.name), map("node_pool", lookup(var.node_pools[count.index], "name")), var.node_pools_labels["all"], var.node_pools_labels[lookup(var.node_pools[count.index], "name")])}" |
| 116 | + metadata = "${merge(map("cluster_name", var.name), map("node_pool", lookup(var.node_pools[count.index], "name")), var.node_pools_metadata["all"], var.node_pools_metadata[lookup(var.node_pools[count.index], "name")])}" |
| 117 | + taint = "${concat(var.node_pools_taints["all"], var.node_pools_taints[lookup(var.node_pools[count.index], "name")])}" |
| 118 | + tags = ["${concat(list("gke-${var.name}"), list("gke-${var.name}-${lookup(var.node_pools[count.index], "name")}"), var.node_pools_tags["all"], var.node_pools_tags[lookup(var.node_pools[count.index], "name")])}"] |
| 119 | + |
| 120 | + disk_size_gb = "${lookup(var.node_pools[count.index], "disk_size_gb", 100)}" |
| 121 | + disk_type = "${lookup(var.node_pools[count.index], "disk_type", "pd-standard")}" |
| 122 | + service_account = "${lookup(var.node_pools[count.index], "service_account", var.service_account)}" |
| 123 | + preemptible = "${lookup(var.node_pools[count.index], "preemptible", false)}" |
| 124 | + |
| 125 | + oauth_scopes = [ |
| 126 | + "https://www.googleapis.com/auth/cloud-platform", |
| 127 | + ] |
| 128 | + } |
| 129 | + |
| 130 | + lifecycle { |
| 131 | + ignore_changes = ["initial_node_count"] |
| 132 | + } |
| 133 | + |
| 134 | + timeouts { |
| 135 | + create = "30m" |
| 136 | + update = "30m" |
| 137 | + delete = "30m" |
| 138 | + } |
| 139 | + |
| 140 | + depends_on = ["google_container_cluster.primary"] |
| 141 | +} |
| 142 | + |
| 143 | +resource "null_resource" "wait_for_regional_cluster" { |
| 144 | + count = "${var.regional ? 1 : 0}" |
| 145 | + |
| 146 | + provisioner "local-exec" { |
| 147 | + command = "${path.module}/scripts/wait-for-cluster.sh ${var.project_id} ${var.name}" |
| 148 | + } |
| 149 | + |
| 150 | + provisioner "local-exec" { |
| 151 | + when = "destroy" |
| 152 | + command = "${path.module}/scripts/wait-for-cluster.sh ${var.project_id} ${var.name}" |
| 153 | + } |
| 154 | + |
| 155 | + depends_on = ["google_container_cluster.primary", "google_container_node_pool.pools"] |
| 156 | +} |
0 commit comments