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

feat(TPG>=5.36)!: add confidential_instance_type to instance_template module #416

Merged
Show file tree
Hide file tree
Changes from 4 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
32 changes: 32 additions & 0 deletions examples/instance_template/confidential_computing/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

module "instance_template" {
source = "../../../modules/instance_template"

region = var.region
project_id = var.project_id
service_account = var.service_account
subnetwork = var.subnetwork

name_prefix = "confidential-template"
source_image_project = "ubuntu-os-cloud"
source_image = "ubuntu-2004-lts"
machine_type = "n2d-standard-2"
min_cpu_platform = "AMD Milan"
enable_confidential_vm = true
confidential_instance_type = "SEV_SNP"
arthurlapertosa marked this conversation as resolved.
Show resolved Hide resolved
}
26 changes: 26 additions & 0 deletions examples/instance_template/confidential_computing/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


output "self_link" {
description = "Self-link to the instance template."
value = module.instance_template.self_link
}

output "name" {
description = "Name of the instance templates."
value = module.instance_template.name
}
40 changes: 40 additions & 0 deletions examples/instance_template/confidential_computing/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

variable "project_id" {
description = "The Google Cloud project ID."
type = string
}

variable "region" {
description = "The GCP region to create and test resources in."
type = string
default = "us-central1"
}

variable "subnetwork" {
description = "The subnetwork selflink to host the compute instances in."
type = string
}

variable "service_account" {
description = "Service account to attach to the instance. See https://www.terraform.io/docs/providers/google/r/compute_instance_template#service_account."
type = object({
email = string,
scopes = set(string)
})
default = null
}
2 changes: 2 additions & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ spec:
location: examples/instance_template/alias_ip_range
- name: autoscaler
location: examples/mig/autoscaler
- name: confidential_computing
location: examples/instance_template/confidential_computing
- name: disk_snapshot
location: examples/compute_instance/disk_snapshot
- name: encrypted_disks
Expand Down
2 changes: 2 additions & 0 deletions modules/compute_disk_snapshot/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ spec:
location: examples/instance_template/alias_ip_range
- name: autoscaler
location: examples/mig/autoscaler
- name: confidential_computing
location: examples/instance_template/confidential_computing
- name: disk_snapshot
location: examples/compute_instance/disk_snapshot
- name: encrypted_disks
Expand Down
2 changes: 2 additions & 0 deletions modules/compute_instance/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ spec:
location: examples/instance_template/alias_ip_range
- name: autoscaler
location: examples/mig/autoscaler
- name: confidential_computing
location: examples/instance_template/confidential_computing
- name: disk_snapshot
location: examples/compute_instance/disk_snapshot
- name: encrypted_disks
Expand Down
1 change: 1 addition & 0 deletions modules/instance_template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ See the [simple](../../examples/instance_template/simple) for a usage example.
| auto\_delete | Whether or not the boot disk should be auto-deleted | `string` | `"true"` | no |
| automatic\_restart | (Optional) Specifies whether the instance should be automatically restarted if it is terminated by Compute Engine (not terminated by a user). | `bool` | `true` | no |
| can\_ip\_forward | Enable IP forwarding, for NAT instances for example | `string` | `"false"` | no |
| confidential\_instance\_type | Defines the confidential computing technology the instance uses. If this is set to "SEV\_SNP", var.min\_cpu\_platform will be automatically set to "AMD Milan". | `string` | `null` | no |
| description | The template's description | `string` | `""` | no |
| disk\_encryption\_key | The id of the encryption key that is stored in Google Cloud KMS to use to encrypt all the disks on this instance | `string` | `null` | no |
| disk\_labels | Labels to be assigned to boot disk, provided as a map | `map(string)` | `{}` | no |
Expand Down
14 changes: 10 additions & 4 deletions modules/instance_template/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ locals {
# initialize the block only if it is enabled.
shielded_vm_configs = var.enable_shielded_vm ? [true] : []

gpu_enabled = var.gpu != null
alias_ip_range_enabled = var.alias_ip_range != null
gpu_enabled = var.gpu != null
alias_ip_range_enabled = var.alias_ip_range != null
snp_confidential_instance_type = var.confidential_instance_type == "SEV_SNP"
on_host_maintenance = (
var.preemptible || var.enable_confidential_vm || local.gpu_enabled || var.spot
var.preemptible || var.enable_confidential_vm || local.gpu_enabled || var.spot || local.snp_confidential_instance_type
arthurlapertosa marked this conversation as resolved.
Show resolved Hide resolved
? "TERMINATE"
: var.on_host_maintenance
)

# must be set to "AMD Milan" if confidential_instance_type is set to "SEV_SNP", or this will fail to create the VM.
min_cpu_platform = local.snp_confidential_instance_type ? "AMD Milan" : var.min_cpu_platform

automatic_restart = (
# must be false when preemptible or spot is true
var.preemptible || var.spot ? false : var.automatic_restart
Expand All @@ -76,7 +81,7 @@ resource "google_compute_instance_template" "tpl" {
can_ip_forward = var.can_ip_forward
metadata_startup_script = var.startup_script
region = var.region
min_cpu_platform = var.min_cpu_platform
min_cpu_platform = local.min_cpu_platform
resource_policies = var.resource_policies
dynamic "disk" {
for_each = local.all_disks
Expand Down Expand Up @@ -204,6 +209,7 @@ resource "google_compute_instance_template" "tpl" {

confidential_instance_config {
enable_confidential_compute = var.enable_confidential_vm
confidential_instance_type = var.confidential_instance_type
arthurlapertosa marked this conversation as resolved.
Show resolved Hide resolved
}

network_performance_config {
Expand Down
5 changes: 5 additions & 0 deletions modules/instance_template/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ spec:
location: examples/instance_template/alias_ip_range
- name: autoscaler
location: examples/mig/autoscaler
- name: confidential_computing
location: examples/instance_template/confidential_computing
- name: disk_snapshot
location: examples/compute_instance/disk_snapshot
- name: encrypted_disks
Expand Down Expand Up @@ -146,6 +148,9 @@ spec:
description: Enable IP forwarding, for NAT instances for example
varType: string
defaultValue: "false"
- name: confidential_instance_type
description: Defines the confidential computing technology the instance uses. If this is set to "SEV_SNP", var.min_cpu_platform will be automatically set to "AMD Milan".
arthurlapertosa marked this conversation as resolved.
Show resolved Hide resolved
varType: string
- name: description
description: The template's description
varType: string
Expand Down
6 changes: 6 additions & 0 deletions modules/instance_template/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,12 @@ variable "enable_confidential_vm" {
description = "Whether to enable the Confidential VM configuration on the instance. Note that the instance image must support Confidential VMs. See https://cloud.google.com/compute/docs/images"
}

variable "confidential_instance_type" {
type = string
default = null
description = "Defines the confidential computing technology the instance uses. If this is set to \"SEV_SNP\", var.min_cpu_platform will be automatically set to \"AMD Milan\"."
}
arthurlapertosa marked this conversation as resolved.
Show resolved Hide resolved

###########################
# Public IP
###########################
Expand Down
2 changes: 1 addition & 1 deletion modules/instance_template/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ terraform {
required_providers {
google-beta = {
source = "hashicorp/google-beta"
version = ">= 5.5, < 6"
version = ">= 5.36, < 6"
}
}
provider_meta "google" {
Expand Down
2 changes: 2 additions & 0 deletions modules/mig/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ spec:
location: examples/instance_template/alias_ip_range
- name: autoscaler
location: examples/mig/autoscaler
- name: confidential_computing
location: examples/instance_template/confidential_computing
- name: disk_snapshot
location: examples/compute_instance/disk_snapshot
- name: encrypted_disks
Expand Down
2 changes: 2 additions & 0 deletions modules/mig_with_percent/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ spec:
location: examples/instance_template/alias_ip_range
- name: autoscaler
location: examples/mig/autoscaler
- name: confidential_computing
location: examples/instance_template/confidential_computing
- name: disk_snapshot
location: examples/compute_instance/disk_snapshot
- name: encrypted_disks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ spec:
location: examples/instance_template/alias_ip_range
- name: autoscaler
location: examples/mig/autoscaler
- name: confidential_computing
location: examples/instance_template/confidential_computing
- name: disk_snapshot
location: examples/compute_instance/disk_snapshot
- name: encrypted_disks
Expand Down
2 changes: 2 additions & 0 deletions modules/umig/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ spec:
location: examples/instance_template/alias_ip_range
- name: autoscaler
location: examples/mig/autoscaler
- name: confidential_computing
location: examples/instance_template/confidential_computing
- name: disk_snapshot
location: examples/compute_instance/disk_snapshot
- name: encrypted_disks
Expand Down
23 changes: 23 additions & 0 deletions test/fixtures/confidential_instance_template/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

module "confidential_simple" {
source = "../../../examples/instance_template/confidential_computing"
project_id = var.project_id
region = "us-central1"
subnetwork = google_compute_subnetwork.main.self_link
service_account = var.service_account
}
1 change: 1 addition & 0 deletions test/fixtures/confidential_instance_template/network.tf
35 changes: 35 additions & 0 deletions test/fixtures/confidential_instance_template/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

output "self_link" {
description = "Self-link to the instance template."
value = module.confidential_simple.self_link
}

output "name" {
description = "Name of the instance templates."
value = module.confidential_simple.name
}

output "project_id" {
arthurlapertosa marked this conversation as resolved.
Show resolved Hide resolved
description = "The GCP project to use for integration tests."
value = var.project_id
}

output "service_account" {
description = "Service account to attach to the instance"
value = var.service_account
}
29 changes: 29 additions & 0 deletions test/fixtures/confidential_instance_template/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

variable "project_id" {
description = "The GCP project to use for integration tests."
type = string
}

variable "service_account" {
description = "Service account to attach to the instance. See https://www.terraform.io/docs/providers/google/r/compute_instance_template#service_account."
type = object({
email = string
scopes = list(string)
})
default = null
}
19 changes: 19 additions & 0 deletions test/fixtures/confidential_instance_template/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

terraform {
required_version = ">=0.13"
}
Loading