-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.tf
243 lines (198 loc) · 6.96 KB
/
cluster.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// This file was automatically generated from a template in ./autogen/main
/******************************************
Create Container Cluster
*****************************************/
resource "google_container_cluster" "primary" {
provider = google
name = var.name
description = var.description
project = var.project_id
resource_labels = var.cluster_resource_labels
location = local.location
node_locations = local.node_locations
cluster_ipv4_cidr = var.cluster_ipv4_cidr
network = data.google_compute_network.gke_network.self_link
dynamic "network_policy" {
for_each = local.cluster_network_policy
content {
enabled = network_policy.value.enabled
provider = network_policy.value.provider
}
}
subnetwork = data.google_compute_subnetwork.gke_subnetwork.self_link
min_master_version = local.master_version
logging_service = var.logging_service
monitoring_service = var.monitoring_service
dynamic "master_authorized_networks_config" {
for_each = local.master_authorized_networks_config
content {
dynamic "cidr_blocks" {
for_each = master_authorized_networks_config.value.cidr_blocks
content {
cidr_block = lookup(cidr_blocks.value, "cidr_block", "")
display_name = lookup(cidr_blocks.value, "display_name", "")
}
}
}
}
master_auth {
username = var.basic_auth_username
password = var.basic_auth_password
client_certificate_config {
issue_client_certificate = var.issue_client_certificate
}
}
addons_config {
http_load_balancing {
disabled = ! var.http_load_balancing
}
horizontal_pod_autoscaling {
disabled = ! var.horizontal_pod_autoscaling
}
network_policy_config {
disabled = ! var.network_policy
}
}
ip_allocation_policy {
cluster_secondary_range_name = var.ip_range_pods
services_secondary_range_name = var.ip_range_services
}
maintenance_policy {
daily_maintenance_window {
start_time = var.maintenance_start_time
}
}
lifecycle {
ignore_changes = [node_pool, initial_node_count]
}
timeouts {
create = "30m"
update = "30m"
delete = "30m"
}
node_pool {
name = "default-pool"
initial_node_count = var.initial_node_count
node_config {
service_account = lookup(var.node_pools[0], "service_account", local.service_account)
}
}
remove_default_node_pool = var.remove_default_node_pool
}
/******************************************
Create Container Cluster node pools
*****************************************/
resource "google_container_node_pool" "pools" {
provider = google
for_each = local.node_pools
name = each.key
project = var.project_id
location = local.location
cluster = google_container_cluster.primary.name
version = lookup(each.value, "auto_upgrade", false) ? "" : lookup(
each.value,
"version",
google_container_cluster.primary.min_master_version,
)
initial_node_count = lookup(each.value, "autoscaling", true) ? lookup(
each.value,
"initial_node_count",
lookup(each.value, "min_count", 1)
) : null
node_count = lookup(each.value, "autoscaling", true) ? null : lookup(each.value, "node_count", 1)
dynamic "autoscaling" {
for_each = lookup(each.value, "autoscaling", true) ? [each.value] : []
content {
min_node_count = lookup(autoscaling.value, "min_count", 1)
max_node_count = lookup(autoscaling.value, "max_count", 100)
}
}
management {
auto_repair = lookup(each.value, "auto_repair", true)
auto_upgrade = lookup(each.value, "auto_upgrade", local.default_auto_upgrade)
}
node_config {
image_type = lookup(each.value, "image_type", "COS")
machine_type = lookup(each.value, "machine_type", "n1-standard-2")
labels = merge(
lookup(lookup(local.node_pools_labels, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {},
lookup(lookup(local.node_pools_labels, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {},
local.node_pools_labels["all"],
local.node_pools_labels[each.value["name"]],
)
metadata = merge(
lookup(lookup(local.node_pools_metadata, "default_values", {}), "cluster_name", true) ? { "cluster_name" = var.name } : {},
lookup(lookup(local.node_pools_metadata, "default_values", {}), "node_pool", true) ? { "node_pool" = each.value["name"] } : {},
local.node_pools_metadata["all"],
local.node_pools_metadata[each.value["name"]],
{
"disable-legacy-endpoints" = var.disable_legacy_metadata_endpoints
},
)
tags = concat(
lookup(local.node_pools_tags, "default_values", [true, true])[0] ? ["gke-${var.name}"] : [],
lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["gke-${var.name}-${each.value["name"]}"] : [],
local.node_pools_tags["all"],
local.node_pools_tags[each.value["name"]],
)
local_ssd_count = lookup(each.value, "local_ssd_count", 0)
disk_size_gb = lookup(each.value, "disk_size_gb", 100)
disk_type = lookup(each.value, "disk_type", "pd-standard")
service_account = lookup(
each.value,
"service_account",
local.service_account,
)
preemptible = lookup(each.value, "preemptible", false)
oauth_scopes = concat(
local.node_pools_oauth_scopes["all"],
local.node_pools_oauth_scopes[each.value["name"]],
)
guest_accelerator = [
for guest_accelerator in lookup(each.value, "accelerator_count", 0) > 0 ? [{
type = lookup(each.value, "accelerator_type", "")
count = lookup(each.value, "accelerator_count", 0)
}] : [] : {
type = guest_accelerator["type"]
count = guest_accelerator["count"]
}
]
}
lifecycle {
ignore_changes = [initial_node_count]
}
timeouts {
create = "30m"
update = "30m"
delete = "30m"
}
}
resource "null_resource" "wait_for_cluster" {
count = var.skip_provisioners ? 0 : 1
provisioner "local-exec" {
command = "${path.module}/scripts/wait-for-cluster.sh ${var.project_id} ${var.name}"
}
provisioner "local-exec" {
when = destroy
command = "${path.module}/scripts/wait-for-cluster.sh ${var.project_id} ${var.name}"
}
depends_on = [
google_container_cluster.primary,
google_container_node_pool.pools,
]
}