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 option to attach multiple snapshot schedule to disks #2639

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 4 additions & 4 deletions modules/compute-vm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -735,13 +735,13 @@ module "instance" {
}]
boot_disk = {
image = "projects/cos-cloud/global/images/family/cos-stable"
snapshot_schedule = "boot"
snapshot_schedule = ["boot"]
}
attached_disks = [
{
name = "disk-1"
size = 10
snapshot_schedule = "generic-vm"
snapshot_schedule = ["generic-vm"]
}
]
snapshot_schedules = {
Expand Down Expand Up @@ -855,8 +855,8 @@ module "sole-tenancy" {
| [project_id](variables.tf#L293) | Project id. | <code>string</code> | ✓ | |
| [zone](variables.tf#L391) | Compute zone. | <code>string</code> | ✓ | |
| [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> |
| [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;string&#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> |
| [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;string&#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> |
| [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> |
| [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> |
| [can_ip_forward](variables.tf#L117) | Enable IP forwarding. | <code>bool</code> | | <code>false</code> |
| [confidential_compute](variables.tf#L123) | Enable Confidential Compute for these instances. | <code>bool</code> | | <code>false</code> |
| [create_template](variables.tf#L129) | Create instance template instead of instances. | <code>bool</code> | | <code>false</code> |
Expand Down
52 changes: 39 additions & 13 deletions modules/compute-vm/resource-policies.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ locals {
# externally managed policy
: [var.instance_schedule.resource_policy_id]
)

disk_zonal_schedule_attachments = flatten([
for disk_key, disk_data in try(local.attached_disks_zonal, []) :
disk_data.snapshot_schedule != null ? [
for schedule in disk_data.snapshot_schedule : {
disk_key = disk_key
source_type = disk_data.source_type
source = disk_data.source
snapshot_schedule = schedule
}
] : []
])
disk_regional_schedule_attachments = flatten([
for disk_key, disk_data in try(local.attached_disks_regional, []) :
disk_data.snapshot_schedule != null ? [
for schedule in disk_data.snapshot_schedule : {
disk_key = disk_key
source_type = disk_data.source_type
source = disk_data.source
snapshot_schedule = schedule
}
] : []
])
}

resource "google_compute_resource_policy" "schedule" {
Expand Down Expand Up @@ -118,22 +141,25 @@ resource "google_compute_resource_policy" "snapshot" {
}

resource "google_compute_disk_resource_policy_attachment" "boot" {
count = var.boot_disk.snapshot_schedule != null ? 1 : 0
project = var.project_id
zone = var.zone
for_each = var.boot_disk.snapshot_schedule != null ? {
for i, v in var.boot_disk.snapshot_schedule : i => v
ludoo marked this conversation as resolved.
Show resolved Hide resolved
} : {}
project = var.project_id
zone = var.zone
name = try(
google_compute_resource_policy.snapshot[var.boot_disk.snapshot_schedule].name,
var.boot_disk.snapshot_schedule
google_compute_resource_policy.snapshot[each.value].name,
each.value
)
disk = var.name
depends_on = [google_compute_instance.default]
}

resource "google_compute_disk_resource_policy_attachment" "attached" {
for_each = {
for k, v in local.attached_disks_zonal :
k => v if v.snapshot_schedule != null
for attachment in local.disk_zonal_schedule_attachments :
"${attachment.disk_key}-${attachment.snapshot_schedule}" => attachment
}

project = var.project_id
zone = var.zone
name = try(
Expand All @@ -143,7 +169,7 @@ resource "google_compute_disk_resource_policy_attachment" "attached" {
disk = (
each.value.source_type == "attach"
? each.value.source
: google_compute_disk.disks[each.key].name
: google_compute_disk.disks[each.value.disk_key].name
)
depends_on = [
google_compute_instance.default,
Expand All @@ -153,22 +179,22 @@ resource "google_compute_disk_resource_policy_attachment" "attached" {

resource "google_compute_region_disk_resource_policy_attachment" "attached" {
for_each = {
for k, v in local.attached_disks_regional :
k => v if v.snapshot_schedule != null
for attachment in local.disk_regional_schedule_attachments :
"${attachment.disk_key}-${attachment.snapshot_schedule}" => attachment
}

project = var.project_id
region = substr(var.zone, 0, length(var.zone) - 2)
name = try(
google_compute_resource_policy.snapshot[each.value.snapshot_schedule].name,
each.value.snapshot_schedule
)
disk = (
each.value.source_type == "attach"
? each.value.source
: google_compute_region_disk.disks[each.key].name
: google_compute_disk.disks[each.value.disk_key].name
)
depends_on = [
google_compute_instance.default,
google_compute_region_disk.disks
google_compute_disk.disks
]
}
4 changes: 2 additions & 2 deletions modules/compute-vm/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ variable "attached_disks" {
device_name = optional(string)
# TODO: size can be null when source_type is attach
size = string
snapshot_schedule = optional(string)
snapshot_schedule = optional(list(string))
source = optional(string)
source_type = optional(string)
options = optional(
Expand Down Expand Up @@ -84,7 +84,7 @@ variable "boot_disk" {
description = "Boot disk properties."
type = object({
auto_delete = optional(bool, true)
snapshot_schedule = optional(string)
snapshot_schedule = optional(list(string))
source = optional(string)
initialize_params = optional(object({
image = optional(string, "projects/debian-cloud/global/images/family/debian-11")
Expand Down
Loading