-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.tf
More file actions
566 lines (511 loc) · 14.6 KB
/
Copy pathmain.tf
File metadata and controls
566 lines (511 loc) · 14.6 KB
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
helm = {
source = "hashicorp/helm"
version = ">= 2.17.0"
}
kubernetes = {
source = "hashicorp/kubernetes"
}
}
}
variable "release_name" {
description = "The release name of the installed Helm app."
type = string
default = "coder"
}
variable "chart_name" {
description = "The chart name of the installed Helm app."
type = string
default = "coder"
}
variable "cluster_name" {
type = string
}
variable "cluster_oidc_provider_arn" {
type = string
}
variable "policy_resource_region" {
type = string
default = null
}
variable "policy_resource_account" {
type = string
default = null
}
variable "policy_name" {
type = string
default = "coder-srv"
}
variable "role_name" {
type = string
default = "coder-srv"
}
##
# Kubernetes Inputs
##
variable "namespace" {
type = string
}
variable "helm_timeout" {
type = number
default = 300 # In Seconds
}
variable "chart_version" {
type = string
default = "2.25.1"
}
variable "coder" {
type = object({
access_url = string
wildcard_url = string
redirect = optional(bool, true)
mount_ssl = optional(bool, false)
mount_ssl_name = optional(string, "cert")
image_repo = optional(string, "ghcr.io/coder/coder")
image_tag = optional(string, "latest")
image_pull_policy = optional(string, "IfNotPresent")
image_pull_secrets = optional(list(string), null)
csp_policy = optional(string, null)
env_vars = optional(map(string), {})
rep_cnt = optional(number, 1)
prov_rep_cnt = optional(number, 2)
prov_force_cancel_interval = optional(string, "10m0s")
quiet_hours = optional(string, "CRON_TZ=America/Los_Angeles 50 23 * * *")
allow_custom_quiet = optional(bool, true)
tf_debug_mode = optional(bool, true)
trace_logs = optional(bool, true)
enable_tracing = optional(bool, true)
swagger_enable = optional(bool, true)
update_check = optional(bool, true)
cli_upgr_msg = optional(bool, true)
log_filter = optional(string, ".*")
})
}
variable "prometheus" {
type = object({
enable = optional(bool, true)
collect_agent_status = optional(bool, true)
collect_db_metrics = optional(bool, true)
})
default = {
enable = false
collect_agent_status = false
collect_db_metrics = false
}
}
variable "termination_grace_period" {
type = number
default = 600
}
variable "resource_request" {
type = object({
cpu = string
memory = string
})
default = {
cpu = "2000m"
memory = "4Gi"
}
}
variable "tags" {
type = map(string)
default = {}
}
variable "resource_limit" {
type = map(any)
default = {}
}
variable "svc_annot" {
type = map(string)
default = {}
}
variable "lb_class" {
type = string
default = "service.k8s.aws/nlb"
}
variable "svc_acc_annot" {
type = map(string)
default = {}
}
variable "node_selector" {
type = map(string)
default = {}
}
variable "tolerations" {
type = list(any)
default = []
}
variable "topology_spread" {
type = list(object({
max_skew = number
topology_key = string
when_unsatisfiable = optional(string, "DoNotSchedule")
label_selector = object({
match_labels = map(string)
})
match_label_keys = list(string)
}))
default = []
}
variable "affinity" {
type = any
default = {}
}
variable "db" {
type = object({
url = string
username = string
password = string
db = optional(string, "coder")
pg_auth = optional(string, "password")
})
}
variable "oidc" {
type = object({
enable = bool
sign_in_text = string
icon_url = string
scopes = optional(list(string), null)
email_domain = string
issuer_url = optional(string, null)
client_id = optional(string, null)
client_secret = optional(string, null)
})
default = {
enable = false
sign_in_text = null
icon_url = null
scopes = null
email_domain = null
issuer_url = null
client_id = null
client_secret = null
}
}
variable "oauth2" {
type = object({
enable = bool
default_provider_enable = bool
allow_signups = bool
device_flow = bool
allowed_orgs = list(string) # Empty list means allow everyone
client_id = string
client_secret = string
use_extern_auth = bool
})
default = {
enable = false
default_provider_enable = false
allow_signups = true
device_flow = false
allowed_orgs = []
client_id = null
client_secret = null
use_extern_auth = false
}
}
variable "extern_auth" {
type = list(object({
id = string
type = string
client_id = string
client_secret = string
auth_url = optional(string, null)
token_url = optional(string, null)
revoke_url = optional(string, null)
validate_url = optional(string, null)
regex = optional(string, null)
}))
default = []
}
variable "aibridge" {
type = object({
enabled = bool
enable_structured_logging = optional(bool, true)
})
default = {
enabled = false
enable_structured_logging = true
}
}
locals {
coder = {
CODER_ACCESS_URL = var.coder.access_url
CODER_WILDCARD_ACCESS_URL = var.coder.wildcard_url
# TLS Termination handled on the LB
CODER_REDIRECT_TO_ACCESS_URL = var.coder.mount_ssl
CODER_TLS_ENABLE = var.coder.mount_ssl
CODER_ENABLE_TERRAFORM_DEBUG_MODE = var.coder.tf_debug_mode
CODER_TRACE_LOGS = var.coder.trace_logs
CODER_TRACE_ENABLE = var.coder.enable_tracing
CODER_LOG_FILTER = var.coder.log_filter
CODER_SWAGGER_ENABLE = var.coder.swagger_enable
CODER_UPDATE_CHECK = var.coder.update_check
CODER_CLI_UPGRADE_MESSAGE = var.coder.cli_upgr_msg
CODER_PROVISIONER_DAEMONS = var.coder.prov_rep_cnt
CODER_PROVISIONER_FORCE_CANCEL_INTERVAL = var.coder.prov_force_cancel_interval
CODER_QUIET_HOURS_DEFAULT_SCHEDULE = var.coder.quiet_hours
CODER_ALLOW_CUSTOM_QUIET_HOURS = var.coder.allow_custom_quiet
}
prom = {
CODER_PROMETHEUS_ENABLE = var.prometheus.enable
CODER_PROMETHEUS_COLLECT_AGENT_STATS = var.prometheus.collect_agent_status
CODER_PROMETHEUS_COLLECT_DB_METRICS = var.prometheus.collect_db_metrics
}
db = merge({
CODER_PG_AUTH = var.db.pg_auth
}, var.db.pg_auth == "awsiamrds" ? {
CODER_PG_CONNECTION_URL = "postgresql://${var.db.username}@${var.db.url}/${var.db.db}"
} : {
CODER_PG_CONNECTION_URL = "postgresql://${var.db.username}:${var.db.password}@${var.db.url}/${var.db.db}"
})
oidc = !var.oidc.enable ? {} : {
CODER_OIDC_ISSUER_URL = var.oidc.issuer_url
CODER_OIDC_CLIENT_ID = var.oidc.client_id
CODER_OIDC_CLIENT_SECRET = var.oidc.client_secret
CODER_OIDC_SIGN_IN_TEXT = var.oidc.sign_in_text
CODER_OIDC_ICON_URL = var.oidc.icon_url
CODER_OIDC_SCOPES = join(",", var.oidc.scopes)
CODER_OIDC_EMAIL_DOMAIN = var.oidc.email_domain
}
oauth2 = !var.oauth2.enable ? {
CODER_OAUTH2_GITHUB_DEFAULT_PROVIDER_ENABLE = false
} : {
CODER_OAUTH2_GITHUB_DEFAULT_PROVIDER_ENABLE = var.oauth2.default_provider_enable
CODER_OAUTH2_GITHUB_CLIENT_ID = var.oauth2.client_id
CODER_OAUTH2_GITHUB_CLIENT_SECRET = var.oauth2.client_secret
CODER_OAUTH2_GITHUB_ALLOW_SIGNUPS = var.oauth2.allow_signups
CODER_OAUTH2_GITHUB_DEVICE_FLOW = var.oauth2.device_flow
CODER_OAUTH2_GITHUB_ALLOW_EVERYONE = "${length(var.oauth2.allowed_orgs) == 0}"
CODER_OAUTH2_GITHUB_ALLOWED_ORGS = join(",", var.oauth2.allowed_orgs)
}
extern_auth = merge([for index, obj in var.extern_auth : {
"CODER_EXTERNAL_AUTH_${index}_ID" = obj.id
"CODER_EXTERNAL_AUTH_${index}_TYPE" = obj.type
"CODER_EXTERNAL_AUTH_${index}_CLIENT_ID" = obj.client_id
"CODER_EXTERNAL_AUTH_${index}_CLIENT_SECRET" = obj.client_secret
"CODER_EXTERNAL_AUTH_${index}_AUTH_URL" = obj.auth_url
"CODER_EXTERNAL_AUTH_${index}_TOKEN_URL" = obj.token_url
"CODER_EXTERNAL_AUTH_${index}_REVOKE_URL" = obj.revoke_url
"CODER_EXTERNAL_AUTH_${index}_VALIDATE_URL" = obj.validate_url
"CODER_EXTERNAL_AUTH_${index}_REGEX" = obj.regex
}]...)
aibridge = merge(
{
CODER_AIBRIDGE_ENABLED = var.aibridge.enabled
CODER_AIBRIDGE_STRUCTURED_LOGGING = var.aibridge.enable_structured_logging
}
)
secrets = merge({
CODER_OAUTH2_GITHUB_CLIENT_SECRET = try(local.oauth2["CODER_OAUTH2_GITHUB_CLIENT_SECRET"], null)
CODER_OIDC_CLIENT_SECRET = try(local.oidc["CODER_OIDC_CLIENT_SECRET"], null)
CODER_PG_CONNECTION_URL = try(local.db["CODER_PG_CONNECTION_URL"], null)
}, { for index, obj in var.extern_auth :
"CODER_EXTERNAL_AUTH_${index}_CLIENT_SECRET" => obj.client_secret
})
secret_key = "key"
secret_keys = keys(local.secrets)
env = concat([ for k,v in merge(
local.coder,
local.prom,
local.db,
local.oidc,
local.oauth2,
local.extern_auth,
local.aibridge,
var.coder.env_vars
) : {
name = k,
value = tostring(v)
} if lookup(local.secrets, k, null) == null ], [
for k,v in local.secrets : {
name = k,
valueFrom = {
secretKeyRef = {
name = replace(lower(k), "_", "-"),
key = local.secret_key
}
}
} if v != null
])
}
resource "kubernetes_secret_v1" "coder" {
for_each = toset(local.secret_keys)
metadata {
name = replace(lower(each.key), "_", "-")
namespace = kubernetes_namespace_v1.this.metadata[0].name
annotations = {
"custom.kubernetes.secret/key" = local.secret_key
}
}
data = {
"${local.secret_key}" = sensitive(local.secrets[each.key])
}
}
data "aws_region" "this" {}
data "aws_caller_identity" "this" {}
locals {
topology_spread = [
for k, v in var.topology_spread : {
maxSkew = v.max_skew
topologyKey = v.topology_key
whenUnsatisfiable = v.when_unsatisfiable
labelSelector = {
matchLabels = try(v.label_selector.match_labels, {})
}
matchLabelKeys = v.match_label_keys
}
]
}
locals {
region = data.aws_region.this.region
account_id = data.aws_caller_identity.this.account_id
}
module "provisioner-policy" {
count = var.coder.prov_rep_cnt == 0 ? 0 : 1
source = "../../../security/policy"
name = var.policy_name
path = "/${var.cluster_name}/${local.region}/"
description = "Coder Terraform External Provisioner Policy"
policy_json = data.aws_iam_policy_document.provisioner.json
}
module "rds-policy" {
source = "../../../security/policy"
name = "${var.policy_name}-${local.rds_db_name}"
path = "/${var.cluster_name}/${local.region}/"
description = "Coder DB IAM Access Policy"
policy_json = data.aws_iam_policy_document.rds.json
}
module "provisioner-oidc-role" {
source = "../../../security/role/access-entry"
name = var.role_name
path = "/${var.cluster_name}/${local.region}/"
cluster_name = var.cluster_name
policy_arns = merge({
"AmazonEC2ReadOnlyAccess" = "arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAccess"
"CoderRDSDBPolicy" = module.rds-policy.policy_arn
}, var.coder.prov_rep_cnt == 0 ? {} : {
"TFProvisionerPolicy" = module.provisioner-policy[0].policy_arn
})
cluster_policy_arns = {}
oidc_principals = {
"${var.cluster_oidc_provider_arn}" = ["system:serviceaccount:*:*"]
}
tags = var.tags
}
resource "kubernetes_namespace_v1" "this" {
metadata {
name = var.namespace
}
}
resource "kubernetes_service_v1" "coder" {
wait_for_load_balancer = true
metadata {
name = var.release_name
namespace = kubernetes_namespace_v1.this.metadata[0].name
labels = {}
annotations = var.svc_annot
}
spec {
type = "LoadBalancer"
load_balancer_class = var.lb_class
port {
name = "http"
port = 80
protocol = "TCP"
target_port = "http"
}
port {
name = "https"
port = 443
protocol = "TCP"
target_port = var.coder.mount_ssl ? "https" : "http"
}
selector = {
"app.kubernetes.io/instance" = var.chart_name
"app.kubernetes.io/name" = var.release_name
}
}
}
resource "kubernetes_service_v1" "prometheus" {
count = var.prometheus.enable ? 1 : 0
metadata {
name = "${var.release_name}-prometheus"
namespace = kubernetes_namespace_v1.this.metadata[0].name
labels = {}
}
spec {
type = "ClusterIP"
cluster_ip = "None"
port {
name = "http"
protocol = "TCP"
port = 2112
target_port = 2112
}
selector = {
"app.kubernetes.io/instance" = var.chart_name
"app.kubernetes.io/name" = var.release_name
}
}
}
resource "helm_release" "coder-server" {
name = var.release_name
namespace = kubernetes_namespace_v1.this.metadata[0].name
chart = var.chart_name
repository = "https://helm.coder.com/v2"
create_namespace = false
upgrade_install = true
skip_crds = false
wait = true
wait_for_jobs = true
version = var.chart_version
timeout = var.helm_timeout
values = [yamlencode({
coder = {
image = {
repo = var.coder.image_repo
tag = var.coder.image_tag
pullPolicy = var.coder.image_pull_policy
pullSecrets = var.coder.image_pull_secrets
}
env = local.env
annotations = var.prometheus.enable ? {
"prometheus.io/scrape" = "true"
"prometheus.io/port" = kubernetes_service_v1.prometheus[0].spec[0].port[0].port
} : {}
podAnnotations = var.prometheus.enable ? {
"prometheus.io/scrape" = "true"
"prometheus.io/port" = kubernetes_service_v1.prometheus[0].spec[0].port[0].port
} : {}
service = {
enable = false
}
tls = {
secretNames = var.coder.mount_ssl ? [ var.coder.mount_ssl_name ] : []
}
replicaCount = var.coder.rep_cnt
resources = {
requests = var.resource_request
limits = var.resource_limit
}
serviceAccount = {
annotations = merge({
"eks.amazonaws.com/role-arn" : module.provisioner-oidc-role.role_arn
}, var.svc_acc_annot)
}
nodeSelector = var.node_selector
tolerations = var.tolerations
topologySpreadConstraints = local.topology_spread
affinity = var.affinity
terminationGracePeriodSeconds = var.termination_grace_period
}
})]
}
output "namespace" {
value = kubernetes_namespace_v1.this.metadata[0].name
}