-
Notifications
You must be signed in to change notification settings - Fork 4
/
workflows.tf
187 lines (169 loc) · 5.87 KB
/
workflows.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
resource "google_project_service" "workflows" {
service = "workflows.googleapis.com"
disable_on_destroy = false
disable_dependent_services = false
}
data "google_project" "project" {
}
resource "google_project_iam_member" "cloudscheduler" {
count = var.create_cloudscheduler_sa ? 1 : 0
project = var.project_id
role = "roles/cloudscheduler.jobRunner"
member = "serviceAccount:service-${local.deployment_project_number}@gcp-sa-cloudscheduler.iam.gserviceaccount.com"
}
resource "google_workflows_workflow" "scale_down" {
name = "${var.prefix}-${var.cluster_name}-scale-down-workflow"
region = lookup(var.workflow_map_region, var.region, var.region)
description = "scale down workflow"
service_account = local.sa_email
source_contents = <<-EOF
- fetch:
call: http.post
args:
url: ${google_cloudfunctions2_function.cloud_internal_function.service_config[0].uri}
query:
action: fetch
auth:
type: OIDC
result: FetchResult
- scale_down:
call: http.post
args:
url: ${google_cloudfunctions2_function.scale_down_function.service_config[0].uri}
body: $${FetchResult.body}
auth:
type: OIDC
result: ScaleResult
- terminate:
call: http.post
args:
url: ${google_cloudfunctions2_function.cloud_internal_function.service_config[0].uri}
query:
action: terminate
body: $${ScaleResult.body}
auth:
type: OIDC
result: TerminateResult
- transient:
call: http.post
args:
url: ${google_cloudfunctions2_function.cloud_internal_function.service_config[0].uri}
query:
action: transient
body: $${TerminateResult.body}
auth:
type: OIDC
result: TransientResult
- returnOutput:
return: $${TransientResult}
EOF
labels = {
goog-partner-solution = "isol_plb32_0014m00001h34hnqai_by7vmugtismizv6y46toim6jigajtrwh"
}
depends_on = [google_project_service.workflows, google_cloudfunctions2_function.scale_down_function, google_cloudfunctions2_function.cloud_internal_function]
}
resource "google_pubsub_topic" "scale_down_trigger_topic" {
name = "${var.prefix}-${var.cluster_name}-scale-down"
labels = {
goog-partner-solution = "isol_plb32_0014m00001h34hnqai_by7vmugtismizv6y46toim6jigajtrwh"
}
}
# needed for google_eventarc_trigger
resource "google_project_service" "eventarc_api" {
service = "eventarc.googleapis.com"
disable_on_destroy = false
disable_dependent_services = false
}
resource "google_eventarc_trigger" "scale_down_trigger" {
name = "${var.prefix}-${var.cluster_name}-scale-down"
location = var.region
matching_criteria {
attribute = "type"
value = "google.cloud.pubsub.topic.v1.messagePublished"
}
destination {
workflow = google_workflows_workflow.scale_down.name
}
transport {
pubsub {
topic = google_pubsub_topic.scale_down_trigger_topic.name
}
}
service_account = local.sa_email
labels = {
goog-partner-solution = "isol_plb32_0014m00001h34hnqai_by7vmugtismizv6y46toim6jigajtrwh"
}
depends_on = [google_workflows_workflow.scale_down, google_pubsub_topic.scale_down_trigger_topic]
}
resource "google_cloud_scheduler_job" "scale_down_job" {
name = "${var.prefix}-${var.cluster_name}-scale-down"
description = "scale down job"
schedule = "* * * * *"
region = lookup(var.cloud_scheduler_region_map, var.region, var.region)
pubsub_target {
topic_name = google_pubsub_topic.scale_down_trigger_topic.id
data = base64encode("placeholder")
}
depends_on = [google_eventarc_trigger.scale_down_trigger]
}
resource "google_workflows_workflow" "scale_up" {
name = "${var.prefix}-${var.cluster_name}-scale-up-workflow"
region = lookup(var.workflow_map_region, var.region, var.region)
description = "scale up workflow"
service_account = local.sa_email
source_contents = <<-EOF
- scale_up:
call: http.get
args:
url: ${google_cloudfunctions2_function.cloud_internal_function.service_config[0].uri}
query:
action: scale_up
auth:
type: OIDC
result: ScaleUpResult
- returnOutput:
return: $${ScaleUpResult}
EOF
labels = {
goog-partner-solution = "isol_plb32_0014m00001h34hnqai_by7vmugtismizv6y46toim6jigajtrwh"
}
depends_on = [google_project_service.workflows, google_cloudfunctions2_function.cloud_internal_function]
}
resource "google_pubsub_topic" "scale_up_trigger_topic" {
name = "${var.prefix}-${var.cluster_name}-scale-up"
labels = {
goog-partner-solution = "isol_plb32_0014m00001h34hnqai_by7vmugtismizv6y46toim6jigajtrwh"
}
}
resource "google_eventarc_trigger" "scale_up_trigger" {
name = "${var.prefix}-${var.cluster_name}-scale-up"
location = var.region
matching_criteria {
attribute = "type"
value = "google.cloud.pubsub.topic.v1.messagePublished"
}
destination {
workflow = google_workflows_workflow.scale_up.name
}
transport {
pubsub {
topic = google_pubsub_topic.scale_up_trigger_topic.name
}
}
service_account = local.sa_email
labels = {
goog-partner-solution = "isol_plb32_0014m00001h34hnqai_by7vmugtismizv6y46toim6jigajtrwh"
}
depends_on = [google_workflows_workflow.scale_up, google_pubsub_topic.scale_up_trigger_topic]
}
resource "google_cloud_scheduler_job" "scale_up_job" {
name = "${var.prefix}-${var.cluster_name}-scale-up"
description = "scale up job"
schedule = "* * * * *"
region = lookup(var.cloud_scheduler_region_map, var.region, var.region)
pubsub_target {
topic_name = google_pubsub_topic.scale_up_trigger_topic.id
data = base64encode("placeholder")
}
depends_on = [google_eventarc_trigger.scale_up_trigger]
}