From 1fb7e114fd6f405fbc0c4851927506c3383133e2 Mon Sep 17 00:00:00 2001 From: Valerio Ponza Date: Wed, 29 Mar 2023 09:57:40 +0000 Subject: [PATCH] fixing tests and refactoring default --- modules/gke-cluster/README.md | 14 +++++----- modules/gke-cluster/main.tf | 4 +-- modules/gke-cluster/variables.tf | 5 ++-- .../modules/gke_cluster/examples/backup.yaml | 28 +++++++++++++------ 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/modules/gke-cluster/README.md b/modules/gke-cluster/README.md index 55ead077c..dc3b1ff49 100644 --- a/modules/gke-cluster/README.md +++ b/modules/gke-cluster/README.md @@ -126,30 +126,30 @@ module "cluster-1" { ### Backup for GKE -This example shows how to [enable the Backup for GKE agent and configure a Backup Plan](https://cloud.google.com/kubernetes-engine/docs/how-to/cloud-dns) for GKE Standard clusters. +This example shows how to [enable the Backup for GKE agent and configure a Backup Plan](https://cloud.google.com/kubernetes-engine/docs/add-on/backup-for-gke/concepts/backup-for-gke) for GKE Standard clusters. ```hcl module "cluster-1" { source = "./fabric/modules/gke-cluster" project_id = var.project_id name = "cluster-1" - location = "europe-west1" + location = "europe-west1-b" vpc_config = { network = var.vpc.self_link subnetwork = var.subnet.self_link secondary_range_names = { pods = "pods", services = "services" } } - backup_configs = { + backup_configs = { enable_backup_agent = true backup_plans = { "backup-1" = { - region = "europe-west-2" - schedule = "0 9 * * 1" - } + region = "europe-west-2" + schedule = "0 9 * * 1" + } } } } -# tftest modules=1 resources=1 inventory=backup.yaml +# tftest modules=1 resources=2 inventory=backup.yaml ``` diff --git a/modules/gke-cluster/main.tf b/modules/gke-cluster/main.tf index 3d3b51301..814ae26b7 100644 --- a/modules/gke-cluster/main.tf +++ b/modules/gke-cluster/main.tf @@ -119,7 +119,7 @@ resource "google_container_cluster" "cluster" { enabled = var.enable_addons.config_connector } gke_backup_agent_config { - enabled = try(var.backup_configs.enable_backup_agent, false) + enabled = var.backup_configs.enable_backup_agent } } @@ -387,7 +387,7 @@ resource "google_container_cluster" "cluster" { } resource "google_gke_backup_backup_plan" "backup_plan" { - for_each = try(var.backup_configs.enable_backup_agent, false) ? var.backup_configs.backup_plans : {} + for_each = var.backup_configs.enable_backup_agent ? var.backup_configs.backup_plans : {} name = each.key cluster = google_container_cluster.cluster.id location = each.value.region diff --git a/modules/gke-cluster/variables.tf b/modules/gke-cluster/variables.tf index da85c60ff..321e0516f 100644 --- a/modules/gke-cluster/variables.tf +++ b/modules/gke-cluster/variables.tf @@ -24,9 +24,10 @@ variable "backup_configs" { retention_policy_days = optional(string) retention_policy_lock = optional(bool, false) retention_policy_delete_lock_days = optional(string) - }))) + })), {}) }) - default = null + default = {} + nullable = false } variable "cluster_autoscaling" { diff --git a/tests/modules/gke_cluster/examples/backup.yaml b/tests/modules/gke_cluster/examples/backup.yaml index c9c63e122..b65b2a4f4 100644 --- a/tests/modules/gke_cluster/examples/backup.yaml +++ b/tests/modules/gke_cluster/examples/backup.yaml @@ -14,15 +14,25 @@ values: module.cluster-1.google_container_cluster.cluster: - backup_configs: - enable_backup_agent: true - backup_plans: - backup-1: - region: europe-west2 - schedule: 0 9 * * 1 - retention_policy_days: 30 - retention_policy_lock: false - retention_policy_delete_lock_days: 15 + location: europe-west1-b + name: cluster-1 + module.cluster-1.google_gke_backup_backup_plan.backup_plan["backup-1"]: + backup_config: + - all_namespaces: true + encryption_key: [] + include_secrets: true + include_volume_data: true + selected_applications: [] + selected_namespaces: [] + backup_schedule: + - cron_schedule: 0 9 * * 1 + location: europe-west-2 + name: backup-1 + project: project-id + retention_policy: + - locked: false + counts: google_container_cluster: 1 + google_gke_backup_backup_plan: 1