Skip to content

Commit 5b3eb5f

Browse files
Relax the validation of master ipv4 cidr for GKE with private endpoint subnetwork (#8338) (#6025)
Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Riley Karson <[email protected]>
1 parent 213c6b3 commit 5b3eb5f

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

.changelog/8338.txt

Lines changed: 3 additions & 0 deletions
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-beta/resource_container_cluster_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4730,6 +4730,82 @@ resource "google_container_cluster" "with_private_endpoint_subnetwork" {
47304730
`, containerNetName, s1Name, s1Cidr, s2Name, s2Cidr, clusterName)
47314731
}
47324732

4733+
func TestAccContainerCluster_withPrivateClusterConfigPrivateEndpointSubnetwork(t *testing.T) {
4734+
t.Parallel()
4735+
4736+
r := acctest.RandString(t, 10)
4737+
4738+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
4739+
containerNetName := fmt.Sprintf("tf-test-container-net-%s", r)
4740+
4741+
acctest.VcrTest(t, resource.TestCase{
4742+
PreCheck: func() { acctest.AccTestPreCheck(t) },
4743+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
4744+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
4745+
Steps: []resource.TestStep{
4746+
{
4747+
Config: testAccContainerCluster_withPrivateClusterConfigPrivateEndpointSubnetwork(containerNetName, clusterName),
4748+
},
4749+
{
4750+
ResourceName: "google_container_cluster.with_private_endpoint_subnetwork",
4751+
ImportState: true,
4752+
ImportStateVerify: true,
4753+
ImportStateVerifyIgnore: []string{"min_master_version"},
4754+
},
4755+
},
4756+
})
4757+
}
4758+
4759+
func testAccContainerCluster_withPrivateClusterConfigPrivateEndpointSubnetwork(containerNetName, clusterName string) string {
4760+
return fmt.Sprintf(`
4761+
resource "google_compute_network" "container_network" {
4762+
name = "%s"
4763+
auto_create_subnetworks = false
4764+
}
4765+
4766+
resource "google_compute_subnetwork" "container_subnetwork" {
4767+
name = google_compute_network.container_network.name
4768+
network = google_compute_network.container_network.name
4769+
ip_cidr_range = "10.0.36.0/24"
4770+
region = "us-central1"
4771+
private_ip_google_access = true
4772+
4773+
secondary_ip_range {
4774+
range_name = "pod"
4775+
ip_cidr_range = "10.0.0.0/19"
4776+
}
4777+
4778+
secondary_ip_range {
4779+
range_name = "svc"
4780+
ip_cidr_range = "10.0.32.0/22"
4781+
}
4782+
}
4783+
4784+
resource "google_container_cluster" "with_private_endpoint_subnetwork" {
4785+
name = "%s"
4786+
location = "us-central1-a"
4787+
initial_node_count = 1
4788+
networking_mode = "VPC_NATIVE"
4789+
4790+
network = google_compute_network.container_network.name
4791+
subnetwork = google_compute_subnetwork.container_subnetwork.name
4792+
4793+
private_cluster_config {
4794+
enable_private_nodes = true
4795+
enable_private_endpoint = true
4796+
private_endpoint_subnetwork = google_compute_subnetwork.container_subnetwork.name
4797+
}
4798+
master_authorized_networks_config {
4799+
gcp_public_cidrs_access_enabled = false
4800+
}
4801+
ip_allocation_policy {
4802+
cluster_secondary_range_name = google_compute_subnetwork.container_subnetwork.secondary_ip_range[0].range_name
4803+
services_secondary_range_name = google_compute_subnetwork.container_subnetwork.secondary_ip_range[1].range_name
4804+
}
4805+
}
4806+
`, containerNetName, clusterName)
4807+
}
4808+
47334809
func TestAccContainerCluster_withEnablePrivateEndpointToggle(t *testing.T) {
47344810
t.Parallel()
47354811

google-beta/services/container/resource_container_cluster.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5939,6 +5939,9 @@ func validatePrivateClusterConfig(cluster *container.Cluster) error {
59395939
return fmt.Errorf("master_ipv4_cidr_block can only be set if enable_private_nodes is true")
59405940
}
59415941
if cluster.PrivateClusterConfig.EnablePrivateNodes && len(cluster.PrivateClusterConfig.MasterIpv4CidrBlock) == 0 {
5942+
if len(cluster.PrivateClusterConfig.PrivateEndpointSubnetwork) > 0 {
5943+
return nil
5944+
}
59425945
if cluster.Autopilot == nil || !cluster.Autopilot.Enabled {
59435946
return fmt.Errorf("master_ipv4_cidr_block must be set if enable_private_nodes is true")
59445947
}

0 commit comments

Comments
 (0)