Skip to content

Commit e4ac12f

Browse files
committed
refactor to object config
1 parent 4473737 commit e4ac12f

File tree

8 files changed

+66
-45
lines changed

8 files changed

+66
-45
lines changed

examples/default/.terraform.lock.hcl

+30-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/default/main.tf

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module "runners" {
3232
webhook_secret = random_id.random.hex
3333
}
3434

35+
enable_dynamic_ec2_types = true
3536
# configure the block device mappings, default for Amazon Linux2
3637
# block_device_mappings = [{
3738
# device_name = "/dev/xvda"
@@ -98,7 +99,7 @@ module "runners" {
9899
}
99100

100101
# Enable debug logging for the lambda functions
101-
# log_level = "debug"
102+
log_level = "debug"
102103

103104
# tracing_config = {
104105
# mode = "Active"

lambdas/functions/control-plane/src/scale-runners/scale-up.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ export async function scaleUp(eventSource: string, payload: ActionRequestMessage
244244
if (eventSource !== 'aws:sqs') throw Error('Cannot handle non-SQS events!');
245245

246246
const dynamicEc2TypesEnabled = yn(process.env.ENABLE_DYNAMIC_EC2_TYPES, { default: false });
247-
const requestedInstanceType = payload.labels?.find(label => label.startsWith('ghr-ec2-'))?.replace('ghr-ec2-', '');
247+
const labelPrefix = process.env.WORKFLOW_LABEL_TYPE_PREFIX || 'ghr-ec2-';
248+
const requestedInstanceType = payload.labels?.find(label => label.startsWith(labelPrefix))?.replace(labelPrefix, '');
248249

249250
if (dynamicEc2TypesEnabled && requestedInstanceType) {
250251
logger.info(`Dynamic EC2 instance type requested: ${requestedInstanceType}`);
@@ -261,7 +262,7 @@ export async function scaleUp(eventSource: string, payload: ActionRequestMessage
261262
// Combine configured runner labels with dynamic EC2 instance type label if present
262263
let runnerLabels = process.env.RUNNER_LABELS || '';
263264
if (dynamicEc2TypesEnabled && requestedInstanceType) {
264-
const ec2Label = `ghr-ec2-${requestedInstanceType}`;
265+
const ec2Label = `${labelPrefix}${requestedInstanceType}`;
265266
runnerLabels = runnerLabels ? `${runnerLabels},${ec2Label}` : ec2Label;
266267
logger.debug(`Added dynamic EC2 instance type label: ${ec2Label} to runner config.`);
267268
}

main.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ module "runners" {
187187
github_app_parameters = local.github_app_parameters
188188
enable_organization_runners = var.enable_organization_runners
189189
enable_ephemeral_runners = var.enable_ephemeral_runners
190-
enable_dynamic_ec2_types = var.enable_dynamic_ec2_types
190+
dynamic_ec2 = var.dynamic_ec2
191191
enable_job_queued_check = var.enable_job_queued_check
192192
enable_jit_config = var.enable_jit_config
193193
enable_on_demand_failover_for_errors = var.enable_runner_on_demand_failover_for_errors

modules/multi-runner/variables.tf

+8-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ variable "multi_runner_config" {
7575
disable_runner_autoupdate = optional(bool, false)
7676
ebs_optimized = optional(bool, false)
7777
enable_ephemeral_runners = optional(bool, false)
78-
enable_dynamic_ec2_types = optional(bool, false)
78+
dynamic_ec2 = optional(object({
79+
enable_types = bool
80+
workflow_label_type_prefix = string
81+
}), {
82+
enable_types = false
83+
workflow_label_type_prefix = "ghr-ec2-"
84+
})
7985
enable_job_queued_check = optional(bool, null)
8086
enable_on_demand_failover_for_errors = optional(list(string), [])
8187
enable_organization_runners = optional(bool, false)
@@ -180,7 +186,7 @@ variable "multi_runner_config" {
180186
disable_runner_autoupdate: "Disable the auto update of the github runner agent. Be aware there is a grace period of 30 days, see also the [GitHub article](https://github.blog/changelog/2022-02-01-github-actions-self-hosted-runners-can-now-disable-automatic-updates/)"
181187
ebs_optimized: "The EC2 EBS optimized configuration."
182188
enable_ephemeral_runners: "Enable ephemeral runners, runners will only be used once."
183-
enable_dynamic_ec2_types: "Enable dynamic EC2 instance types based on workflow job labels. When enabled, jobs can request specific instance types via the 'gh-ec2-instance-type' label (e.g., 'gh-ec2-t3.large')."
189+
dynamic_ec2: "Configuration for dynamic EC2 instance types feature. This object allows you to enable dynamic instance types and configure the label prefix used in workflows."
184190
enable_job_queued_check: "(Optional) Only scale if the job event received by the scale up lambda is is in the state queued. By default enabled for non ephemeral runners and disabled for ephemeral. Set this variable to overwrite the default behavior."
185191
enable_on_demand_failover_for_errors: "Enable on-demand failover. For example to fall back to on demand when no spot capacity is available the variable can be set to `InsufficientInstanceCapacity`. When not defined the default behavior is to retry later."
186192
enable_organization_runners: "Register runners to organization, instead of repo level"

modules/runners/scale-up.tf

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ resource "aws_lambda_function" "scale_up" {
2828
AMI_ID_SSM_PARAMETER_NAME = var.ami_id_ssm_parameter_name
2929
DISABLE_RUNNER_AUTOUPDATE = var.disable_runner_autoupdate
3030
ENABLE_EPHEMERAL_RUNNERS = var.enable_ephemeral_runners
31-
ENABLE_DYNAMIC_EC2_TYPES = var.enable_dynamic_ec2_types
31+
ENABLE_DYNAMIC_EC2_TYPES = var.dynamic_ec2.enable_types
32+
WORKFLOW_LABEL_TYPE_PREFIX = var.dynamic_ec2.workflow_label_type_prefix
3233
ENABLE_JIT_CONFIG = var.enable_jit_config
3334
ENABLE_JOB_QUEUED_CHECK = local.enable_job_queued_check
3435
ENABLE_METRIC_GITHUB_APP_RATE_LIMIT = var.metrics.enable && var.metrics.metric.enable_github_app_rate_limit

modules/runners/variables.tf

+10-4
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,16 @@ variable "enable_ephemeral_runners" {
526526
default = false
527527
}
528528

529-
variable "enable_dynamic_ec2_types" {
530-
description = "Enable dynamic EC2 instance types based on workflow job labels. When enabled, jobs can request specific instance types via the 'gh:ec2:instance-type' label."
531-
type = bool
532-
default = false
529+
variable "dynamic_ec2" {
530+
description = "Configuration for dynamic EC2 instance types feature."
531+
type = object({
532+
enable_types = bool
533+
workflow_label_type_prefix = string
534+
})
535+
default = {
536+
enable_types = false
537+
workflow_label_type_prefix = "ghr-ec2-"
538+
}
533539
}
534540

535541
variable "enable_job_queued_check" {

variables.tf

+10-4
Original file line numberDiff line numberDiff line change
@@ -647,10 +647,16 @@ variable "enable_ephemeral_runners" {
647647
default = false
648648
}
649649

650-
variable "enable_dynamic_ec2_types" {
651-
description = "Enable dynamic EC2 instance types based on workflow job labels. When enabled, jobs can request specific instance types via the 'gh-ec2-instance-type' label (e.g., 'gh-ec2-t3.large')."
652-
type = bool
653-
default = false
650+
variable "dynamic_ec2" {
651+
description = "Configuration for dynamic EC2 instance types feature."
652+
type = object({
653+
enable_types = bool
654+
workflow_label_type_prefix = string
655+
})
656+
default = {
657+
enable_types = false
658+
workflow_label_type_prefix = "ghr-ec2-"
659+
}
654660
}
655661

656662
variable "enable_job_queued_check" {

0 commit comments

Comments
 (0)