Skip to content

Commit

Permalink
Require master_ipv4_cidr_block if enable_private_nodes is true (#2063)
Browse files Browse the repository at this point in the history
Merged PR #2063.
  • Loading branch information
Ty Larrabee authored and modular-magician committed Jul 17, 2019
1 parent a8ce5f1 commit 5ef00b5
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
20 changes: 20 additions & 0 deletions third_party/terraform/resources/resource_container_cluster.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func resourceContainerCluster() *schema.Resource {
CustomizeDiff: customdiff.All(
resourceContainerClusterIpAllocationCustomizeDiff,
resourceNodeConfigEmptyGuestAccelerator,
containerClusterPrivateClusterConfigCustomDiff,
),

Timeouts: &schema.ResourceTimeout{
Expand Down Expand Up @@ -2780,6 +2781,25 @@ func containerClusterPrivateClusterConfigSuppress(k, old, new string, d *schema.
return false
}

func containerClusterPrivateClusterConfigCustomDiff(d *schema.ResourceDiff, meta interface{}) error {
pcc, ok := d.GetOk("private_cluster_config")
if !ok {
return nil
}
pccList := pcc.([]interface{})
if len(pccList) == 0 {
return nil
}
config := pccList[0].(map[string]interface{})
if config["enable_private_nodes"].(bool) == true {
block := config["master_ipv4_cidr_block"]
if block == nil || block == "" {
return fmt.Errorf("master_ipv4_cidr_block must be set if enable_private_nodes == true")
}
}
return nil
}

<% unless version == 'ga' -%>
func podSecurityPolicyCfgSuppress(k, old, new string, r *schema.ResourceData) bool {
if k == "pod_security_policy_config.#" && old == "1" && new == "0" {
Expand Down
63 changes: 63 additions & 0 deletions third_party/terraform/tests/resource_container_cluster_test.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,24 @@ func TestAccContainerCluster_withPrivateClusterConfig(t *testing.T) {
})
}

func TestAccContainerCluster_withPrivateClusterConfigMissingCidrBlock(t *testing.T) {
t.Parallel()

clusterName := fmt.Sprintf("cluster-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckContainerClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_withPrivateClusterConfigMissingCidrBlock(clusterName),
ExpectError: regexp.MustCompile("master_ipv4_cidr_block must be set if enable_private_nodes == true"),
},
},
})
}

<% unless version == 'ga' -%>
func TestAccContainerCluster_withIntraNodeVisibility(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -2742,6 +2760,51 @@ func testAccContainerCluster_withResourceUsageExportConfig(clusterName, datasetI
}
<% end -%>

func testAccContainerCluster_withPrivateClusterConfigMissingCidrBlock(clusterName string) string {
return fmt.Sprintf(`
resource "google_compute_network" "container_network" {
name = "container-net-%s"
auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "container_subnetwork" {
name = "${google_compute_network.container_network.name}"
network = "${google_compute_network.container_network.name}"
ip_cidr_range = "10.0.36.0/24"
region = "us-central1"
private_ip_google_access = true

secondary_ip_range {
range_name = "pod"
ip_cidr_range = "10.0.0.0/19"
}

secondary_ip_range {
range_name = "svc"
ip_cidr_range = "10.0.32.0/22"
}
}

resource "google_container_cluster" "with_private_cluster" {
name = "cluster-test-%s"
zone = "us-central1-a"
initial_node_count = 1

network = "${google_compute_network.container_network.name}"
subnetwork = "${google_compute_subnetwork.container_subnetwork.name}"

private_cluster_config {
enable_private_endpoint = true
enable_private_nodes = true
}
master_authorized_networks_config { }
ip_allocation_policy {
cluster_secondary_range_name = "${google_compute_subnetwork.container_subnetwork.secondary_ip_range.0.range_name}"
services_secondary_range_name = "${google_compute_subnetwork.container_subnetwork.secondary_ip_range.1.range_name}"
}
}`, clusterName, clusterName)
}

func testAccContainerCluster_withPrivateClusterConfig(clusterName string) string {
return fmt.Sprintf(`
resource "google_compute_network" "container_network" {
Expand Down

0 comments on commit 5ef00b5

Please sign in to comment.