How to attach a scheduler policy to a persistent volume claim in Gcloud with terraform

Viewed 97

I created a webserver infrastructure with terraform (v0.12.21) in Gcloud to deploy a lot of websites.

I created a persistent volume claim for each deploy (1GB each): persistent volumes

I used this code to create them:

resource "kubernetes_persistent_volume_claim" "wordpress_volumeclaim" {
  for_each = var.wordpress_site

  metadata {
    name      = "wordpress-volumeclaim-${terraform.workspace}-${each.value.name}"
    namespace = "default"
  }
  spec {
    access_modes = ["ReadWriteOnce"]
    resources {
      requests = {
        storage = each.value.disk
        resource_policies = google_compute_resource_policy.policy.name
      }
    }
  }
}
resource "kubernetes_deployment" "wordpress" {
  for_each = var.wordpress_site

  metadata {
    name   = each.value.name
    labels = { app = each.value.name }
  }
  spec {
    replicas = 1
    selector {
      match_labels = { app = each.value.name }
    }
    template {
      metadata {
        labels = { app = each.value.name }
      }
      spec {
        
        volume {
          name = "wordpress-persistent-storage-${terraform.workspace}-${each.value.name}"
          persistent_volume_claim {
            claim_name = "wordpress-volumeclaim-${terraform.workspace}-${each.value.name}"
          }
        }
[...]

But now I need to backup all these disks, and my best idea is using the Gcloud snapshot functionallity, and it must be dynamic, as the creation of these disks are dynamic.

First of all, I created a Snapshot policy:

resource "google_compute_resource_policy" "policy" {
  name = "my-resource-policy"
  region = "zone-region-here"
  project = var.project
  snapshot_schedule_policy {
    schedule {
      daily_schedule {
        days_in_cycle = 1
        start_time = "04:00"
      }
    }
    retention_policy {
      max_retention_days    = 7
      on_source_disk_delete = "KEEP_AUTO_SNAPSHOTS"
    }
  }
}

And now I want to add it to my persistent volumen claim. But I dont know how, because this line is not working at all:

resource_policies = google_compute_resource_policy.policy.name

All my tries resulted in errors. Could you help me here?

0 Answers
Related