-
Notifications
You must be signed in to change notification settings - Fork 71
/
main.tf
411 lines (336 loc) · 11.2 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/**
* 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.
*/
provider "google" {
project = var.project_id
}
provider "google-beta" {
project = var.project_id
}
locals {
gitlab_db_name = var.gitlab_db_random_prefix ? "${var.gitlab_db_name}-${random_id.suffix[0].hex}" : var.gitlab_db_name
}
resource "random_id" "suffix" {
count = var.gitlab_db_random_prefix ? 1 : 0
byte_length = 4
}
module "gke_auth" {
source = "terraform-google-modules/kubernetes-engine/google//modules/auth"
version = "~> 24.0"
project_id = module.project_services.project_id
cluster_name = module.gke.name
location = module.gke.location
depends_on = [time_sleep.sleep_for_cluster_fix_helm_6361]
}
provider "helm" {
kubernetes {
cluster_ca_certificate = module.gke_auth.cluster_ca_certificate
host = module.gke_auth.host
token = module.gke_auth.token
}
}
provider "kubernetes" {
cluster_ca_certificate = module.gke_auth.cluster_ca_certificate
host = module.gke_auth.host
token = module.gke_auth.token
}
// Services
module "project_services" {
source = "terraform-google-modules/project-factory/google//modules/project_services"
version = "~> 14.0"
project_id = var.project_id
disable_services_on_destroy = false
activate_apis = [
"compute.googleapis.com",
"container.googleapis.com",
"servicenetworking.googleapis.com",
"cloudresourcemanager.googleapis.com",
"redis.googleapis.com"
]
}
// GCS Service Account
resource "google_service_account" "gitlab_gcs" {
project = var.project_id
account_id = "gitlab-gcs"
display_name = "GitLab Cloud Storage"
}
resource "google_service_account_key" "gitlab_gcs" {
service_account_id = google_service_account.gitlab_gcs.name
}
resource "google_project_iam_member" "project" {
project = var.project_id
role = "roles/storage.admin"
member = "serviceAccount:${google_service_account.gitlab_gcs.email}"
}
// Networking
resource "google_compute_network" "gitlab" {
name = "gitlab"
project = module.project_services.project_id
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "subnetwork" {
name = "gitlab"
ip_cidr_range = var.gitlab_nodes_subnet_cidr
region = var.region
network = google_compute_network.gitlab.self_link
secondary_ip_range {
range_name = "gitlab-cluster-pod-cidr"
ip_cidr_range = var.gitlab_pods_subnet_cidr
}
secondary_ip_range {
range_name = "gitlab-cluster-service-cidr"
ip_cidr_range = var.gitlab_services_subnet_cidr
}
}
resource "google_compute_address" "gitlab" {
name = "gitlab"
region = var.region
address_type = "EXTERNAL"
description = "Gitlab Ingress IP"
depends_on = [module.project_services.project_id]
count = var.gitlab_address_name == "" ? 1 : 0
}
// Database
resource "google_compute_global_address" "gitlab_sql" {
provider = google-beta
project = var.project_id
name = "gitlab-sql"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
network = google_compute_network.gitlab.self_link
address = "10.1.0.0"
prefix_length = 16
}
resource "google_service_networking_connection" "private_vpc_connection" {
provider = google-beta
network = google_compute_network.gitlab.self_link
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.gitlab_sql.name]
depends_on = [module.project_services.project_id]
}
resource "google_sql_database_instance" "gitlab_db" {
depends_on = [google_service_networking_connection.private_vpc_connection]
name = local.gitlab_db_name
region = var.region
database_version = "POSTGRES_11"
deletion_protection = var.gitlab_deletion_protection
settings {
tier = "db-custom-4-15360"
disk_autoresize = true
ip_configuration {
ipv4_enabled = "false"
private_network = google_compute_network.gitlab.self_link
}
}
}
resource "google_sql_database" "gitlabhq_production" {
name = "gitlabhq_production"
instance = google_sql_database_instance.gitlab_db.name
depends_on = [google_sql_user.gitlab]
}
resource "random_string" "autogenerated_gitlab_db_password" {
length = 16
special = false
}
resource "google_sql_user" "gitlab" {
name = "gitlab"
instance = google_sql_database_instance.gitlab_db.name
# Postgres users cannot be deleted if they have been granted SQL roles
deletion_policy = "ABANDON"
password = var.gitlab_db_password != "" ? var.gitlab_db_password : random_string.autogenerated_gitlab_db_password.result
}
// Redis
resource "google_redis_instance" "gitlab" {
name = "gitlab"
tier = "STANDARD_HA"
memory_size_gb = 5
region = var.region
authorized_network = google_compute_network.gitlab.self_link
depends_on = [module.project_services.project_id]
display_name = "GitLab Redis"
}
// Cloud Storage
resource "google_storage_bucket" "gitlab-backups" {
name = "${var.project_id}-gitlab-backups"
location = var.region
force_destroy = var.allow_force_destroy
}
resource "google_storage_bucket" "gitlab-uploads" {
name = "${var.project_id}-gitlab-uploads"
location = var.region
force_destroy = var.allow_force_destroy
}
resource "google_storage_bucket" "gitlab-artifacts" {
name = "${var.project_id}-gitlab-artifacts"
location = var.region
force_destroy = var.allow_force_destroy
}
resource "google_storage_bucket" "git-lfs" {
name = "${var.project_id}-git-lfs"
location = var.region
force_destroy = var.allow_force_destroy
}
resource "google_storage_bucket" "gitlab-packages" {
name = "${var.project_id}-gitlab-packages"
location = var.region
force_destroy = var.allow_force_destroy
}
resource "google_storage_bucket" "gitlab-registry" {
name = "${var.project_id}-registry"
location = var.region
force_destroy = var.allow_force_destroy
}
resource "google_storage_bucket" "gitlab-pseudo" {
name = "${var.project_id}-pseudo"
location = var.region
force_destroy = var.allow_force_destroy
}
resource "google_storage_bucket" "gitlab-runner-cache" {
name = "${var.project_id}-runner-cache"
location = var.region
force_destroy = var.allow_force_destroy
}
// GKE Cluster
module "gke" {
source = "terraform-google-modules/kubernetes-engine/google"
version = "~> 24.0"
# Create an implicit dependency on service activation
project_id = module.project_services.project_id
name = "gitlab"
region = var.region
regional = true
kubernetes_version = var.gke_version
remove_default_node_pool = true
initial_node_count = 1
network = google_compute_network.gitlab.name
subnetwork = google_compute_subnetwork.subnetwork.name
ip_range_pods = "gitlab-cluster-pod-cidr"
ip_range_services = "gitlab-cluster-service-cidr"
issue_client_certificate = true
node_pools = [
{
name = "gitlab"
autoscaling = false
machine_type = var.gke_machine_type
node_count = 1
},
]
node_pools_oauth_scopes = {
all = ["https://www.googleapis.com/auth/cloud-platform"]
}
}
resource "kubernetes_storage_class" "pd-ssd" {
metadata {
name = "pd-ssd"
}
storage_provisioner = "kubernetes.io/gce-pd"
parameters = {
type = "pd-ssd"
}
depends_on = [time_sleep.sleep_for_cluster_fix_helm_6361]
}
resource "kubernetes_secret" "gitlab_pg" {
metadata {
name = "gitlab-pg"
}
data = {
password = var.gitlab_db_password != "" ? var.gitlab_db_password : random_string.autogenerated_gitlab_db_password.result
}
depends_on = [time_sleep.sleep_for_cluster_fix_helm_6361]
}
resource "kubernetes_secret" "gitlab_rails_storage" {
metadata {
name = "gitlab-rails-storage"
}
data = {
connection = <<EOT
provider: Google
google_project: ${var.project_id}
google_client_email: ${google_service_account.gitlab_gcs.email}
google_json_key_string: '${base64decode(google_service_account_key.gitlab_gcs.private_key)}'
EOT
}
depends_on = [time_sleep.sleep_for_cluster_fix_helm_6361]
}
resource "kubernetes_secret" "gitlab_registry_storage" {
metadata {
name = "gitlab-registry-storage"
}
data = {
"gcs.json" = <<EOT
${base64decode(google_service_account_key.gitlab_gcs.private_key)}
EOT
storage = <<EOT
gcs:
bucket: ${var.project_id}-registry
keyfile: /etc/docker/registry/storage/gcs.json
EOT
}
depends_on = [time_sleep.sleep_for_cluster_fix_helm_6361]
}
resource "kubernetes_secret" "gitlab_gcs_credentials" {
metadata {
name = "google-application-credentials"
}
data = {
gcs-application-credentials-file = base64decode(google_service_account_key.gitlab_gcs.private_key)
}
depends_on = [time_sleep.sleep_for_cluster_fix_helm_6361]
}
data "google_compute_address" "gitlab" {
name = var.gitlab_address_name
region = var.region
# Do not get data if the address is being created as part of the run
count = var.gitlab_address_name == "" ? 0 : 1
}
locals {
gitlab_address = var.gitlab_address_name == "" ? google_compute_address.gitlab[0].address : data.google_compute_address.gitlab[0].address
domain = var.domain != "" ? var.domain : "${local.gitlab_address}.nip.io"
}
data "template_file" "helm_values" {
template = file("${path.module}/values.yaml.tpl")
vars = {
DOMAIN = local.domain
INGRESS_IP = local.gitlab_address
DB_PRIVATE_IP = google_sql_database_instance.gitlab_db.private_ip_address
REDIS_PRIVATE_IP = google_redis_instance.gitlab.host
PROJECT_ID = var.project_id
CERT_MANAGER_EMAIL = var.certmanager_email
GITLAB_RUNNER_INSTALL = var.gitlab_runner_install
}
}
resource "time_sleep" "sleep_for_cluster_fix_helm_6361" {
create_duration = "180s"
destroy_duration = "180s"
depends_on = [module.gke.endpoint, google_sql_database.gitlabhq_production]
}
resource "helm_release" "gitlab" {
name = "gitlab"
repository = "https://charts.gitlab.io"
chart = "gitlab"
version = var.helm_chart_version
timeout = 1200
values = [data.template_file.helm_values.rendered]
depends_on = [
google_redis_instance.gitlab,
google_sql_user.gitlab,
kubernetes_storage_class.pd-ssd,
kubernetes_secret.gitlab_pg,
kubernetes_secret.gitlab_rails_storage,
kubernetes_secret.gitlab_registry_storage,
kubernetes_secret.gitlab_gcs_credentials,
time_sleep.sleep_for_cluster_fix_helm_6361,
]
}