@@ -1822,6 +1822,27 @@ func ResourceContainerCluster() *schema.Resource {
18221822 },
18231823 },
18241824 },
1825+ "additional_ip_ranges_config" : {
1826+ Type : schema .TypeList ,
1827+ Optional : true ,
1828+ Description : `AdditionalIPRangesConfig is the configuration for individual additional subnetworks attached to the cluster` ,
1829+ Elem : & schema.Resource {
1830+ Schema : map [string ]* schema.Schema {
1831+ "subnetwork" : {
1832+ Type : schema .TypeString ,
1833+ Required : true ,
1834+ DiffSuppressFunc : tpgresource .CompareSelfLinkOrResourceName ,
1835+ Description : `Name of the subnetwork. This can be the full path of the subnetwork or just the name.` ,
1836+ },
1837+ "pod_ipv4_range_names" : {
1838+ Type : schema .TypeList ,
1839+ Optional : true ,
1840+ Description : `List of secondary ranges names within this subnetwork that can be used for pod IPs.` ,
1841+ Elem : & schema.Schema {Type : schema .TypeString },
1842+ },
1843+ },
1844+ },
1845+ },
18251846 },
18261847 },
18271848 },
@@ -2663,7 +2684,7 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
26632684 }
26642685 }
26652686
2666- ipAllocationBlock , err := expandIPAllocationPolicy (d .Get ("ip_allocation_policy" ), d .Get ("networking_mode" ).(string ), d .Get ("enable_autopilot" ).(bool ))
2687+ ipAllocationBlock , aircs , err := expandIPAllocationPolicy (d .Get ("ip_allocation_policy" ), d , d .Get ("networking_mode" ).(string ), d .Get ("enable_autopilot" ).(bool ), config )
26672688 if err != nil {
26682689 return err
26692690 }
@@ -2889,6 +2910,10 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
28892910
28902911 needUpdateAfterCreate := false
28912912
2913+ if len (aircs ) > 0 {
2914+ needUpdateAfterCreate = true
2915+ }
2916+
28922917 // For now PSC based cluster don't support `enable_private_endpoint` on `create`, but only on `update` API call.
28932918 // If cluster is PSC based and enable_private_endpoint is set to true we will ignore it on `create` call and update cluster right after creation.
28942919 enablePrivateEndpointPSCCluster := isEnablePrivateEndpointPSCCluster (cluster )
@@ -3014,6 +3039,13 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
30143039 }
30153040 update .ForceSendFields = append (update .ForceSendFields , "DesiredAddonsConfig.GcePersistentDiskCsiDriverConfig.Enabled" )
30163041 }
3042+
3043+ if len (aircs ) > 0 {
3044+ update .DesiredAdditionalIpRangesConfig = & container.DesiredAdditionalIPRangesConfig {
3045+ AdditionalIpRangesConfigs : aircs ,
3046+ }
3047+ }
3048+
30173049 req := & container.UpdateClusterRequest {Update : update }
30183050
30193051 err = transport_tpg .Retry (transport_tpg.RetryOptions {
@@ -4214,6 +4246,30 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
42144246 log .Printf ("[INFO] GKE cluster %s's AdditionalPodRangesConfig has been updated" , d .Id ())
42154247 }
42164248
4249+ if d .HasChange ("ip_allocation_policy.0.additional_ip_ranges_config" ) {
4250+ c := d .Get ("ip_allocation_policy.0.additional_ip_ranges_config" )
4251+ aircs , err := expandAdditionalIpRangesConfigs (c , d , config )
4252+ if err != nil {
4253+ return err
4254+ }
4255+
4256+ req := & container.UpdateClusterRequest {
4257+ Update : & container.ClusterUpdate {
4258+ DesiredAdditionalIpRangesConfig : & container.DesiredAdditionalIPRangesConfig {
4259+ AdditionalIpRangesConfigs : aircs ,
4260+ },
4261+ },
4262+ }
4263+
4264+ updateF := updateFunc (req , "updating AdditionalIpRangesConfig" )
4265+ // Call update serially.
4266+ if err := transport_tpg .LockedCall (lockKey , updateF ); err != nil {
4267+ return err
4268+ }
4269+
4270+ log .Printf ("[INFO] GKE cluster %s's AdditionalIpRangesConfig has been updated" , d .Id ())
4271+ }
4272+
42174273 if n , ok := d .GetOk ("node_pool.#" ); ok {
42184274 for i := 0 ; i < n .(int ); i ++ {
42194275 nodePoolInfo , err := extractNodePoolInformationFromCluster (d , config , clusterName )
@@ -5366,23 +5422,66 @@ func expandPodCidrOverprovisionConfig(configured interface{}) *container.PodCIDR
53665422 }
53675423}
53685424
5369- func expandIPAllocationPolicy (configured interface {}, networkingMode string , autopilot bool ) (* container.IPAllocationPolicy , error ) {
5425+ func expandPodIpv4RangeNames (configured interface {}) []string {
5426+ l := configured .([]interface {})
5427+ if len (l ) == 0 || l [0 ] == nil {
5428+ return nil
5429+ }
5430+ var ranges []string
5431+ for _ , rawRange := range l {
5432+ ranges = append (ranges , rawRange .(string ))
5433+ }
5434+ return ranges
5435+ }
5436+
5437+ func expandAdditionalIpRangesConfigs (configured interface {}, d * schema.ResourceData , c * transport_tpg.Config ) ([]* container.AdditionalIPRangesConfig , error ) {
5438+ l := configured .([]interface {})
5439+ if len (l ) == 0 || l [0 ] == nil {
5440+ return nil , nil
5441+ }
5442+ var additionalIpRangesConfig []* container.AdditionalIPRangesConfig
5443+ for _ , rawConfig := range l {
5444+ config := rawConfig .(map [string ]interface {})
5445+ subnetwork , err := tpgresource .ParseSubnetworkFieldValue (config ["subnetwork" ].(string ), d , c )
5446+ if err != nil {
5447+ return nil , err
5448+ }
5449+ additionalIpRangesConfig = append (additionalIpRangesConfig , & container.AdditionalIPRangesConfig {
5450+ Subnetwork : subnetwork .RelativeLink (),
5451+ PodIpv4RangeNames : expandPodIpv4RangeNames (config ["pod_ipv4_range_names" ]),
5452+ })
5453+ }
5454+
5455+ return additionalIpRangesConfig , nil
5456+ }
5457+
5458+ func expandIPAllocationPolicy (configured interface {}, d * schema.ResourceData , networkingMode string , autopilot bool , c * transport_tpg.Config ) (* container.IPAllocationPolicy , []* container.AdditionalIPRangesConfig , error ) {
53705459 l := configured .([]interface {})
53715460 if len (l ) == 0 || l [0 ] == nil {
53725461 if networkingMode == "VPC_NATIVE" {
5373- return nil , nil
5462+ return nil , nil , nil
53745463 }
53755464 return & container.IPAllocationPolicy {
53765465 UseIpAliases : false ,
53775466 UseRoutes : true ,
53785467 StackType : "IPV4" ,
53795468 ForceSendFields : []string {"UseIpAliases" },
5380- }, nil
5469+ }, nil , nil
53815470 }
53825471
53835472 config := l [0 ].(map [string ]interface {})
53845473 stackType := config ["stack_type" ].(string )
53855474
5475+ // We expand and return additional_ip_ranges_config separately because
5476+ // this field is OUTPUT_ONLY for ClusterCreate RPCs. Instead, during the
5477+ // Terraform Create flow, we follow the CreateCluster (without
5478+ // additional_ip_ranges_config populated) with an UpdateCluster (_with_
5479+ // additional_ip_ranges_config populated).
5480+ additionalIpRangesConfigs , err := expandAdditionalIpRangesConfigs (config ["additional_ip_ranges_config" ], d , c )
5481+ if err != nil {
5482+ return nil , nil , err
5483+ }
5484+
53865485 return & container.IPAllocationPolicy {
53875486 UseIpAliases : networkingMode == "VPC_NATIVE" || networkingMode == "" ,
53885487 ClusterIpv4CidrBlock : config ["cluster_ipv4_cidr_block" ].(string ),
@@ -5393,7 +5492,7 @@ func expandIPAllocationPolicy(configured interface{}, networkingMode string, aut
53935492 UseRoutes : networkingMode == "ROUTES" ,
53945493 StackType : stackType ,
53955494 PodCidrOverprovisionConfig : expandPodCidrOverprovisionConfig (config ["pod_cidr_overprovision_config" ]),
5396- }, nil
5495+ }, additionalIpRangesConfigs , nil
53975496}
53985497
53995498func expandMaintenancePolicy (d * schema.ResourceData , meta interface {}) * container.MaintenancePolicy {
@@ -6980,6 +7079,23 @@ func flattenPodCidrOverprovisionConfig(c *container.PodCIDROverprovisionConfig)
69807079 }
69817080}
69827081
7082+ func flattenAdditionalIpRangesConfigs (c []* container.AdditionalIPRangesConfig ) []map [string ]interface {} {
7083+ if len (c ) == 0 {
7084+ return nil
7085+ }
7086+
7087+ var outRanges []map [string ]interface {}
7088+ for _ , rangeConfig := range c {
7089+ outRangeConfig := map [string ]interface {}{
7090+ "subnetwork" : rangeConfig .Subnetwork ,
7091+ "pod_ipv4_range_names" : rangeConfig .PodIpv4RangeNames ,
7092+ }
7093+ outRanges = append (outRanges , outRangeConfig )
7094+ }
7095+
7096+ return outRanges
7097+ }
7098+
69837099func flattenIPAllocationPolicy (c * container.Cluster , d * schema.ResourceData , config * transport_tpg.Config ) ([]map [string ]interface {}, error ) {
69847100 // If IP aliasing isn't enabled, none of the values in this block can be set.
69857101 if c == nil || c .IpAllocationPolicy == nil || ! c .IpAllocationPolicy .UseIpAliases {
@@ -7010,6 +7126,7 @@ func flattenIPAllocationPolicy(c *container.Cluster, d *schema.ResourceData, con
70107126 "stack_type" : p .StackType ,
70117127 "pod_cidr_overprovision_config" : flattenPodCidrOverprovisionConfig (p .PodCidrOverprovisionConfig ),
70127128 "additional_pod_ranges_config" : flattenAdditionalPodRangesConfig (c .IpAllocationPolicy ),
7129+ "additional_ip_ranges_config" : flattenAdditionalIpRangesConfigs (p .AdditionalIpRangesConfigs ),
70137130 },
70147131 }, nil
70157132}
0 commit comments