-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.tf
368 lines (308 loc) · 11.7 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
* Copyright 2022 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 = "projects/${local.network_project_id}/global/networks/${var.network}"
dynamic "network_policy" {
for_each = local.cluster_network_policy
content {
enabled = network_policy.value.enabled
provider = network_policy.value.provider
}
}
dynamic "release_channel" {
for_each = local.release_channel
content {
channel = release_channel.value.channel
}
}
subnetwork = "projects/${local.network_project_id}/regions/${local.region}/subnetworks/${var.subnetwork}"
min_master_version = var.release_channel != null ? null : local.master_version
logging_service = var.logging_service
monitoring_service = var.monitoring_service
cluster_autoscaling {
enabled = var.cluster_autoscaling.enabled
dynamic "auto_provisioning_defaults" {
for_each = var.cluster_autoscaling.enabled ? [1] : []
content {
service_account = local.service_account
oauth_scopes = local.node_pools_oauth_scopes["all"]
}
}
dynamic "resource_limits" {
for_each = local.autoscaling_resource_limits
content {
resource_type = lookup(resource_limits.value, "resource_type")
minimum = lookup(resource_limits.value, "minimum")
maximum = lookup(resource_limits.value, "maximum")
}
}
}
vertical_pod_autoscaling {
enabled = var.enable_vertical_pod_autoscaling
}
default_max_pods_per_node = var.default_max_pods_per_node
enable_shielded_nodes = var.enable_shielded_nodes
enable_binary_authorization = var.enable_binary_authorization
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 {
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
}
gcp_filestore_csi_driver_config {
enabled = var.filestore_csi_driver
}
}
datapath_provider = var.datapath_provider
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, resource_labels["asmv"], resource_labels["mesh_id"]]
}
timeouts {
create = "45m"
update = "45m"
delete = "45m"
}
node_pool {
name = "default-pool"
initial_node_count = var.initial_node_count
node_config {
image_type = lookup(var.node_pools[0], "image_type", "COS_CONTAINERD")
machine_type = lookup(var.node_pools[0], "machine_type", "e2-medium")
min_cpu_platform = lookup(var.node_pools[0], "min_cpu_platform", "")
dynamic "gcfs_config" {
for_each = lookup(var.node_pools[0], "enable_gcfs", false) ? [true] : []
content {
enabled = gcfs_config.value
}
}
service_account = lookup(var.node_pools[0], "service_account", local.service_account)
tags = concat(
lookup(local.node_pools_tags, "default_values", [true, true])[0] ? [local.cluster_network_tag] : [],
lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["${local.cluster_network_tag}-default-pool"] : [],
lookup(local.node_pools_tags, "all", []),
lookup(local.node_pools_tags, var.node_pools[0].name, []),
)
dynamic "workload_metadata_config" {
for_each = local.cluster_node_metadata_config
content {
mode = workload_metadata_config.value.mode
}
}
metadata = local.node_pools_metadata["all"]
shielded_instance_config {
enable_secure_boot = lookup(var.node_pools[0], "enable_secure_boot", false)
enable_integrity_monitoring = lookup(var.node_pools[0], "enable_integrity_monitoring", true)
}
}
}
dynamic "resource_usage_export_config" {
for_each = var.resource_usage_export_dataset_id != "" ? [{
enable_network_egress_metering = var.enable_network_egress_export
enable_resource_consumption_metering = var.enable_resource_consumption_export
dataset_id = var.resource_usage_export_dataset_id
}] : []
content {
enable_network_egress_metering = resource_usage_export_config.value.enable_network_egress_metering
enable_resource_consumption_metering = resource_usage_export_config.value.enable_resource_consumption_metering
bigquery_destination {
dataset_id = resource_usage_export_config.value.dataset_id
}
}
}
remove_default_node_pool = var.remove_default_node_pool
dynamic "database_encryption" {
for_each = var.database_encryption
content {
key_name = database_encryption.value.key_name
state = database_encryption.value.state
}
}
dynamic "workload_identity_config" {
for_each = local.cluster_workload_identity_config
content {
workload_pool = workload_identity_config.value.workload_pool
}
}
dynamic "authenticator_groups_config" {
for_each = local.cluster_authenticator_security_group
content {
security_group = authenticator_groups_config.value.security_group
}
}
}
/******************************************
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
// use node_locations if provided, defaults to cluster level node_locations if not specified
node_locations = lookup(each.value, "node_locations", "") != "" ? split(",", each.value["node_locations"]) : null
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
max_pods_per_node = lookup(each.value, "max_pods_per_node", 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_CONTAINERD")
machine_type = lookup(each.value, "machine_type", "e2-medium")
min_cpu_platform = lookup(each.value, "min_cpu_platform", "")
dynamic "gcfs_config" {
for_each = lookup(each.value, "enable_gcfs", false) ? [true] : []
content {
enabled = gcfs_config.value
}
}
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
},
)
dynamic "taint" {
for_each = concat(
local.node_pools_taints["all"],
local.node_pools_taints[each.value["name"]],
)
content {
effect = taint.value.effect
key = taint.value.key
value = taint.value.value
}
}
tags = concat(
lookup(local.node_pools_tags, "default_values", [true, true])[0] ? [local.cluster_network_tag] : [],
lookup(local.node_pools_tags, "default_values", [true, true])[1] ? ["${local.cluster_network_tag}-${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)
gpu_partition_size = lookup(each.value, "gpu_partition_size", null)
}] : [] : {
type = guest_accelerator["type"]
count = guest_accelerator["count"]
gpu_partition_size = guest_accelerator["gpu_partition_size"]
}
]
dynamic "workload_metadata_config" {
for_each = local.cluster_node_metadata_config
content {
mode = lookup(each.value, "node_metadata", workload_metadata_config.value.mode)
}
}
shielded_instance_config {
enable_secure_boot = lookup(each.value, "enable_secure_boot", false)
enable_integrity_monitoring = lookup(each.value, "enable_integrity_monitoring", true)
}
}
lifecycle {
ignore_changes = [initial_node_count]
}
timeouts {
create = "45m"
update = "45m"
delete = "45m"
}
}