Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Commit 772e1a5

Browse files
jgutierrezgleznpalmgithub-actions[bot]
authored
feat(runners): add support to disable default labels (Linux) (#3491)
In case the pool of runners deployed using this module are fully available for an wide GH org (no repository restrictions) any workflow configured to run in runners that contain just default labels in the runs-on definition: e.g. `runs-on: self-hosted` `runs-on: Linux` can end-up running in this pool without knowing it. That's why I have decided to remove the default labels from our runners and just rely on unique custom labels, and I believed the best way to do it by adding a runner_enable_default_labels variable - that by default is true (so, it doesn't change the current behavior), but it can help other people to deal with similar issues like the one described above. --------- Co-authored-by: Niek Palm <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Niek Palm <[email protected]>
1 parent 78b910e commit 772e1a5

File tree

11 files changed

+55
-12
lines changed

11 files changed

+55
-12
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,10 @@ Talk to the forestkeepers in the `runners-channel` on Slack.
207207
| <a name="input_runner_binaries_syncer_lambda_zip"></a> [runner\_binaries\_syncer\_lambda\_zip](#input\_runner\_binaries\_syncer\_lambda\_zip) | File location of the binaries sync lambda zip file. | `string` | `null` | no |
208208
| <a name="input_runner_boot_time_in_minutes"></a> [runner\_boot\_time\_in\_minutes](#input\_runner\_boot\_time\_in\_minutes) | The minimum time for an EC2 runner to boot and register as a runner. | `number` | `5` | no |
209209
| <a name="input_runner_credit_specification"></a> [runner\_credit\_specification](#input\_runner\_credit\_specification) | The credit option for CPU usage of a T instance. Can be unset, "standard" or "unlimited". | `string` | `null` | no |
210+
| <a name="input_runner_disable_default_labels"></a> [runner\_disable\_default\_labels](#input\_runner\_disable\_default\_labels) | Disable default labels for the runners (os, architecture and `self-hosted`). If enabled, the runner will only have the extra labels provided in `runner_extra_labels`. In case you on own start script is used, this configuration parameter needs to be parsed via SSM. For Windows no support yet. | `bool` | `false` | no |
210211
| <a name="input_runner_ec2_tags"></a> [runner\_ec2\_tags](#input\_runner\_ec2\_tags) | Map of tags that will be added to the launch template instance tag specifications. | `map(string)` | `{}` | no |
211212
| <a name="input_runner_egress_rules"></a> [runner\_egress\_rules](#input\_runner\_egress\_rules) | List of egress rules for the GitHub runner instances. | <pre>list(object({<br/> cidr_blocks = list(string)<br/> ipv6_cidr_blocks = list(string)<br/> prefix_list_ids = list(string)<br/> from_port = number<br/> protocol = string<br/> security_groups = list(string)<br/> self = bool<br/> to_port = number<br/> description = string<br/> }))</pre> | <pre>[<br/> {<br/> "cidr_blocks": [<br/> "0.0.0.0/0"<br/> ],<br/> "description": null,<br/> "from_port": 0,<br/> "ipv6_cidr_blocks": [<br/> "::/0"<br/> ],<br/> "prefix_list_ids": null,<br/> "protocol": "-1",<br/> "security_groups": null,<br/> "self": null,<br/> "to_port": 0<br/> }<br/>]</pre> | no |
212-
| <a name="input_runner_extra_labels"></a> [runner\_extra\_labels](#input\_runner\_extra\_labels) | Extra (custom) labels for the runners (GitHub). Labels checks on the webhook can be enforced by setting `enable_runner_workflow_job_labels_check_all`. GitHub read-only labels should not be provided. | `list(string)` | `[]` | no |
213+
| <a name="input_runner_extra_labels"></a> [runner\_extra\_labels](#input\_runner\_extra\_labels) | Extra (custom) labels for the runners (GitHub). Separate each label by a comma. Labels checks on the webhook can be enforced by setting `enable_workflow_job_labels_check`. GitHub read-only labels should not be provided. | `list(string)` | `[]` | no |
213214
| <a name="input_runner_group_name"></a> [runner\_group\_name](#input\_runner\_group\_name) | Name of the runner group. | `string` | `"Default"` | no |
214215
| <a name="input_runner_iam_role_managed_policy_arns"></a> [runner\_iam\_role\_managed\_policy\_arns](#input\_runner\_iam\_role\_managed\_policy\_arns) | Attach AWS or customer-managed IAM policies (by ARN) to the runner IAM role | `list(string)` | `[]` | no |
215216
| <a name="input_runner_log_files"></a> [runner\_log\_files](#input\_runner\_log\_files) | (optional) Replaces the module default cloudwatch log config. See https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html for details. | <pre>list(object({<br/> log_group_name = string<br/> prefix_log_group = bool<br/> file_path = string<br/> log_stream_name = string<br/> }))</pre> | `null` | no |

docs/configuration.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ To be able to support a number of use-cases, the module has quite a lot of confi
1313
- GitHub Cloud vs GitHub Enterprise Server (GHES). The runners support GitHub Cloud as well GitHub Enterprise Server. For GHES, we rely on our community for support and testing. We at Philips have no capability to test GHES ourselves.
1414
- Spot vs on-demand. The runners use either the EC2 spot or on-demand life cycle. Runners will be created via the AWS [CreateFleet API](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateFleet.html). The module (scale up lambda) will request via the CreateFleet API to create instances in one of the subnets and of the specified instance types.
1515
- ARM64 support via Graviton/Graviton2 instance-types. When using the default example or top-level module, specifying `instance_types` that match a Graviton/Graviton 2 (ARM64) architecture (e.g. a1, t4g or any 6th-gen `g` or `gd` type), you must also specify `runner_architecture = "arm64"` and the sub-modules will be automatically configured to provision with ARM64 AMIs and leverage GitHub's ARM64 action runner. See below for more details.
16+
- Disable default labels for the runners (os, architecture and `self-hosted`) can achieve by setting `runner_disable_default_labels` = true. If enabled, the runner will only have the extra labels provided in `runner_extra_labels`. In case you on own start script is used, this configuration parameter needs to be parsed via SSM. For Windows no support yet.
1617

1718
## AWS SSM Parameters
1819

@@ -24,14 +25,16 @@ The module uses the AWS System Manager Parameter Store to store configuration fo
2425
| `ssm_paths.root/var.prefix?/runners/config/<name>` | Configuration parameters used by runner start script |
2526
| `ssm_paths.root/var.prefix?/runners/tokens/<ec2-instance-id>` | Either JIT configuration (ephemeral runners) or registration tokens (non ephemeral runners) generated by the control plane (scale-up lambda), and consumed by the start script on the runner to activate / register the runner. |
2627
| `ssm_paths.root/var.prefix?/webhook/runner-matcher-config` | Runner matcher config used by webhook to decide the target for the webhook event. |
28+
2729
Available configuration parameters:
2830

29-
| Parameter name | Description |
30-
| ------------------- | ----------------------------------------------------------- |
31-
| `agent_mode` | Indicates if the agent is running in ephemeral mode or not. |
32-
| `enable_cloudwatch` | Configuration for the cloudwatch agent to stream logging. |
33-
| `run_as` | The user used for running the GitHub action runner agent. |
34-
| `token_path` | The path where tokens are stored. |
31+
| Parameter name | Description |
32+
|-------------------------------------|---------------------------------------------------------------------------------------------------|
33+
| `agent_mode` | Indicates if the agent is running in ephemeral mode or not. |
34+
| `disable_default_labels` | Indicates if the default labels for the runners (os, architecture and `self-hosted`) are disabled |
35+
| `enable_cloudwatch` | Configuration for the cloudwatch agent to stream logging. |
36+
| `run_as` | The user used for running the GitHub action runner agent. |
37+
| `token_path` | The path where tokens are stored. |
3538

3639
## Encryption
3740

main.tf

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ locals {
88
key_base64 = module.ssm.parameters.github_app_key_base64
99
}
1010

11-
runner_labels = sort(distinct(concat(["self-hosted", var.runner_os, var.runner_architecture], var.runner_extra_labels)))
11+
default_runner_labels = distinct(concat(["self-hosted", var.runner_os, var.runner_architecture]))
12+
runner_labels = (var.runner_disable_default_labels == false) ? sort(concat(local.default_runner_labels, var.runner_extra_labels)) : var.runner_extra_labels
1213

1314
ssm_root_path = var.ssm_paths.use_prefix ? "/${var.ssm_paths.root}/${var.prefix}" : "/${var.ssm_paths.root}"
1415
}
@@ -199,6 +200,7 @@ module "runners" {
199200
scale_down_schedule_expression = var.scale_down_schedule_expression
200201
minimum_running_time_in_minutes = var.minimum_running_time_in_minutes
201202
runner_boot_time_in_minutes = var.runner_boot_time_in_minutes
203+
runner_disable_default_labels = var.runner_disable_default_labels
202204
runner_labels = local.runner_labels
203205
runner_as_root = var.runner_as_root
204206
runner_run_as = var.runner_run_as

modules/multi-runner/README.md

+1-1
Large diffs are not rendered by default.

modules/multi-runner/runners.tf

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ module "runners" {
4545
scale_down_schedule_expression = each.value.runner_config.scale_down_schedule_expression
4646
minimum_running_time_in_minutes = each.value.runner_config.minimum_running_time_in_minutes
4747
runner_boot_time_in_minutes = each.value.runner_config.runner_boot_time_in_minutes
48-
runner_labels = sort(distinct(concat(["self-hosted", each.value.runner_config.runner_os, each.value.runner_config.runner_architecture], each.value.runner_config.runner_extra_labels)))
48+
runner_disable_default_labels = each.value.runner_config.runner_disable_default_labels
49+
runner_labels = each.value.runner_config.runner_disable_default_labels ? sort(distinct(each.value.runner_config.runner_extra_labels)) : sort(distinct(concat(["self-hosted", each.value.runner_config.runner_os, each.value.runner_config.runner_architecture], each.value.runner_config.runner_extra_labels)))
4950
runner_as_root = each.value.runner_config.runner_as_root
5051
runner_run_as = each.value.runner_config.runner_run_as
5152
runners_maximum_count = each.value.runner_config.runners_maximum_count

modules/multi-runner/variables.tf

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ variable "multi_runner_config" {
6161
pool_runner_owner = optional(string, null)
6262
runner_as_root = optional(bool, false)
6363
runner_boot_time_in_minutes = optional(number, 5)
64+
runner_disable_default_labels = optional(bool, false)
6465
runner_extra_labels = optional(list(string), [])
6566
runner_group_name = optional(string, "Default")
6667
runner_name_prefix = optional(string, "")
@@ -164,6 +165,7 @@ variable "multi_runner_config" {
164165
runner_additional_security_group_ids: "List of additional security groups IDs to apply to the runner. If added outside the multi_runner_config block, the additional security group(s) will be applied to all runner configs. If added inside the multi_runner_config, the additional security group(s) will be applied to the individual runner."
165166
runner_as_root: "Run the action runner under the root user. Variable `runner_run_as` will be ignored."
166167
runner_boot_time_in_minutes: "The minimum time for an EC2 runner to boot and register as a runner."
168+
runner_disable_default_labels: "Disable default labels for the runners (os, architecture and `self-hosted`). If enabled, the runner will only have the extra labels provided in `runner_extra_labels`. In case you on own start script is used, this configuration parameter needs to be parsed via SSM. For Windows no support yet."
167169
runner_extra_labels: "Extra (custom) labels for the runners (GitHub). Separate each label by a comma. Labels checks on the webhook can be enforced by setting `multi_runner_config.matcherConfig.exactMatch`. GitHub read-only labels should not be provided."
168170
runner_group_name: "Name of the runner group."
169171
runner_name_prefix: "Prefix for the GitHub runner name."

modules/runners/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ yarn run dist
117117
| [aws_launch_template.runner](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template) | resource |
118118
| [aws_security_group.runner_sg](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
119119
| [aws_ssm_parameter.cloudwatch_agent_config_runner](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_parameter) | resource |
120+
| [aws_ssm_parameter.disable_default_labels](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_parameter) | resource |
120121
| [aws_ssm_parameter.jit_config_enabled](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_parameter) | resource |
121122
| [aws_ssm_parameter.runner_agent_mode](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_parameter) | resource |
122123
| [aws_ssm_parameter.runner_config_run_as](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_parameter) | resource |
@@ -199,6 +200,7 @@ yarn run dist
199200
| <a name="input_runner_architecture"></a> [runner\_architecture](#input\_runner\_architecture) | The platform architecture of the runner instance\_type. | `string` | `"x64"` | no |
200201
| <a name="input_runner_as_root"></a> [runner\_as\_root](#input\_runner\_as\_root) | Run the action runner under the root user. Variable `runner_run_as` will be ignored. | `bool` | `false` | no |
201202
| <a name="input_runner_boot_time_in_minutes"></a> [runner\_boot\_time\_in\_minutes](#input\_runner\_boot\_time\_in\_minutes) | The minimum time for an EC2 runner to boot and register as a runner. | `number` | `5` | no |
203+
| <a name="input_runner_disable_default_labels"></a> [runner\_disable\_default\_labels](#input\_runner\_disable\_default\_labels) | Disable default labels for the runners (os, architecture and `self-hosted`). If enabled, the runner will only have the extra labels provided in `runner_extra_labels`. | `bool` | `false` | no |
202204
| <a name="input_runner_ec2_tags"></a> [runner\_ec2\_tags](#input\_runner\_ec2\_tags) | Map of tags that will be added to the launch template instance tag specifications. | `map(string)` | `{}` | no |
203205
| <a name="input_runner_group_name"></a> [runner\_group\_name](#input\_runner\_group\_name) | Name of the runner group. | `string` | `"Default"` | no |
204206
| <a name="input_runner_iam_role_managed_policy_arns"></a> [runner\_iam\_role\_managed\_policy\_arns](#input\_runner\_iam\_role\_managed\_policy\_arns) | Attach AWS or customer-managed IAM policies (by ARN) to the runner IAM role | `list(string)` | `[]` | no |

modules/runners/runner-config.tf

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ resource "aws_ssm_parameter" "runner_agent_mode" {
1212
tags = local.tags
1313
}
1414

15+
resource "aws_ssm_parameter" "disable_default_labels" {
16+
name = "${var.ssm_paths.root}/${var.ssm_paths.config}/disable_default_labels"
17+
type = "String"
18+
value = var.runner_disable_default_labels
19+
tags = local.tags
20+
}
21+
1522
resource "aws_ssm_parameter" "jit_config_enabled" {
1623
name = "${var.ssm_paths.root}/${var.ssm_paths.config}/enable_jit_config"
1724
type = "String"

modules/runners/templates/start-runner.sh

+9-1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ echo "Retrieved /$ssm_config_path/enable_cloudwatch parameter - ($enable_cloudwa
141141
agent_mode=$(echo "$parameters" | jq --arg ssm_config_path "$ssm_config_path" -r '.[] | select(.Name == "'$ssm_config_path'/agent_mode") | .Value')
142142
echo "Retrieved /$ssm_config_path/agent_mode parameter - ($agent_mode)"
143143

144+
disable_default_labels=$(echo "$parameters" | jq --arg ssm_config_path "$ssm_config_path" -r '.[] | select(.Name == "'$ssm_config_path'/disable_default_labels") | .Value')
145+
echo "Retrieved /$ssm_config_path/disable_default_labels parameter - ($disable_default_labels)"
146+
144147
enable_jit_config=$(echo "$parameters" | jq --arg ssm_config_path "$ssm_config_path" -r '.[] | select(.Name == "'$ssm_config_path'/enable_jit_config") | .Value')
145148
echo "Retrieved /$ssm_config_path/enable_jit_config parameter - ($enable_jit_config)"
146149

@@ -216,7 +219,12 @@ echo "Starting the runner as user $run_as"
216219
# configure the runner if the runner is non ephemeral or jit config is disabled
217220
if [[ "$enable_jit_config" == "false" || $agent_mode != "ephemeral" ]]; then
218221
echo "Configure GH Runner as user $run_as"
219-
sudo --preserve-env=RUNNER_ALLOW_RUNASROOT -u "$run_as" -- ./config.sh --unattended --name "$runner_name_prefix$instance_id" --work "_work" $${config}
222+
if [[ "$disable_default_labels" == "true" ]]; then
223+
extra_flags="--no-default-labels"
224+
else
225+
extra_flags=""
226+
fi
227+
sudo --preserve-env=RUNNER_ALLOW_RUNASROOT -u "$run_as" -- ./config.sh $${extra_flags} --unattended --name "$runner_name_prefix$instance_id" --work "_work" $${config}
220228
fi
221229

222230
create_xray_success_segment "$SEGMENT"

modules/runners/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ variable "runner_boot_time_in_minutes" {
217217
default = 5
218218
}
219219

220+
variable "runner_disable_default_labels" {
221+
description = "Disable default labels for the runners (os, architecture and `self-hosted`). If enabled, the runner will only have the extra labels provided in `runner_extra_labels`."
222+
type = bool
223+
default = false
224+
}
225+
220226
variable "runner_labels" {
221227
description = "All the labels for the runners (GitHub) including the default one's(e.g: self-hosted, linux, x64, label1, label2). Separate each label by a comma"
222228
type = list(string)

variables.tf

+12-1
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,21 @@ variable "runner_boot_time_in_minutes" {
5858
default = 5
5959
}
6060

61+
variable "runner_disable_default_labels" {
62+
description = "Disable default labels for the runners (os, architecture and `self-hosted`). If enabled, the runner will only have the extra labels provided in `runner_extra_labels`. In case you on own start script is used, this configuration parameter needs to be parsed via SSM. For Windows no support yet."
63+
type = bool
64+
default = false
65+
}
66+
6167
variable "runner_extra_labels" {
62-
description = "Extra (custom) labels for the runners (GitHub). Labels checks on the webhook can be enforced by setting `enable_runner_workflow_job_labels_check_all`. GitHub read-only labels should not be provided."
68+
description = "Extra (custom) labels for the runners (GitHub). Separate each label by a comma. Labels checks on the webhook can be enforced by setting `enable_workflow_job_labels_check`. GitHub read-only labels should not be provided."
6369
type = list(string)
6470
default = []
71+
72+
validation {
73+
condition = var.runner_extra_labels != null
74+
error_message = "Extra labels should not be null."
75+
}
6576
}
6677

6778
variable "runner_group_name" {

0 commit comments

Comments
 (0)