Skip to content

Commit a203d54

Browse files
authored
Merge pull request #109 from MaterializeInc/private-eks-gcp
Private + Public GKE Cluster Endpoints
2 parents 92bac02 + e10beba commit a203d54

File tree

9 files changed

+89
-16
lines changed

9 files changed

+89
-16
lines changed

gcp/examples/simple/outputs.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ output "gke_cluster_endpoint" {
1919
value = module.gke.cluster_endpoint
2020
}
2121

22+
output "gke_cluster_private_endpoint" {
23+
description = "GKE cluster private endpoint"
24+
value = module.gke.cluster_private_endpoint
25+
sensitive = true
26+
}
27+
2228
output "gke_cluster_location" {
2329
description = "GKE cluster location"
2430
value = module.gke.cluster_location

gcp/modules/gke/main.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ resource "google_container_cluster" "primary" {
4242
services_secondary_range_name = var.services_secondary_range_name
4343
}
4444

45+
# Enable private cluster with both private and public endpoint access
46+
# ref : https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/container_cluster#private_cluster_config-1
47+
private_cluster_config {
48+
# enables private cluster feature,creating a private endpoint on cluster
49+
enable_private_nodes = true
50+
# when enable_private_endpoint is true, it disables the access through public endpoint, hence this is set to false.
51+
enable_private_endpoint = false
52+
master_ipv4_cidr_block = var.master_ipv4_cidr_block
53+
}
54+
55+
# Allow access to the cluster endpoint from specific IP ranges
56+
master_authorized_networks_config {
57+
cidr_blocks {
58+
cidr_block = var.master_authorized_networks_cidr_block
59+
display_name = "Authorized networks"
60+
}
61+
}
62+
4563
release_channel {
4664
channel = var.release_channel
4765
}

gcp/modules/gke/outputs.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ output "cluster_name" {
44
}
55

66
output "cluster_endpoint" {
7-
description = "The endpoint of the GKE cluster"
7+
description = "The public endpoint of the GKE cluster"
88
value = google_container_cluster.primary.endpoint
99
sensitive = true
1010
}
1111

12+
output "cluster_private_endpoint" {
13+
description = "The private endpoint of the GKE cluster (used by nodes and VPC resources)"
14+
value = google_container_cluster.primary.private_cluster_config != null && length(google_container_cluster.primary.private_cluster_config) > 0 ? google_container_cluster.primary.private_cluster_config[0].private_endpoint : null
15+
sensitive = true
16+
}
17+
1218
output "cluster_ca_certificate" {
1319
value = google_container_cluster.primary.master_auth[0].cluster_ca_certificate
1420
}

gcp/modules/gke/variables.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,19 @@ variable "labels" {
9696
type = map(string)
9797
default = {}
9898
}
99+
100+
# GCP manages this CIDR block when not provided as input
101+
variable "master_ipv4_cidr_block" {
102+
description = "The IP range in CIDR notation to use for the hosted master network. This range must not overlap with any other ranges in use within the cluster's network."
103+
type = string
104+
default = null
105+
nullable = true
106+
}
107+
108+
# modify this to restrict public access to master endpoint from specific IP ranges
109+
variable "master_authorized_networks_cidr_block" {
110+
description = "CIDR block to allow access to the Kubernetes master endpoint. Defaults to 0.0.0.0/0 to allow access from anywhere."
111+
type = string
112+
default = "0.0.0.0/0"
113+
nullable = false
114+
}

test/gcp/fixtures/materialize/main.tf

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ data "google_client_config" "default" {}
1010
module "gke" {
1111
source = "../../../../gcp/modules/gke"
1212

13-
project_id = var.project_id
14-
region = var.region
15-
prefix = var.prefix
16-
network_name = var.network_name
17-
subnet_name = var.subnet_name
18-
namespace = var.namespace
19-
labels = var.labels
13+
project_id = var.project_id
14+
region = var.region
15+
prefix = var.prefix
16+
network_name = var.network_name
17+
subnet_name = var.subnet_name
18+
namespace = var.namespace
19+
master_authorized_networks_cidr_block = var.master_authorized_networks_cidr_block
20+
master_ipv4_cidr_block = var.master_ipv4_cidr_block
21+
labels = var.labels
2022
}
2123

2224
# Nodepool creation

test/gcp/fixtures/materialize/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ output "cluster_endpoint" {
1212
value = module.gke.cluster_endpoint
1313
}
1414

15+
output "cluster_private_endpoint" {
16+
description = "GKE cluster private endpoint"
17+
value = module.gke.cluster_private_endpoint
18+
}
19+
1520
output "cluster_ca_certificate" {
1621
description = "GKE cluster CA certificate"
1722
value = module.gke.cluster_ca_certificate

test/gcp/fixtures/materialize/variables.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,18 @@ variable "license_key" {
176176
default = null
177177
sensitive = true
178178
}
179+
180+
# GCP manages this CIDR block when not provided as input
181+
variable "master_ipv4_cidr_block" {
182+
description = "The IP range in CIDR notation to use for the hosted master network. This range must not overlap with any other ranges in use within the cluster's network."
183+
type = string
184+
default = null
185+
nullable = true
186+
}
187+
188+
# modify this to restrict public access to master endpoint from specific IP ranges
189+
variable "master_authorized_networks_cidr_block" {
190+
description = "CIDR block to allow access to the Kubernetes master endpoint"
191+
type = string
192+
nullable = false
193+
}

test/gcp/staged_deployment_test.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -340,14 +340,15 @@ func (suite *StagedDeploymentSuite) setupMaterializeConsolidatedStage(stage, sta
340340
"subnet_name": subnetNames[0],
341341

342342
// GKE Configuration
343-
"namespace": TestGKENamespace,
344-
"materialize_node_type": machineType,
345-
"min_nodes": TestGKEMinNodes,
346-
"max_nodes": TestGKEMaxNodes,
347-
"enable_private_nodes": true,
348-
"swap_enabled": diskEnabled,
349-
"disk_size": diskSize,
350-
"local_ssd_count": localSSDCount,
343+
"namespace": TestGKENamespace,
344+
"master_authorized_networks_cidr_block": TestMasterAuthorizedNetworksCIDRBlock,
345+
"materialize_node_type": machineType,
346+
"min_nodes": TestGKEMinNodes,
347+
"max_nodes": TestGKEMaxNodes,
348+
"enable_private_nodes": true,
349+
"swap_enabled": diskEnabled,
350+
"disk_size": diskSize,
351+
"local_ssd_count": localSSDCount,
351352

352353
// Node Labels
353354
"labels": map[string]string{
@@ -429,6 +430,7 @@ func (suite *StagedDeploymentSuite) setupMaterializeConsolidatedStage(stage, sta
429430
// GKE Cluster Outputs
430431
clusterName := terraform.Output(t, materializeOptions, "cluster_name")
431432
clusterEndpoint := terraform.Output(t, materializeOptions, "cluster_endpoint")
433+
clusterPrivateEndpoint := terraform.Output(t, materializeOptions, "cluster_private_endpoint")
432434
clusterCA := terraform.Output(t, materializeOptions, "cluster_ca_certificate")
433435
workloadIdentitySA := terraform.Output(t, materializeOptions, "workload_identity_sa_email")
434436

@@ -443,6 +445,7 @@ func (suite *StagedDeploymentSuite) setupMaterializeConsolidatedStage(stage, sta
443445
t.Log("✅ Validating GKE Cluster Outputs...")
444446
suite.NotEmpty(clusterName, "GKE cluster name should not be empty")
445447
suite.NotEmpty(clusterEndpoint, "GKE cluster endpoint should not be empty")
448+
suite.NotEmpty(clusterPrivateEndpoint, "GKE cluster private endpoint should not be empty")
446449
suite.NotEmpty(clusterCA, "GKE cluster CA certificate should not be empty")
447450
suite.NotEmpty(workloadIdentitySA, "Workload identity SA email should not be empty")
448451

test/gcp/test_constants.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const (
1313
TestMachineTypeMedium = "n2-standard-4" // 4 vCPU, 16 GB RAM
1414
TestMachineTypeMemory = "n2-highmem-2" // 2 vCPU, 16 GB RAM
1515

16+
TestMasterAuthorizedNetworksCIDRBlock = "0.0.0.0/0"
17+
1618
// GKE-specific machine types
1719
TestGKEMachineType = "n2-standard-2" // Standard GKE node type
1820
TestAlternativeGKEMachineType = "n2-standard-4" // Alternative GKE node type

0 commit comments

Comments
 (0)