Refactor compute-mig module for Terraform 1.3 (#931)

* wip: autoscaler

* wip: fix autoscaler

* wip: health check

* wip: untested

* wip: tests and examples missing

* wip: examples

* wip: consumers

* blueprint tests

* fast
This commit is contained in:
Ludovico Magnocavallo
2022-11-01 09:38:59 +01:00
committed by GitHub
parent d7e0af75b2
commit a30c186f1f
13 changed files with 1019 additions and 816 deletions

View File

@@ -24,21 +24,18 @@ resource "google_compute_disk" "default" {
}
module "test" {
source = "../../../../modules/compute-mig"
project_id = "my-project"
location = "europe-west1"
name = "test-mig"
target_size = 2
default_version = {
instance_template = "foo-template"
name = "foo"
}
autoscaler_config = var.autoscaler_config
health_check_config = var.health_check_config
named_ports = var.named_ports
regional = var.regional
stateful_config = var.stateful_config
update_policy = var.update_policy
versions = var.versions
source = "../../../../modules/compute-mig"
project_id = "my-project"
name = "test-mig"
target_size = 2
default_version_name = "foo"
instance_template = "foo-template"
location = var.location
autoscaler_config = var.autoscaler_config
health_check_config = var.health_check_config
named_ports = var.named_ports
stateful_config = var.stateful_config
stateful_disks = var.stateful_disks
update_policy = var.update_policy
versions = var.versions
}

View File

@@ -14,101 +14,82 @@
* limitations under the License.
*/
variable "autoscaler_config" {
type = object({
max_replicas = number
min_replicas = number
cooldown_period = number
cpu_utilization_target = number
load_balancing_utilization_target = number
metric = object({
name = string
single_instance_assignment = number
target = number
type = string # GAUGE, DELTA_PER_SECOND, DELTA_PER_MINUTE
filter = string
})
})
variable "all_instances_config" {
type = any
default = null
}
variable "auto_healing_policies" {
type = object({
health_check = string
initial_delay_sec = number
})
type = any
default = null
}
variable "autoscaler_config" {
type = any
default = null
}
variable "default_version_name" {
type = any
default = "default"
}
variable "description" {
type = any
default = "Terraform managed."
}
variable "distribution_policy" {
type = any
default = null
}
variable "health_check_config" {
type = object({
type = string # http https tcp ssl http2
check = map(any) # actual health check block attributes
config = map(number) # interval, thresholds, timeout
logging = bool
})
type = any
default = null
}
variable "location" {
type = any
default = "europe-west1-b"
}
variable "named_ports" {
type = map(number)
type = any
default = null
}
variable "regional" {
type = bool
default = false
variable "stateful_disks" {
type = any
default = {}
}
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
})
}))
type = any
default = {}
}
mig_config = object({
stateful_disks = map(object({
#device_name is the key
delete_rule = string # NEVER | ON_PERMANENT_INSTANCE_DELETION
}))
})
variable "target_pools" {
type = any
default = []
}
})
variable "target_size" {
type = any
default = null
}
variable "update_policy" {
type = object({
type = string # OPPORTUNISTIC | PROACTIVE
minimal_action = string # REPLACE | RESTART
min_ready_sec = number
max_surge_type = string # fixed | percent
max_surge = number
max_unavailable_type = string
max_unavailable = number
})
type = any
default = null
}
variable "versions" {
type = map(object({
instance_template = string
target_type = string # fixed | percent
target_size = number
}))
type = any
default = {}
}
variable "wait_for_instances" {
type = any
default = null
}

View File

@@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
def test_defaults(plan_runner):
"Test variable defaults."
_, resources = plan_runner()
@@ -21,7 +22,7 @@ def test_defaults(plan_runner):
assert mig['type'] == 'google_compute_instance_group_manager'
assert mig['values']['target_size'] == 2
assert mig['values']['zone']
_, resources = plan_runner(regional='true')
_, resources = plan_runner(location='"europe-west1"')
assert len(resources) == 1
mig = resources[0]
assert mig['type'] == 'google_compute_region_instance_group_manager'
@@ -31,7 +32,12 @@ def test_defaults(plan_runner):
def test_health_check(plan_runner):
"Test health check resource."
health_check_config = '{type="tcp", check={port=80}, config=null, logging=false}'
health_check_config = '''{
enable_logging = true
tcp = {
port = 80
}
}'''
_, resources = plan_runner(health_check_config=health_check_config)
assert len(resources) == 2
assert any(r['type'] == 'google_compute_health_check' for r in resources)
@@ -39,20 +45,26 @@ def test_health_check(plan_runner):
def test_autoscaler(plan_runner):
"Test autoscaler resource."
autoscaler_config = (
'{'
'max_replicas=3, min_replicas=1, cooldown_period=60,'
'cpu_utilization_target=65, load_balancing_utilization_target=null,'
'metric=null'
'}'
)
autoscaler_config = '''{
colldown_period = 60
max_replicas = 3
min_replicas = 1
scaling_signals = {
cpu_utilization = {
target = 65
}
}
}'''
_, resources = plan_runner(autoscaler_config=autoscaler_config)
assert len(resources) == 2
autoscaler = resources[0]
assert autoscaler['type'] == 'google_compute_autoscaler'
assert autoscaler['values']['autoscaling_policy'] == [{
'cooldown_period': 60,
'cpu_utilization': [{'predictive_method': 'NONE', 'target': 65}],
'cpu_utilization': [{
'predictive_method': 'NONE',
'target': 65
}],
'load_balancing_utilization': [],
'max_replicas': 3,
'metric': [],
@@ -62,7 +74,7 @@ def test_autoscaler(plan_runner):
'scaling_schedules': [],
}]
_, resources = plan_runner(autoscaler_config=autoscaler_config,
regional='true')
location='"europe-west1"')
assert len(resources) == 2
autoscaler = resources[0]
assert autoscaler['type'] == 'google_compute_region_autoscaler'
@@ -71,17 +83,10 @@ def test_autoscaler(plan_runner):
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(stateful_config=stateful_config)
stateful_disks = '''{
persistent-disk-1 = null
}'''
_, resources = plan_runner(stateful_disks=stateful_disks)
assert len(resources) == 1
statefuldisk = resources[0]
assert statefuldisk['type'] == 'google_compute_instance_group_manager'
@@ -93,35 +98,19 @@ def test_stateful_mig(plan_runner):
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"}'
'}'
'}'
'}'
)
stateful_config = '''{
instance-1 = {
most_disruptive_action = "REPLACE",
preserved_state = {
disks = {
persistent-disk-1 = {
source = "test-disk"
}
}
metadata = { foo = "bar" }
}
}
}'''
_, resources = plan_runner(stateful_config=stateful_config)
assert len(resources) == 2
instanceconfig = resources[0]
@@ -134,13 +123,12 @@ def test_stateful_instance(plan_runner):
'device_name': 'persistent-disk-1',
'delete_rule': 'NEVER',
'source': 'test-disk',
'mode': 'READ_ONLY',
'mode': 'READ_WRITE',
}],
'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