Skip to content

Commit

Permalink
feat!: add idle_count_min and idle_scale_factor` to Docker Machine …
Browse files Browse the repository at this point in the history
…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 <github-public@matthiaskay.de>
  • Loading branch information
tmeijn and kayman-mk authored Apr 20, 2023
1 parent 93657e6 commit babc50a
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 28 deletions.
7 changes: 4 additions & 3 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"version": "0.2",
"language": "en",
"words": [
"alltrue",
"amazonec",
"amannn",
"amazonec",
Expand All @@ -22,9 +23,7 @@
"endfor",
"formatlist",
"gitter",
"godotenv",
"golangci",
"gruntwork",
"glrunners",
"instancelifecycle",
"kics",
"joho",
Expand All @@ -39,10 +38,12 @@
"pylint",
"pylintrc",
"pyright",
"setsubtract",
"shuf",
"signoff",
"signum",
"stretchr",
"subkey",
"substr",
"templatefile",
"terrascan",
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion .terraform-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.8
1.3.0
4 changes: 2 additions & 2 deletions examples/runner-default/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/runner-pre-registered/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 6 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions migrations/migrate-to-7-0-0.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
5 changes: 5 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 7 additions & 1 deletion template/runner-config.tftpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 ~}

7 changes: 0 additions & 7 deletions template/runners_machine_autoscaling.tftpl

This file was deleted.

23 changes: 18 additions & 5 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
}

Expand Down

0 comments on commit babc50a

Please sign in to comment.