-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
133 lines (115 loc) · 3.69 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
locals {
name_prefix = "${var.namespace}-${var.environment}"
}
resource "kubernetes_namespace" "materialize" {
metadata {
name = var.operator_namespace
}
}
resource "kubernetes_namespace" "instance_namespaces" {
for_each = toset(compact([for instance in var.instances : instance.namespace if instance.namespace != null]))
metadata {
name = each.key
}
}
resource "helm_release" "materialize_operator" {
name = local.name_prefix
namespace = kubernetes_namespace.materialize.metadata[0].name
repository = var.helm_repository
chart = "materialize-operator"
version = var.operator_version
values = [
yamlencode(var.helm_values)
]
depends_on = [kubernetes_namespace.materialize]
}
resource "kubernetes_secret" "materialize_backends" {
for_each = { for idx, instance in var.instances : instance.name => instance }
metadata {
name = "${each.key}-materialize-backend"
namespace = coalesce(each.value.namespace, var.operator_namespace)
}
data = {
metadata_backend_url = each.value.metadata_backend_url
persist_backend_url = each.value.persist_backend_url
}
}
# The kubernetes_manifest resource is used to create Materialize instances
# It currently has a few limitations:
# - It requires the Kubernetes cluster to be running, otherwise it will fail to connect
# - It requires the Materialize operator to be installed in the cluster, otherwise it will fail
# Tracking issue:
# https://github.com/hashicorp/terraform-provider-kubernetes/issues/1775
resource "kubernetes_manifest" "materialize_instances" {
for_each = { for idx, instance in var.instances : instance.name => instance }
manifest = {
apiVersion = "materialize.cloud/v1alpha1"
kind = "Materialize"
metadata = {
name = each.value.name
namespace = coalesce(each.value.namespace, var.operator_namespace)
}
spec = {
environmentdImageRef = "materialize/environmentd:${each.value.environmentd_version}"
backendSecretName = "${each.key}-materialize-backend"
environmentdResourceRequirements = {
limits = {
memory = each.value.memory_limit
}
requests = {
cpu = each.value.cpu_request
memory = each.value.memory_request
}
}
balancerdResourceRequirements = {
limits = {
memory = "256Mi"
}
requests = {
cpu = "100m"
memory = "256Mi"
}
}
}
}
depends_on = [
helm_release.materialize_operator,
kubernetes_secret.materialize_backends,
kubernetes_namespace.instance_namespaces
]
}
# Materialize does not currently create databases within the instances, so we need to create them ourselves
resource "kubernetes_job" "db_init_job" {
for_each = { for idx, instance in var.instances : instance.database_name => instance }
metadata {
name = replace("create-db-${each.key}", "_", "-")
namespace = coalesce(each.value.namespace, var.operator_namespace)
}
spec {
backoff_limit = 3
template {
metadata {
labels = {
app = "init-db-${each.key}"
}
}
spec {
container {
name = "init-db"
image = "postgres:${var.postgres_version}"
command = [
"/bin/sh",
"-c",
"psql $DATABASE_URL -c \"CREATE DATABASE ${each.key};\""
]
env {
name = "DATABASE_URL"
# Use the default postgres database for connecting to the postgres instance
value = replace(each.value.metadata_backend_url, "/${basename(each.value.metadata_backend_url)}", "/postgres")
}
}
restart_policy = "OnFailure"
}
}
}
}