Skip to content

Commit

Permalink
Add support for capacity aware modes to `google_compute_region_instan…
Browse files Browse the repository at this point in the history
…ce_group_manager (GoogleCloudPlatform#4348)

* add dpts to RIGM

Co-authored-by: upodroid <cy@borg.dev>

* typo fix

* add doc note
  • Loading branch information
upodroid authored Jan 21, 2021
1 parent 81b5bb5 commit 27ab753
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ func resourceComputeRegionInstanceGroupManager() *schema.Resource {
},
},

"distribution_policy_target_shape": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: `The shape to which the group converges either proactively or on resize events (depending on the value set in updatePolicy.instanceRedistributionType).`,
},

"update_policy": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -345,7 +353,7 @@ func resourceComputeRegionInstanceGroupManagerCreate(d *schema.ResourceData, met
AutoHealingPolicies: expandAutoHealingPolicies(d.Get("auto_healing_policies").([]interface{})),
Versions: expandVersions(d.Get("version").([]interface{})),
UpdatePolicy: expandRegionUpdatePolicy(d.Get("update_policy").([]interface{})),
DistributionPolicy: expandDistributionPolicy(d.Get("distribution_policy_zones").(*schema.Set)),
DistributionPolicy: expandDistributionPolicy(d),
StatefulPolicy: expandStatefulPolicy(d.Get("stateful_disk").(*schema.Set).List()),
// Force send TargetSize to allow size of 0.
ForceSendFields: []string{"TargetSize"},
Expand Down Expand Up @@ -466,6 +474,9 @@ func resourceComputeRegionInstanceGroupManagerRead(d *schema.ResourceData, meta
if err := d.Set("distribution_policy_zones", flattenDistributionPolicy(manager.DistributionPolicy)); err != nil {
return err
}
if err := d.Set("distribution_policy_target_shape", manager.DistributionPolicy.TargetShape); err != nil {
return err
}
if err := d.Set("self_link", ConvertSelfLinkToV1(manager.SelfLink)); err != nil {
return fmt.Errorf("Error setting self_link: %s", err)
}
Expand Down Expand Up @@ -716,21 +727,24 @@ func flattenRegionUpdatePolicy(updatePolicy *computeBeta.InstanceGroupManagerUpd
return results
}

func expandDistributionPolicy(configured *schema.Set) *computeBeta.DistributionPolicy {
if configured.Len() == 0 {
func expandDistributionPolicy(d *schema.ResourceData) *computeBeta.DistributionPolicy {
dpz := d.Get("distribution_policy_zones").(*schema.Set)
dpts := d.Get("distribution_policy_target_shape").(string)
if dpz.Len() == 0 && dpts == "" {
return nil
}

distributionPolicyZoneConfigs := make([]*computeBeta.DistributionPolicyZoneConfiguration, 0, configured.Len())
for _, raw := range configured.List() {
distributionPolicyZoneConfigs := make([]*computeBeta.DistributionPolicyZoneConfiguration, 0, dpz.Len())
for _, raw := range dpz.List() {
data := raw.(string)
distributionPolicyZoneConfig := computeBeta.DistributionPolicyZoneConfiguration{
Zone: "zones/" + data,
}

distributionPolicyZoneConfigs = append(distributionPolicyZoneConfigs, &distributionPolicyZoneConfig)
}
return &computeBeta.DistributionPolicy{Zones: distributionPolicyZoneConfigs}

return &computeBeta.DistributionPolicy{Zones: distributionPolicyZoneConfigs, TargetShape: dpts}
}

func flattenDistributionPolicy(distributionPolicy *computeBeta.DistributionPolicy) []string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1057,10 +1057,11 @@ resource "google_compute_region_instance_group_manager" "igm-basic" {
name = "primary"
}
base_instance_name = "igm-basic"
region = "us-central1"
target_size = 2
distribution_policy_zones = ["%s"]
base_instance_name = "igm-basic"
region = "us-central1"
target_size = 2
distribution_policy_zones = ["%s"]
distribution_policy_target_shape = "ANY"
}
`, template, igm, strings.Join(zones, "\",\""))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ description: |-

The Google Compute Engine Regional Instance Group Manager API creates and manages pools
of homogeneous Compute Engine virtual machine instances from a common instance
template. For more information, see [the official documentation](https://cloud.google.com/compute/docs/instance-groups/distributing-instances-with-regional-instance-groups)
and [API](https://cloud.google.com/compute/docs/reference/latest/regionInstanceGroupManagers)
template.

~> **Note:** Use [google_compute_instance_group_manager](/docs/providers/google/r/compute_instance_group_manager.html) to create a single-zone instance group manager.
To get more information about regionInstanceGroupManagers, see:

* [API documentation](https://cloud.google.com/compute/docs/reference/latest/regionInstanceGroupManagers)
* How-to Guides
* [Regional Instance Groups Guide](https://cloud.google.com/compute/docs/instance-groups/distributing-instances-with-regional-instance-groups)

~> **Note:** Use [google_compute_instance_group_manager](/docs/providers/google/r/compute_instance_group_manager.html) to create a zonal instance group manager.

## Example Usage with top level instance template (`google` provider)

Expand Down Expand Up @@ -138,6 +143,8 @@ group. You can specify only one value. Structure is documented below. For more i
* `distribution_policy_zones` - (Optional) The distribution policy for this managed instance
group. You can specify one or more values. For more information, see the [official documentation](https://cloud.google.com/compute/docs/instance-groups/distributing-instances-with-regional-instance-groups#selectingzones).

* `distribution_policy_target_shape` - (Optional) The shape to which the group converges either proactively or on resize events (depending on the value set in update_policy.0.instance_redistribution_type). For more information see the [official documentation](https://cloud.google.com/compute/docs/instance-groups/regional-mig-distribution-shape).

* `stateful_disk` - (Optional) Disks created on the instances that will be preserved on instance delete, update, etc. Structure is documented below. For more information see the [official documentation](https://cloud.google.com/compute/docs/instance-groups/configuring-stateful-disks-in-migs). Proactive cross zone instance redistribution must be disabled before you can update stateful disks on existing instance group managers. This can be controlled via the `update_policy`.

- - -
Expand Down

0 comments on commit 27ab753

Please sign in to comment.