Skip to content

Commit d3ed591

Browse files
authored
feat: Support new regional HA for MySQL (#99)
1 parent 952bc80 commit d3ed591

File tree

5 files changed

+17
-89
lines changed

5 files changed

+17
-89
lines changed

examples/mysql-ha/main.tf

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ module "mysql" {
4949
// Master configurations
5050
tier = "db-n1-standard-1"
5151
zone = "c"
52+
availability_type = "REGIONAL"
5253
maintenance_window_day = 7
5354
maintenance_window_hour = 12
5455
maintenance_window_update_track = "stable"
@@ -133,57 +134,6 @@ module "mysql" {
133134
]
134135
}
135136

136-
// Failover replica configurations
137-
failover_replica = true
138-
failover_replica_name_suffix = "-test"
139-
failover_replica_tier = "db-n1-standard-1"
140-
failover_replica_zone = "a"
141-
failover_replica_activation_policy = "ALWAYS"
142-
failover_replica_crash_safe_replication = true
143-
failover_replica_disk_autoresize = true
144-
failover_replica_disk_type = "PD_SSD"
145-
failover_replica_replication_type = "SYNCHRONOUS"
146-
failover_replica_maintenance_window_day = 3
147-
failover_replica_maintenance_window_hour = 20
148-
failover_replica_maintenance_window_update_track = "canary"
149-
150-
failover_replica_user_labels = {
151-
baz = "boo"
152-
}
153-
154-
failover_replica_database_flags = [
155-
{
156-
name = "long_query_time"
157-
value = "1"
158-
},
159-
]
160-
161-
failover_replica_configuration = {
162-
dump_file_path = "gs://${var.project_id}.appspot.com/tmp"
163-
connect_retry_interval = 5
164-
ca_certificate = null
165-
client_certificate = null
166-
client_key = null
167-
failover_target = null
168-
master_heartbeat_period = null
169-
password = null
170-
ssl_cipher = null
171-
username = null
172-
verify_server_certificate = null
173-
}
174-
175-
failover_replica_ip_configuration = {
176-
ipv4_enabled = true
177-
require_ssl = false
178-
private_network = null
179-
authorized_networks = [
180-
{
181-
name = "${var.project_id}-cidr"
182-
value = var.mysql_ha_external_ip_range
183-
},
184-
]
185-
}
186-
187137
db_name = var.mysql_ha_name
188138
db_charset = "utf8mb4"
189139
db_collation = "utf8mb4_general_ci"

modules/mysql/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
| additional\_databases | A list of databases to be created in your cluster | object | `<list>` | no |
1010
| additional\_users | A list of users to be created in your cluster | object | `<list>` | no |
1111
| authorized\_gae\_applications | The list of authorized App Engine project names | list(string) | `<list>` | no |
12+
| availability\_type | The availability type for the master instance. Can be either `REGIONAL` or `null`. | string | `"REGIONAL"` | no |
1213
| backup\_configuration | The backup_configuration settings subblock for the database setings | object | `<map>` | no |
1314
| create\_timeout | The optional timout that is applied to limit long database creates. | string | `"10m"` | no |
1415
| database\_flags | List of Cloud SQL flags that are applied to the database server | object | `<list>` | no |

modules/mysql/main.tf

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ locals {
2222
enabled = var.ip_configuration
2323
disabled = {}
2424
}
25+
26+
// HA method using REGIONAL availability_type requires binary logs to be enabled
27+
binary_log_enabled = var.availability_type == "REGIONAL" ? true : lookup(var.backup_configuration, "binary_log_enabled", null)
28+
backups_enabled = var.availability_type == "REGIONAL" ? true : lookup(var.backup_configuration, "enabled", null)
2529
}
2630

2731
resource "google_sql_database_instance" "default" {
@@ -33,12 +37,13 @@ resource "google_sql_database_instance" "default" {
3337
settings {
3438
tier = var.tier
3539
activation_policy = var.activation_policy
40+
availability_type = var.availability_type
3641
authorized_gae_applications = var.authorized_gae_applications
3742
dynamic "backup_configuration" {
3843
for_each = [var.backup_configuration]
3944
content {
40-
binary_log_enabled = lookup(backup_configuration.value, "binary_log_enabled", null)
41-
enabled = lookup(backup_configuration.value, "enabled", null)
45+
binary_log_enabled = local.binary_log_enabled
46+
enabled = local.backups_enabled
4247
start_time = lookup(backup_configuration.value, "start_time", null)
4348
}
4449
}

modules/mysql/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ variable "activation_policy" {
5555
default = "ALWAYS"
5656
}
5757

58+
variable "availability_type" {
59+
description = "The availability type for the master instance. Can be either `REGIONAL` or `null`."
60+
type = string
61+
default = "REGIONAL"
62+
}
63+
5864
variable "authorized_gae_applications" {
5965
description = "The list of authorized App Engine project names"
6066
type = list(string)

test/integration/mysql-ha/controls/mysql.rb

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
authorized_network = attribute('authorized_network')
1818

1919
describe google_sql_database_instances(project: project_id).where(instance_name: /#{basename}/) do
20-
its(:count) { should eq 5 }
20+
its(:count) { should eq 4 }
2121
end
2222

2323
describe google_sql_database_instance(project: project_id, database: basename) do
2424
let(:expected_settings) {
2525
{
2626
activation_policy: "ALWAYS",
27+
availability_type: "REGIONAL",
2728
data_disk_size_gb: 10,
2829
data_disk_type: "PD_SSD",
2930
kind: "sql#settings",
@@ -57,41 +58,6 @@
5758
it { expect(user_labels).to include(foo: "bar") }
5859
end
5960

60-
describe google_sql_database_instance(project: project_id, database: "#{basename}-failover-test") do
61-
let(:expected_settings) {
62-
{
63-
activation_policy: "ALWAYS",
64-
data_disk_size_gb: 10,
65-
data_disk_type: "PD_SSD",
66-
kind: "sql#settings",
67-
pricing_plan: "PER_USE",
68-
replication_type: "SYNCHRONOUS",
69-
storage_auto_resize: true,
70-
storage_auto_resize_limit: 0,
71-
tier: "db-n1-standard-1",
72-
}
73-
}
74-
let(:settings) { subject.settings.item }
75-
let(:ip_configuration) { settings[:ip_configuration] }
76-
let(:database_flags) { settings[:database_flags] }
77-
let(:location_preference) { settings[:location_preference] }
78-
let(:maintenance_window) { settings[:maintenance_window] }
79-
let(:user_labels) { settings[:user_labels] }
80-
81-
its(:backend_type) { should eq 'SECOND_GEN' }
82-
its(:database_version) { should eq 'MYSQL_5_7' }
83-
its(:state) { should eq 'RUNNABLE' }
84-
its(:region) { should eq 'us-central1' }
85-
its(:gce_zone) { should eq 'us-central1-a' }
86-
87-
it { expect(settings).to include(expected_settings) }
88-
it { expect(ip_configuration).to include(authorized_networks: [{kind: 'sql#aclEntry', name: "#{project_id}-cidr", value: authorized_network}], ipv4_enabled: true, require_ssl: false) }
89-
it { expect(database_flags).to include(name: "long_query_time", value: "1") }
90-
it { expect(location_preference).to include(kind: "sql#locationPreference", zone: "us-central1-a") }
91-
it { expect(maintenance_window).to include(kind: "sql#maintenanceWindow", day: 3, hour: 20, update_track: "canary") }
92-
it { expect(user_labels).to include(baz: "boo") }
93-
end
94-
9561
%i[a b c].each_with_index do |zone, index|
9662
name = "#{basename}-replica-test#{index}"
9763
describe google_sql_database_instance(project: project_id, database: name) do

0 commit comments

Comments
 (0)