Skip to content

Commit b304907

Browse files
ludoowiktorn
authored andcommitted
Add support for advanced machine features to compute-vm (GoogleCloudPlatform#2855)
* add support for advanced machine features to compute-vm * fix validation * Update modules/compute-vm/variables.tf Co-authored-by: Wiktor Niesiobędzki <[email protected]> --------- Co-authored-by: Wiktor Niesiobędzki <[email protected]>
1 parent 0f765f0 commit b304907

File tree

3 files changed

+90
-12
lines changed

3 files changed

+90
-12
lines changed

modules/compute-vm/README.md

+36-10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ In both modes, an optional service account can be created and assigned to either
3131
- [Spot VM](#spot-vm)
3232
- [Confidential compute](#confidential-compute)
3333
- [Disk encryption with Cloud KMS](#disk-encryption-with-cloud-kms)
34+
- [Advanced machine features](#advanced-machine-features)
3435
- [Instance template](#instance-template)
3536
- [Instance group](#instance-group)
3637
- [Instance Schedule](#instance-schedule)
@@ -614,6 +615,31 @@ module "kms-vm-example" {
614615
# tftest inventory=cmek.yaml e2e
615616
```
616617

618+
### Advanced machine features
619+
620+
Advanced machine features can be configured via the `options.advanced_machine_features` variable.
621+
622+
```hcl
623+
module "simple-vm-example" {
624+
source = "./fabric/modules/compute-vm"
625+
project_id = var.project_id
626+
zone = "${var.region}-b"
627+
name = "test"
628+
network_interfaces = [{
629+
network = var.vpc.self_link
630+
subnetwork = var.subnet.self_link
631+
}]
632+
options = {
633+
advanced_machine_features = {
634+
enable_nested_virtualization = true
635+
enable_turbo_mode = true
636+
threads_per_core = 2
637+
}
638+
}
639+
}
640+
# tftest modules=1 resources=1
641+
```
642+
617643
### Instance template
618644

619645
This example shows how to use the module to manage an instance template that defines an additional attached disk for each instance, and overrides defaults for the boot disk image and service account.
@@ -886,8 +912,8 @@ module "sole-tenancy" {
886912
|---|---|:---:|:---:|:---:|
887913
| [name](variables.tf#L264) | Instance name. | <code>string</code> || |
888914
| [network_interfaces](variables.tf#L276) | Network interfaces configuration. Use self links for Shared VPC, set addresses to null if not needed. | <code title="list&#40;object&#40;&#123;&#10; network &#61; string&#10; subnetwork &#61; string&#10; alias_ips &#61; optional&#40;map&#40;string&#41;, &#123;&#125;&#41;&#10; nat &#61; optional&#40;bool, false&#41;&#10; nic_type &#61; optional&#40;string&#41;&#10; stack_type &#61; optional&#40;string&#41;&#10; addresses &#61; optional&#40;object&#40;&#123;&#10; internal &#61; optional&#40;string&#41;&#10; external &#61; optional&#40;string&#41;&#10; &#125;&#41;, null&#41;&#10;&#125;&#41;&#41;">list&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> || |
889-
| [project_id](variables.tf#L322) | Project id. | <code>string</code> || |
890-
| [zone](variables.tf#L420) | Compute zone. | <code>string</code> || |
915+
| [project_id](variables.tf#L345) | Project id. | <code>string</code> || |
916+
| [zone](variables.tf#L443) | Compute zone. | <code>string</code> || |
891917
| [attached_disk_defaults](variables.tf#L17) | Defaults for attached disks options. | <code title="object&#40;&#123;&#10; auto_delete &#61; optional&#40;bool, false&#41;&#10; mode &#61; string&#10; replica_zone &#61; string&#10; type &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; auto_delete &#61; true&#10; mode &#61; &#34;READ_WRITE&#34;&#10; replica_zone &#61; null&#10; type &#61; &#34;pd-balanced&#34;&#10;&#125;">&#123;&#8230;&#125;</code> |
892918
| [attached_disks](variables.tf#L37) | Additional disks, if options is null defaults will be used in its place. Source type is one of 'image' (zonal disks in vms and template), 'snapshot' (vm), 'existing', and null. | <code title="list&#40;object&#40;&#123;&#10; name &#61; string&#10; device_name &#61; optional&#40;string&#41;&#10; size &#61; string&#10; snapshot_schedule &#61; optional&#40;list&#40;string&#41;&#41;&#10; source &#61; optional&#40;string&#41;&#10; source_type &#61; optional&#40;string&#41;&#10; options &#61; optional&#40;&#10; object&#40;&#123;&#10; auto_delete &#61; optional&#40;bool, false&#41;&#10; mode &#61; optional&#40;string, &#34;READ_WRITE&#34;&#41;&#10; replica_zone &#61; optional&#40;string&#41;&#10; type &#61; optional&#40;string, &#34;pd-balanced&#34;&#41;&#10; &#125;&#41;,&#10; &#123;&#10; auto_delete &#61; true&#10; mode &#61; &#34;READ_WRITE&#34;&#10; replica_zone &#61; null&#10; type &#61; &#34;pd-balanced&#34;&#10; &#125;&#10; &#41;&#10;&#125;&#41;&#41;">list&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#91;&#93;</code> |
893919
| [boot_disk](variables.tf#L83) | Boot disk properties. | <code title="object&#40;&#123;&#10; auto_delete &#61; optional&#40;bool, true&#41;&#10; snapshot_schedule &#61; optional&#40;list&#40;string&#41;&#41;&#10; source &#61; optional&#40;string&#41;&#10; initialize_params &#61; optional&#40;object&#40;&#123;&#10; image &#61; optional&#40;string, &#34;projects&#47;debian-cloud&#47;global&#47;images&#47;family&#47;debian-11&#34;&#41;&#10; size &#61; optional&#40;number, 10&#41;&#10; type &#61; optional&#40;string, &#34;pd-balanced&#34;&#41;&#10; &#125;&#41;&#41;&#10; use_independent_disk &#61; optional&#40;bool, false&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; initialize_params &#61; &#123;&#125;&#10;&#125;">&#123;&#8230;&#125;</code> |
@@ -907,14 +933,14 @@ module "sole-tenancy" {
907933
| [metadata](variables.tf#L252) | Instance metadata. | <code>map&#40;string&#41;</code> | | <code>&#123;&#125;</code> |
908934
| [min_cpu_platform](variables.tf#L258) | Minimum CPU platform. | <code>string</code> | | <code>null</code> |
909935
| [network_attached_interfaces](variables.tf#L269) | Network interfaces using network attachments. | <code>list&#40;string&#41;</code> | | <code>&#91;&#93;</code> |
910-
| [options](variables.tf#L292) | Instance options. | <code title="object&#40;&#123;&#10; allow_stopping_for_update &#61; optional&#40;bool, true&#41;&#10; deletion_protection &#61; optional&#40;bool, false&#41;&#10; max_run_duration &#61; optional&#40;object&#40;&#123;&#10; nanos &#61; optional&#40;number&#41;&#10; seconds &#61; number&#10; &#125;&#41;&#41;&#10; node_affinities &#61; optional&#40;map&#40;object&#40;&#123;&#10; values &#61; list&#40;string&#41;&#10; in &#61; optional&#40;bool, true&#41;&#10; &#125;&#41;&#41;, &#123;&#125;&#41;&#10; spot &#61; optional&#40;bool, false&#41;&#10; termination_action &#61; optional&#40;string&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; allow_stopping_for_update &#61; true&#10; deletion_protection &#61; false&#10; spot &#61; false&#10; termination_action &#61; null&#10;&#125;">&#123;&#8230;&#125;</code> |
911-
| [scratch_disks](variables.tf#L327) | Scratch disks configuration. | <code title="object&#40;&#123;&#10; count &#61; number&#10; interface &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; count &#61; 0&#10; interface &#61; &#34;NVME&#34;&#10;&#125;">&#123;&#8230;&#125;</code> |
912-
| [service_account](variables.tf#L339) | Service account email and scopes. If email is null, the default Compute service account will be used unless auto_create is true, in which case a service account will be created. Set the variable to null to avoid attaching a service account. | <code title="object&#40;&#123;&#10; auto_create &#61; optional&#40;bool, false&#41;&#10; email &#61; optional&#40;string&#41;&#10; scopes &#61; optional&#40;list&#40;string&#41;&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>&#123;&#125;</code> |
913-
| [shielded_config](variables.tf#L349) | Shielded VM configuration of the instances. | <code title="object&#40;&#123;&#10; enable_secure_boot &#61; bool&#10; enable_vtpm &#61; bool&#10; enable_integrity_monitoring &#61; bool&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
914-
| [snapshot_schedules](variables.tf#L359) | Snapshot schedule resource policies that can be attached to disks. | <code title="map&#40;object&#40;&#123;&#10; schedule &#61; object&#40;&#123;&#10; daily &#61; optional&#40;object&#40;&#123;&#10; days_in_cycle &#61; number&#10; start_time &#61; string&#10; &#125;&#41;&#41;&#10; hourly &#61; optional&#40;object&#40;&#123;&#10; hours_in_cycle &#61; number&#10; start_time &#61; string&#10; &#125;&#41;&#41;&#10; weekly &#61; optional&#40;list&#40;object&#40;&#123;&#10; day &#61; string&#10; start_time &#61; string&#10; &#125;&#41;&#41;&#41;&#10; &#125;&#41;&#10; description &#61; optional&#40;string&#41;&#10; retention_policy &#61; optional&#40;object&#40;&#123;&#10; max_retention_days &#61; number&#10; on_source_disk_delete_keep &#61; optional&#40;bool&#41;&#10; &#125;&#41;&#41;&#10; snapshot_properties &#61; optional&#40;object&#40;&#123;&#10; chain_name &#61; optional&#40;string&#41;&#10; guest_flush &#61; optional&#40;bool&#41;&#10; labels &#61; optional&#40;map&#40;string&#41;&#41;&#10; storage_locations &#61; optional&#40;list&#40;string&#41;&#41;&#10; &#125;&#41;&#41;&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
915-
| [tag_bindings](variables.tf#L402) | Resource manager tag bindings for this instance, in tag key => tag value format. | <code>map&#40;string&#41;</code> | | <code>null</code> |
916-
| [tag_bindings_firewall](variables.tf#L408) | Firewall (network scoped) tag bindings for this instance, in tag key => tag value format. | <code>map&#40;string&#41;</code> | | <code>null</code> |
917-
| [tags](variables.tf#L414) | Instance network tags for firewall rule targets. | <code>list&#40;string&#41;</code> | | <code>&#91;&#93;</code> |
936+
| [options](variables.tf#L292) | Instance options. | <code title="object&#40;&#123;&#10; advanced_machine_features &#61; optional&#40;object&#40;&#123;&#10; enable_nested_virtualization &#61; optional&#40;bool&#41;&#10; enable_turbo_mode &#61; optional&#40;bool&#41;&#10; enable_uefi_networking &#61; optional&#40;bool&#41;&#10; performance_monitoring_unit &#61; optional&#40;string&#41;&#10; threads_per_core &#61; optional&#40;number&#41;&#10; visible_core_count &#61; optional&#40;number&#41;&#10; &#125;&#41;&#41;&#10; allow_stopping_for_update &#61; optional&#40;bool, true&#41;&#10; deletion_protection &#61; optional&#40;bool, false&#41;&#10; max_run_duration &#61; optional&#40;object&#40;&#123;&#10; nanos &#61; optional&#40;number&#41;&#10; seconds &#61; number&#10; &#125;&#41;&#41;&#10; node_affinities &#61; optional&#40;map&#40;object&#40;&#123;&#10; values &#61; list&#40;string&#41;&#10; in &#61; optional&#40;bool, true&#41;&#10; &#125;&#41;&#41;, &#123;&#125;&#41;&#10; spot &#61; optional&#40;bool, false&#41;&#10; termination_action &#61; optional&#40;string&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; allow_stopping_for_update &#61; true&#10; deletion_protection &#61; false&#10; spot &#61; false&#10; termination_action &#61; null&#10;&#125;">&#123;&#8230;&#125;</code> |
937+
| [scratch_disks](variables.tf#L350) | Scratch disks configuration. | <code title="object&#40;&#123;&#10; count &#61; number&#10; interface &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; count &#61; 0&#10; interface &#61; &#34;NVME&#34;&#10;&#125;">&#123;&#8230;&#125;</code> |
938+
| [service_account](variables.tf#L362) | Service account email and scopes. If email is null, the default Compute service account will be used unless auto_create is true, in which case a service account will be created. Set the variable to null to avoid attaching a service account. | <code title="object&#40;&#123;&#10; auto_create &#61; optional&#40;bool, false&#41;&#10; email &#61; optional&#40;string&#41;&#10; scopes &#61; optional&#40;list&#40;string&#41;&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>&#123;&#125;</code> |
939+
| [shielded_config](variables.tf#L372) | Shielded VM configuration of the instances. | <code title="object&#40;&#123;&#10; enable_secure_boot &#61; bool&#10; enable_vtpm &#61; bool&#10; enable_integrity_monitoring &#61; bool&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
940+
| [snapshot_schedules](variables.tf#L382) | Snapshot schedule resource policies that can be attached to disks. | <code title="map&#40;object&#40;&#123;&#10; schedule &#61; object&#40;&#123;&#10; daily &#61; optional&#40;object&#40;&#123;&#10; days_in_cycle &#61; number&#10; start_time &#61; string&#10; &#125;&#41;&#41;&#10; hourly &#61; optional&#40;object&#40;&#123;&#10; hours_in_cycle &#61; number&#10; start_time &#61; string&#10; &#125;&#41;&#41;&#10; weekly &#61; optional&#40;list&#40;object&#40;&#123;&#10; day &#61; string&#10; start_time &#61; string&#10; &#125;&#41;&#41;&#41;&#10; &#125;&#41;&#10; description &#61; optional&#40;string&#41;&#10; retention_policy &#61; optional&#40;object&#40;&#123;&#10; max_retention_days &#61; number&#10; on_source_disk_delete_keep &#61; optional&#40;bool&#41;&#10; &#125;&#41;&#41;&#10; snapshot_properties &#61; optional&#40;object&#40;&#123;&#10; chain_name &#61; optional&#40;string&#41;&#10; guest_flush &#61; optional&#40;bool&#41;&#10; labels &#61; optional&#40;map&#40;string&#41;&#41;&#10; storage_locations &#61; optional&#40;list&#40;string&#41;&#41;&#10; &#125;&#41;&#41;&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
941+
| [tag_bindings](variables.tf#L425) | Resource manager tag bindings for this instance, in tag key => tag value format. | <code>map&#40;string&#41;</code> | | <code>null</code> |
942+
| [tag_bindings_firewall](variables.tf#L431) | Firewall (network scoped) tag bindings for this instance, in tag key => tag value format. | <code>map&#40;string&#41;</code> | | <code>null</code> |
943+
| [tags](variables.tf#L437) | Instance network tags for firewall rule targets. | <code>list&#40;string&#41;</code> | | <code>&#91;&#93;</code> |
918944

919945
## Outputs
920946

modules/compute-vm/main.tf

+29
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
locals {
18+
advanced_mf = var.options.advanced_machine_features
1819
attached_disks = {
1920
for disk in var.attached_disks :
2021
(disk.name != null ? disk.name : disk.device_name) => merge(disk, {
@@ -165,6 +166,20 @@ resource "google_compute_instance" "default" {
165166
metadata = var.metadata
166167
resource_policies = local.ischedule_attach
167168

169+
dynamic "advanced_machine_features" {
170+
for_each = local.advanced_mf != null ? [""] : []
171+
content {
172+
enable_nested_virtualization = local.advanced_mf.enable_nested_virtualization
173+
enable_uefi_networking = local.advanced_mf.enable_uefi_networking
174+
performance_monitoring_unit = local.advanced_mf.performance_monitoring_unit
175+
threads_per_core = local.advanced_mf.threads_per_core
176+
turbo_mode = (
177+
local.advanced_mf.enable_turbo_mode ? "ALL_CORE_MAX" : null
178+
)
179+
visible_core_count = local.advanced_mf.visible_core_count
180+
}
181+
}
182+
168183
dynamic "attached_disk" {
169184
for_each = local.attached_disks_zonal
170185
iterator = config
@@ -369,6 +384,20 @@ resource "google_compute_instance_template" "default" {
369384
labels = var.labels
370385
resource_manager_tags = local.tags_combined
371386

387+
dynamic "advanced_machine_features" {
388+
for_each = local.advanced_mf != null ? [""] : []
389+
content {
390+
enable_nested_virtualization = local.advanced_mf.enable_nested_virtualization
391+
enable_uefi_networking = local.advanced_mf.enable_uefi_networking
392+
performance_monitoring_unit = local.advanced_mf.performance_monitoring_unit
393+
threads_per_core = local.advanced_mf.threads_per_core
394+
turbo_mode = (
395+
local.advanced_mf.enable_turbo_mode ? "ALL_CORE_MAX" : null
396+
)
397+
visible_core_count = local.advanced_mf.visible_core_count
398+
}
399+
}
400+
372401
disk {
373402
auto_delete = var.boot_disk.auto_delete
374403
boot = true

modules/compute-vm/variables.tf

+25-2
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,14 @@ variable "network_interfaces" {
292292
variable "options" {
293293
description = "Instance options."
294294
type = object({
295+
advanced_machine_features = optional(object({
296+
enable_nested_virtualization = optional(bool)
297+
enable_turbo_mode = optional(bool)
298+
enable_uefi_networking = optional(bool)
299+
performance_monitoring_unit = optional(string)
300+
threads_per_core = optional(number)
301+
visible_core_count = optional(number)
302+
}))
295303
allow_stopping_for_update = optional(bool, true)
296304
deletion_protection = optional(bool, false)
297305
max_run_duration = optional(object({
@@ -312,11 +320,26 @@ variable "options" {
312320
termination_action = null
313321
}
314322
validation {
315-
condition = (var.options.termination_action == null
323+
condition = (
324+
var.options.termination_action == null
316325
||
317-
contains(["STOP", "DELETE"], coalesce(var.options.termination_action, "1")))
326+
contains(["STOP", "DELETE"], coalesce(var.options.termination_action, "1"))
327+
)
318328
error_message = "Allowed values for options.termination_action are 'STOP', 'DELETE' and null."
319329
}
330+
validation {
331+
condition = (
332+
try(var.options.advanced_machine_features.performance_monitoring_unit, null) == null
333+
||
334+
contains(["ARCHITECTURAL", "ENHANCED", "STANDARD"], coalesce(
335+
try(
336+
var.options.advanced_machine_features.performance_monitoring_unit, null
337+
), "-"
338+
)
339+
)
340+
)
341+
error_message = "Allowed values for options.advanced_machine_features.performance_monitoring_unit are ARCHITECTURAL', 'ENHANCED', 'STANDARD' and null."
342+
}
320343
}
321344

322345
variable "project_id" {

0 commit comments

Comments
 (0)