forked from dominodatalab/terraform-gcp-gke
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
358 lines (281 loc) · 9.07 KB
/
main.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
terraform {
required_version = ">= 0.12.0"
backend "gcs" {
bucket = "domino-terraform-default" # Should specify using cli -backend-config="bucket=domino-terraform-default"
# Override with `terraform init -backend-config="prefix=/terraform/state/[YOUR/PATH]"`
prefix = "terraform/state"
}
}
data "http" "myip" {
url = "http://ipv4.icanhazip.com"
}
locals {
cluster = var.cluster == null ? terraform.workspace : var.cluster
enable_private_endpoint = length(var.master_authorized_networks_config) == 0
uuid = "${local.cluster}-${random_uuid.id.result}"
# Converts a cluster's location to a zone/region. A 'location' may be a region or zone: a region becomes the '[region]-a' zone.
region = length(split("-", var.location)) == 2 ? var.location : substr(var.location, 0, length(var.location) - 2)
zone = length(split("-", var.location)) == 3 ? var.location : format("%s-a", var.location)
authorized_networks = var.allow_local_ip_access ? concat(var.master_authorized_networks_config, [{ "display_name" : "myip", "cidr_block" : "${chomp(data.http.myip.body)}/32" }]) : var.master_authorized_networks_config
}
provider "google" {
project = var.project
region = local.region
}
provider "google-beta" {
project = var.project
region = local.region
}
data "google_project" "domino" {
project_id = var.project
}
resource "random_uuid" "id" {}
resource "google_compute_global_address" "static_ip" {
name = local.uuid
description = "External static IPv4 address for var.description"
}
resource "google_dns_record_set" "a" {
name = "${local.cluster}.${var.google_dns_managed_zone.dns_name}"
managed_zone = var.google_dns_managed_zone.name
type = "A"
ttl = 300
rrdatas = [google_compute_global_address.static_ip.address]
}
resource "google_dns_record_set" "caa" {
name = "${local.cluster}.${var.google_dns_managed_zone.dns_name}"
managed_zone = var.google_dns_managed_zone.name
type = "CAA"
ttl = 300
rrdatas = ["0 issue \"letsencrypt.org\"", "0 issue \"pki.goog\""]
}
resource "google_compute_network" "vpc_network" {
name = local.uuid
description = var.description
# This helps lowers our subnet quota utilization
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "default" {
name = local.uuid
ip_cidr_range = "10.138.0.0/20"
network = google_compute_network.vpc_network.self_link
private_ip_google_access = true
description = "${local.cluster} default network"
}
resource "google_compute_router" "router" {
name = local.uuid
network = google_compute_network.vpc_network.self_link
}
resource "google_compute_router_nat" "nat" {
name = local.uuid
router = google_compute_router.router.name
region = local.region
nat_ip_allocate_option = "AUTO_ONLY"
source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"
}
resource "google_storage_bucket" "bucket" {
name = "dominodatalab-${local.cluster}"
location = split("-", var.location)[0]
versioning {
enabled = true
}
force_destroy = true
}
resource "google_filestore_instance" "nfs" {
name = local.uuid
tier = "STANDARD"
zone = local.zone
file_shares {
capacity_gb = var.filestore_capacity_gb
name = "share1"
}
networks {
network = google_compute_network.vpc_network.name
modes = ["MODE_IPV4"]
}
count = var.filestore_disabled ? 0 : 1
}
resource "google_container_cluster" "domino_cluster" {
provider = google-beta
name = local.cluster
location = var.location
description = var.description
release_channel {
channel = var.gke_release_channel
}
# We can't create a cluster with no node pool defined, but we want to only use
# separately managed node pools. So we create the smallest possible default
# node pool and immediately delete it.
remove_default_node_pool = true
# Workaround for https://github.com/terraform-providers/terraform-provider-google/issues/3385
initial_node_count = max(1, var.compute_nodes_min) + max(0, var.gpu_nodes_min) + var.platform_nodes_max
network = google_compute_network.vpc_network.self_link
subnetwork = google_compute_subnetwork.default.self_link
enable_tpu = true
master_auth {
client_certificate_config {
issue_client_certificate = true
}
}
# This resource's provider has issues with reconciling the remote/local state
# of the `issue_client_certificate` field because we use channels to
# implicitly set a cluster version.
#
# We're going to ignore all changes to the `master_auth` block since we set
# these values statically. Hopefully, this issue will be resolved in a future
# version of the provider. See the following issue for more context.
#
# https://github.com/terraform-providers/terraform-provider-google/issues/3369#issuecomment-487226330
lifecycle {
ignore_changes = [master_auth]
}
vertical_pod_autoscaling {
enabled = var.enable_vertical_pod_autoscaling
}
private_cluster_config {
enable_private_endpoint = local.enable_private_endpoint
enable_private_nodes = true
master_ipv4_cidr_block = "172.16.1.0/28"
}
ip_allocation_policy {}
master_authorized_networks_config {
dynamic "cidr_blocks" {
for_each = local.authorized_networks
content {
cidr_block = cidr_blocks.value.cidr_block
display_name = cidr_blocks.value.display_name
}
}
}
resource_labels = {
"uuid" = local.uuid
}
# Application-layer Secrets Encryption
database_encryption {
state = "ENCRYPTED"
key_name = google_kms_crypto_key.crypto_key.self_link
}
workload_identity_config {
identity_namespace = "${data.google_project.domino.project_id}.svc.id.goog"
}
network_policy {
provider = "CALICO"
enabled = var.enable_network_policy
}
pod_security_policy_config {
enabled = var.enable_pod_security_policy
}
}
resource "google_container_node_pool" "platform" {
name = "platform"
location = google_container_cluster.domino_cluster.location
cluster = google_container_cluster.domino_cluster.name
initial_node_count = var.platform_nodes_max
autoscaling {
max_node_count = var.platform_nodes_max
min_node_count = var.platform_nodes_min
}
node_config {
preemptible = var.platform_nodes_preemptible
machine_type = var.platform_node_type
tags = [
"iap-tcp-forwarding-allowed"
]
labels = {
"dominodatalab.com/node-pool" = "platform"
}
disk_size_gb = var.platform_nodes_ssd_gb
local_ssd_count = 1
}
management {
auto_repair = true
auto_upgrade = true
}
timeouts {
delete = "20m"
}
}
resource "google_container_node_pool" "compute" {
name = "compute"
location = google_container_cluster.domino_cluster.location
cluster = google_container_cluster.domino_cluster.name
initial_node_count = max(1, var.compute_nodes_min)
autoscaling {
max_node_count = var.compute_nodes_max
min_node_count = var.compute_nodes_min
}
node_config {
preemptible = var.compute_nodes_preemptible
machine_type = var.compute_node_type
tags = [
"iap-tcp-forwarding-allowed"
]
labels = {
"domino/build-node" = "true"
"dominodatalab.com/build-node" = "true"
"dominodatalab.com/node-pool" = "default"
}
disk_size_gb = var.compute_nodes_ssd_gb
local_ssd_count = 1
}
management {
auto_repair = true
auto_upgrade = true
}
timeouts {
delete = "20m"
}
}
resource "google_kms_key_ring" "key_ring" {
name = local.uuid
location = local.region
}
resource "google_kms_crypto_key" "crypto_key" {
name = local.uuid
key_ring = google_kms_key_ring.key_ring.self_link
rotation_period = "86400s"
purpose = "ENCRYPT_DECRYPT"
}
resource "google_container_node_pool" "gpu" {
name = "gpu"
location = google_container_cluster.domino_cluster.location
cluster = google_container_cluster.domino_cluster.name
initial_node_count = max(0, var.gpu_nodes_min)
autoscaling {
max_node_count = var.gpu_nodes_max
min_node_count = var.gpu_nodes_min
}
node_config {
preemptible = var.gpu_nodes_preemptible
machine_type = var.gpu_node_type
guest_accelerator {
type = var.gpu_nodes_accelerator
count = 1
}
tags = [
"iap-tcp-forwarding-allowed"
]
labels = {
"dominodatalab.com/node-pool" = "default-gpu"
}
disk_size_gb = var.gpu_nodes_ssd_gb
local_ssd_count = 1
}
management {
auto_repair = true
auto_upgrade = true
}
timeouts {
delete = "20m"
}
}
# https://cloud.google.com/iap/docs/using-tcp-forwarding
resource "google_compute_firewall" "iap-tcp-forwarding" {
name = "${local.uuid}-iap"
network = google_compute_network.vpc_network.name
allow {
protocol = "tcp"
ports = ["22"]
}
source_ranges = var.allowed_ssh_ranges
target_tags = ["iap-tcp-forwarding-allowed"]
}