-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
382 lines (316 loc) · 10.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
/**
* 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
credentials = var.google_credentials
}
provider "google-beta" {
project = var.project_id
credentials = var.google_credentials
}
provider "helm" {
service_account = "tiller"
install_tiller = true
namespace = "kube-system"
kubernetes {
host = google_container_cluster.gitlab.endpoint
client_certificate = base64decode(google_container_cluster.gitlab.master_auth[0].client_certificate)
client_key = base64decode(google_container_cluster.gitlab.master_auth[0].client_key)
cluster_ca_certificate = base64decode(google_container_cluster.gitlab.master_auth[0].cluster_ca_certificate)
}
}
provider "kubernetes" {
host = google_container_cluster.gitlab.endpoint
client_certificate = base64decode(google_container_cluster.gitlab.master_auth[0].client_certificate)
client_key = base64decode(google_container_cluster.gitlab.master_auth[0].client_key)
cluster_ca_certificate = base64decode(google_container_cluster.gitlab.master_auth[0].cluster_ca_certificate)
}
// IAM
resource "google_project_service" "compute" {
project = var.project_id
service = "compute.googleapis.com"
disable_on_destroy = false
}
resource "google_project_service" "gke" {
project = var.project_id
service = "container.googleapis.com"
disable_on_destroy = false
}
resource "google_project_service" "service_networking" {
project = var.project_id
service = "servicenetworking.googleapis.com"
disable_on_destroy = false
}
resource "google_project_service" "cloudresourcemanager" {
project = var.project_id
service = "cloudresourcemanager.googleapis.com"
disable_on_destroy = false
}
resource "google_project_service" "redis" {
project = var.project_id
service = "redis.googleapis.com"
disable_on_destroy = false
}
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 = var.project_id
auto_create_subnetworks = false
depends_on = [google_project_service.compute]
}
resource "google_compute_subnetwork" "us-central" {
name = "gitlab"
ip_cidr_range = "10.0.0.0/16"
region = var.region
network = google_compute_network.gitlab.self_link
}
resource "google_compute_address" "gitlab" {
name = "gitlab"
region = var.region
address_type = "EXTERNAL"
description = "Gitlab Ingress IP"
depends_on = [google_project_service.compute]
}
// 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
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]
}
resource "google_sql_database_instance" "gitlab_db" {
depends_on = [google_service_networking_connection.private_vpc_connection]
name = "gitlab-db"
region = var.region
database_version = "POSTGRES_9_6"
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
}
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
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 = [google_project_service.redis]
location_id = "${var.region}-a"
alternative_location_id = "${var.region}-c"
display_name = "GitLab Redis"
}
// Cloud Storage
resource "google_storage_bucket" "gitlab-uploads" {
name = "${var.project_id}-uploads"
location = var.region
}
resource "google_storage_bucket" "gitlab-artifacts" {
name = "${var.project_id}-artifacts"
location = var.region
}
resource "google_storage_bucket" "gitlab-lfs" {
name = "${var.project_id}-lfs"
location = var.region
}
resource "google_storage_bucket" "gitlab-packages" {
name = "${var.project_id}-packages"
location = var.region
}
resource "google_storage_bucket" "gitlab-registry" {
name = "${var.project_id}-registry"
location = var.region
}
// GKE Cluster
resource "google_container_cluster" "gitlab" {
project = var.project_id
name = "gitlab"
location = var.region
min_master_version = "1.11"
# 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
initial_node_count = 1
network = google_compute_network.gitlab.self_link
subnetwork = google_compute_subnetwork.us-central.self_link
ip_allocation_policy {
# Allocate ranges automatically
cluster_ipv4_cidr_block = ""
services_ipv4_cidr_block = ""
}
enable_legacy_abac = true
# Setting an empty username and password explicitly disables basic auth
master_auth {
username = ""
password = ""
}
node_config {
oauth_scopes = [
"https://www.googleapis.com/auth/compute",
"https://www.googleapis.com/auth/devstorage.read_only",
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring",
]
}
depends_on = [google_project_service.gke]
}
resource "google_container_node_pool" "gitlab" {
name = "gitlab"
location = var.region
cluster = google_container_cluster.gitlab.name
node_count = 1
depends_on = []
node_config {
preemptible = false
machine_type = "n1-standard-4"
oauth_scopes = [
"https://www.googleapis.com/auth/compute",
"https://www.googleapis.com/auth/devstorage.read_only",
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring",
]
}
}
resource "kubernetes_service_account" "tiller" {
metadata {
name = "tiller"
namespace = "kube-system"
}
}
resource "kubernetes_cluster_role_binding" "tiller-admin" {
metadata {
name = "tiller-admin"
}
role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "ClusterRole"
name = "cluster-admin"
}
subject {
kind = "ServiceAccount"
name = "tiller"
namespace = "kube-system"
}
}
resource "kubernetes_storage_class" "pd-ssd" {
metadata {
name = "pd-ssd"
}
storage_provisioner = "kubernetes.io/gce-pd"
parameters = {
type = "pd-ssd"
}
}
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
}
}
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
}
}
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)
}
}
data "helm_repository" "gitlab" {
name = "gitlab"
url = "https://charts.gitlab.io"
}
data "template_file" "helm_values" {
template = file("${path.module}/values.yaml.tpl")
vars = {
INGRESS_IP = google_compute_address.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
OMNIAUTH_ENABLED = var.omniauth_enable
BLOCK_AUTO_CREATED_USERS = var.blockAutoCreatedUsers
}
}
resource "helm_release" "gitlab" {
name = "gitlab"
repository = data.helm_repository.gitlab.name
chart = "gitlab"
version = "1.7.1"
timeout = 600
values = [data.template_file.helm_values.rendered]
depends_on = [
google_redis_instance.gitlab,
google_sql_database.gitlabhq_production,
google_sql_user.gitlab,
kubernetes_cluster_role_binding.tiller-admin,
kubernetes_storage_class.pd-ssd,
]
}