Skip to content

Commit

Permalink
use the cluster subnet to look up the node cidr block (#3654)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and danawillow committed May 20, 2019
1 parent 31aa844 commit da338ed
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
31 changes: 16 additions & 15 deletions google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
return err
}

if err := d.Set("ip_allocation_policy", flattenIPAllocationPolicy(cluster.IpAllocationPolicy, d, config)); err != nil {
if err := d.Set("ip_allocation_policy", flattenIPAllocationPolicy(cluster, d, config)); err != nil {
return err
}

Expand Down Expand Up @@ -1783,35 +1783,36 @@ func flattenPrivateClusterConfig(c *containerBeta.PrivateClusterConfig) []map[st
}
}

func flattenIPAllocationPolicy(c *containerBeta.IPAllocationPolicy, d *schema.ResourceData, config *Config) []map[string]interface{} {
if c == nil {
func flattenIPAllocationPolicy(c *containerBeta.Cluster, d *schema.ResourceData, config *Config) []map[string]interface{} {
if c == nil || c.IpAllocationPolicy == nil {
return nil
}
node_cidr_block := ""
if c.SubnetworkName != "" {
subnetwork, err := ParseSubnetworkFieldValue(c.SubnetworkName, d, config)
nodeCidrBlock := ""
if c.Subnetwork != "" {
subnetwork, err := ParseSubnetworkFieldValue(c.Subnetwork, d, config)
if err == nil {
sn, err := config.clientCompute.Subnetworks.Get(subnetwork.Project, subnetwork.Region, subnetwork.Name).Do()
if err == nil {
node_cidr_block = sn.IpCidrRange
nodeCidrBlock = sn.IpCidrRange
}
} else {
log.Printf("[WARN] Unable to parse subnetwork name, got error while trying to get new subnetwork: %s", err)
}
}
p := c.IpAllocationPolicy
return []map[string]interface{}{
{
"use_ip_aliases": c.UseIpAliases,
"use_ip_aliases": p.UseIpAliases,

"create_subnetwork": c.CreateSubnetwork,
"subnetwork_name": c.SubnetworkName,
"create_subnetwork": p.CreateSubnetwork,
"subnetwork_name": p.SubnetworkName,

"cluster_ipv4_cidr_block": c.ClusterIpv4CidrBlock,
"services_ipv4_cidr_block": c.ServicesIpv4CidrBlock,
"node_ipv4_cidr_block": node_cidr_block,
"cluster_ipv4_cidr_block": p.ClusterIpv4CidrBlock,
"services_ipv4_cidr_block": p.ServicesIpv4CidrBlock,
"node_ipv4_cidr_block": nodeCidrBlock,

"cluster_secondary_range_name": c.ClusterSecondaryRangeName,
"services_secondary_range_name": c.ServicesSecondaryRangeName,
"cluster_secondary_range_name": p.ClusterSecondaryRangeName,
"services_secondary_range_name": p.ServicesSecondaryRangeName,
},
}
}
Expand Down
9 changes: 1 addition & 8 deletions google/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2260,22 +2260,15 @@ resource "google_compute_network" "container_network" {
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.128.0.0/9"
region = "us-central1"
}
resource "google_container_cluster" "with_ip_allocation_policy" {
name = "%s"
zone = "us-central1-a"
network = "${google_compute_network.container_network.name}"
subnetwork = "${google_compute_subnetwork.container_subnetwork.name}"
initial_node_count = 1
ip_allocation_policy {
use_ip_aliases = true
create_subnetwork = true
cluster_ipv4_cidr_block = "10.0.0.0/16"
services_ipv4_cidr_block = "10.1.0.0/16"
node_ipv4_cidr_block = "10.2.0.0/16"
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/container_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ API is `false`; afterwards, it's `true`.
pick a specific range to use.

* `node_ipv4_cidr_block` - (Optional) The IP address range of the node IPs in this cluster.
This should be set only if `create_subnetwork` is true.
Set to blank to have a range chosen with the default size. Set to /netmask (e.g. /14)
to have a range chosen with a specific netmask. Set to a CIDR notation (e.g. 10.96.0.0/14)
from the RFC-1918 private networks (e.g. 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) to
Expand Down

0 comments on commit da338ed

Please sign in to comment.