Skip to content

Commit 7952976

Browse files
committed
Small bug fixes and improvements
1 parent f51392a commit 7952976

File tree

10 files changed

+131
-14
lines changed

10 files changed

+131
-14
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ No resources.
4444

4545
| Name | Description | Type | Default | Required |
4646
|------|-------------|------|---------|:--------:|
47-
| <a name="input_database_config"></a> [database\_config](#input\_database\_config) | Cloud SQL configuration | <pre>object({<br/> tier = string<br/> version = string<br/> password = string<br/> })</pre> | <pre>{<br/> "password": null,<br/> "tier": "db-custom-2-4096",<br/> "version": "POSTGRES_15"<br/>}</pre> | no |
48-
| <a name="input_gke_config"></a> [gke\_config](#input\_gke\_config) | GKE cluster configuration | <pre>object({<br/> node_count = number<br/> machine_type = string<br/> disk_size_gb = number<br/> min_nodes = number<br/> max_nodes = number<br/> node_locations = list(string)<br/> })</pre> | <pre>{<br/> "disk_size_gb": 100,<br/> "machine_type": "e2-standard-4",<br/> "max_nodes": 5,<br/> "min_nodes": 1,<br/> "node_count": 3,<br/> "node_locations": []<br/>}</pre> | no |
47+
| <a name="input_database_config"></a> [database\_config](#input\_database\_config) | Cloud SQL configuration | <pre>object({<br/> tier = optional(string, "db-custom-2-4096")<br/> version = optional(string, "POSTGRES_15")<br/> password = string<br/> username = optional(string, "materialize")<br/> db_name = optional(string, "materialize")<br/> })</pre> | n/a | yes |
48+
| <a name="input_gke_config"></a> [gke\_config](#input\_gke\_config) | GKE cluster configuration | <pre>object({<br/> node_count = number<br/> machine_type = string<br/> disk_size_gb = number<br/> min_nodes = number<br/> max_nodes = number<br/> node_locations = list(string)<br/> })</pre> | <pre>{<br/> "disk_size_gb": 100,<br/> "machine_type": "e2-standard-2",<br/> "max_nodes": 5,<br/> "min_nodes": 1,<br/> "node_count": 3,<br/> "node_locations": []<br/>}</pre> | no |
4949
| <a name="input_labels"></a> [labels](#input\_labels) | Labels to apply to all resources | `map(string)` | `{}` | no |
5050
| <a name="input_namespace"></a> [namespace](#input\_namespace) | Kubernetes namespace for Materialize | `string` | `"materialize"` | no |
5151
| <a name="input_network_config"></a> [network\_config](#input\_network\_config) | Network configuration for the GKE cluster | <pre>object({<br/> subnet_cidr = string<br/> pods_cidr = string<br/> services_cidr = string<br/> })</pre> | <pre>{<br/> "pods_cidr": "10.48.0.0/14",<br/> "services_cidr": "10.52.0.0/20",<br/> "subnet_cidr": "10.0.0.0/20"<br/>}</pre> | no |
@@ -57,6 +57,7 @@ No resources.
5757

5858
| Name | Description |
5959
|------|-------------|
60+
| <a name="output_connection_strings"></a> [connection\_strings](#output\_connection\_strings) | Formatted connection strings for Materialize |
6061
| <a name="output_database"></a> [database](#output\_database) | Cloud SQL instance details |
6162
| <a name="output_gke_cluster"></a> [gke\_cluster](#output\_gke\_cluster) | GKE cluster details |
6263
| <a name="output_service_accounts"></a> [service\_accounts](#output\_service\_accounts) | Service account details |

main.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ module "gke" {
2828
module "database" {
2929
source = "./modules/database"
3030

31+
depends_on = [ module.gke ]
32+
33+
database_name = var.database_config.db_name
34+
database_user = var.database_config.username
35+
3136
project_id = var.project_id
3237
region = var.region
3338
prefix = var.prefix

modules/database/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ resource "google_sql_database_instance" "materialize" {
2929
user_labels = var.labels
3030
}
3131

32-
deletion_protection = true
32+
deletion_protection = false
3333
}
3434

3535
resource "google_sql_database" "materialize" {
36-
name = "materialize_db"
36+
name = var.database_name
3737
instance = google_sql_database_instance.materialize.name
3838
project = var.project_id
3939
}
4040

4141
resource "google_sql_user" "materialize" {
42-
name = "materialize_user"
42+
name = var.database_user
4343
instance = google_sql_database_instance.materialize.name
4444
password = var.password
4545
project = var.project_id

modules/database/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ variable "password" {
3838
sensitive = true
3939
}
4040

41+
variable "database_user" {
42+
description = "The name of the database user"
43+
default = "materialize"
44+
type = string
45+
}
46+
47+
variable "database_name" {
48+
description = "The name of the database"
49+
default = "materialize"
50+
type = string
51+
}
52+
4153
variable "labels" {
4254
description = "Labels to apply to resources"
4355
type = map(string)

modules/gke/main.tf

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@ resource "google_compute_network" "vpc" {
22
name = "${var.prefix}-network"
33
auto_create_subnetworks = false
44
project = var.project_id
5+
6+
lifecycle {
7+
create_before_destroy = true
8+
}
9+
}
10+
11+
resource "google_compute_route" "default_route" {
12+
name = "${var.prefix}-default-route"
13+
project = var.project_id
14+
network = google_compute_network.vpc.name
15+
dest_range = "0.0.0.0/0"
16+
priority = 1000
17+
next_hop_gateway = "default-internet-gateway"
18+
19+
# Ensure this is destroyed before the network
20+
depends_on = [google_compute_network.vpc]
21+
22+
lifecycle {
23+
create_before_destroy = true
24+
}
525
}
626

727
resource "google_compute_subnetwork" "subnet" {
@@ -30,6 +50,23 @@ resource "google_service_account" "gke_sa" {
3050
display_name = "GKE Service Account for Materialize"
3151
}
3252

53+
resource "google_compute_global_address" "private_ip_address" {
54+
provider = google
55+
project = var.project_id
56+
name = "${var.prefix}-private-ip"
57+
purpose = "VPC_PEERING"
58+
address_type = "INTERNAL"
59+
prefix_length = 16
60+
network = google_compute_network.vpc.id
61+
}
62+
63+
resource "google_service_networking_connection" "private_vpc_connection" {
64+
provider = google
65+
network = google_compute_network.vpc.id
66+
service = "servicenetworking.googleapis.com"
67+
reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
68+
}
69+
3370
resource "google_service_account" "workload_identity_sa" {
3471
project = var.project_id
3572
account_id = "${var.prefix}-materialize-sa"
@@ -39,6 +76,16 @@ resource "google_service_account" "workload_identity_sa" {
3976
resource "google_container_cluster" "primary" {
4077
provider = google
4178

79+
deletion_protection = false
80+
81+
depends_on = [
82+
google_service_account.gke_sa,
83+
google_service_account.workload_identity_sa,
84+
google_service_networking_connection.private_vpc_connection,
85+
google_compute_subnetwork.subnet,
86+
google_compute_route.default_route
87+
]
88+
4289
name = "${var.prefix}-gke"
4390
location = var.region
4491
project = var.project_id
@@ -110,9 +157,17 @@ resource "google_container_node_pool" "primary_nodes" {
110157
mode = "GKE_METADATA"
111158
}
112159
}
160+
161+
lifecycle {
162+
create_before_destroy = true
163+
164+
prevent_destroy = false
165+
}
166+
113167
}
114168

115169
resource "google_service_account_iam_binding" "workload_identity" {
170+
depends_on = [google_service_account.workload_identity_sa]
116171
service_account_id = google_service_account.workload_identity_sa.name
117172
role = "roles/iam.workloadIdentityUser"
118173
members = [

modules/storage/main.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ resource "google_storage_bucket_iam_member" "materialize_storage" {
3434
role = "roles/storage.admin"
3535
member = "serviceAccount:${var.service_account}"
3636
}
37+
38+
resource "google_storage_hmac_key" "materialize" {
39+
project = var.project_id
40+
service_account_email = var.service_account
41+
}

modules/storage/outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@ output "bucket_self_link" {
1212
description = "The self_link of the GCS bucket"
1313
value = google_storage_bucket.materialize.self_link
1414
}
15+
16+
output "hmac_access_id" {
17+
value = google_storage_hmac_key.materialize.access_id
18+
sensitive = true
19+
}
20+
21+
output "hmac_secret" {
22+
value = google_storage_hmac_key.materialize.secret
23+
sensitive = true
24+
}

outputs.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,34 @@ output "service_accounts" {
3434
materialize_sa = module.gke.workload_identity_sa_email
3535
}
3636
}
37+
38+
locals {
39+
metadata_backend_url = format(
40+
"postgres://%s:%s@%s:5432/%s?sslmode=disable",
41+
var.database_config.username,
42+
var.database_config.password,
43+
module.database.private_ip,
44+
var.database_config.db_name
45+
)
46+
47+
encoded_endpoint = urlencode("https://storage.googleapis.com")
48+
encoded_secret = urlencode(module.storage.hmac_secret)
49+
50+
persist_backend_url = format(
51+
"s3://%s:%s@%s/materialize?endpoint=%s&region=%s",
52+
module.storage.hmac_access_id,
53+
local.encoded_secret,
54+
module.storage.bucket_name,
55+
local.encoded_endpoint,
56+
var.region
57+
)
58+
}
59+
60+
output "connection_strings" {
61+
description = "Formatted connection strings for Materialize"
62+
value = {
63+
metadata_backend_url = local.metadata_backend_url
64+
persist_backend_url = local.persist_backend_url
65+
}
66+
sensitive = true
67+
}

terraform.tfvars.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ network_config = {
1313
# GKE Configuration
1414
gke_config = {
1515
node_count = 3
16-
machine_type = "e2-standard-4"
16+
machine_type = "e2-standard-2"
1717
disk_size_gb = 100
1818
min_nodes = 1
1919
max_nodes = 5

variables.tf

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ variable "gke_config" {
4141
})
4242
default = {
4343
node_count = 3
44-
machine_type = "e2-standard-4"
44+
machine_type = "e2-standard-2"
4545
disk_size_gb = 100
4646
min_nodes = 1
4747
max_nodes = 5
@@ -52,15 +52,13 @@ variable "gke_config" {
5252
variable "database_config" {
5353
description = "Cloud SQL configuration"
5454
type = object({
55-
tier = string
56-
version = string
55+
tier = optional(string, "db-custom-2-4096")
56+
version = optional(string, "POSTGRES_15")
5757
password = string
58+
username = optional(string, "materialize")
59+
db_name = optional(string, "materialize")
5860
})
59-
default = {
60-
tier = "db-custom-2-4096"
61-
version = "POSTGRES_15"
62-
password = null # Must be provided
63-
}
61+
6462
validation {
6563
condition = var.database_config.password != null
6664
error_message = "database_config.password must be provided"

0 commit comments

Comments
 (0)