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 provisioning-model to instance and instance template to support Spot VM for Beta #5662

Merged
merged 18 commits into from
Feb 7, 2022
Merged
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
6 changes: 4 additions & 2 deletions mmv1/third_party/terraform/go.mod.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/hashicorp/terraform-provider-google<%= "-" + version unless ve

require (
cloud.google.com/go/bigtable v1.10.1
cloud.google.com/go/iam v0.1.1 // indirect
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20220125025424-6dfdf699127c
github.com/apparentlymart/go-cidr v1.1.0
github.com/client9/misspell v0.3.4
Expand All @@ -25,8 +26,9 @@ require (
golang.org/x/mod v0.5.0 // indirect
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
google.golang.org/api v0.61.0
google.golang.org/grpc v1.40.0
google.golang.org/api v0.65.0
google.golang.org/grpc v1.40.1
)


go 1.16
87 changes: 67 additions & 20 deletions mmv1/third_party/terraform/go.sum

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ var (
"scheduling.0.preemptible",
"scheduling.0.node_affinities",
"scheduling.0.min_node_cpus",
<% unless version == 'ga' -%>
"scheduling.0.provisioning_model",
<% end -%>
}

shieldedInstanceConfigKeys = []string{
Expand Down Expand Up @@ -627,6 +630,15 @@ func resourceComputeInstance() *schema.Resource {
Optional: true,
AtLeastOneOf: schedulingKeys,
},
<% unless version == 'ga' -%>
"provisioning_model": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
AtLeastOneOf: schedulingKeys,
Description: `Whether the instance is spot. If this is set as SPOT.`,
},
<% end -%>
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ var (
"scheduling.0.preemptible",
"scheduling.0.node_affinities",
"scheduling.0.min_node_cpus",
<% unless version == 'ga' -%>
"scheduling.0.provisioning_model",
<% end -%>
}

shieldedInstanceTemplateConfigKeys = []string{
Expand Down Expand Up @@ -538,6 +541,15 @@ func resourceComputeInstanceTemplate() *schema.Resource {
AtLeastOneOf: schedulingInstTemplateKeys,
Description: `Minimum number of cpus for the instance.`,
},
<% unless version == 'ga' -%>
"provisioning_model": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
AtLeastOneOf: schedulingInstTemplateKeys,
Description: `Whether the instance is spot. If this is set as SPOT.`,
},
<% end -%>
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,37 @@ func TestAccComputeInstanceTemplate_queueCount(t *testing.T) {
})
}

<% unless version == 'ga' -%>
func TestAccComputeInstanceTemplate_spot(t *testing.T) {
t.Parallel()

var instanceTemplate compute.InstanceTemplate

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceTemplateDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeInstanceTemplate_spot(randString(t, 10)),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceTemplateExists(
t, "google_compute_instance_template.foobar", &instanceTemplate),
testAccCheckComputeInstanceTemplateAutomaticRestart(&instanceTemplate, false),
testAccCheckComputeInstanceTemplatePreemptible(&instanceTemplate, true),
testAccCheckComputeInstanceTemplateProvisioningModel(&instanceTemplate, "SPOT"),
),
},
{
ResourceName: "google_compute_instance_template.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
<% end -%>

func testAccCheckComputeInstanceTemplateDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
config := googleProviderConfig(t)
Expand Down Expand Up @@ -1227,6 +1258,18 @@ func testAccCheckComputeInstanceTemplatePreemptible(instanceTemplate *compute.In
}
}

<% unless version == 'ga' -%>
func testAccCheckComputeInstanceTemplateProvisioningModel(instanceTemplate *compute.InstanceTemplate, provisioning_model string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if instanceTemplate.Properties.Scheduling.ProvisioningModel != provisioning_model {
return fmt.Errorf("Expected provisioning_model %v, got %v", provisioning_model, instanceTemplate.Properties.Scheduling.ProvisioningModel)
}
return nil
}
}
<% end -%>


func testAccCheckComputeInstanceTemplateAutomaticRestart(instanceTemplate *compute.InstanceTemplate, automaticRestart bool) resource.TestCheckFunc {
return func(s *terraform.State) error {
ar := instanceTemplate.Properties.Scheduling.AutomaticRestart
Expand Down Expand Up @@ -2687,3 +2730,47 @@ resource "google_compute_instance_template" "foobar" {
}
`, instanceTemplateName)
}

<% unless version == 'ga' -%>
func testAccComputeInstanceTemplate_spot(suffix string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-9"
project = "debian-cloud"
}

resource "google_compute_instance_template" "foobar" {
name = "tf-test-instance-template-%s"
machine_type = "e2-medium"
can_ip_forward = false
tags = ["foo", "bar"]

disk {
source_image = data.google_compute_image.my_image.self_link
auto_delete = true
boot = true
}

network_interface {
network = "default"
}

scheduling {
preemptible = true
automatic_restart = false
provisioning_model = "SPOT"
}

metadata = {
foo = "bar"
}

service_account {
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
}
}
`, suffix)
}
<% end -%>


Original file line number Diff line number Diff line change
Expand Up @@ -2217,6 +2217,62 @@ func TestAccComputeInstance_queueCount(t *testing.T) {
})
}

<% unless version == 'ga' -%>
func TestAccComputeInstance_spotVM(t *testing.T) {
t.Parallel()

var instance compute.Instance
var instanceName = fmt.Sprintf("tf-test-%s", randString(t, 10))

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeInstance_spotVM(instanceName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
t, "google_compute_instance.foobar", &instance),
),
},
computeInstanceImportStep("us-central1-a", instanceName, []string{}),
},
})
}

func TestAccComputeInstance_spotVM_update(t *testing.T) {
t.Parallel()

var instance compute.Instance
var instanceName = fmt.Sprintf("tf-test-%s", randString(t, 10))

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeInstance_scheduling(instanceName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
t, "google_compute_instance.foobar", &instance),
),
},
computeInstanceImportStep("us-central1-a", instanceName, []string{}),
{
Config: testAccComputeInstance_spotVM(instanceName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
t, "google_compute_instance.foobar", &instance),
),
},
computeInstanceImportStep("us-central1-a", instanceName, []string{}),
},
})
}
<% end -%>

func TestComputeInstance_networkIPCustomizedDiff(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -6181,3 +6237,39 @@ resource "google_compute_instance" "foobar" {
}
`, instance)
}

<% unless version == 'ga' -%>
func testAccComputeInstance_spotVM(instance string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "ubuntu-2004-lts"
project = "ubuntu-os-cloud"
}

resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "e2-medium"
zone = "us-central1-a"

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

network_interface {
network = "default"
}

scheduling {
provisioning_model = "SPOT"
automatic_restart = false
preemptible = true
}

}
`, instance)
}
<% end -%>


16 changes: 15 additions & 1 deletion mmv1/third_party/terraform/utils/compute_instance_helpers.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ func expandScheduling(v interface{}) (*compute.Scheduling, error) {
if v, ok := original["min_node_cpus"]; ok {
scheduling.MinNodeCpus = int64(v.(int))
}

<% unless version == 'ga' -%>
if v, ok := original["provisioning_model"]; ok {
scheduling.ProvisioningModel = v.(string)
scheduling.ForceSendFields = append(scheduling.ForceSendFields, "ProvisioningModel")
}
<% end -%>
return scheduling, nil
}

Expand All @@ -132,6 +137,9 @@ func flattenScheduling(resp *compute.Scheduling) []map[string]interface{} {
"on_host_maintenance": resp.OnHostMaintenance,
"preemptible": resp.Preemptible,
"min_node_cpus": resp.MinNodeCpus,
<% unless version == 'ga' -%>
"provisioning_model": resp.ProvisioningModel,
<% end -%>
}

if resp.AutomaticRestart != nil {
Expand Down Expand Up @@ -476,6 +484,12 @@ func schedulingHasChangeWithoutReboot(d *schema.ResourceData) bool {
return true
}

<% unless version == 'ga' -%>
if oScheduling["provisioning_model"] != newScheduling["provisioning_model"] {
return true
}
<% end -%>

return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ The following arguments are supported:

* `automatic_restart` - Specifies if the instance should be
restarted if it was terminated by Compute Engine (not a user).

* `provisioning_model`(Beta) - Describe the type of preemptible VM.

<a name="nested_guest_accelerator"></a>The `guest_accelerator` block supports:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ The `disk_encryption_key` block supports:
groups will use as host systems. Read more on sole-tenant node creation
[here](https://cloud.google.com/compute/docs/nodes/create-nodes).
Structure [documented below](#nested_node_affinities).

* `provisioning_model`(Beta) - Describe the type of preemptible VM.

<a name="nested_guest_accelerator"></a>The `guest_accelerator` block supports:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ specified, then this instance will have no external IPv6 Internet access. Struct

* `min_node_cpus` - (Optional) The minimum number of virtual CPUs this instance will consume when running on a sole-tenant node.

* `provisioning_model`(Optional, Beta) - Describe the type of preemptible VM. If this
is set, `preemptible` should be `true` and `auto_restart` should be
`false`. One of `STANDARD_PROVISION` or `SPOT`, for more info about
`SPOT`, read [here](https://cloud.google.com/compute/docs/instances/spot)

<a name="nested_guest_accelerator"></a>The `guest_accelerator` block supports:

* `type` (Required) - The accelerator type resource to expose to this instance. E.g. `nvidia-tesla-k80`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,12 @@ specified, then this instance will have no external IPv6 Internet access. Struct
groups will use as host systems. Read more on sole-tenant node creation
[here](https://cloud.google.com/compute/docs/nodes/create-nodes).
Structure [documented below](#nested_node_affinities).


* `provisioning_model`(Optional, Beta) - Describe the type of preemptible VM. If this
is set, `preemptible` should be `true` and `auto_restart` should be
`false`. One of `STANDARD_PROVISION` or `SPOT`, for more info about
`SPOT`, read [here](https://cloud.google.com/compute/docs/instances/spot)

<a name="nested_guest_accelerator"></a>The `guest_accelerator` block supports:

* `type` (Required) - The accelerator type resource to expose to this instance. E.g. `nvidia-tesla-k80`.
Expand Down
3 changes: 3 additions & 0 deletions mmv1/third_party/validator/compute_instance.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func expandComputeInstance(project string, d TerraformResourceData, config *Conf
AutomaticRestart: googleapi.Bool(d.Get(prefix + ".automatic_restart").(bool)),
Preemptible: d.Get(prefix + ".preemptible").(bool),
OnHostMaintenance: d.Get(prefix + ".on_host_maintenance").(string),
<% unless version == 'ga' -%>
ProvisioningModel: d.Get(prefix + ".provisioning_model").(string),
<% end -%>
ForceSendFields: []string{"AutomaticRestart", "Preemptible"},
}
}
Expand Down