Added support for Stateful Managed Instance Groups (#367)

* First iteration updates

* All tests passing

* Updated README and var descriptions

* Updated README

* Updated example README

* Consolidated stateful vars

* consolidated stateful vars

* Updated README

* Requested changes to try

* Fixed README examples and try

Co-authored-by: Ludovico Magnocavallo <ludomagno@google.com>
This commit is contained in:
Stenio Ferreira
2021-12-15 07:56:53 -06:00
committed by GitHub
parent 5beba11058
commit 601ebd028e
6 changed files with 390 additions and 7 deletions

View File

@@ -14,6 +14,15 @@
* limitations under the License.
*/
# Used in stateful disk test
resource "google_compute_disk" "default" {
name = "test-disk"
type = "pd-ssd"
zone = "europe-west1-c"
image = "debian-9-stretch-v20200805"
physical_block_size_bytes = 4096
}
module "test" {
source = "../../../../modules/compute-mig"
project_id = "my-project"
@@ -28,6 +37,8 @@ module "test" {
health_check_config = var.health_check_config
named_ports = var.named_ports
regional = var.regional
update_policy = var.update_policy
versions = var.versions
stateful_config = var.stateful_config
update_policy = var.update_policy
versions = var.versions
}

View File

@@ -60,6 +60,37 @@ variable "regional" {
default = false
}
variable "stateful_config" {
description = "Stateful configuration can be done by individual instances or for all instances in the MIG. They key in per_instance_config is the name of the specific instance. The key of the stateful_disks is the 'device_name' field of the resource. Please note that device_name is defined at the OS mount level, unlike the disk name."
type = object({
per_instance_config = map(object({
#name is the key
#name = string
stateful_disks = map(object({
#device_name is the key
source = string
mode = string # READ_WRITE | READ_ONLY
delete_rule = string # NEVER | ON_PERMANENT_INSTANCE_DELETION
}))
metadata = map(string)
update_config = object({
minimal_action = string # NONE | REPLACE | RESTART | REFRESH
most_disruptive_allowed_action = string # REPLACE | RESTART | REFRESH | NONE
remove_instance_state_on_destroy = bool
})
}))
mig_config = object({
stateful_disks = map(object({
#device_name is the key
delete_rule = string # NEVER | ON_PERMANENT_INSTANCE_DELETION
}))
})
})
default = null
}
variable "update_policy" {
type = object({
type = string # OPPORTUNISTIC | PROACTIVE

View File

@@ -24,6 +24,7 @@ def test_defaults(plan_runner):
"Test variable defaults."
_, resources = plan_runner(FIXTURES_DIR)
assert len(resources) == 1
print(resources[0]['type'])
mig = resources[0]
assert mig['type'] == 'google_compute_instance_group_manager'
assert mig['values']['target_size'] == 2
@@ -35,7 +36,6 @@ def test_defaults(plan_runner):
assert mig['values']['target_size'] == 2
assert mig['values']['region']
def test_health_check(plan_runner):
"Test health check resource."
health_check_config = '{type="tcp", check={port=80}, config=null, logging=false}'
@@ -75,3 +75,81 @@ def test_autoscaler(plan_runner):
assert len(resources) == 2
autoscaler = resources[0]
assert autoscaler['type'] == 'google_compute_region_autoscaler'
def test_stateful_mig(plan_runner):
"Test stateful instances - mig."
stateful_config = (
'{'
'per_instance_config = {},'
'mig_config = {'
'stateful_disks = {'
'persistent-disk-1 = {delete_rule="NEVER"}'
'}'
'}'
'}'
)
_, resources = plan_runner(
FIXTURES_DIR, stateful_config=stateful_config)
assert len(resources) == 1
statefuldisk = resources[0]
assert statefuldisk['type'] == 'google_compute_instance_group_manager'
assert statefuldisk['values']['stateful_disk'] == [{
'device_name': 'persistent-disk-1',
'delete_rule': 'NEVER',
}]
def test_stateful_instance(plan_runner):
"Test stateful instances - instance."
stateful_config = (
'{'
'per_instance_config = {'
'instance-1 = {'
'stateful_disks = {'
'persistent-disk-1 = {'
'source = "test-disk",'
'mode = "READ_ONLY",'
'delete_rule= "NEVER",'
'},'
'},'
'metadata = {'
'foo = "bar"'
'},'
'update_config = {'
'minimal_action = "NONE",'
'most_disruptive_allowed_action = "REPLACE",'
'remove_instance_state_on_destroy = false,'
'},'
'},'
'},'
'mig_config = {'
'stateful_disks = {'
'persistent-disk-1 = {delete_rule="NEVER"}'
'}'
'}'
'}'
)
_, resources = plan_runner(
FIXTURES_DIR, stateful_config=stateful_config)
assert len(resources) == 2
instanceconfig = resources[0]
assert instanceconfig['type'] == 'google_compute_instance_group_manager'
instanceconfig = resources[1]
assert instanceconfig['type'] == 'google_compute_per_instance_config'
assert instanceconfig['values']['preserved_state'] == [{
'disk': [{
'device_name': 'persistent-disk-1',
'delete_rule': 'NEVER',
'source': 'test-disk',
'mode': 'READ_ONLY',
}],
'metadata': {
'foo': 'bar'
}
}]
assert instanceconfig['values']['minimal_action'] == 'NONE'
assert instanceconfig['values']['most_disruptive_allowed_action'] == 'REPLACE'
assert instanceconfig['values']['remove_instance_state_on_destroy'] == False