Skip to content

Commit 8425614

Browse files
authored
Add support for auto ipam configuration (#15035)
1 parent 7d1e129 commit 8425614

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed

mmv1/third_party/terraform/services/container/resource_container_cluster.go.tmpl

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,6 +1858,22 @@ func ResourceContainerCluster() *schema.Resource {
18581858
},
18591859

18601860
},
1861+
"auto_ipam_config": {
1862+
Type: schema.TypeList,
1863+
MaxItems: 1,
1864+
Optional: true,
1865+
Computed: true,
1866+
Description: `AutoIpamConfig contains all information related to Auto IPAM.`,
1867+
Elem: &schema.Resource{
1868+
Schema: map[string]*schema.Schema{
1869+
"enabled": {
1870+
Type: schema.TypeBool,
1871+
Required: true,
1872+
Description: `The flag that enables Auto IPAM on this cluster.`,
1873+
},
1874+
},
1875+
},
1876+
},
18611877
},
18621878
},
18631879
},
@@ -4312,6 +4328,21 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
43124328
log.Printf("[INFO] GKE cluster %s's AdditionalIpRangesConfig has been updated", d.Id())
43134329
}
43144330

4331+
if d.HasChange("ip_allocation_policy.0.auto_ipam_config") {
4332+
req := &container.UpdateClusterRequest{
4333+
Update: &container.ClusterUpdate{
4334+
DesiredAutoIpamConfig: &container.AutoIpamConfig{Enabled: d.Get("ip_allocation_policy.0.auto_ipam_config.0.enabled").(bool)},
4335+
},
4336+
}
4337+
4338+
updateF := updateFunc(req, "updating AutoIpamConfig")
4339+
if err := transport_tpg.LockedCall(lockKey, updateF); err != nil {
4340+
return err
4341+
}
4342+
4343+
log.Printf("[INFO] GKE cluster %s's AutoIpamConfig has been updated", d.Id())
4344+
}
4345+
43154346
if n, ok := d.GetOk("node_pool.#"); ok {
43164347
for i := 0; i < n.(int); i++ {
43174348
nodePoolInfo, err := extractNodePoolInformationFromCluster(d, config, clusterName)
@@ -5562,9 +5593,21 @@ func expandIPAllocationPolicy(configured interface{}, d *schema.ResourceData, ne
55625593
UseRoutes: networkingMode == "ROUTES",
55635594
StackType: stackType,
55645595
PodCidrOverprovisionConfig: expandPodCidrOverprovisionConfig(config["pod_cidr_overprovision_config"]),
5596+
AutoIpamConfig: expandAutoIpamConfig(config["auto_ipam_config"]),
55655597
}, additionalIpRangesConfigs, nil
55665598
}
55675599

5600+
func expandAutoIpamConfig(configured interface{}) *container.AutoIpamConfig {
5601+
l, ok := configured.([]interface{})
5602+
if !ok || len(l) == 0 || l[0] == nil {
5603+
return nil
5604+
}
5605+
5606+
return &container.AutoIpamConfig{
5607+
Enabled: l[0].(map[string]interface{})["enabled"].(bool),
5608+
}
5609+
}
5610+
55685611
func expandMaintenancePolicy(d *schema.ResourceData, meta interface{}) *container.MaintenancePolicy {
55695612
config := meta.(*transport_tpg.Config)
55705613
// We have to perform a full Get() as part of this, to get the fingerprint. We can't do this
@@ -7227,10 +7270,23 @@ func flattenIPAllocationPolicy(c *container.Cluster, d *schema.ResourceData, con
72277270
"pod_cidr_overprovision_config": flattenPodCidrOverprovisionConfig(p.PodCidrOverprovisionConfig),
72287271
"additional_pod_ranges_config": flattenAdditionalPodRangesConfig(c.IpAllocationPolicy),
72297272
"additional_ip_ranges_config": flattenAdditionalIpRangesConfigs(p.AdditionalIpRangesConfigs),
7273+
"auto_ipam_config": flattenAutoIpamConfig(p.AutoIpamConfig),
72307274
},
72317275
}, nil
72327276
}
72337277

7278+
func flattenAutoIpamConfig(aic *container.AutoIpamConfig) []map[string]interface{} {
7279+
if aic == nil {
7280+
return nil
7281+
}
7282+
7283+
return []map[string]interface{}{
7284+
{
7285+
"enabled": aic.Enabled,
7286+
},
7287+
}
7288+
}
7289+
72347290
func flattenMaintenancePolicy(mp *container.MaintenancePolicy) []map[string]interface{} {
72357291
if mp == nil || mp.Window == nil {
72367292
return nil

mmv1/third_party/terraform/services/container/resource_container_cluster_meta.yaml.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ fields:
162162
- field: 'ip_allocation_policy.additional_pod_ranges_config.pod_range_names'
163163
- field: 'ip_allocation_policy.additional_ip_ranges_config.subnetwork'
164164
- field: 'ip_allocation_policy.additional_ip_ranges_config.pod_ipv4_range_names'
165+
- field: 'ip_allocation_policy.auto_ipam_config.enabled'
165166
- field: 'ip_allocation_policy.cluster_ipv4_cidr_block'
166167
- field: 'ip_allocation_policy.cluster_secondary_range_name'
167168
- field: 'ip_allocation_policy.pod_cidr_overprovision_config.disabled'

mmv1/third_party/terraform/services/container/resource_container_cluster_test.go.tmpl

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14565,6 +14565,118 @@ func TestAccContainerCluster_additional_ip_ranges_config_on_update(t *testing.T)
1456514565
})
1456614566
}
1456714567

14568+
func TestAccContainerCluster_auto_ipam_config_enabled(t *testing.T) {
14569+
t.Parallel()
14570+
14571+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
14572+
networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
14573+
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)
14574+
14575+
acctest.VcrTest(t, resource.TestCase{
14576+
PreCheck: func() { acctest.AccTestPreCheck(t) },
14577+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
14578+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
14579+
Steps: []resource.TestStep{
14580+
{
14581+
Config: testAccContainerCluster_auto_ipam_config_enabled(clusterName, networkName, subnetworkName, true),
14582+
Check: resource.ComposeTestCheckFunc(
14583+
resource.TestCheckResourceAttr("google_container_cluster.primary", "ip_allocation_policy.0.auto_ipam_config.0.enabled", "true"),
14584+
),
14585+
},
14586+
{
14587+
ResourceName: "google_container_cluster.primary",
14588+
ImportState: true,
14589+
ImportStateVerify: true,
14590+
ImportStateVerifyIgnore: []string{"deletion_protection"},
14591+
},
14592+
{
14593+
Config: testAccContainerCluster_auto_ipam_config_enabled(clusterName, networkName, subnetworkName, false),
14594+
Check: resource.ComposeTestCheckFunc(
14595+
resource.TestCheckResourceAttr("google_container_cluster.primary", "ip_allocation_policy.0.auto_ipam_config.0.enabled", "false"),
14596+
),
14597+
},
14598+
{
14599+
ResourceName: "google_container_cluster.primary",
14600+
ImportState: true,
14601+
ImportStateVerify: true,
14602+
ImportStateVerifyIgnore: []string{"deletion_protection"},
14603+
},
14604+
{
14605+
Config: testAccContainerCluster_auto_ipam_config_enabled(clusterName, networkName, subnetworkName, true),
14606+
Check: resource.ComposeTestCheckFunc(
14607+
resource.TestCheckResourceAttr("google_container_cluster.primary", "ip_allocation_policy.0.auto_ipam_config.0.enabled", "true"),
14608+
),
14609+
},
14610+
{
14611+
ResourceName: "google_container_cluster.primary",
14612+
ImportState: true,
14613+
ImportStateVerify: true,
14614+
ImportStateVerifyIgnore: []string{"deletion_protection"},
14615+
},
14616+
},
14617+
})
14618+
}
14619+
14620+
func testAccContainerCluster_auto_ipam_config_enabled(clusterName, networkName, subnetworkName string, enabled bool) string {
14621+
return fmt.Sprintf(`
14622+
resource "google_container_cluster" "primary" {
14623+
name = "%s"
14624+
location = "us-central1-a"
14625+
initial_node_count = 1
14626+
network = "%s"
14627+
subnetwork = "%s"
14628+
14629+
deletion_protection = false
14630+
14631+
ip_allocation_policy {
14632+
auto_ipam_config {
14633+
enabled = %t
14634+
}
14635+
}
14636+
}
14637+
`, clusterName, networkName, subnetworkName, enabled)
14638+
}
14639+
14640+
func TestAccContainerCluster_auto_ipam_config_none(t *testing.T) {
14641+
t.Parallel()
14642+
14643+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
14644+
networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
14645+
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)
14646+
14647+
acctest.VcrTest(t, resource.TestCase{
14648+
PreCheck: func() { acctest.AccTestPreCheck(t) },
14649+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
14650+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
14651+
Steps: []resource.TestStep{
14652+
{
14653+
Config: testAccContainerCluster_auto_ipam_config_none(clusterName, networkName, subnetworkName),
14654+
},
14655+
{
14656+
ResourceName: "google_container_cluster.primary",
14657+
ImportState: true,
14658+
ImportStateVerify: true,
14659+
ImportStateVerifyIgnore: []string{"deletion_protection"},
14660+
},
14661+
},
14662+
})
14663+
}
14664+
14665+
func testAccContainerCluster_auto_ipam_config_none(clusterName, networkName, subnetworkName string) string {
14666+
return fmt.Sprintf(`
14667+
resource "google_container_cluster" "primary" {
14668+
name = "%s"
14669+
location = "us-central1-a"
14670+
initial_node_count = 1
14671+
14672+
network = "%s"
14673+
subnetwork = "%s"
14674+
14675+
deletion_protection = false
14676+
}
14677+
`, clusterName, networkName, subnetworkName)
14678+
}
14679+
1456814680
func TestAccContainerCluster_withAnonymousAuthenticationConfig(t *testing.T) {
1456914681
t.Parallel()
1457014682

mmv1/third_party/terraform/website/docs/r/container_cluster.html.markdown

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

832+
* `auto_ipam_config` - (Optional) All the information related to Auto IPAM. Structure is [documented below](#nested_auto_ipam_config)
833+
834+
<a name="nested_auto_ipam_config"></a>The auto ipam config supports:
835+
836+
* `enabled` - (Required) The flag that enables Auto IPAM on this cluster.
837+
832838

833839
<a name="nested_additional_pod_ranges_config"></a>The `additional_pod_ranges_config` block supports:
834840

0 commit comments

Comments
 (0)