Skip to content

Commit 49a37d5

Browse files
kayman-mktmeijn
andcommitted
refactor!: group variables for better overview (#810)
Groups variables into objects to - reduce the number of variables (currently 118) - to gain a better overview of all configuration settings Creates new groups of variables: - `runner_manager` in case it configures the "main" process which sets the defaults for all runners - `runner` in case it configures the runner created by the runner manager - `runner_worker` in case it configures the docker/docker+machine or leave it as it is, if it is a global scope, e.g. common tags, the environment, ... Yes and a script is provided to do that. It covers 98% of all migrations (see migrations/migrate-to-7-0-0.sh) Please mention the examples you have verified. --------- Co-authored-by: Tyrone Meijn <[email protected]>
1 parent a410d53 commit 49a37d5

File tree

23 files changed

+1630
-1206
lines changed

23 files changed

+1630
-1206
lines changed

.cspell.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
"codeowners",
1919
"companys",
2020
"concat",
21+
"cpu",
22+
"cpus",
23+
"cpuset",
2124
"devskim",
2225
"dind",
2326
"endfor",
27+
"filesha",
2428
"formatlist",
2529
"gitter",
2630
"glrunners",
@@ -45,6 +49,8 @@
4549
"stretchr",
4650
"subkey",
4751
"substr",
52+
"sysctl",
53+
"sysctls",
4854
"templatefile",
4955
"terrascan",
5056
"terratest",
@@ -58,7 +64,9 @@
5864
"trivy",
5965
"typecheck",
6066
"userdata",
61-
"xanzy"
67+
"userns",
68+
"xanzy",
69+
"xvda"
6270
],
6371
"flagWords": []
6472
}

.github/workflows/ci.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ jobs:
137137
run: tflint --init
138138

139139
- name: Run TFLint
140-
run: tflint --var 'enable_kms=true'
140+
# assign necessary variables to avoid errors
141+
run: "tflint --var 'enable_kms=true' --var='runner_instance={\"name_prefix\": \"a\", \"name\": \"b\"}'"
141142

142143
tfsec:
143144
name: tfsec PR commenter

.mega-linter.yml

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ DISABLE_LINTERS:
44
- TERRAFORM_TFLINT
55
# Super slow linter, but useful. We disable it here and run it in parallel to Megalinter saves some minutes.
66
- REPOSITORY_KICS
7+
# has issues with the Terraform code `optional` variable definitions: https://github.com/tenable/terrascan/issues/1532
8+
- TERRAFORM_TERRASCAN
79
# Nice linter to report CVEs and other cool stuff. But it reports problems with the Terraform code which can't be disabled by
810
# configuration.
911
- REPOSITORY_TRIVY

README.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,12 @@ module "runner" {
386386

387387
Since spot instances can be taken over by AWS depending on the instance type and AZ you are using, you may want multiple instances
388388
types in multiple AZs. This is where spot fleets come in, when there is no capacity on one instance type and one AZ, AWS will take
389-
the next instance type and so on. This update has been possible since the [fork](https://gitlab.com/cki-project/docker-machine/-/tree/v0.16.2-gitlab.19-cki.2)
390-
of docker-machine supports spot fleets.
389+
the next instance type and so on. This update has been possible since the
390+
[fork](https://gitlab.com/cki-project/docker-machine/-/tree/v0.16.2-gitlab.19-cki.2) of docker-machine supports spot fleets.
391391

392392
We have seen that the [fork](https://gitlab.com/cki-project/docker-machine/-/tree/v0.16.2-gitlab.19-cki.2) of docker-machine this
393-
module is using consume more RAM using spot fleets.
394-
For comparison, if you launch 50 machines in the same time, it consumes ~1.2GB of RAM. In our case, we had to change the
395-
`instance_type` of the runner from `t3.micro` to `t3.small`.
393+
module is using consume more RAM using spot fleets. For comparison, if you launch 50 machines in the same time, it consumes
394+
~1.2GB of RAM. In our case, we had to change the `instance_type` of the runner from `t3.micro` to `t3.small`.
396395

397396
#### Configuration example
398397

examples/runner-certificates/README.md

+27-20
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,24 @@ Create a PEM-encoded `.crt` file containing the public certificate of your Gitla
3232

3333
```hcl
3434
module {
35-
...
35+
# ...
3636
# Public cert of my companys gitlab instance
37-
runners_gitlab_certificate = file("${path.module}/my_gitlab_instance_cert.crt")
38-
...
37+
runner_gitlab = {
38+
certificate = file("${path.module}/my_gitlab_instance_cert.crt")
39+
}
40+
# ...
3941
}
4042
```
4143

4244
Add your CA and intermediary certs to a second PEM-encoded `.crt` file.
4345
```hcl
4446
module {
45-
...
47+
# ...
4648
# Other public certs relating to my company.
47-
runners_ca_certificate = file("${path.module}/my_company_ca_cert_bundle.crt")
48-
...
49+
runner_gitlab = {
50+
ca_certificate = file("${path.module}/my_company_ca_cert_bundle.crt")
51+
}
52+
# ...
4953
}
5054
```
5155

@@ -58,15 +62,17 @@ For **user images**, you must:
5862
The runner module can be configured to do this step. Configure the module like so:
5963

6064
```terraform
61-
module {
65+
module "runner" {
6266
# ...
6367
6468
# Mount EC2 host certs in docker so all user docker images can reference them.
65-
runners_additional_volumes = ["/etc/gitlab-runner/certs/:/etc/gitlab-runner/certs:ro"]
66-
67-
# ...
69+
runner_worker_docker_options = {
70+
volumes = ["/etc/gitlab-runner/certs/:/etc/gitlab-runner/certs:ro"]
6871
}
69-
```
72+
73+
# ...
74+
}
75+
```
7076
7177
2. Trust the certificates from within the user image.
7278
@@ -107,17 +113,18 @@ For **user images**, you must:
107113
This avoids maintaining the script in each pipeline file, but expects that all user images use the same OS.
108114
109115
```terraform
110-
module {
116+
module "runner" {
111117
# ...
112118
113-
runners_pre_build_script = <<EOT
114-
'''
115-
apt-get install -y ca-certificates
116-
cp /etc/gitlab-runner/certs/* /usr/local/share/ca-certificates/
117-
update-ca-certificates
118-
'''
119-
EOT
120-
119+
runner_worker_gitlab_pipeline = {
120+
pre_build_script = <<EOT
121+
'''
122+
apt-get install -y ca-certificates
123+
cp /etc/gitlab-runner/certs/* /usr/local/share/ca-certificates/
124+
update-ca-certificates
125+
'''
126+
EOT
127+
}
121128
# ...
122129
}
123130
```

examples/runner-certificates/main.tf

+13-13
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,26 @@ module "runner" {
2727
###############################################
2828
# General
2929
###############################################
30-
31-
runners_name = var.runner_name
32-
runners_gitlab_url = var.gitlab_url
33-
34-
runners_executor = "docker"
35-
36-
aws_region = var.aws_region
3730
environment = var.environment
3831

3932
###############################################
4033
# Certificates
4134
###############################################
4235

4336
# Public cert of my companys gitlab instance
44-
runners_gitlab_certificate = file("${path.module}/my_gitlab_instance_cert.crt")
45-
4637
# Other public certs relating to my company.
47-
runners_ca_certificate = file("${path.module}/my_company_ca_cert_bundle.crt")
38+
runner_gitlab = {
39+
url = var.gitlab_url
40+
certificate = file("${path.module}/my_gitlab_instance_cert.crt")
41+
ca_certificate = file("${path.module}/my_company_ca_cert_bundle.crt")
42+
}
4843

4944
# Mount EC2 host certs in docker so all user docker images can reference them.
5045
# Each user image will need to do:
5146
# cp /etc/gitlab-runner/certs/* /usr/local/share/ca-certificates/
5247
# update-ca-certificates
5348
# Or similar OS-dependent commands. The above are an example for Ubuntu.
54-
runners_docker_options = {
49+
runner_worker_docker_options = {
5550
volumes = [
5651
"/cache",
5752
"/etc/gitlab-runner/certs/:/etc/gitlab-runner/certs:ro"
@@ -61,8 +56,7 @@ module "runner" {
6156
###############################################
6257
# Registration
6358
###############################################
64-
65-
gitlab_runner_registration_config = {
59+
runner_gitlab_registration_config = {
6660
registration_token = var.registration_token
6761
tag_list = "docker_runner"
6862
description = "runner docker - auto"
@@ -76,5 +70,11 @@ module "runner" {
7670
###############################################
7771
vpc_id = module.vpc.vpc_id
7872
subnet_id = element(module.vpc.public_subnets, 0)
73+
runner_instance = {
74+
name = var.runner_name
75+
}
7976

77+
runner_worker = {
78+
type = "docker"
79+
}
8080
}

examples/runner-default/main.tf

+39-33
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,26 @@ module "vpc_endpoints" {
5050
module "runner" {
5151
source = "../../"
5252

53-
aws_region = var.aws_region
5453
environment = var.environment
5554

56-
vpc_id = module.vpc.vpc_id
57-
subnet_id = element(module.vpc.private_subnets, 0)
58-
metrics_autoscaling = ["GroupDesiredCapacity", "GroupInServiceCapacity"]
55+
vpc_id = module.vpc.vpc_id
56+
subnet_id = element(module.vpc.private_subnets, 0)
5957

60-
runners_name = var.runner_name
61-
runners_gitlab_url = var.gitlab_url
62-
enable_runner_ssm_access = true
58+
runner_instance = {
59+
collect_autoscaling_metrics = ["GroupDesiredCapacity", "GroupInServiceCapacity"]
60+
name = var.runner_name
61+
ssm_access = true
62+
}
6363

64-
gitlab_runner_security_group_ids = [data.aws_security_group.default.id]
64+
runner_networking = {
65+
allow_incoming_ping_security_group_ids = [data.aws_security_group.default.id]
66+
}
6567

66-
docker_machine_spot_price_bid = "on-demand-price"
68+
runner_gitlab = {
69+
url = var.gitlab_url
70+
}
6771

68-
gitlab_runner_registration_config = {
72+
runner_gitlab_registration_config = {
6973
registration_token = var.registration_token
7074
tag_list = "docker_spot_runner"
7175
description = "runner default - auto"
@@ -74,27 +78,37 @@ module "runner" {
7478
maximum_timeout = "3600"
7579
}
7680

77-
tags = {
78-
"tf-aws-gitlab-runner:example" = "runner-default"
79-
"tf-aws-gitlab-runner:instancelifecycle" = "spot:yes"
81+
runner_worker_gitlab_pipeline = {
82+
pre_build_script = <<EOT
83+
'''
84+
echo 'multiline 1'
85+
echo 'multiline 2'
86+
'''
87+
EOT
88+
post_build_script = "\"echo 'single line'\""
8089
}
8190

82-
runners_volumes_tmpfs = [
91+
runner_worker_docker_options = {
92+
privileged = "true"
93+
volumes = ["/cache", "/certs/client"]
94+
}
95+
96+
runner_worker_docker_volumes_tmpfs = [
8397
{
8498
volume = "/var/opt/cache",
8599
options = "rw,noexec"
86100
}
87101
]
88102

89-
runners_services_volumes_tmpfs = [
103+
runner_worker_docker_services_volumes_tmpfs = [
90104
{
91105
volume = "/var/lib/mysql",
92106
options = "rw,noexec"
93107
}
94108
]
95109

96-
# working 9 to 5 :)
97-
runners_machine_autoscaling_options = [
110+
runner_worker_docker_machine_autoscaling_options = [
111+
# working 9 to 5 :)
98112
{
99113
periods = ["* * 0-9,17-23 * * mon-fri *", "* * * * * sat,sun *"]
100114
idle_count = 0
@@ -103,20 +117,11 @@ module "runner" {
103117
}
104118
]
105119

106-
runners_docker_options = {
107-
privileged = "true"
108-
volumes = ["/cache", "/certs/client"]
120+
tags = {
121+
"tf-aws-gitlab-runner:example" = "runner-default"
122+
"tf-aws-gitlab-runner:instancelifecycle" = "spot:yes"
109123
}
110124

111-
runners_pre_build_script = <<EOT
112-
'''
113-
echo 'multiline 1'
114-
echo 'multiline 2'
115-
'''
116-
EOT
117-
118-
runners_post_build_script = "\"echo 'single line'\""
119-
120125
# Uncomment the HCL code below to configure a docker service so that registry mirror is used in auto-devops jobs
121126
# 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
122127
# You can check this works with a CI job like:
@@ -141,7 +146,7 @@ module "runner" {
141146
#
142147
# If not using an official docker image for your job, you may need to specify `DOCKER_HOST: tcp://docker:2375`
143148
## UNCOMMENT 6 LINES BELOW
144-
# runners_docker_services = [{
149+
# runner_worker_docker_services = [{
145150
# name = "docker:20.10.16-dind"
146151
# alias = "docker"
147152
# command = ["--registry-mirror", "https://mirror.gcr.io"]
@@ -151,7 +156,8 @@ module "runner" {
151156

152157
# Example how to configure runners, to utilize EC2 user-data feature
153158
# example template, creates (configurable) swap file for the runner
154-
# runners_userdata = templatefile("${path.module}/../../templates/swap.tpl", {
155-
# swap_size = "512"
156-
# })
159+
# runner_worker_docker_machine_instance = {
160+
# start_script = templatefile("${path.module}/../../templates/swap.tpl", {
161+
# swap_size = "512"
162+
# }
157163
}

examples/runner-docker/main.tf

+23-12
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,39 @@ module "vpc_endpoints" {
4141
module "runner" {
4242
source = "../../"
4343

44-
aws_region = var.aws_region
44+
vpc_id = module.vpc.vpc_id
45+
subnet_id = element(module.vpc.public_subnets, 0)
4546
environment = var.environment
4647

47-
runners_use_private_address = false
48-
enable_eip = true
49-
50-
docker_machine_security_group_description = "Custom description for docker-machine"
51-
gitlab_runner_security_group_description = "Custom description for gitlab-runner"
48+
runner_instance = {
49+
runner_use_eip = true
50+
name = var.runner_name
51+
}
5252

53-
vpc_id = module.vpc.vpc_id
54-
subnet_id = element(module.vpc.public_subnets, 0)
53+
runner_networking = {
54+
security_group_description = "Custom description for gitlab-runner"
55+
}
5556

56-
runners_executor = "docker"
57-
runners_name = var.runner_name
58-
runners_gitlab_url = var.gitlab_url
57+
runner_gitlab = {
58+
url = var.gitlab_url
59+
}
5960

60-
gitlab_runner_registration_config = {
61+
runner_gitlab_registration_config = {
6162
registration_token = var.registration_token
6263
tag_list = "docker_runner"
6364
description = "runner docker - auto"
6465
locked_to_project = "true"
6566
run_untagged = "false"
6667
maximum_timeout = "3600"
6768
}
69+
70+
runner_worker = {
71+
type = "docker"
72+
}
73+
74+
runner_worker_docker_machine_instance = {
75+
private_address_only = false
76+
}
77+
78+
runner_worker_docker_machine_security_group_description = "Custom description for docker-machine"
6879
}

0 commit comments

Comments
 (0)