Skip to content

Commit 81d707c

Browse files
halilkayaHalil Kayakayman-mk
authored
refactor: avoid creating some resources if runners_executor is not docker+machine (#369)
No need to create the some resources that are directly relevant to docker machine since variable runners_executor does not have value docker+machine. Co-authored-by: Halil Kaya <[email protected]> Co-authored-by: Matthias Kay <[email protected]> Co-authored-by: kayma <[email protected]>
1 parent fe291e2 commit 81d707c

File tree

3 files changed

+52
-35
lines changed

3 files changed

+52
-35
lines changed

Diff for: main.tf

+25-18
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ locals {
8787
runners_aws_zone = data.aws_availability_zone.runners.name_suffix
8888
runners_instance_type = var.docker_machine_instance_type
8989
runners_spot_price_bid = var.docker_machine_spot_price_bid == "on-demand-price" ? "" : var.docker_machine_spot_price_bid
90-
runners_ami = data.aws_ami.docker_machine.id
91-
runners_security_group_name = aws_security_group.docker_machine.name
90+
runners_ami = var.runners_executor == "docker+machine" ? data.aws_ami.docker-machine[0].id : ""
91+
runners_security_group_name = var.runners_executor == "docker+machine" ? aws_security_group.docker_machine[0].name : ""
9292
runners_monitoring = var.runners_monitoring
9393
runners_ebs_optimized = var.runners_ebs_optimized
94-
runners_instance_profile = aws_iam_instance_profile.docker_machine.name
94+
runners_instance_profile = var.runners_executor == "docker+machine" ? aws_iam_instance_profile.docker_machine[0].name : ""
9595
runners_additional_volumes = local.runners_additional_volumes
9696
docker_machine_options = length(local.docker_machine_options_string) == 1 ? "" : local.docker_machine_options_string
9797
docker_machine_name = format("%s-%s", local.runner_tags_merged["Name"], "%s") # %s is always needed
@@ -137,7 +137,9 @@ locals {
137137
)
138138
}
139139

140-
data "aws_ami" "docker_machine" {
140+
data "aws_ami" "docker-machine" {
141+
count = var.runners_executor == "docker+machine" ? 1 : 0
142+
141143
most_recent = "true"
142144

143145
dynamic "filter" {
@@ -343,7 +345,8 @@ resource "aws_iam_instance_profile" "instance" {
343345
}
344346

345347
resource "aws_iam_role" "instance" {
346-
count = var.create_runner_iam_role ? 1 : 0
348+
count = var.create_runner_iam_role ? 1 : 0
349+
347350
name = local.aws_iam_role_instance_name
348351
assume_role_policy = length(var.instance_role_json) > 0 ? var.instance_role_json : templatefile("${path.module}/policies/instance-role-trust-policy.json", {})
349352
permissions_boundary = var.permissions_boundary == "" ? null : "arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:policy/${var.permissions_boundary}"
@@ -356,22 +359,23 @@ resource "aws_iam_role" "instance" {
356359
### iam:PassRole To pass the role from the agent to the docker machine runners
357360
################################################################################
358361
resource "aws_iam_policy" "instance_docker_machine_policy" {
359-
count = var.create_runner_iam_role ? 1 : 0
362+
count = var.runners_executor == "docker+machine" && var.create_runner_iam_role ? 1 : 0
363+
360364
name = "${local.name_iam_objects}-docker-machine"
361365
path = "/"
362366
description = "Policy for docker machine."
363367
policy = templatefile("${path.module}/policies/instance-docker-machine-policy.json",
364368
{
365-
docker_machine_role_arn = aws_iam_role.docker_machine.arn
369+
docker_machine_role_arn = aws_iam_role.docker_machine[0].arn
366370
})
367371
tags = local.tags
368372
}
369373

370374
resource "aws_iam_role_policy_attachment" "instance_docker_machine_policy" {
371-
count = var.create_runner_iam_role ? 1 : 0
375+
count = var.runners_executor == "docker+machine" && var.create_runner_iam_role ? 1 : 0
372376

373-
role = local.aws_iam_role_instance_name
374-
policy_arn = aws_iam_policy.instance_docker_machine_policy[count.index].arn
377+
role = aws_iam_role.instance[0].name
378+
policy_arn = aws_iam_policy.instance_docker_machine_policy[0].arn
375379
}
376380

377381
################################################################################
@@ -418,7 +422,7 @@ resource "aws_iam_role_policy_attachment" "docker_machine_cache_instance" {
418422
/* If the S3 cache adapter is configured to use an IAM instance profile, the
419423
adapter uses the profile attached to the GitLab Runner machine. So do not
420424
use aws_iam_role.docker_machine.name here! See https://docs.gitlab.com/runner/configuration/advanced-configuration.html */
421-
count = var.cache_bucket["create"] || length(lookup(var.cache_bucket, "policy", "")) > 0 ? 1 : 0
425+
count = var.runners_executor == "docker+machine" ? (var.cache_bucket["create"] || lookup(var.cache_bucket, "policy", "") != "" ? 1 : 0) : 0
422426

423427
role = local.aws_iam_role_instance_name
424428
policy_arn = local.bucket_policy
@@ -428,32 +432,35 @@ resource "aws_iam_role_policy_attachment" "docker_machine_cache_instance" {
428432
### docker machine instance policy
429433
################################################################################
430434
resource "aws_iam_role" "docker_machine" {
435+
count = var.runners_executor == "docker+machine" ? 1 : 0
431436
name = "${local.name_iam_objects}-docker-machine"
432437
assume_role_policy = length(var.docker_machine_role_json) > 0 ? var.docker_machine_role_json : templatefile("${path.module}/policies/instance-role-trust-policy.json", {})
433438
permissions_boundary = var.permissions_boundary == "" ? null : "arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:policy/${var.permissions_boundary}"
434439
tags = local.tags
435440
}
436441

437442
resource "aws_iam_instance_profile" "docker_machine" {
438-
name = "${local.name_iam_objects}-docker-machine"
439-
role = aws_iam_role.docker_machine.name
440-
tags = local.tags
443+
count = var.runners_executor == "docker+machine" ? 1 : 0
444+
name = "${local.name_iam_objects}-docker-machine"
445+
role = aws_iam_role.docker_machine[0].name
446+
tags = local.tags
441447
}
442448

443449
################################################################################
444450
### Add user defined policies
445451
################################################################################
446452
resource "aws_iam_role_policy_attachment" "docker_machine_user_defined_policies" {
447-
count = length(var.docker_machine_iam_policy_arns)
448-
role = aws_iam_role.docker_machine.name
453+
count = var.runners_executor == "docker+machine" ? length(var.docker_machine_iam_policy_arns) : 0
454+
455+
role = aws_iam_role.docker_machine[0].name
449456
policy_arn = var.docker_machine_iam_policy_arns[count.index]
450457
}
451458

452459
################################################################################
453460
resource "aws_iam_role_policy_attachment" "docker_machine_session_manager_aws_managed" {
454-
count = var.enable_docker_machine_ssm_access ? 1 : 0
461+
count = (var.runners_executor == "docker+machine" && var.enable_docker_machine_ssm_access) ? 1 : 0
455462

456-
role = aws_iam_role.docker_machine.name
463+
role = aws_iam_role.docker_machine[0].name
457464
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AmazonSSMManagedInstanceCore"
458465
}
459466

Diff for: outputs.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ output "runner_agent_role_name" {
2525

2626
output "runner_role_arn" {
2727
description = "ARN of the role used for the docker machine runners."
28-
value = aws_iam_role.docker_machine.arn
28+
value = element(concat(aws_iam_role.docker_machine.*.arn, [""]), 0)
2929
}
3030

3131
output "runner_role_name" {
3232
description = "Name of the role used for the docker machine runners."
33-
value = aws_iam_role.docker_machine.name
33+
value = element(concat(aws_iam_role.docker_machine.*.name, [""]), 0)
3434
}
3535

3636
output "runner_agent_sg_id" {
@@ -40,7 +40,7 @@ output "runner_agent_sg_id" {
4040

4141
output "runner_sg_id" {
4242
description = "ID of the security group attached to the docker machine runners."
43-
value = aws_security_group.docker_machine.id
43+
value = element(concat(aws_security_group.docker_machine.*.id, [""]), 0)
4444
}
4545

4646
output "runner_eip" {

Diff for: security_groups.tf

+24-14
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ resource "aws_security_group_rule" "runner_ping_group" {
6464
########################################
6565

6666
resource "aws_security_group" "docker_machine" {
67+
count = var.runners_executor == "docker+machine" ? 1 : 0
6768
name_prefix = "${local.name_sg}-docker-machine"
6869
vpc_id = var.vpc_id
6970
description = var.docker_machine_security_group_description
@@ -103,18 +104,20 @@ resource "aws_security_group" "docker_machine" {
103104

104105
# Allow docker-machine traffic from gitlab-runner agent instances to docker-machine instances
105106
resource "aws_security_group_rule" "docker_machine_docker_runner" {
107+
count = var.runners_executor == "docker+machine" ? 1 : 0
108+
106109
type = "ingress"
107110
from_port = 2376
108111
to_port = 2376
109112
protocol = "tcp"
110113

111114
source_security_group_id = aws_security_group.runner.id
112-
security_group_id = aws_security_group.docker_machine.id
115+
security_group_id = aws_security_group.docker_machine[0].id
113116

114117
description = format(
115118
"Allow docker-machine traffic from group %s to docker-machine instances in group %s",
116119
aws_security_group.runner.name,
117-
aws_security_group.docker_machine.name
120+
aws_security_group.docker_machine[0].name
118121
)
119122
}
120123

@@ -130,37 +133,39 @@ locals {
130133

131134
# Allow SSH traffic from gitlab-runner agent instances and security group IDs to docker-machine instances
132135
resource "aws_security_group_rule" "docker_machine_ssh_runner" {
136+
count = var.runners_executor == "docker+machine" ? 1 : 0
137+
133138
type = "ingress"
134139
from_port = 22
135140
to_port = 22
136141
protocol = "tcp"
137142

138143
source_security_group_id = aws_security_group.runner.id
139-
security_group_id = aws_security_group.docker_machine.id
144+
security_group_id = aws_security_group.docker_machine[0].id
140145

141146
description = format(
142147
"Allow SSH traffic from %s to docker-machine instances in group %s on port 22",
143148
aws_security_group.runner.id,
144-
aws_security_group.docker_machine.name
149+
aws_security_group.docker_machine[0].name
145150
)
146151
}
147152

148153
# Allow ICMP traffic from gitlab-runner agent instances and security group IDs to docker-machine instances
149154
resource "aws_security_group_rule" "docker_machine_ping_runner" {
150-
count = length(local.security_groups_ping)
155+
count = var.runners_executor == "docker+machine" ? length(local.security_groups_ping) : 0
151156

152157
type = "ingress"
153158
from_port = -1
154159
to_port = -1
155160
protocol = "icmp"
156161

157162
source_security_group_id = element(local.security_groups_ping, count.index)
158-
security_group_id = aws_security_group.docker_machine.id
163+
security_group_id = aws_security_group.docker_machine[0].id
159164

160165
description = format(
161166
"Allow ICMP traffic from %s to docker-machine instances in group %s",
162167
element(local.security_groups_ping, count.index),
163-
aws_security_group.docker_machine.name
168+
aws_security_group.docker_machine[0].name
164169
)
165170
}
166171

@@ -170,49 +175,54 @@ resource "aws_security_group_rule" "docker_machine_ping_runner" {
170175

171176
# Allow docker-machine traffic from docker-machine instances to docker-machine instances on port 2376
172177
resource "aws_security_group_rule" "docker_machine_docker_self" {
178+
count = var.runners_executor == "docker+machine" ? 1 : 0
179+
173180
type = "ingress"
174181
from_port = 2376
175182
to_port = 2376
176183
protocol = "tcp"
177184
self = true
178185

179-
security_group_id = aws_security_group.docker_machine.id
186+
security_group_id = aws_security_group.docker_machine[0].id
180187

181188
description = format(
182189
"Allow docker-machine traffic within group %s on port 2376",
183-
aws_security_group.docker_machine.name,
190+
aws_security_group.docker_machine[0].name,
184191
)
185192
}
186193

187194
# Allow SSH traffic from docker-machine instances to docker-machine instances on port 22
188195
resource "aws_security_group_rule" "docker_machine_ssh_self" {
196+
count = var.runners_executor == "docker+machine" ? 1 : 0
197+
189198
type = "ingress"
190199
from_port = 22
191200
to_port = 22
192201
protocol = "tcp"
193202
self = true
194203

195-
security_group_id = aws_security_group.docker_machine.id
204+
security_group_id = aws_security_group.docker_machine[0].id
196205

197206
description = format(
198207
"Allow SSH traffic within group %s on port 22",
199-
aws_security_group.docker_machine.name,
208+
aws_security_group.docker_machine[0].name,
200209
)
201210
}
202211

203212
# Allow ICMP traffic from docker-machine instances to docker-machine instances
204213
resource "aws_security_group_rule" "docker_machine_ping_self" {
205-
count = var.enable_ping ? 1 : 0
214+
count = (var.runners_executor == "docker+machine" && var.enable_ping) ? 1 : 0
215+
206216
type = "ingress"
207217
from_port = -1
208218
to_port = -1
209219
protocol = "icmp"
210220
self = true
211221

212-
security_group_id = aws_security_group.docker_machine.id
222+
security_group_id = aws_security_group.docker_machine[0].id
213223

214224
description = format(
215225
"Allow ICMP traffic within group %s",
216-
aws_security_group.docker_machine.name,
226+
aws_security_group.docker_machine[0].name,
217227
)
218228
}

0 commit comments

Comments
 (0)