-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcloudrun.tf
172 lines (161 loc) · 5.9 KB
/
cloudrun.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
locals {
cloudrun_ingress_map = {
ALLOW_ALL = "INGRESS_TRAFFIC_ALL"
ALLOW_INTERNAL_ONLY = "INGRESS_TRAFFIC_INTERNAL_ONLY"
ALLOW_INTERNAL_AND_GCLB = "INGRESS_TRAFFIC_INTERNAL_LOAD_BALANCER"
}
}
# ======================== deploy ============================
resource "google_cloud_run_v2_service" "cloud_internal" {
count = local.is_using_cloudfunctions ? 0 : 1
name = local.cloud_internal_function_name
description = "deploy, fetch, resize, clusterize, clusterize finalization, join, join_finalization, terminate, transient, terminate_cluster, scale_up functions"
location = lookup(var.cloud_functions_region_map, var.region, var.region)
ingress = local.cloudrun_ingress_map[local.function_ingress_settings]
deletion_protection = false
traffic {
type = "TRAFFIC_TARGET_ALLOCATION_TYPE_LATEST"
percent = 100
}
template {
timeout = "540s"
scaling {
max_instance_count = 20
min_instance_count = 1
}
service_account = local.sa_email
vpc_access {
connector = local.vpc_connector_id
egress = "ALL_TRAFFIC"
}
containers {
image = "${var.cloud_run_image_prefix}-cloudinternal"
resources {
limits = {
cpu = "1"
memory = "512Mi"
}
}
dynamic "env" {
for_each = local.cloud_internal_function_environment
content {
name = env.key
value = env.value
}
}
}
}
labels = {
goog-partner-solution = "isol_plb32_0014m00001h34hnqai_by7vmugtismizv6y46toim6jigajtrwh"
}
depends_on = [module.network, module.worker_pool, module.shared_vpc_peering, module.peering, google_project_service.run_api]
}
# IAM entry for all users to invoke the function
resource "google_cloud_run_v2_service_iam_member" "cloud_internal_invoker" {
# can't use for_each here, as the elements of `cloud_function_invoker_allowed_members` are known after apply on first run
count = local.is_using_cloudfunctions ? 0 : length(local.cloud_function_invoker_allowed_members)
location = google_cloud_run_v2_service.cloud_internal[0].location
name = google_cloud_run_v2_service.cloud_internal[0].name
role = "roles/run.invoker"
member = local.cloud_function_invoker_allowed_members[count.index]
}
# ======================== scale_down ============================
resource "google_cloud_run_v2_service" "scale_down" {
count = local.is_using_cloudfunctions ? 0 : 1
name = "${var.prefix}-${var.cluster_name}-scale-down"
description = "scale cluster down"
location = lookup(var.cloud_functions_region_map, var.region, var.region)
ingress = local.cloudrun_ingress_map[local.function_ingress_settings]
deletion_protection = false
traffic {
type = "TRAFFIC_TARGET_ALLOCATION_TYPE_LATEST"
percent = 100
}
template {
timeout = "540s"
scaling {
max_instance_count = 3
min_instance_count = 1
}
service_account = local.sa_email
vpc_access {
connector = local.vpc_connector_id
egress = "ALL_TRAFFIC"
}
containers {
image = "${var.cloud_run_image_prefix}-scaledown"
resources {
limits = {
cpu = "1"
memory = "512Mi"
}
}
}
}
labels = {
goog-partner-solution = "isol_plb32_0014m00001h34hnqai_by7vmugtismizv6y46toim6jigajtrwh"
}
depends_on = [module.network, module.worker_pool, module.shared_vpc_peering, google_project_service.run_api]
}
# IAM entry for all users to invoke the function
resource "google_cloud_run_v2_service_iam_member" "weka_internal_invoker" {
# can't use for_each here, as the elements of `cloud_function_invoker_allowed_members` are known after apply on first run
count = local.is_using_cloudfunctions ? 0 : length(local.cloud_function_invoker_allowed_members)
location = google_cloud_run_v2_service.scale_down[0].location
name = google_cloud_run_v2_service.scale_down[0].name
role = "roles/run.invoker"
member = local.cloud_function_invoker_allowed_members[count.index]
}
# ======================== status ============================
resource "google_cloud_run_v2_service" "status" {
count = local.is_using_cloudfunctions ? 0 : 1
name = "${var.prefix}-${var.cluster_name}-status"
description = "get cluster status"
location = lookup(var.cloud_functions_region_map, var.region, var.region)
ingress = local.cloudrun_ingress_map[local.function_ingress_settings]
deletion_protection = false
traffic {
type = "TRAFFIC_TARGET_ALLOCATION_TYPE_LATEST"
percent = 100
}
template {
timeout = "540s"
scaling {
max_instance_count = 3
min_instance_count = 1
}
service_account = local.sa_email
vpc_access {
connector = local.vpc_connector_id
egress = "ALL_TRAFFIC"
}
containers {
image = "${var.cloud_run_image_prefix}-status"
resources {
limits = {
cpu = "1"
memory = "512Mi"
}
}
dynamic "env" {
for_each = local.status_function_environment
content {
name = env.key
value = env.value
}
}
}
}
labels = {
goog-partner-solution = "isol_plb32_0014m00001h34hnqai_by7vmugtismizv6y46toim6jigajtrwh"
}
depends_on = [module.network, module.worker_pool, module.shared_vpc_peering, google_project_service.run_api]
}
# IAM entry for all users to invoke the function
resource "google_cloud_run_v2_service_iam_member" "status_invoker" {
count = local.is_using_cloudfunctions ? 0 : length(local.cloud_function_invoker_allowed_members)
location = google_cloud_run_v2_service.status[0].location
name = google_cloud_run_v2_service.status[0].name
role = "roles/run.invoker"
member = local.cloud_function_invoker_allowed_members[count.index]
}