Skip to content

use the cluster subnet to look up the node cidr block #3654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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