Skip to content

Commit 7b7746a

Browse files
authored
feat: Support creating separate PSC for each zone in GCP (#127)
Usage example ```hcl module "topology_aware_psc" { source = "git::https://github.com/streamnative/terraform-managed-cloud//modules/gcp/private-service?ref=main" domain_name = "gcp-usc1-example.example.com" project = "<your-client-gcp-project>" region = "us-central1" network_name = "<your-client-network>" subnet_name = "<your-client-subnet>" suffix = "gcp-usc1-example" service_attachments = [ { zone = "us-central1-a", id = "projects/<pulsar-project>/regions/us-central1/serviceAttachments/istio-system-istio-ingressgateway-us-central1-a", }, { zone = "us-central1-b", id = "projects/<pulsar-project>/regions/us-central1/serviceAttachments/istio-system-istio-ingressgateway-us-central1-b", }, { zone = "us-central1-c", id = "projects/<pulsar-project>/regions/us-central1/serviceAttachments/istio-system-istio-ingressgateway-us-central1-c", } ] } ```
1 parent f266629 commit 7b7746a

File tree

3 files changed

+109
-15
lines changed

3 files changed

+109
-15
lines changed

examples/gcp/private-service/main.tf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,30 @@ module "gcp-private-service-shared" {
5555
suffix = "shared"
5656
}
5757

58+
# Expose Topology Aware Private Pulsar Service to region us-east1 in network default
59+
60+
module "gcp-private-service-topology-aware" {
61+
source = "github.com/streamnative/terraform-managed-cloud//modules/gcp/private-service?ref=v3.21.0"
62+
63+
region = local.region
64+
project = local.project_id
65+
network_name = "default"
66+
subnet_name = "default"
67+
domain_name = "gcp-use1-prod-snc.o-xxxx.g.snio.cloud"
68+
service_attachments = [
69+
{
70+
zone = "us-east1-a",
71+
id = "projects/<pulsar-project>/regions/us-east1/serviceAttachments/istio-system-istio-ingressgateway-us-central1-a",
72+
},
73+
{
74+
zone = "us-east1-b",
75+
id = "projects/<pulsar-project>/regions/us-east1/serviceAttachments/istio-system-istio-ingressgateway-us-central1-b",
76+
},
77+
{
78+
zone = "us-east1-c",
79+
id = "projects/<pulsar-project>/regions/us-east1/serviceAttachments/istio-system-istio-ingressgateway-us-central1-c",
80+
}
81+
]
82+
cross_region_access = false
83+
suffix = "topology-aware"
84+
}
Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,60 @@
11
variable "region" {
2-
type = string
2+
type = string
33
description = "The GCP region where the private service connection will be configured."
44
}
55

66
variable "project" {
7-
type = string
7+
type = string
88
description = "The GCP project where the private service connection will be configured."
99
}
1010

1111
variable "network_project" {
12-
type = string
12+
type = string
1313
description = "The GCP project where the shared VPC located in."
14-
default = ""
14+
default = ""
1515
}
1616

1717
variable "network_name" {
18-
type = string
18+
type = string
1919
description = "The GCP network where the private service connection will be available."
2020
}
2121

2222
variable "subnet_name" {
23-
type = string
23+
type = string
2424
description = "The GCP subnet where the endpoint IP of private service connection will be allocated."
2525
}
2626

2727
variable "domain_name" {
2828
type = string
29+
validation {
30+
condition = can(regex("^[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$", var.domain_name))
31+
error_message = "The domain name must be a valid DNS name."
32+
}
2933
description = "The base domain of private pulsar service."
3034
}
3135

3236
variable "service_attachment" {
33-
type = string
37+
type = string
38+
default = ""
3439
description = "The id of pulsar private service attachment."
3540
}
3641

3742
variable "cross_region_access" {
38-
type = bool
39-
default = false
43+
type = bool
44+
default = false
4045
description = "Allow access cross regions in the network."
4146
}
4247

4348
variable "suffix" {
4449
description = "The suffix that will be part of the name of resources."
4550
}
51+
52+
53+
variable "service_attachments" {
54+
type = list(object({
55+
id = string
56+
zone = string
57+
}))
58+
default = []
59+
description = "The list of service attachments, only used when enable_topology_aware_gateway is true."
60+
}

modules/gcp/private-service/main.tf

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
locals {
2-
dns_name = "${var.domain_name}."
3-
network_project = var.network_project != "" ? var.network_project : var.project
2+
dns_name = "${var.domain_name}."
3+
network_project = var.network_project != "" ? var.network_project : var.project
4+
enable_topology_aware_gateway = length(var.service_attachments) > 0
5+
service_attachments = {
6+
for idx, sa in var.service_attachments : sa.zone => sa.id
7+
}
48
}
59

610

@@ -16,13 +20,23 @@ data "google_compute_subnetwork" "subnet" {
1620
}
1721

1822
resource "google_compute_address" "psc_endpoint_address" {
23+
count = local.enable_topology_aware_gateway ? 0 : 1
1924
name = "pulsar-psc-${var.suffix}"
2025
region = var.region
2126
subnetwork = data.google_compute_subnetwork.subnet.id
2227
address_type = "INTERNAL"
2328
project = local.network_project
2429
}
2530

31+
resource "google_compute_address" "psc_endpoint_addresses" {
32+
for_each = local.service_attachments
33+
name = "pulsar-psc-${var.suffix}-${each.key}"
34+
region = var.region
35+
subnetwork = data.google_compute_subnetwork.subnet.id
36+
address_type = "INTERNAL"
37+
project = local.network_project
38+
}
39+
2640

2741
resource "google_dns_managed_zone" "psc_endpoint_zone" {
2842
name = "pulsar-psc-${var.suffix}"
@@ -37,30 +51,68 @@ resource "google_dns_managed_zone" "psc_endpoint_zone" {
3751
}
3852

3953
resource "google_dns_record_set" "wildcard_endpoint" {
54+
count = local.enable_topology_aware_gateway ? 0 : 1
4055
managed_zone = google_dns_managed_zone.psc_endpoint_zone.name
4156
name = "*.${local.dns_name}"
4257
type = "A"
4358
ttl = 300
44-
rrdatas = [google_compute_address.psc_endpoint_address.address]
59+
rrdatas = [google_compute_address.psc_endpoint_address[0].address]
4560
project = var.project
4661
}
4762

4863

64+
resource "google_dns_record_set" "zonal_wildcard_endpoint" {
65+
count = local.enable_topology_aware_gateway ? 1 : 0
66+
managed_zone = google_dns_managed_zone.psc_endpoint_zone.name
67+
name = "*.${local.dns_name}"
68+
type = "A"
69+
ttl = 300
70+
rrdatas = [
71+
for zone, id in local.service_attachments : google_compute_address.psc_endpoint_addresses[zone].address
72+
]
73+
project = var.project
74+
}
75+
76+
resource "google_dns_record_set" "zonal_endpoint" {
77+
for_each = local.service_attachments
78+
managed_zone = google_dns_managed_zone.psc_endpoint_zone.name
79+
name = "*.${each.key}.${local.dns_name}"
80+
type = "A"
81+
ttl = 300
82+
rrdatas = [google_compute_address.psc_endpoint_addresses[each.key].address]
83+
project = var.project
84+
}
85+
4986
resource "google_compute_forwarding_rule" "psc_endpoint" {
87+
count = local.enable_topology_aware_gateway ? 0 : 1
5088
name = "pulsar-psc-${var.suffix}"
5189
region = var.region
5290
load_balancing_scheme = ""
5391
allow_psc_global_access = var.cross_region_access
5492
target = var.service_attachment
5593
network = data.google_compute_network.network.id
56-
ip_address = google_compute_address.psc_endpoint_address.id
94+
ip_address = google_compute_address.psc_endpoint_address[0].id
95+
project = var.project
96+
}
97+
98+
resource "google_compute_forwarding_rule" "zonal_psc_endpoint" {
99+
for_each = local.service_attachments
100+
name = "pulsar-psc-${var.suffix}-${each.key}"
101+
region = var.region
102+
load_balancing_scheme = ""
103+
allow_psc_global_access = var.cross_region_access
104+
target = each.value
105+
network = data.google_compute_network.network.id
106+
ip_address = google_compute_address.psc_endpoint_addresses[each.key].id
57107
project = var.project
58108
}
59109

60110
output "network_id" {
61111
value = data.google_compute_network.network.id
62112
}
63113

64-
output "endpoint_address" {
65-
value = google_compute_address.psc_endpoint_address.id
114+
output "endpoint_addresses" {
115+
value = local.enable_topology_aware_gateway ? [
116+
for zone, id in local.service_attachments : google_compute_address.psc_endpoint_addresses[zone].address
117+
] : [google_compute_address.psc_endpoint_address[0].address]
66118
}

0 commit comments

Comments
 (0)