Skip to content

Commit 40c012e

Browse files
authored
Merge pull request #439 from sap-contributions/parameterize-deletion-protection
parameterize deletion protection for SQL instance, credhub secret and…
2 parents bfc2d07 + af91913 commit 40c012e

File tree

9 files changed

+53
-11
lines changed

9 files changed

+53
-11
lines changed

docs/concourse/region_change.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ For cost saving reasons, you can migrate the Concourse deployment to a different
3636
:warning: The file `credhub_backup.json` contains sensitive data in plaintext, so handle it with care and delete it after the migration.
3737

3838
## Destroy the Current Concourse Deployment
39-
1. Open file `terraform-modules/concourse/dr_create/credhub_encryption_key.tf`.
40-
1.1 In resource "google_secret_manager_secret_version", comment the "lifecycle" block (to disable `prevent_destroy = true`).
41-
1.1 Comment module "assertion_encryption_key_identical" (if you receive `Error: Unsupported OpenTofu Core version`).
42-
1. In `terraform-modules/concourse/infra/database.tf`, set `deletion_protection` and `deletion_protection_enabled` to `false`.
43-
1. In `terraform-modules/concourse/infra/gke_cluster.tf` add `deletion_protection = false` (the default is `true`).
39+
1. Credhub secret deletion prevention
40+
41+
1.1 In `config.yaml` set `credhub_secret_prevent_destroy` to `false` (the default is `true`).
42+
43+
1.1 Open file `terraform-modules/concourse/dr_create/credhub_encryption_key.tf` Comment module "assertion_encryption_key_identical" (if you receive `Error: Unsupported OpenTofu Core version`).
44+
1. In `config.yaml`, set `db_terraform_deletion_protection` and `db_engine_level_deletion_protection` to `false` (the default is `true`).
45+
1. In `config.yaml` set `gke_deletion_protection` to `false` (the default is `true`).
4446
1. Go to folder `terragrunt/concourse-wg-ci[-test]/infra` and run `terragrunt apply`. This updates the deletion protection settings for the Cloud SQL database and the GKE cluster.
4547
1. Go to folder `terragrunt/concourse-wg-ci[-test]`. Run `terragrunt run-all plan -destroy` to see what will be destroyed.
4648
1. If there were no errors, run `terragrunt run-all destroy` to destroy the Concourse deployment in the current region.
@@ -61,10 +63,10 @@ For cost saving reasons, you can migrate the Concourse deployment to a different
6163
gke_controlplane_version: "1.31"
6264
```
6365
1. Revert the changes in the Terraform files:
64-
- In `terraform-modules/concourse/dr_create/credhub_encryption_key.tf`, uncomment the "lifecycle" block.
66+
- In `config.yaml` set `credhub_secret_prevent_destroy` to `true`.
6567
- Uncomment module "assertion_encryption_key_identical" (if you commented it before).
66-
- In `terraform-modules/concourse/infra/database.tf`, set `deletion_protection` and `deletion_protection_enabled` to `true`.
67-
- In `terraform-modules/concourse/infra/gke_cluster.tf`, remove `deletion_protection = false`.
68+
- In `config.yaml`, set `db_terraform_deletion_protection` and `db_engine_level_deletion_protection` to `true`.
69+
- In `config.yaml`, set `gke_deletion_protection` to `true`.
6870
1. Now you can check the Terraform plan:
6971
```bash
7072
terragrunt run-all plan

terraform-modules/concourse/dr_create/credhub_encryption_key.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ resource "google_secret_manager_secret_version" "credhub_encryption_key" {
3131
secret = google_secret_manager_secret.credhub_encryption_key.id
3232
secret_data = base64decode(data.kubernetes_secret_v1.credhub_encryption_key.binary_data.password)
3333
lifecycle {
34-
prevent_destroy = true
34+
prevent_destroy = var.credhub_secret_prevent_destroy
3535

3636
# If omitted or unset terraform destroys previous versions which will make it impossible to
3737
# restore them. This is relevant in case of a desaster recovery where the

terraform-modules/concourse/dr_create/variables.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@ variable "region" { nullable = false }
33
variable "zone" { nullable = false }
44

55
variable "gke_name" { nullable = false }
6+
7+
variable "credhub_secret_prevent_destroy" {
8+
description = "Prevent deletion of credhub encryption key secret version"
9+
type = bool
10+
default = true
11+
nullable = false
12+
}

terraform-modules/concourse/infra/database.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ resource "google_sql_database_instance" "concourse" {
55
region = var.region
66

77
# This option prevents Terraform from deleting an instance
8-
deletion_protection = true
8+
deletion_protection = var.db_terraform_deletion_protection
99

1010
settings {
1111
activation_policy = "ALWAYS"
@@ -26,7 +26,7 @@ resource "google_sql_database_instance" "concourse" {
2626
transaction_log_retention_days = "7"
2727
}
2828

29-
deletion_protection_enabled = "true"
29+
deletion_protection_enabled = var.db_engine_level_deletion_protection
3030

3131
disk_autoresize = "true"
3232
disk_autoresize_limit = "0"

terraform-modules/concourse/infra/gke_cluster.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,6 @@ resource "google_container_cluster" "wg_ci" {
104104
enabled = "true"
105105
}
106106

107+
deletion_protection = var.gke_deletion_protection
108+
107109
}

terraform-modules/concourse/infra/variables.tf

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@ variable "database_version" {
2020
nullable = false
2121
default = "POSTGRES_13"
2222
}
23+
24+
variable "db_terraform_deletion_protection" {
25+
description = "Enable deletion protection for the Cloud SQL instance via Terraform/GCP API"
26+
type = bool
27+
default = true
28+
nullable = false
29+
}
30+
31+
variable "db_engine_level_deletion_protection" {
32+
description = "Enable engine-level deletion protection for Cloud SQL"
33+
type = bool
34+
default = true
35+
nullable = false
36+
}
37+
38+
variable "gke_deletion_protection" {
39+
description = "Enable deletion protection for the GKE cluster"
40+
type = bool
41+
default = true
42+
nullable = false
43+
}
44+
2345
variable "sql_instance_name" { nullable = false }
2446
variable "sql_instance_secondary_zone" { nullable = false }
2547
variable "sql_instance_backup_location" { nullable = false }

terragrunt/concourse-wg-ci/config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ tf_modules:
4747
# ---------------------------------------------------------
4848
# SQL
4949
database_version: "POSTGRES_16"
50+
db_terraform_deletion_protection: true
51+
db_engine_level_deletion_protection: true
5052
sql_instance_tier: db-custom-1-4096
5153
sql_instance_backup_location: eu
5254
sql_instance_disk_size: 38
5355

5456
# Other GKE vars
57+
gke_deletion_protection : true
5558
gke_controlplane_version: "1.31"
5659
gke_cluster_ipv4_cidr: 10.104.0.0/14
5760
gke_services_ipv4_cidr_block: 10.108.0.0/20
@@ -77,6 +80,8 @@ gke_cloud_nat_min_ports_per_vm: 16384
7780
# provisioning of loadbalancers
7881
gke_http_load_balancing_disabled: false
7982

83+
credhub_secret_prevent_destroy: true
84+
8085
# IAM
8186
wg_ci_human_account_permissions: [
8287
"container.clusterRoles.bind",

terragrunt/concourse-wg-ci/dr_create/terragrunt.hcl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ inputs = {
3535
zone = local.config.zone
3636

3737
gke_name = local.config.gke_name
38+
credhub_secret_prevent_destroy = local.config.credhub_secret_prevent_destroy
3839
}

terragrunt/concourse-wg-ci/infra/terragrunt.hcl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ inputs = {
3030
zone = local.config.zone
3131

3232
gke_name = local.config.gke_name
33+
gke_deletion_protection = local.config.gke_deletion_protection
3334
gke_controlplane_version = local.config.gke_controlplane_version
3435
gke_cluster_ipv4_cidr = local.config.gke_cluster_ipv4_cidr
3536
gke_services_ipv4_cidr_block = local.config.gke_services_ipv4_cidr_block
@@ -51,6 +52,8 @@ inputs = {
5152
gke_http_load_balancing_disabled = local.config.gke_http_load_balancing_disabled
5253

5354
database_version = local.config.database_version
55+
db_terraform_deletion_protection = local.config.db_terraform_deletion_protection
56+
db_engine_level_deletion_protection = local.config.db_engine_level_deletion_protection
5457
sql_instance_name = "${local.config.gke_name}-concourse"
5558
sql_instance_tier = local.config.sql_instance_tier
5659
sql_instance_disk_size = local.config.sql_instance_disk_size

0 commit comments

Comments
 (0)