diff --git a/.cspell.json b/.cspell.json index b8a95ecdd..877c3e397 100644 --- a/.cspell.json +++ b/.cspell.json @@ -2,6 +2,7 @@ "version": "0.2", "language": "en", "words": [ + "alltrue", "amazonec", "amannn", "amazonec", @@ -22,9 +23,7 @@ "endfor", "formatlist", "gitter", - "godotenv", - "golangci", - "gruntwork", + "glrunners", "instancelifecycle", "kics", "joho", @@ -39,10 +38,12 @@ "pylint", "pylintrc", "pyright", + "setsubtract", "shuf", "signoff", "signum", "stretchr", + "subkey", "substr", "templatefile", "terrascan", diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 324db2a70..a96f7f751 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,7 +31,7 @@ jobs: strategy: fail-fast: false matrix: - terraform: [ 1.0.11, 1.3.9, latest ] + terraform: [1.3.9, latest] example: [ "runner-default", diff --git a/.terraform-version b/.terraform-version index 337a6a8f1..589268e6f 100644 --- a/.terraform-version +++ b/.terraform-version @@ -1 +1 @@ -1.0.8 \ No newline at end of file +1.3.0 \ No newline at end of file diff --git a/examples/runner-default/main.tf b/examples/runner-default/main.tf index 84e089824..39a49fbe1 100644 --- a/examples/runner-default/main.tf +++ b/examples/runner-default/main.tf @@ -97,9 +97,9 @@ module "runner" { ] # working 9 to 5 :) - runners_machine_autoscaling = [ + runners_machine_autoscaling_options = [ { - periods = ["\"* * 0-9,17-23 * * mon-fri *\"", "\"* * * * * sat,sun *\""] + periods = ["* * 0-9,17-23 * * mon-fri *", "* * * * * sat,sun *"] idle_count = 0 idle_time = 60 timezone = var.timezone diff --git a/examples/runner-pre-registered/main.tf b/examples/runner-pre-registered/main.tf index e6d204c15..75d50d7b6 100644 --- a/examples/runner-pre-registered/main.tf +++ b/examples/runner-pre-registered/main.tf @@ -40,9 +40,9 @@ module "runner" { runners_token = var.runner_token # working 9 to 5 :) - runners_machine_autoscaling = [ + runners_machine_autoscaling_options = [ { - periods = ["\"* * 0-9,17-23 * * mon-fri *\"", "\"* * * * * sat,sun *\""] + periods = ["* * 0-9,17-23 * * mon-fri *", "* * * * * sat,sun *"] idle_count = 0 idle_time = 60 timezone = var.timezone diff --git a/locals.tf b/locals.tf index 58122f219..6d9342440 100644 --- a/locals.tf +++ b/locals.tf @@ -68,11 +68,6 @@ locals { %{~if var.runners_add_dind_volumes~},"/certs/client", "/builds", "/var/run/docker.sock:/var/run/docker.sock"%{endif~}%{~for volume in var.runners_additional_volumes~},"${volume}"%{endfor~} EOT - runners_machine_autoscaling = templatefile("${path.module}/template/runners_machine_autoscaling.tftpl", { - runners_machine_autoscaling = var.runners_machine_autoscaling - } - ) - runners_docker_services = templatefile("${path.module}/template/runners_docker_services.tftpl", { runners_docker_services = var.runners_docker_services } diff --git a/main.tf b/main.tf index a130b656e..0405e7d44 100644 --- a/main.tf +++ b/main.tf @@ -81,6 +81,12 @@ locals { template_runner_config = templatefile("${path.module}/template/runner-config.tftpl", { + runners_machine_autoscaling = [for config in var.runners_machine_autoscaling_options : { + for key, value in config : + # Convert key from snake_case to PascalCase which is the casing for this section. + join("", [for subkey in split("_", key) : title(subkey)]) => jsonencode(value) if value != null + }] + aws_region = var.aws_region gitlab_url = var.runners_gitlab_url gitlab_clone_url = var.runners_clone_url @@ -116,7 +122,6 @@ locals { runners_idle_count = var.runners_idle_count runners_idle_time = var.runners_idle_time runners_max_builds = local.runners_max_builds_string - runners_machine_autoscaling = local.runners_machine_autoscaling runners_root_size = var.runners_root_size runners_volume_type = var.runners_volume_type runners_iam_instance_profile_name = var.runners_iam_instance_profile_name diff --git a/migrations/migrate-to-7-0-0.sh b/migrations/migrate-to-7-0-0.sh index 8aaaa9b4e..de69a715d 100755 --- a/migrations/migrate-to-7-0-0.sh +++ b/migrations/migrate-to-7-0-0.sh @@ -23,3 +23,8 @@ sed -i '/asg_terminate_lifecycle_hook_heartbeat_timeout/d' "$converted_file" sed -i '/asg_terminate_lifecycle_lambda_memory_size/d' "$converted_file" sed -i '/asg_terminate_lifecycle_lambda_runtime/d' "$converted_file" sed -i '/asg_terminate_lifecycle_lambda_timeout/d' "$converted_file" + +# +# PR #711 feat!: refactor Docker Machine autoscaling options +# +sed -i 's/runners_machine_autoscaling/runners_machine_autoscaling_options/g' "$converted_file" diff --git a/outputs.tf b/outputs.tf index 66a6ba3bd..6956b3c90 100644 --- a/outputs.tf +++ b/outputs.tf @@ -57,3 +57,8 @@ output "runner_user_data" { description = "The user data of the Gitlab Runner Agent's launch template." value = local.template_user_data } + +output "runner_config_toml_rendered" { + description = "The rendered config.toml given to the Runner Manager." + value = local.template_runner_config +} diff --git a/template/runner-config.tftpl b/template/runner-config.tftpl index dda5e801a..eca80c910 100644 --- a/template/runner-config.tftpl +++ b/template/runner-config.tftpl @@ -71,4 +71,10 @@ listen_address = "${prometheus_listen_address}" ${docker_machine_options} ] -${runners_machine_autoscaling} +%{~ for config in runners_machine_autoscaling ~} + [[runners.machine.autoscaling]] + %{~ for key, value in config ~} + ${key} = ${value} + %{~ endfor ~} +%{~ endfor ~} + diff --git a/template/runners_machine_autoscaling.tftpl b/template/runners_machine_autoscaling.tftpl deleted file mode 100644 index 71b40fda1..000000000 --- a/template/runners_machine_autoscaling.tftpl +++ /dev/null @@ -1,7 +0,0 @@ -%{ for config in runners_machine_autoscaling ~} - [[runners.machine.autoscaling]] - Periods = [${replace(format("\"%s\"", join("\",\"", config.periods)), "/\"{2,}/", "\"")}] - IdleCount = ${config.idle_count} - IdleTime = ${config.idle_time} - Timezone = "${config.timezone}" -%{ endfor ~} diff --git a/variables.tf b/variables.tf index f02bc51db..72b90e9b7 100644 --- a/variables.tf +++ b/variables.tf @@ -260,14 +260,27 @@ variable "runners_ebs_optimized" { default = true } -variable "runners_machine_autoscaling" { +variable "runners_machine_autoscaling_options" { description = "Set autoscaling parameters based on periods, see https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-runnersmachine-section" type = list(object({ - periods = list(string) - idle_count = number - idle_time = number - timezone = string + periods = list(string) + idle_count = optional(number) + idle_scale_factor = optional(number) + idle_count_min = optional(number) + idle_time = optional(number) + timezone = optional(string, "UTC") })) + + validation { + condition = alltrue([ + for options in var.runners_machine_autoscaling_options : + length( + setsubtract([for key, value in options : key if value != null], ["periods", "timezone"]) + ) > 0 + ]) + + error_message = "Please specify an attribute that affects Autoscaling." + } default = [] }