Skip to content

Commit 05b052f

Browse files
tmeijnkayman-mk
andcommitted
feat!: add idle_count_min and idle_scale_factor` to Docker Machine autoscaling options (#711)
## Description Switches from hardcoded options to free-from scaling configuration. This reduces the module complexity by allowing to get rid of a number of variables while giving more control to the user to define their options without us having to build support into it for. Adds `idle_scale_factor` and `idle_count_min` Docker Machine options. See [documentation](https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-runnersmachine-section"). ## Migrations required YES - users will have to change the input name from `runners_machine_autoscaling` to `runners_machine_autoscaling_options`. No other changes should be needed, we just support _more_ options. A migration script is available. ## Verification No input given: (end of rendered `config.toml`) ![image](https://user-images.githubusercontent.com/17970041/225890782-02fe4adc-4c6a-4237-9752-a64349464113.png) Input: ```hcl runners_machine_autoscaling_options = [ { periods = ["* * 9-17 * * mon-fri *", "* * 9-17 * * mon-fri *"] idle_count = 50 idle_count_min = 10 idle_time = 3600 timezone = "UTC" idle_scale_factor = 1.5 }, { periods = ["* * 9-17 * * mon-fri *", "* * 9-17 * * mon-fri *"] idle_count = 50 idle_time = 3600 timezone = "Europe/Amsterdam" } ] ``` Rendered `config.toml`: ![image](https://user-images.githubusercontent.com/17970041/225891085-add03ee8-3943-4c56-96a4-d1a8c252deb0.png) Apply results: ![image](https://user-images.githubusercontent.com/17970041/225893020-a9850486-4aa6-4eb0-b996-558ec7bccfea.png) Closes #556 --------- Co-authored-by: Matthias Kay <[email protected]>
1 parent 2cb01ad commit 05b052f

File tree

12 files changed

+51
-28
lines changed

12 files changed

+51
-28
lines changed

.cspell.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"version": "0.2",
33
"language": "en",
44
"words": [
5+
"alltrue",
56
"amazonec",
67
"amannn",
78
"amazonec",
@@ -22,9 +23,7 @@
2223
"endfor",
2324
"formatlist",
2425
"gitter",
25-
"godotenv",
26-
"golangci",
27-
"gruntwork",
26+
"glrunners",
2827
"instancelifecycle",
2928
"kics",
3029
"joho",
@@ -39,10 +38,12 @@
3938
"pylint",
4039
"pylintrc",
4140
"pyright",
41+
"setsubtract",
4242
"shuf",
4343
"signoff",
4444
"signum",
4545
"stretchr",
46+
"subkey",
4647
"substr",
4748
"templatefile",
4849
"terrascan",

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
strategy:
3232
fail-fast: false
3333
matrix:
34-
terraform: [ 1.0.11, 1.3.9, latest ]
34+
terraform: [1.3.9, latest]
3535
example:
3636
[
3737
"runner-default",

.terraform-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.8
1+
1.3.0

examples/runner-default/main.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ module "runner" {
9797
]
9898

9999
# working 9 to 5 :)
100-
runners_machine_autoscaling = [
100+
runners_machine_autoscaling_options = [
101101
{
102-
periods = ["\"* * 0-9,17-23 * * mon-fri *\"", "\"* * * * * sat,sun *\""]
102+
periods = ["* * 0-9,17-23 * * mon-fri *", "* * * * * sat,sun *"]
103103
idle_count = 0
104104
idle_time = 60
105105
timezone = var.timezone

examples/runner-pre-registered/main.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ module "runner" {
4040
runners_token = var.runner_token
4141

4242
# working 9 to 5 :)
43-
runners_machine_autoscaling = [
43+
runners_machine_autoscaling_options = [
4444
{
45-
periods = ["\"* * 0-9,17-23 * * mon-fri *\"", "\"* * * * * sat,sun *\""]
45+
periods = ["* * 0-9,17-23 * * mon-fri *", "* * * * * sat,sun *"]
4646
idle_count = 0
4747
idle_time = 60
4848
timezone = var.timezone

locals.tf

-5
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,6 @@ locals {
6868
%{~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~}
6969
EOT
7070

71-
runners_machine_autoscaling = templatefile("${path.module}/template/runners_machine_autoscaling.tftpl", {
72-
runners_machine_autoscaling = var.runners_machine_autoscaling
73-
}
74-
)
75-
7671
runners_docker_services = templatefile("${path.module}/template/runners_docker_services.tftpl", {
7772
runners_docker_services = var.runners_docker_services
7873
}

main.tf

+6-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ locals {
8484

8585
template_runner_config = templatefile("${path.module}/template/runner-config.tftpl",
8686
{
87+
runners_machine_autoscaling = [for config in var.runners_machine_autoscaling_options : {
88+
for key, value in config :
89+
# Convert key from snake_case to PascalCase which is the casing for this section.
90+
join("", [for subkey in split("_", key) : title(subkey)]) => jsonencode(value) if value != null
91+
}]
92+
8793
aws_region = var.aws_region
8894
gitlab_url = var.runners_gitlab_url
8995
gitlab_clone_url = var.runners_clone_url
@@ -121,7 +127,6 @@ locals {
121127
runners_idle_count = var.runners_idle_count
122128
runners_idle_time = var.runners_idle_time
123129
runners_max_builds = local.runners_max_builds_string
124-
runners_machine_autoscaling = local.runners_machine_autoscaling
125130
runners_root_size = var.runners_root_size
126131
runners_volume_type = var.runners_volume_type
127132
runners_iam_instance_profile_name = var.runners_iam_instance_profile_name

migrations/migrate-to-7-0-0.sh

+5
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ sed -i '/asg_terminate_lifecycle_hook_heartbeat_timeout/d' "$converted_file"
2323
sed -i '/asg_terminate_lifecycle_lambda_memory_size/d' "$converted_file"
2424
sed -i '/asg_terminate_lifecycle_lambda_runtime/d' "$converted_file"
2525
sed -i '/asg_terminate_lifecycle_lambda_timeout/d' "$converted_file"
26+
27+
#
28+
# PR #711 feat!: refactor Docker Machine autoscaling options
29+
#
30+
sed -i 's/runners_machine_autoscaling/runners_machine_autoscaling_options/g' "$converted_file"

outputs.tf

+5
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,8 @@ output "runner_user_data" {
5757
description = "The user data of the Gitlab Runner Agent's launch template."
5858
value = local.template_user_data
5959
}
60+
61+
output "runner_config_toml_rendered" {
62+
description = "The rendered config.toml given to the Runner Manager."
63+
value = local.template_runner_config
64+
}

template/runner-config.tftpl

+7-1
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,10 @@ listen_address = "${prometheus_listen_address}"
8080
${docker_machine_options}
8181
]
8282

83-
${runners_machine_autoscaling}
83+
%{~ for config in runners_machine_autoscaling ~}
84+
[[runners.machine.autoscaling]]
85+
%{~ for key, value in config ~}
86+
${key} = ${value}
87+
%{~ endfor ~}
88+
%{~ endfor ~}
89+

template/runners_machine_autoscaling.tftpl

-7
This file was deleted.

variables.tf

+18-5
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,27 @@ variable "runners_ebs_optimized" {
278278
default = true
279279
}
280280

281-
variable "runners_machine_autoscaling" {
281+
variable "runners_machine_autoscaling_options" {
282282
description = "Set autoscaling parameters based on periods, see https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-runnersmachine-section"
283283
type = list(object({
284-
periods = list(string)
285-
idle_count = number
286-
idle_time = number
287-
timezone = string
284+
periods = list(string)
285+
idle_count = optional(number)
286+
idle_scale_factor = optional(number)
287+
idle_count_min = optional(number)
288+
idle_time = optional(number)
289+
timezone = optional(string, "UTC")
288290
}))
291+
292+
validation {
293+
condition = alltrue([
294+
for options in var.runners_machine_autoscaling_options :
295+
length(
296+
setsubtract([for key, value in options : key if value != null], ["periods", "timezone"])
297+
) > 0
298+
])
299+
300+
error_message = "Please specify an attribute that affects Autoscaling."
301+
}
289302
default = []
290303
}
291304

0 commit comments

Comments
 (0)