Skip to content

Commit

Permalink
Merge pull request #320 from modular-magician/codegen-pr-1165
Browse files Browse the repository at this point in the history
Enable flex pod CIDR for GKE (part 2)
  • Loading branch information
emilymye committed Jan 7, 2019
2 parents be11e15 + 3526b90 commit 26b13d7
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
24 changes: 24 additions & 0 deletions google-beta/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,13 @@ func resourceContainerCluster() *schema.Resource {
Computed: true,
Type: schema.TypeString,
},

"default_max_pods_per_node": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -685,6 +692,10 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
ResourceLabels: expandStringMap(d, "resource_labels"),
}

if v, ok := d.GetOk("default_max_pods_per_node"); ok {
cluster.DefaultMaxPodsConstraint = expandDefaultMaxPodsConstraint(v)
}

// Only allow setting node_version on create if it's set to the equivalent master version,
// since `InitialClusterVersion` only accepts valid master-style versions.
if v, ok := d.GetOk("node_version"); ok {
Expand Down Expand Up @@ -862,6 +873,9 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
if err := d.Set("cluster_autoscaling", flattenClusterAutoscaling(cluster.Autoscaling)); err != nil {
return err
}
if cluster.DefaultMaxPodsConstraint != nil {
d.Set("default_max_pods_per_node", cluster.DefaultMaxPodsConstraint.MaxPodsPerNode)
}
if err := d.Set("node_config", flattenNodeConfig(cluster.NodeConfig)); err != nil {
return err
}
Expand Down Expand Up @@ -1719,6 +1733,16 @@ func expandPodSecurityPolicyConfig(configured interface{}) *containerBeta.PodSec
}
}

func expandDefaultMaxPodsConstraint(v interface{}) *containerBeta.MaxPodsConstraint {
if v == nil {
return nil
}

return &containerBeta.MaxPodsConstraint{
MaxPodsPerNode: int64(v.(int)),
}
}

func flattenNetworkPolicy(c *containerBeta.NetworkPolicy) []map[string]interface{} {
result := []map[string]interface{}{}
if c != nil {
Expand Down
74 changes: 74 additions & 0 deletions google-beta/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,29 @@ func TestAccContainerCluster_withBinaryAuthorization(t *testing.T) {
})
}

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

cluster := 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_withFlexiblePodCIDR(cluster),
},
{
ResourceName: "google_container_cluster.with_flexible_cidr",
ImportStateIdPrefix: "us-central1-a/",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckContainerClusterDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)

Expand Down Expand Up @@ -2774,3 +2797,54 @@ resource "google_container_cluster" "with_binary_authorization" {
}
`, clusterName, enabled)
}

func testAccContainerCluster_withFlexiblePodCIDR(cluster 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.35.0/24"
region = "us-central1"
private_ip_google_access = true
secondary_ip_range {
range_name = "pod"
ip_cidr_range = "10.1.0.0/19"
}
secondary_ip_range {
range_name = "svc"
ip_cidr_range = "10.2.0.0/22"
}
}
resource "google_container_cluster" "with_flexible_cidr" {
name = "%s"
zone = "us-central1-a"
initial_node_count = 3
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_ipv4_cidr_block = "10.42.0.0/28"
}
master_authorized_networks_config { cidr_blocks = [] }
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}"
}
default_max_pods_per_node = 100
}
`, cluster, cluster)
}

0 comments on commit 26b13d7

Please sign in to comment.