Skip to content

Commit 16afff5

Browse files
Add support for auto ipam configuration (#15035) (#10737)
[upstream:8425614fd16cf3a096b319bb447a0b81b0f1ed4c] Signed-off-by: Modular Magician <[email protected]>
1 parent 333b06a commit 16afff5

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed

.changelog/15035.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
container: added `auto_ipam_config` to `google_container_cluster` resource.
3+
```

google-beta/services/container/resource_container_cluster.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,22 @@ func ResourceContainerCluster() *schema.Resource {
18481848
},
18491849
},
18501850
},
1851+
"auto_ipam_config": {
1852+
Type: schema.TypeList,
1853+
MaxItems: 1,
1854+
Optional: true,
1855+
Computed: true,
1856+
Description: `AutoIpamConfig contains all information related to Auto IPAM.`,
1857+
Elem: &schema.Resource{
1858+
Schema: map[string]*schema.Schema{
1859+
"enabled": {
1860+
Type: schema.TypeBool,
1861+
Required: true,
1862+
Description: `The flag that enables Auto IPAM on this cluster.`,
1863+
},
1864+
},
1865+
},
1866+
},
18511867
},
18521868
},
18531869
},
@@ -4281,6 +4297,21 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
42814297
log.Printf("[INFO] GKE cluster %s's AdditionalIpRangesConfig has been updated", d.Id())
42824298
}
42834299

4300+
if d.HasChange("ip_allocation_policy.0.auto_ipam_config") {
4301+
req := &container.UpdateClusterRequest{
4302+
Update: &container.ClusterUpdate{
4303+
DesiredAutoIpamConfig: &container.AutoIpamConfig{Enabled: d.Get("ip_allocation_policy.0.auto_ipam_config.0.enabled").(bool)},
4304+
},
4305+
}
4306+
4307+
updateF := updateFunc(req, "updating AutoIpamConfig")
4308+
if err := transport_tpg.LockedCall(lockKey, updateF); err != nil {
4309+
return err
4310+
}
4311+
4312+
log.Printf("[INFO] GKE cluster %s's AutoIpamConfig has been updated", d.Id())
4313+
}
4314+
42844315
if n, ok := d.GetOk("node_pool.#"); ok {
42854316
for i := 0; i < n.(int); i++ {
42864317
nodePoolInfo, err := extractNodePoolInformationFromCluster(d, config, clusterName)
@@ -5521,9 +5552,21 @@ func expandIPAllocationPolicy(configured interface{}, d *schema.ResourceData, ne
55215552
UseRoutes: networkingMode == "ROUTES",
55225553
StackType: stackType,
55235554
PodCidrOverprovisionConfig: expandPodCidrOverprovisionConfig(config["pod_cidr_overprovision_config"]),
5555+
AutoIpamConfig: expandAutoIpamConfig(config["auto_ipam_config"]),
55245556
}, additionalIpRangesConfigs, nil
55255557
}
55265558

5559+
func expandAutoIpamConfig(configured interface{}) *container.AutoIpamConfig {
5560+
l, ok := configured.([]interface{})
5561+
if !ok || len(l) == 0 || l[0] == nil {
5562+
return nil
5563+
}
5564+
5565+
return &container.AutoIpamConfig{
5566+
Enabled: l[0].(map[string]interface{})["enabled"].(bool),
5567+
}
5568+
}
5569+
55275570
func expandMaintenancePolicy(d *schema.ResourceData, meta interface{}) *container.MaintenancePolicy {
55285571
config := meta.(*transport_tpg.Config)
55295572
// We have to perform a full Get() as part of this, to get the fingerprint. We can't do this
@@ -7169,10 +7212,23 @@ func flattenIPAllocationPolicy(c *container.Cluster, d *schema.ResourceData, con
71697212
"pod_cidr_overprovision_config": flattenPodCidrOverprovisionConfig(p.PodCidrOverprovisionConfig),
71707213
"additional_pod_ranges_config": flattenAdditionalPodRangesConfig(c.IpAllocationPolicy),
71717214
"additional_ip_ranges_config": flattenAdditionalIpRangesConfigs(p.AdditionalIpRangesConfigs),
7215+
"auto_ipam_config": flattenAutoIpamConfig(p.AutoIpamConfig),
71727216
},
71737217
}, nil
71747218
}
71757219

7220+
func flattenAutoIpamConfig(aic *container.AutoIpamConfig) []map[string]interface{} {
7221+
if aic == nil {
7222+
return nil
7223+
}
7224+
7225+
return []map[string]interface{}{
7226+
{
7227+
"enabled": aic.Enabled,
7228+
},
7229+
}
7230+
}
7231+
71767232
func flattenMaintenancePolicy(mp *container.MaintenancePolicy) []map[string]interface{} {
71777233
if mp == nil || mp.Window == nil {
71787234
return nil

google-beta/services/container/resource_container_cluster_meta.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ fields:
150150
- field: 'ip_allocation_policy.additional_pod_ranges_config.pod_range_names'
151151
- field: 'ip_allocation_policy.additional_ip_ranges_config.subnetwork'
152152
- field: 'ip_allocation_policy.additional_ip_ranges_config.pod_ipv4_range_names'
153+
- field: 'ip_allocation_policy.auto_ipam_config.enabled'
153154
- field: 'ip_allocation_policy.cluster_ipv4_cidr_block'
154155
- field: 'ip_allocation_policy.cluster_secondary_range_name'
155156
- field: 'ip_allocation_policy.pod_cidr_overprovision_config.disabled'

google-beta/services/container/resource_container_cluster_test.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14526,6 +14526,118 @@ func TestAccContainerCluster_additional_ip_ranges_config_on_update(t *testing.T)
1452614526
})
1452714527
}
1452814528

14529+
func TestAccContainerCluster_auto_ipam_config_enabled(t *testing.T) {
14530+
t.Parallel()
14531+
14532+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
14533+
networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
14534+
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)
14535+
14536+
acctest.VcrTest(t, resource.TestCase{
14537+
PreCheck: func() { acctest.AccTestPreCheck(t) },
14538+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
14539+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
14540+
Steps: []resource.TestStep{
14541+
{
14542+
Config: testAccContainerCluster_auto_ipam_config_enabled(clusterName, networkName, subnetworkName, true),
14543+
Check: resource.ComposeTestCheckFunc(
14544+
resource.TestCheckResourceAttr("google_container_cluster.primary", "ip_allocation_policy.0.auto_ipam_config.0.enabled", "true"),
14545+
),
14546+
},
14547+
{
14548+
ResourceName: "google_container_cluster.primary",
14549+
ImportState: true,
14550+
ImportStateVerify: true,
14551+
ImportStateVerifyIgnore: []string{"deletion_protection"},
14552+
},
14553+
{
14554+
Config: testAccContainerCluster_auto_ipam_config_enabled(clusterName, networkName, subnetworkName, false),
14555+
Check: resource.ComposeTestCheckFunc(
14556+
resource.TestCheckResourceAttr("google_container_cluster.primary", "ip_allocation_policy.0.auto_ipam_config.0.enabled", "false"),
14557+
),
14558+
},
14559+
{
14560+
ResourceName: "google_container_cluster.primary",
14561+
ImportState: true,
14562+
ImportStateVerify: true,
14563+
ImportStateVerifyIgnore: []string{"deletion_protection"},
14564+
},
14565+
{
14566+
Config: testAccContainerCluster_auto_ipam_config_enabled(clusterName, networkName, subnetworkName, true),
14567+
Check: resource.ComposeTestCheckFunc(
14568+
resource.TestCheckResourceAttr("google_container_cluster.primary", "ip_allocation_policy.0.auto_ipam_config.0.enabled", "true"),
14569+
),
14570+
},
14571+
{
14572+
ResourceName: "google_container_cluster.primary",
14573+
ImportState: true,
14574+
ImportStateVerify: true,
14575+
ImportStateVerifyIgnore: []string{"deletion_protection"},
14576+
},
14577+
},
14578+
})
14579+
}
14580+
14581+
func testAccContainerCluster_auto_ipam_config_enabled(clusterName, networkName, subnetworkName string, enabled bool) string {
14582+
return fmt.Sprintf(`
14583+
resource "google_container_cluster" "primary" {
14584+
name = "%s"
14585+
location = "us-central1-a"
14586+
initial_node_count = 1
14587+
network = "%s"
14588+
subnetwork = "%s"
14589+
14590+
deletion_protection = false
14591+
14592+
ip_allocation_policy {
14593+
auto_ipam_config {
14594+
enabled = %t
14595+
}
14596+
}
14597+
}
14598+
`, clusterName, networkName, subnetworkName, enabled)
14599+
}
14600+
14601+
func TestAccContainerCluster_auto_ipam_config_none(t *testing.T) {
14602+
t.Parallel()
14603+
14604+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
14605+
networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
14606+
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)
14607+
14608+
acctest.VcrTest(t, resource.TestCase{
14609+
PreCheck: func() { acctest.AccTestPreCheck(t) },
14610+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
14611+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
14612+
Steps: []resource.TestStep{
14613+
{
14614+
Config: testAccContainerCluster_auto_ipam_config_none(clusterName, networkName, subnetworkName),
14615+
},
14616+
{
14617+
ResourceName: "google_container_cluster.primary",
14618+
ImportState: true,
14619+
ImportStateVerify: true,
14620+
ImportStateVerifyIgnore: []string{"deletion_protection"},
14621+
},
14622+
},
14623+
})
14624+
}
14625+
14626+
func testAccContainerCluster_auto_ipam_config_none(clusterName, networkName, subnetworkName string) string {
14627+
return fmt.Sprintf(`
14628+
resource "google_container_cluster" "primary" {
14629+
name = "%s"
14630+
location = "us-central1-a"
14631+
initial_node_count = 1
14632+
14633+
network = "%s"
14634+
subnetwork = "%s"
14635+
14636+
deletion_protection = false
14637+
}
14638+
`, clusterName, networkName, subnetworkName)
14639+
}
14640+
1452914641
func TestAccContainerCluster_withAnonymousAuthenticationConfig(t *testing.T) {
1453014642
t.Parallel()
1453114643

website/docs/r/container_cluster.html.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,12 @@ secondary Pod IP address assignment to node pools isn't needed. Structure is [do
843843
* `additional_ip_ranges_config` - (Optional) The configuration for individual additional subnetworks attached to the cluster.
844844
Structure is [documented below](#nested_additional_ip_ranges_config).
845845

846+
* `auto_ipam_config` - (Optional) All the information related to Auto IPAM. Structure is [documented below](#nested_auto_ipam_config)
847+
848+
<a name="nested_auto_ipam_config"></a>The auto ipam config supports:
849+
850+
* `enabled` - (Required) The flag that enables Auto IPAM on this cluster.
851+
846852

847853
<a name="nested_additional_pod_ranges_config"></a>The `additional_pod_ranges_config` block supports:
848854

0 commit comments

Comments
 (0)