-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.tf
97 lines (94 loc) · 3.29 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
locals {
count = var.sonarqube_config.postgresql_external_server_url != "" ? [] : [1]
}
resource "random_password" "sonarqube_password" {
length = 20
special = true
}
resource "random_password" "postgresql_password" {
length = 20
special = true
}
resource "random_password" "monitoringPasscode" {
length = 20
special = true
}
resource "kubernetes_namespace" "sonarqube" {
metadata {
name = var.namespace
}
}
resource "helm_release" "sonarqube" {
depends_on = [kubernetes_namespace.sonarqube]
name = "sonarqube"
chart = "sonarqube"
timeout = 600
version = var.chart_version
namespace = var.namespace
repository = "https://sonarsource.github.io/helm-chart-sonarqube"
values = [
templatefile("${path.module}/helm/values.yaml", {
monitoringPasscode = var.sonarqube_config.monitoringPasscode != "" ? var.sonarqube_config.monitoringPasscode : random_password.monitoringPasscode.result
hostname = var.sonarqube_config.hostname
volume_size = var.sonarqube_config.sonarqube_volume_size
sonarqube_sc = var.sonarqube_config.storage_class_name
postgresql_enable = var.sonarqube_config.postgresql_external_server_url != "" ? false : true
sonarqube_password = random_password.sonarqube_password.result
sonarqube_current_password = var.sonarqube_config.sonarqube_current_password
postgresql_password = var.sonarqube_config.postgresql_current_password != "" ? var.sonarqube_config.postgresql_current_password : random_password.postgresql_password.result
postgresql_disk_size = var.sonarqube_config.postgresql_volume_size
prometheus_exporter_enable = var.sonarqube_config.grafana_monitoring_enabled
postgresql_password_external = var.sonarqube_config.postgresql_password_external
postgresql_external_server_url = var.sonarqube_config.postgresql_external_server_url
}),
var.sonarqube_config.values_yaml
]
dynamic "set" {
for_each = local.count
content {
name = "postgresql.postgresqlServer"
value = var.sonarqube_config.postgresql_external_server_url
}
}
dynamic "set" {
for_each = local.count
content {
name = "postgresql.existingSecretPasswordKey"
value = var.sonarqube_config.postgresql_password_external
}
}
}
resource "kubernetes_manifest" "migration_job" {
manifest = {
apiVersion = "batch/v1"
kind = "Job"
metadata = {
name = "db-migration-watcher-job"
namespace = "sonarqube"
}
spec = {
backoffLimit = 4
completions = 1
parallelism = 1
template = {
spec = {
restartPolicy = "Never"
containers = [
{
name = "db-migration-watcher"
image = "alpine:latest"
command = [
"/bin/sh", "-c", <<-EOT
sleep 180 &&
apk add --no-cache curl &&
curl -s -X POST -u admin:"${var.sonarqube_config.sonarqube_current_password}" "http://sonarqube-sonarqube:9000/api/system/migrate_db" &&
echo "DB Migration triggered. Exiting watcher."
EOT
]
}
]
}
}
}
}
}