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: Support no read-replication for Memorystore [DEVOP-4801] #18

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .github/workflows/checkov.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: "Checkov GitHub Action"
permissions: read-all
on:
pull_request:
branches: [test, dev, qa, prod, main]
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/semantic-pr.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
name: "Semantic Pull Request"

permissions:
contents: write
pull-requests: write
on:
pull_request:
types:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/terraform.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: "terraform"
permissions: read-all
on:
pull_request:
branches:
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/terratest.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
name: terratest
permissions:
contents: write
pull-requests: write
on:
pull_request:
branches:
Expand Down
2 changes: 1 addition & 1 deletion examples/create_redis_public_ip/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

| Name | Version |
|------|---------|
| <a name="provider_random"></a> [random](#provider\_random) | 3.3.2 |
| <a name="provider_random"></a> [random](#provider\_random) | n/a |

## Modules

Expand Down
4 changes: 4 additions & 0 deletions examples/create_redis_public_ip/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ resource "random_id" "instance_suffix" {
}

module "private_network" {
#checkov:skip=CKV_TF_1:We use the version tag instead of the commit hash
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we skip examples for checkov?

#checkov:skip=CKV2_GCP_18:We ignore the creation of firewall rules
source = "git::https://github.com/honestbank/terraform-gcp-sql.git//modules/google_compute_network?ref=v1.1.1"
name = "test-redis-terraform-${random_id.instance_suffix.hex}"
}

module "google_compute_global_address_private_ip_address" {
#checkov:skip=CKV_TF_1:We use the version tag instead of the commit hash
source = "git::https://github.com/honestbank/terraform-gcp-sql.git//modules/google_compute_global_address?ref=v1.1.1"

name = "redis-pip-${random_id.instance_suffix.hex}"
Expand All @@ -28,6 +31,7 @@ module "google_compute_global_address_private_ip_address" {
}

module "google_service_networking_connection_private_vpc_connection" {
#checkov:skip=CKV_TF_1:We use the version tag instead of the commit hash
source = "git::https://github.com/honestbank/terraform-gcp-sql.git//modules/google_service_networking_connection?ref=v1.1.1"

network = module.private_network.id
Expand Down
4 changes: 2 additions & 2 deletions modules/memstore_redis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

| Name | Version |
|------|---------|
| <a name="provider_google"></a> [google](#provider\_google) | 4.29.0 |
| <a name="provider_google"></a> [google](#provider\_google) | 5.38.0 |

## Modules

Expand All @@ -33,7 +33,7 @@ No modules.
| <a name="input_read_replicas_enabled"></a> [read\_replicas\_enabled](#input\_read\_replicas\_enabled) | Whether to enable read replicas | `bool` | `false` | no |
| <a name="input_redis_version"></a> [redis\_version](#input\_redis\_version) | The version of Redis to use | `string` | n/a | yes |
| <a name="input_region"></a> [region](#input\_region) | The region to create the instance in | `string` | `"asia-southeast2"` | no |
| <a name="input_replicas"></a> [replicas](#input\_replicas) | The number of instances to create | `number` | `1` | no |
| <a name="input_replicas"></a> [replicas](#input\_replicas) | The number of read replicas to create | `number` | `0` | no |
| <a name="input_reserved_ip_range"></a> [reserved\_ip\_range](#input\_reserved\_ip\_range) | The reserved IP range to use for the instance | `string` | `null` | no |
| <a name="input_tier"></a> [tier](#input\_tier) | The tier of the instance | `string` | n/a | yes |
| <a name="input_zone"></a> [zone](#input\_zone) | The location to create the instance in | `string` | n/a | yes |
Expand Down
16 changes: 15 additions & 1 deletion modules/memstore_redis/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ resource "google_redis_instance" "cache" {
location_id = "${var.region}-${var.zone}"
alternative_location_id = var.tier == "STANDARD_HA" ? "${var.region}-${var.alternative_zone}" : ""

replica_count = var.tier == "STANDARD_HA" ? var.replicas : 0
replica_count = var.replicas
read_replicas_mode = var.read_replicas_enabled ? "READ_REPLICAS_ENABLED" : "READ_REPLICAS_DISABLED"

authorized_network = var.network_id
Expand All @@ -30,4 +30,18 @@ resource "google_redis_instance" "cache" {
display_name = var.name
reserved_ip_range = var.reserved_ip_range != null ? var.reserved_ip_range : null

lifecycle {
precondition {
condition = (var.read_replicas_enabled && var.memory_size >= 5) || var.read_replicas_enabled == false
error_message = "Read replicas cannot be enabled with less than 5GB of memory."
}
precondition {
condition = ((var.tier == "BASIC" && (var.replicas != 0 || var.read_replicas_enabled)) || var.tier == "STANDARD_HA")
error_message = "Read replicas are not supported on the BASIC tier."
}
precondition {
condition = (var.read_replicas_enabled && var.replicas > 0) || (var.replicas == 0 && var.read_replicas_enabled == false)
error_message = "You require at least 1 read replica if read replicas are enabled."
}
}
}
9 changes: 4 additions & 5 deletions modules/memstore_redis/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ variable "reserved_ip_range" {
description = "The reserved IP range to use for the instance"
}


variable "memory_size" {
type = string
default = "2"
Expand All @@ -49,11 +48,11 @@ variable "memory_size" {

variable "replicas" {
type = number
default = 1
description = "The number of instances to create"
default = 0
description = "The number of read replicas to create"
validation {
condition = var.replicas >= 1 && var.replicas <= 5
error_message = "The valid range for the Standard Tier with read replicas enabled is [1-5] and defaults to 1."
condition = var.replicas <= 5
error_message = "The valid range for the Standard Tier with read replicas enabled is [1-5] and defaults to 0 as the default is zero read-replicas."
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since 1.9 you can do cross input variable references in input validations. So can make conditional based on the tier here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice, that's nice to know. I rewrote the validations as preconditions on the resource.

}
}

Expand Down
Loading