Skip to content

Commit f72b8e3

Browse files
authoredDec 11, 2022
feat(asg): Add fine-grained options for schedule_config scale_in and scale_out (#586)
1 parent b069b88 commit f72b8e3

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed
 

Diff for: ‎README.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,34 @@ Once you have created the parameter, you must remove the variable `runners_token
148148

149149
Finally, the runner still supports the manual runner creation. No changes are required. Please keep in mind that this setup will be removed in future releases.
150150

151-
### Auto Scaling Group Instance Termination
151+
### Auto Scaling Group
152+
153+
#### Scheduled scaling
154+
155+
When `enable_schedule=true`, the `schedule_config` variable can be used to scale the Auto Scaling group.
156+
157+
Scaling may be defined with one `scale_out` scheduled action and/or one `scale_in` scheduled action.
158+
159+
For example:
160+
```hcl
161+
module "runner" {
162+
...
163+
enable_schedule = true
164+
schedule_config = {
165+
# Configure optional scale_out scheduled action
166+
scale_out_recurrence = "0 8 * * 1-5"
167+
scale_out_count = 1 # Default for min_size, desired_capacity and max_size
168+
# Override using: scale_out_min_size, scale_out_desired_capacity, scale_out_max_size
169+
170+
# Configure optional scale_in scheduled action
171+
scale_in_recurrence = "0 18 * * 1-5"
172+
scale_in_count = 0 # Default for min_size, desired_capacity and max_size
173+
# Override using: scale_out_min_size, scale_out_desired_capacity, scale_out_max_size
174+
}
175+
}
176+
```
177+
178+
#### Instance Termination
152179

153180
The Auto Scaling Group may be configured with a
154181
[lifecycle hook](https://docs.aws.amazon.com/autoscaling/ec2/userguide/lifecycle-hooks.html)
@@ -505,7 +532,7 @@ Made with [contributors-img](https://contrib.rocks).
505532
| <a name="input_runners_use_private_address"></a> [runners\_use\_private\_address](#input\_runners\_use\_private\_address) | Restrict runners to the use of a private IP address. If `runner_agent_uses_private_address` is set to `true`(default), `runners_use_private_address` will also apply for the agent. | `bool` | `true` | no |
506533
| <a name="input_runners_volume_type"></a> [runners\_volume\_type](#input\_runners\_volume\_type) | Runner instance volume type | `string` | `"gp2"` | no |
507534
| <a name="input_runners_volumes_tmpfs"></a> [runners\_volumes\_tmpfs](#input\_runners\_volumes\_tmpfs) | Mount a tmpfs in runner container. https://docs.gitlab.com/runner/executors/docker.html#mounting-a-directory-in-ram | <pre>list(object({<br> volume = string<br> options = string<br> }))</pre> | `[]` | no |
508-
| <a name="input_schedule_config"></a> [schedule\_config](#input\_schedule\_config) | Map containing the configuration of the ASG scale-in and scale-up for the runner instance. Will only be used if enable\_schedule is set to true. | `map(any)` | <pre>{<br> "scale_in_count": 0,<br> "scale_in_recurrence": "0 18 * * 1-5",<br> "scale_out_count": 1,<br> "scale_out_recurrence": "0 8 * * 1-5"<br>}</pre> | no |
535+
| <a name="input_schedule_config"></a> [schedule\_config](#input\_schedule\_config) | Map containing the configuration of the ASG scale-out and scale-in for the runner instance. Will only be used if enable\_schedule is set to true. | `map(any)` | <pre>{<br> "scale_in_count": 0,<br> "scale_in_recurrence": "0 18 * * 1-5",<br> "scale_out_count": 1,<br> "scale_out_recurrence": "0 8 * * 1-5"<br>}</pre> | no |
509536
| <a name="input_secure_parameter_store_runner_sentry_dsn"></a> [secure\_parameter\_store\_runner\_sentry\_dsn](#input\_secure\_parameter\_store\_runner\_sentry\_dsn) | The Sentry DSN name used to store the Sentry DSN in Secure Parameter Store | `string` | `"sentry-dsn"` | no |
510537
| <a name="input_secure_parameter_store_runner_token_key"></a> [secure\_parameter\_store\_runner\_token\_key](#input\_secure\_parameter\_store\_runner\_token\_key) | The key name used store the Gitlab runner token in Secure Parameter Store | `string` | `"runner-token"` | no |
511538
| <a name="input_sentry_dsn"></a> [sentry\_dsn](#input\_sentry\_dsn) | Sentry DSN of the project for the runner to use (uses legacy DSN format) | `string` | `"__SENTRY_DSN_REPLACED_BY_USER_DATA__"` | no |

Diff for: ‎main.tf

+9-6
Original file line numberDiff line numberDiff line change
@@ -185,26 +185,29 @@ resource "aws_autoscaling_group" "gitlab_runner_instance" {
185185
timeouts {
186186
delete = var.asg_delete_timeout
187187
}
188+
lifecycle {
189+
ignore_changes = [min_size, max_size, desired_capacity]
190+
}
188191
}
189192

190193
resource "aws_autoscaling_schedule" "scale_in" {
191194
count = var.enable_schedule ? 1 : 0
192195
autoscaling_group_name = aws_autoscaling_group.gitlab_runner_instance.name
193196
scheduled_action_name = "scale_in-${aws_autoscaling_group.gitlab_runner_instance.name}"
194197
recurrence = var.schedule_config["scale_in_recurrence"]
195-
min_size = var.schedule_config["scale_in_count"]
196-
desired_capacity = var.schedule_config["scale_in_count"]
197-
max_size = var.schedule_config["scale_in_count"]
198+
min_size = try(var.schedule_config["scale_in_min_size"], var.schedule_config["scale_in_count"])
199+
desired_capacity = try(var.schedule_config["scale_in_desired_capacity"], var.schedule_config["scale_in_count"])
200+
max_size = try(var.schedule_config["scale_in_max_size"], var.schedule_config["scale_in_count"])
198201
}
199202

200203
resource "aws_autoscaling_schedule" "scale_out" {
201204
count = var.enable_schedule ? 1 : 0
202205
autoscaling_group_name = aws_autoscaling_group.gitlab_runner_instance.name
203206
scheduled_action_name = "scale_out-${aws_autoscaling_group.gitlab_runner_instance.name}"
204207
recurrence = var.schedule_config["scale_out_recurrence"]
205-
min_size = var.schedule_config["scale_out_count"]
206-
desired_capacity = var.schedule_config["scale_out_count"]
207-
max_size = var.schedule_config["scale_out_count"]
208+
min_size = try(var.schedule_config["scale_out_min_size"], var.schedule_config["scale_out_count"])
209+
desired_capacity = try(var.schedule_config["scale_out_desired_capacity"], var.schedule_config["scale_out_count"])
210+
max_size = try(var.schedule_config["scale_out_max_size"], var.schedule_config["scale_out_count"])
208211
}
209212

210213
data "aws_ami" "runner" {

Diff for: ‎variables.tf

+9-4
Original file line numberDiff line numberDiff line change
@@ -645,13 +645,18 @@ variable "enable_schedule" {
645645
}
646646

647647
variable "schedule_config" {
648-
description = "Map containing the configuration of the ASG scale-in and scale-up for the runner instance. Will only be used if enable_schedule is set to true. "
648+
description = "Map containing the configuration of the ASG scale-out and scale-in for the runner instance. Will only be used if enable_schedule is set to true. "
649649
type = map(any)
650650
default = {
651-
scale_in_recurrence = "0 18 * * 1-5"
652-
scale_in_count = 0
651+
# Configure optional scale_out scheduled action
653652
scale_out_recurrence = "0 8 * * 1-5"
654-
scale_out_count = 1
653+
scale_out_count = 1 # Default for min_size, desired_capacity and max_size
654+
# Override using: scale_out_min_size, scale_out_desired_capacity, scale_out_max_size
655+
656+
# Configure optional scale_in scheduled action
657+
scale_in_recurrence = "0 18 * * 1-5"
658+
scale_in_count = 0 # Default for min_size, desired_capacity and max_size
659+
# Override using: scale_out_min_size, scale_out_desired_capacity, scale_out_max_size
655660
}
656661
}
657662

0 commit comments

Comments
 (0)
Please sign in to comment.