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 advanced_machine_features to GCE Instance Templates #3337

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
3 changes: 3 additions & 0 deletions .changelog/4850.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added `advanced_machine_features` fields to `google_compute_instance_template`
```
23 changes: 23 additions & 0 deletions google-beta/compute_instance_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,29 @@ func flattenConfidentialInstanceConfig(ConfidentialInstanceConfig *computeBeta.C
}}
}

func expandAdvancedMachineFeatures(d TerraformResourceData) *computeBeta.AdvancedMachineFeatures {
if _, ok := d.GetOk("advanced_machine_features"); !ok {
return nil
}

prefix := "advanced_machine_features.0"
return &computeBeta.AdvancedMachineFeatures{
EnableNestedVirtualization: d.Get(prefix + ".enable_nested_virtualization").(bool),
ThreadsPerCore: int64(d.Get(prefix + ".threads_per_core").(int)),
// ForceSendFields: []string{"EnableSecureBoot"},
}
}

func flattenAdvancedMachineFeatures(AdvancedMachineFeatures *computeBeta.AdvancedMachineFeatures) []map[string]interface{} {
if AdvancedMachineFeatures == nil {
return nil
}
return []map[string]interface{}{{
"enable_nested_virtualization": AdvancedMachineFeatures.EnableNestedVirtualization,
"threads_per_core": AdvancedMachineFeatures.ThreadsPerCore,
}}
}

func flattenShieldedVmConfig(shieldedVmConfig *computeBeta.ShieldedInstanceConfig) []map[string]bool {
if shieldedVmConfig == nil {
return nil
Expand Down
33 changes: 33 additions & 0 deletions google-beta/resource_compute_instance_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,38 @@ func resourceComputeInstanceTemplate() *schema.Resource {
"enable_confidential_compute": {
Type: schema.TypeBool,
Required: true,
ForceNew: true,
Description: `Defines whether the instance should have confidential compute enabled.`,
},
},
},
},
"advanced_machine_features": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
ForceNew: true,
Description: `Controls for advanced machine-related behavior features.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enable_nested_virtualization": {
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
Description: `Whether to enable nested virtualization or not.`,
},
"threads_per_core": {
Type: schema.TypeInt,
Optional: true,
Computed: false,
ForceNew: true,
Description: `The number of threads per physical core. To disable simultaneous multithreading (SMT) set this to 1. If unset, the maximum number of threads supported per core by the underlying processor is assumed.`,
},
},
},
},
"guest_accelerator": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -935,6 +962,7 @@ func resourceComputeInstanceTemplateCreate(d *schema.ResourceData, meta interfac
Tags: resourceInstanceTags(d),
ConfidentialInstanceConfig: expandConfidentialInstanceConfig(d),
ShieldedInstanceConfig: expandShieldedVmConfigs(d),
AdvancedMachineFeatures: expandAdvancedMachineFeatures(d),
DisplayDevice: expandDisplayDevice(d),
ReservationAffinity: reservationAffinity,
}
Expand Down Expand Up @@ -1326,6 +1354,11 @@ func resourceComputeInstanceTemplateRead(d *schema.ResourceData, meta interface{
return fmt.Errorf("Error setting confidential_instance_config: %s", err)
}
}
if instanceTemplate.Properties.AdvancedMachineFeatures != nil {
if err = d.Set("advanced_machine_features", flattenAdvancedMachineFeatures(instanceTemplate.Properties.AdvancedMachineFeatures)); err != nil {
return fmt.Errorf("Error setting advanced_machine_features: %s", err)
}
}
if instanceTemplate.Properties.DisplayDevice != nil {
if err = d.Set("enable_display", flattenEnableDisplay(instanceTemplate.Properties.DisplayDevice)); err != nil {
return fmt.Errorf("Error setting enable_display: %s", err)
Expand Down
54 changes: 54 additions & 0 deletions google-beta/resource_compute_instance_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,26 @@ func TestAccComputeInstanceTemplate_ConfidentialInstanceConfigMain(t *testing.T)
})
}

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

var instanceTemplate computeBeta.InstanceTemplate

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceTemplateDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeInstanceTemplateAdvancedMachineFeatures(randString(t, 10)),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceTemplateExists(t, "google_compute_instance_template.foobar", &instanceTemplate),
),
},
},
})
}

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

Expand Down Expand Up @@ -2347,6 +2367,40 @@ resource "google_compute_instance_template" "foobar" {
`, suffix, enableConfidentialCompute)
}

func testAccComputeInstanceTemplateAdvancedMachineFeatures(suffix string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "ubuntu-2004-lts"
project = "ubuntu-os-cloud"
}

resource "google_compute_instance_template" "foobar" {
name = "tf-test-instance-template-%s"
machine_type = "n2-standard-2" // Nested Virt isn't supported on E2 and N2Ds https://cloud.google.com/compute/docs/instances/nested-virtualization/overview#restrictions and https://cloud.google.com/compute/docs/instances/disabling-smt#limitations

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

network_interface {
network = "default"
}

advanced_machine_features {
threads_per_core = 1
enable_nested_virtualization = true
}

scheduling {
on_host_maintenance = "TERMINATE"
}

}
`, suffix)
}

func testAccComputeInstanceTemplate_enableDisplay(suffix string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
Expand Down
2 changes: 1 addition & 1 deletion google-beta/resource_gke_hub_feature_membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"testing"

"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
gkehub "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/gkehub/beta"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/compute_instance_template.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ The following arguments are supported:

* `confidential_instance_config` (Optional) - Enable [Confidential Mode](https://cloud.google.com/compute/confidential-vm/docs/about-cvm) on this VM.

* `advanced_machine_features` (Optional) - Configure Nested Virtualisation and Simultaneous Hyper Threading on this VM.

The `disk` block supports:

* `auto_delete` - (Optional) Whether or not the disk should be auto-deleted.
Expand Down Expand Up @@ -445,6 +447,12 @@ The `confidential_instance_config` block supports:

* `enable_confidential_compute` (Optional) Defines whether the instance should have confidential compute enabled. [`on_host_maintenance`](#on_host_maintenance) has to be set to TERMINATE or this will fail to create the VM.

The `advanced_machine_features` block supports:

* `enable_nested_virtualization` (Optional) Defines whether the instance should have [nested virtualization](#on_host_maintenance) enabled. Defaults to false.

* `threads_per_core` (Optional) he number of threads per physical core. To disable [simultaneous multithreading (SMT)](https://cloud.google.com/compute/docs/instances/disabling-smt) set this to 1.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are
Expand Down