Skip to content
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

Add availability_domain field to GCE instance scheduling #11618

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ func convertScheduling(sched *compute.Scheduling) []map[string]interface{} {
"preemptible": sched.Preemptible,
"on_host_maintenance": sched.OnHostMaintenance,
// node_affinities are not converted into cai
"node_affinities": convertSchedulingNodeAffinity(sched.NodeAffinities),
"node_affinities": convertSchedulingNodeAffinity(sched.NodeAffinities),
"availability_domain": sched.AvailabilityDomain,
}
if sched.MinNodeCpus > 0 {
data["min_node_cpus"] = sched.MinNodeCpus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ func expandScheduling(v interface{}) (*compute.Scheduling, error) {
scheduling.InstanceTerminationAction = v.(string)
scheduling.ForceSendFields = append(scheduling.ForceSendFields, "InstanceTerminationAction")
}
if v, ok := original["availability_domain"]; ok {
scheduling.AvailabilityDomain = int64(v.(int))
}
if v, ok := original["max_run_duration"]; ok {
transformedMaxRunDuration, err := expandComputeMaxRunDuration(v)
if err != nil {
Expand Down Expand Up @@ -272,6 +275,7 @@ func flattenScheduling(resp *compute.Scheduling) []map[string]interface{} {
"min_node_cpus": resp.MinNodeCpus,
"provisioning_model": resp.ProvisioningModel,
"instance_termination_action": resp.InstanceTerminationAction,
"availability_domain": resp.AvailabilityDomain,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're writing availability_domain to instance template due to the shared code, but the value isn't available. I assume the unset value is nil so we can't just use a conditional write, an approach used in some other places. We may want to add the field to schema in instance template and add a note it doesn't currently work (but that could lead to weird behaviour if it's added in the future).

Otherwise we may need to pass a flag through from the initial callsite about whether we're working on a template or actual instance, and use that to control what we write.

(I would personally like to remove the shared code eventually to avoid these kind of decisions, not that maintaining separate copies will be very fun 🙁 )

}

if resp.AutomaticRestart != nil {
Expand Down Expand Up @@ -737,6 +741,10 @@ func schedulingHasChangeWithoutReboot(d *schema.ResourceData) bool {
if oScheduling["instance_termination_action"] != newScheduling["instance_termination_action"] {
return true
}

if oScheduling["availability_domain"] != newScheduling["availability_domain"] {
return true
}

return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ var (
"scheduling.0.min_node_cpus",
"scheduling.0.provisioning_model",
"scheduling.0.instance_termination_action",
"scheduling.0.availability_domain",
"scheduling.0.max_run_duration",
"scheduling.0.on_instance_stop_action",
<% unless version == 'ga' -%>
Expand Down Expand Up @@ -844,6 +845,13 @@ func ResourceComputeInstance() *schema.Resource {
AtLeastOneOf: schedulingKeys,
Description: `Specifies the action GCE should take when SPOT VM is preempted.`,
},
"availability_domain": {
Type: schema.TypeInt,
Optional: true,
AtLeastOneOf: schedulingKeys,
Description: `Specifies the availability domain (AD), which this instance should be scheduled on.`,

},
"max_run_duration" : {
Type: schema.TypeList,
Optional: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9321,6 +9321,48 @@ resource "google_compute_instance" "foobar" {
`, suffix, suffix, suffix, suffix, suffix, suffix, policy, instance)
}

func testAccComputeInstance_setAvailabilityDomain(instance, suffix string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}

resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "c2-standard-4"
zone = "us-east4-b"
can_ip_forward = false
tags = ["foo", "bar"]

boot_disk {
initialize_params {
image = data.google_compute_image.my_image.self_link
}
}

network_interface {
network = "default"
}

scheduling {
availability_domain = 5
}

resource_policies = [google_compute_resource_policy.foo.self_link]
}


resource "google_compute_resource_policy" "foo" {
name = "tf-test-policy-%s"
region = "us-east4"
group_placement_policy {
availability_domain_count = 8
}
}
`, instance, instance, suffix)
}

func testAccComputeInstance_nic_securityPolicyCreateWithTwoAccessConfigsWithTwoSecurityPoliciesAndStatus(suffix, policy, instance, policyToSetOne, desiredStatus string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ specified, then this instance will have no external IPv6 Internet access. Struct

* `instance_termination_action` - (Optional) Describe the type of termination action for VM. Can be `STOP` or `DELETE`. Read more on [here](https://cloud.google.com/compute/docs/instances/create-use-spot)

* `availability_domain` - (Optional) Specifies the availability domain (AD), which this instance should be scheduled on. The AD belongs to the spread placement policy that has been assigned to the instance. Specify a value from 1 to max count of availability domains in your spread placement policy.

* `max_run_duration` - (Optional) The duration of the instance. Instance will run and be terminated after then, the termination action could be defined in `instance_termination_action`. Structure is [documented below](#nested_max_run_duration).


Expand Down
Loading