Skip to content

Commit 9e0f536

Browse files
Relax the validation of master ipv4 cidr for GKE with private endpoint subnetwork (#8338) (#15422)
Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Riley Karson <[email protected]>
1 parent 2db14f8 commit 9e0f536

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

.changelog/8338.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
container: 'master_ipv4_cidr_block' is not required when 'private_endpoint_subnetwork' is provided for 'google_container_cluster`
3+
```

google/resource_container_cluster_test.go

+76
Original file line numberDiff line numberDiff line change
@@ -4066,6 +4066,82 @@ resource "google_container_cluster" "with_private_endpoint_subnetwork" {
40664066
`, containerNetName, s1Name, s1Cidr, s2Name, s2Cidr, clusterName)
40674067
}
40684068

4069+
func TestAccContainerCluster_withPrivateClusterConfigPrivateEndpointSubnetwork(t *testing.T) {
4070+
t.Parallel()
4071+
4072+
r := acctest.RandString(t, 10)
4073+
4074+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
4075+
containerNetName := fmt.Sprintf("tf-test-container-net-%s", r)
4076+
4077+
acctest.VcrTest(t, resource.TestCase{
4078+
PreCheck: func() { acctest.AccTestPreCheck(t) },
4079+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
4080+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
4081+
Steps: []resource.TestStep{
4082+
{
4083+
Config: testAccContainerCluster_withPrivateClusterConfigPrivateEndpointSubnetwork(containerNetName, clusterName),
4084+
},
4085+
{
4086+
ResourceName: "google_container_cluster.with_private_endpoint_subnetwork",
4087+
ImportState: true,
4088+
ImportStateVerify: true,
4089+
ImportStateVerifyIgnore: []string{"min_master_version"},
4090+
},
4091+
},
4092+
})
4093+
}
4094+
4095+
func testAccContainerCluster_withPrivateClusterConfigPrivateEndpointSubnetwork(containerNetName, clusterName string) string {
4096+
return fmt.Sprintf(`
4097+
resource "google_compute_network" "container_network" {
4098+
name = "%s"
4099+
auto_create_subnetworks = false
4100+
}
4101+
4102+
resource "google_compute_subnetwork" "container_subnetwork" {
4103+
name = google_compute_network.container_network.name
4104+
network = google_compute_network.container_network.name
4105+
ip_cidr_range = "10.0.36.0/24"
4106+
region = "us-central1"
4107+
private_ip_google_access = true
4108+
4109+
secondary_ip_range {
4110+
range_name = "pod"
4111+
ip_cidr_range = "10.0.0.0/19"
4112+
}
4113+
4114+
secondary_ip_range {
4115+
range_name = "svc"
4116+
ip_cidr_range = "10.0.32.0/22"
4117+
}
4118+
}
4119+
4120+
resource "google_container_cluster" "with_private_endpoint_subnetwork" {
4121+
name = "%s"
4122+
location = "us-central1-a"
4123+
initial_node_count = 1
4124+
networking_mode = "VPC_NATIVE"
4125+
4126+
network = google_compute_network.container_network.name
4127+
subnetwork = google_compute_subnetwork.container_subnetwork.name
4128+
4129+
private_cluster_config {
4130+
enable_private_nodes = true
4131+
enable_private_endpoint = true
4132+
private_endpoint_subnetwork = google_compute_subnetwork.container_subnetwork.name
4133+
}
4134+
master_authorized_networks_config {
4135+
gcp_public_cidrs_access_enabled = false
4136+
}
4137+
ip_allocation_policy {
4138+
cluster_secondary_range_name = google_compute_subnetwork.container_subnetwork.secondary_ip_range[0].range_name
4139+
services_secondary_range_name = google_compute_subnetwork.container_subnetwork.secondary_ip_range[1].range_name
4140+
}
4141+
}
4142+
`, containerNetName, clusterName)
4143+
}
4144+
40694145
func TestAccContainerCluster_withEnablePrivateEndpointToggle(t *testing.T) {
40704146
t.Parallel()
40714147

google/services/container/resource_container_cluster.go

+3
Original file line numberDiff line numberDiff line change
@@ -5323,6 +5323,9 @@ func validatePrivateClusterConfig(cluster *container.Cluster) error {
53235323
return fmt.Errorf("master_ipv4_cidr_block can only be set if enable_private_nodes is true")
53245324
}
53255325
if cluster.PrivateClusterConfig.EnablePrivateNodes && len(cluster.PrivateClusterConfig.MasterIpv4CidrBlock) == 0 {
5326+
if len(cluster.PrivateClusterConfig.PrivateEndpointSubnetwork) > 0 {
5327+
return nil
5328+
}
53265329
if cluster.Autopilot == nil || !cluster.Autopilot.Enabled {
53275330
return fmt.Errorf("master_ipv4_cidr_block must be set if enable_private_nodes is true")
53285331
}

0 commit comments

Comments
 (0)