Skip to content

Commit 6d73e99

Browse files
jonmcewennpalmgithub-actions[bot]
authored
feat: allow setting runners.docker.services (#491)
* fix: Pass token to metadata service requests. close: #476 * docs: fix generation tf docs * docs: auto update terraform docs * feat: allow setting runners.docker.services. #489 * add description to new variable * Comment out docker service in example. Improve docs * remove accidentally added file Co-authored-by: Niek Palm <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent bcb0c0e commit 6d73e99

File tree

7 files changed

+86
-29
lines changed

7 files changed

+86
-29
lines changed

examples/runner-default/README.md

+30-29
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ This examples shows:
88
- You can log into the instance via SSM (Session Manager).
99
- Registration via GitLab token.
1010
- Auto scaling using `docker+machine` executor.
11-
- Addtional security groups that are allowed access to the runner agent
11+
- Additional security groups that are allowed access to the runner agent
12+
- Use of `runners.docker.services` to configure docker registry mirror (commented out - uncomment to apply)
1213

1314
![runners-default](https://github.com/npalm/assets/raw/main/images/terraform-aws-gitlab-runner/runner-default.png)
1415

@@ -41,47 +42,47 @@ No output.
4142
<!-- BEGIN_TF_DOCS -->
4243
## Requirements
4344

44-
| Name | Version |
45-
|------|---------|
46-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1 |
47-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 4.7 |
48-
| <a name="requirement_local"></a> [local](#requirement\_local) | ~> 2 |
49-
| <a name="requirement_null"></a> [null](#requirement\_null) | ~> 3.0 |
50-
| <a name="requirement_random"></a> [random](#requirement\_random) | ~> 3.0 |
51-
| <a name="requirement_tls"></a> [tls](#requirement\_tls) | ~> 3 |
45+
| Name | Version |
46+
| ------------------------------------------------------------------------- | ------- |
47+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1 |
48+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 4.7 |
49+
| <a name="requirement_local"></a> [local](#requirement\_local) | ~> 2 |
50+
| <a name="requirement_null"></a> [null](#requirement\_null) | ~> 3.0 |
51+
| <a name="requirement_random"></a> [random](#requirement\_random) | ~> 3.0 |
52+
| <a name="requirement_tls"></a> [tls](#requirement\_tls) | ~> 3 |
5253

5354
## Providers
5455

55-
| Name | Version |
56-
|------|---------|
57-
| <a name="provider_aws"></a> [aws](#provider\_aws) | ~> 4.7 |
58-
| <a name="provider_null"></a> [null](#provider\_null) | ~> 3.0 |
56+
| Name | Version |
57+
| ---------------------------------------------------- | ------- |
58+
| <a name="provider_aws"></a> [aws](#provider\_aws) | ~> 4.7 |
59+
| <a name="provider_null"></a> [null](#provider\_null) | ~> 3.0 |
5960

6061
## Modules
6162

62-
| Name | Source | Version |
63-
|------|--------|---------|
64-
| <a name="module_runner"></a> [runner](#module\_runner) | ../../ | n/a |
65-
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | 2.70 |
63+
| Name | Source | Version |
64+
| ------------------------------------------------------ | ----------------------------- | ------- |
65+
| <a name="module_runner"></a> [runner](#module\_runner) | ../../ | n/a |
66+
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | 2.70 |
6667

6768
## Resources
6869

69-
| Name | Type |
70-
|------|------|
71-
| [null_resource.cancel_spot_requests](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
70+
| Name | Type |
71+
| ------------------------------------------------------------------------------------------------------------------------------------- | ----------- |
72+
| [null_resource.cancel_spot_requests](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
7273
| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
73-
| [aws_security_group.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/security_group) | data source |
74+
| [aws_security_group.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/security_group) | data source |
7475

7576
## Inputs
7677

77-
| Name | Description | Type | Default | Required |
78-
|------|-------------|------|---------|:--------:|
79-
| <a name="input_aws_region"></a> [aws\_region](#input\_aws\_region) | AWS region. | `string` | `"eu-west-1"` | no |
80-
| <a name="input_environment"></a> [environment](#input\_environment) | A name that identifies the environment, will used as prefix and for tagging. | `string` | `"runners-default"` | no |
81-
| <a name="input_gitlab_url"></a> [gitlab\_url](#input\_gitlab\_url) | URL of the gitlab instance to connect to. | `string` | `"https://gitlab.com"` | no |
82-
| <a name="input_registration_token"></a> [registration\_token](#input\_registration\_token) | n/a | `any` | n/a | yes |
83-
| <a name="input_runner_name"></a> [runner\_name](#input\_runner\_name) | Name of the runner, will be used in the runner config.toml | `string` | `"default-auto"` | no |
84-
| <a name="input_timezone"></a> [timezone](#input\_timezone) | Name of the timezone that the runner will be used in. | `string` | `"Europe/Amsterdam"` | no |
78+
| Name | Description | Type | Default | Required |
79+
| ------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------- | -------- | ---------------------- | :------: |
80+
| <a name="input_aws_region"></a> [aws\_region](#input\_aws\_region) | AWS region. | `string` | `"eu-west-1"` | no |
81+
| <a name="input_environment"></a> [environment](#input\_environment) | A name that identifies the environment, will used as prefix and for tagging. | `string` | `"runners-default"` | no |
82+
| <a name="input_gitlab_url"></a> [gitlab\_url](#input\_gitlab\_url) | URL of the gitlab instance to connect to. | `string` | `"https://gitlab.com"` | no |
83+
| <a name="input_registration_token"></a> [registration\_token](#input\_registration\_token) | n/a | `any` | n/a | yes |
84+
| <a name="input_runner_name"></a> [runner\_name](#input\_runner\_name) | Name of the runner, will be used in the runner config.toml | `string` | `"default-auto"` | no |
85+
| <a name="input_timezone"></a> [timezone](#input\_timezone) | Name of the timezone that the runner will be used in. | `string` | `"Europe/Amsterdam"` | no |
8586

8687
## Outputs
8788

examples/runner-default/main.tf

+31
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,37 @@ module "runner" {
9494
EOT
9595

9696
runners_post_build_script = "\"echo 'single line'\""
97+
98+
# Uncomment the HCL code below to configure a docker service so that registry mirror is used in auto-devops jobs
99+
# See https://gitlab.com/gitlab-org/gitlab-runner/-/issues/27171 and https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#the-service-in-the-gitlab-runner-configuration-file
100+
# You can check this works with a CI job like:
101+
# <pre>
102+
# default:
103+
# tags:
104+
# - "docker_spot_runner"
105+
# docker-mirror-check:
106+
# image: docker:20.10.16
107+
# stage: build
108+
# variables:
109+
# DOCKER_TLS_CERTDIR: ''
110+
# script:
111+
# - |
112+
# - docker info
113+
# if ! docker info | grep -i mirror
114+
# then
115+
# exit 1
116+
# echo "No mirror config found"
117+
# fi
118+
# </pre>
119+
#
120+
# If not using an official docker image for your job, you may need to specify `DOCKER_HOST: tcp://docker:2375`
121+
## UNCOMMENT 6 LINES BELOW
122+
# runners_docker_services = [{
123+
# name = "docker:20.10.16-dind"
124+
# alias = "docker"
125+
# command = ["--registry-mirror", "https://mirror.gcr.io"]
126+
# entrypoint = ["dockerd-entrypoint.sh"]
127+
# }]
97128
}
98129

99130
resource "null_resource" "cancel_spot_requests" {

locals.tf

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ locals {
3434
}
3535
)
3636

37+
runners_docker_services = templatefile("${path.module}/template/runners_docker_services.tpl", {
38+
runners_docker_services = var.runners_docker_services
39+
}
40+
)
41+
3742
runners_pull_policies = var.runners_pull_policy != "" ? "[\"${var.runners_pull_policy}\"]" : "[\"${join("\",\"", var.runners_pull_policies)}\"]"
3843

3944
/* determines if the docker machine executable adds the Name tag automatically (versions >= 0.16.2) */

main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ locals {
126126
runners_check_interval = var.runners_check_interval
127127
runners_volumes_tmpfs = join("\n", [for v in var.runners_volumes_tmpfs : format("\"%s\" = \"%s\"", v.volume, v.options)])
128128
runners_services_volumes_tmpfs = join("\n", [for v in var.runners_services_volumes_tmpfs : format("\"%s\" = \"%s\"", v.volume, v.options)])
129+
runners_docker_services = local.runners_docker_services
129130
bucket_name = local.bucket_name
130131
shared_cache = var.cache_shared
131132
sentry_dsn = var.sentry_dsn

template/runner-config.tpl

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ listen_address = "${prometheus_listen_address}"
2828
pull_policy = ${runners_pull_policies}
2929
runtime = "${runners_docker_runtime}"
3030
helper_image = "${runners_helper_image}"
31+
${runners_docker_services}
3132
[runners.docker.tmpfs]
3233
${runners_volumes_tmpfs}
3334
[runners.docker.services_tmpfs]

template/runners_docker_services.tpl

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
%{ for config in runners_docker_services ~}
2+
[[runners.docker.services]]
3+
name = "${config.name}"
4+
alias = "${config.alias}"
5+
entrypoint = [${replace(format("\"%s\"", join("\",\"", config.entrypoint)), "/\"{2,}/", "\"")}]
6+
command = [${replace(format("\"%s\"", join("\",\"", config.command)), "/\"{2,}/", "\"")}]
7+
%{ endfor ~}

variables.tf

+11
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,17 @@ variable "runners_services_volumes_tmpfs" {
696696
default = []
697697
}
698698

699+
variable "runners_docker_services" {
700+
description = "adds `runners.docker.services` blocks to config.toml. All fields must be set (examine the Dockerfile of the service image for the entrypoint - see ./examples/runner-default/main.tf)"
701+
type = list(object({
702+
name = string
703+
alias = string
704+
entrypoint = list(string)
705+
command = list(string)
706+
}))
707+
default = []
708+
}
709+
699710
variable "kms_key_id" {
700711
description = "KMS key id to encrypted the CloudWatch logs. Ensure CloudWatch has access to the provided KMS key."
701712
type = string

0 commit comments

Comments
 (0)