|
| 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 | +/****************************************** |
| 18 | + Get available zones in region |
| 19 | + *****************************************/ |
| 20 | +data "google_compute_zones" "available" { |
| 21 | + project = "${var.project_id}" |
| 22 | + region = "${var.region}" |
| 23 | +} |
| 24 | + |
| 25 | +resource "random_shuffle" "available_zones" { |
| 26 | + input = ["${data.google_compute_zones.available.names}"] |
| 27 | + result_count = 3 |
| 28 | +} |
| 29 | + |
| 30 | +locals { |
| 31 | + kubernetes_version = "${var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_node_version}" |
| 32 | + node_version = "${var.node_version != "" ? var.node_version : local.kubernetes_version}" |
| 33 | + custom_kube_dns_config = "${length(keys(var.stub_domains)) > 0 ? true : false}" |
| 34 | + network_project_id = "${var.network_project_id != "" ? var.network_project_id : var.project_id}" |
| 35 | + |
| 36 | + cluster_type = "${var.regional ? "regional" : "zonal"}" |
| 37 | + |
| 38 | + cluster_type_output_name = { |
| 39 | + regional = "${element(concat(google_container_cluster.primary.*.name, list("")), 0)}" |
| 40 | + zonal = "${element(concat(google_container_cluster.zonal_primary.*.name, list("")), 0)}" |
| 41 | + } |
| 42 | + |
| 43 | + cluster_type_output_location = { |
| 44 | + regional = "${element(concat(google_container_cluster.primary.*.region, list("")), 0)}" |
| 45 | + zonal = "${element(concat(google_container_cluster.zonal_primary.*.zone, list("")), 0)}" |
| 46 | + } |
| 47 | + |
| 48 | + cluster_type_output_region = { |
| 49 | + regional = "${element(concat(google_container_cluster.primary.*.region, list("")), 0)}" |
| 50 | + zonal = "${var.region}" |
| 51 | + } |
| 52 | + |
| 53 | + cluster_type_output_regional_zones = "${concat(google_container_cluster.primary.*.additional_zones, list(list()))}" |
| 54 | + cluster_type_output_zonal_zones = "${concat(slice(var.zones,1,length(var.zones)), list(list()))}" |
| 55 | + |
| 56 | + cluster_type_output_zones = { |
| 57 | + regional = "${local.cluster_type_output_regional_zones[0]}" |
| 58 | + zonal = "${concat(google_container_cluster.zonal_primary.*.zone, local.cluster_type_output_zonal_zones[0])}" |
| 59 | + } |
| 60 | + |
| 61 | + cluster_type_output_endpoint = { |
| 62 | + regional = "${element(concat(google_container_cluster.primary.*.endpoint, list("")), 0)}" |
| 63 | + zonal = "${element(concat(google_container_cluster.zonal_primary.*.endpoint, list("")), 0)}" |
| 64 | + } |
| 65 | + |
| 66 | + cluster_type_output_master_auth = { |
| 67 | + regional = "${concat(google_container_cluster.primary.*.master_auth, list())}" |
| 68 | + zonal = "${concat(google_container_cluster.zonal_primary.*.master_auth, list())}" |
| 69 | + } |
| 70 | + |
| 71 | + cluster_type_output_master_version = { |
| 72 | + regional = "${element(concat(google_container_cluster.primary.*.master_version, list("")), 0)}" |
| 73 | + zonal = "${element(concat(google_container_cluster.zonal_primary.*.master_version, list("")), 0)}" |
| 74 | + } |
| 75 | + |
| 76 | + cluster_type_output_min_master_version = { |
| 77 | + regional = "${element(concat(google_container_cluster.primary.*.min_master_version, list("")), 0)}" |
| 78 | + zonal = "${element(concat(google_container_cluster.zonal_primary.*.min_master_version, list("")), 0)}" |
| 79 | + } |
| 80 | + |
| 81 | + cluster_type_output_logging_service = { |
| 82 | + regional = "${element(concat(google_container_cluster.primary.*.logging_service, list("")), 0)}" |
| 83 | + zonal = "${element(concat(google_container_cluster.zonal_primary.*.logging_service, list("")), 0)}" |
| 84 | + } |
| 85 | + |
| 86 | + cluster_type_output_monitoring_service = { |
| 87 | + regional = "${element(concat(google_container_cluster.primary.*.monitoring_service, list("")), 0)}" |
| 88 | + zonal = "${element(concat(google_container_cluster.zonal_primary.*.monitoring_service, list("")), 0)}" |
| 89 | + } |
| 90 | + |
| 91 | + cluster_type_output_network_policy_enabled = { |
| 92 | + regional = "${element(concat(google_container_cluster.primary.*.addons_config.0.network_policy_config.0.disabled, list("")), 0)}" |
| 93 | + zonal = "${element(concat(google_container_cluster.zonal_primary.*.addons_config.0.network_policy_config.0.disabled, list("")), 0)}" |
| 94 | + } |
| 95 | + |
| 96 | + cluster_type_output_http_load_balancing_enabled = { |
| 97 | + regional = "${element(concat(google_container_cluster.primary.*.addons_config.0.http_load_balancing.0.disabled, list("")), 0)}" |
| 98 | + zonal = "${element(concat(google_container_cluster.zonal_primary.*.addons_config.0.http_load_balancing.0.disabled, list("")), 0)}" |
| 99 | + } |
| 100 | + |
| 101 | + cluster_type_output_horizontal_pod_autoscaling_enabled = { |
| 102 | + regional = "${element(concat(google_container_cluster.primary.*.addons_config.0.horizontal_pod_autoscaling.0.disabled, list("")), 0)}" |
| 103 | + zonal = "${element(concat(google_container_cluster.zonal_primary.*.addons_config.0.horizontal_pod_autoscaling.0.disabled, list("")), 0)}" |
| 104 | + } |
| 105 | + |
| 106 | + cluster_type_output_kubernetes_dashboard_enabled = { |
| 107 | + regional = "${element(concat(google_container_cluster.primary.*.addons_config.0.kubernetes_dashboard.0.disabled, list("")), 0)}" |
| 108 | + zonal = "${element(concat(google_container_cluster.zonal_primary.*.addons_config.0.kubernetes_dashboard.0.disabled, list("")), 0)}" |
| 109 | + } |
| 110 | + |
| 111 | + cluster_type_output_node_pools_names = { |
| 112 | + regional = "${concat(google_container_node_pool.pools.*.name, list(""))}" |
| 113 | + zonal = "${concat(google_container_node_pool.zonal_pools.*.name, list(""))}" |
| 114 | + } |
| 115 | + |
| 116 | + cluster_type_output_node_pools_versions = { |
| 117 | + regional = "${concat(google_container_node_pool.pools.*.version, list(""))}" |
| 118 | + zonal = "${concat(google_container_node_pool.zonal_pools.*.version, list(""))}" |
| 119 | + } |
| 120 | + |
| 121 | + cluster_master_auth_list_layer1 = "${local.cluster_type_output_master_auth[local.cluster_type]}" |
| 122 | + cluster_master_auth_list_layer2 = "${local.cluster_master_auth_list_layer1[0]}" |
| 123 | + cluster_master_auth_map = "${local.cluster_master_auth_list_layer2[0]}" |
| 124 | + |
| 125 | + # cluster locals |
| 126 | + cluster_name = "${local.cluster_type_output_name[local.cluster_type]}" |
| 127 | + cluster_location = "${local.cluster_type_output_location[local.cluster_type]}" |
| 128 | + cluster_region = "${local.cluster_type_output_region[local.cluster_type]}" |
| 129 | + cluster_zones = "${sort(local.cluster_type_output_zones[local.cluster_type])}" |
| 130 | + cluster_endpoint = "${local.cluster_type_output_endpoint[local.cluster_type]}" |
| 131 | + cluster_ca_certificate = "${lookup(local.cluster_master_auth_map, "cluster_ca_certificate")}" |
| 132 | + cluster_master_version = "${local.cluster_type_output_master_version[local.cluster_type]}" |
| 133 | + cluster_min_master_version = "${local.cluster_type_output_min_master_version[local.cluster_type]}" |
| 134 | + cluster_logging_service = "${local.cluster_type_output_logging_service[local.cluster_type]}" |
| 135 | + cluster_monitoring_service = "${local.cluster_type_output_monitoring_service[local.cluster_type]}" |
| 136 | + cluster_node_pools_names = "${local.cluster_type_output_node_pools_names[local.cluster_type]}" |
| 137 | + cluster_node_pools_versions = "${local.cluster_type_output_node_pools_versions[local.cluster_type]}" |
| 138 | + |
| 139 | + cluster_network_policy_enabled = "${local.cluster_type_output_network_policy_enabled[local.cluster_type] ? false : true}" |
| 140 | + cluster_http_load_balancing_enabled = "${local.cluster_type_output_http_load_balancing_enabled[local.cluster_type] ? false : true}" |
| 141 | + cluster_horizontal_pod_autoscaling_enabled = "${local.cluster_type_output_horizontal_pod_autoscaling_enabled[local.cluster_type] ? false : true}" |
| 142 | + cluster_kubernetes_dashboard_enabled = "${local.cluster_type_output_kubernetes_dashboard_enabled[local.cluster_type] ? false : true}" |
| 143 | +} |
| 144 | + |
| 145 | +/****************************************** |
| 146 | + Get available container engine versions |
| 147 | + *****************************************/ |
| 148 | +data "google_container_engine_versions" "region" { |
| 149 | + zone = "${data.google_compute_zones.available.names[0]}" |
| 150 | + project = "${var.project_id}" |
| 151 | +} |
0 commit comments