Skip to content

Commit bfa1b36

Browse files
committed
Add custom egress rules to docker-autoscaler workers security group. Don't provision docker-machine security group when docker-autoscaler is used.
Signed-off-by: Yevgen Karlashov <[email protected]>
1 parent 4764c72 commit bfa1b36

File tree

3 files changed

+100
-50
lines changed

3 files changed

+100
-50
lines changed

docker_autoscaler.tf

+41-27
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,47 @@
66
resource "aws_security_group" "docker_autoscaler" {
77
count = var.runner_worker.type == "docker-autoscaler" ? 1 : 0
88

9-
description = "Docker autoscaler security group"
9+
name_prefix = "${local.name_sg}-docker-autoscaler"
1010
vpc_id = var.vpc_id
11-
name = "${local.name_sg}-docker-autoscaler"
11+
description = "Docker-autoscaler security group"
12+
13+
dynamic "egress" {
14+
for_each = var.runner_worker_docker_autoscaler_egress_rules
15+
iterator = each
16+
17+
content {
18+
# ok, there is no problem with outgoing data to the internet. It's a user setting
19+
# tfsec:ignore:aws-ec2-no-public-egress-sgr
20+
cidr_blocks = each.value.cidr_blocks
21+
# ok, there is no problem with outgoing data to the internet. It's a user setting
22+
# tfsec:ignore:aws-ec2-no-public-egress-sgr
23+
ipv6_cidr_blocks = each.value.ipv6_cidr_blocks
24+
prefix_list_ids = each.value.prefix_list_ids
25+
from_port = each.value.from_port
26+
protocol = each.value.protocol
27+
security_groups = each.value.security_groups
28+
self = each.value.self
29+
to_port = each.value.to_port
30+
description = each.value.description
31+
}
32+
}
33+
34+
dynamic "ingress" {
35+
for_each = var.runner_worker_docker_autoscaler_ingress_rules
36+
iterator = each
37+
38+
content {
39+
cidr_blocks = each.value.cidr_blocks
40+
ipv6_cidr_blocks = each.value.ipv6_cidr_blocks
41+
prefix_list_ids = each.value.prefix_list_ids
42+
from_port = each.value.from_port
43+
protocol = each.value.protocol
44+
security_groups = each.value.security_groups
45+
self = each.value.self
46+
to_port = each.value.to_port
47+
description = each.value.description
48+
}
49+
}
1250

1351
tags = merge(
1452
local.tags,
@@ -18,22 +56,10 @@ resource "aws_security_group" "docker_autoscaler" {
1856
)
1957
}
2058

21-
resource "aws_security_group_rule" "autoscaler_egress" {
22-
count = var.runner_worker.type == "docker-autoscaler" ? 1 : 0
23-
24-
description = "All egress traffic docker autoscaler"
25-
type = "egress"
26-
from_port = 0
27-
to_port = 0
28-
protocol = "-1"
29-
cidr_blocks = ["0.0.0.0/0"]
30-
security_group_id = join("", aws_security_group.docker_autoscaler[*].id)
31-
}
32-
3359
resource "aws_security_group_rule" "autoscaler_ingress" {
3460
count = var.runner_worker.type == "docker-autoscaler" ? 1 : 0
3561

36-
description = "All ingress traffic from runner security group"
62+
description = "Allow Ingress traffic within runner mananger and docker-autoscaler workers security groups"
3763
type = "ingress"
3864
from_port = 0
3965
to_port = 0
@@ -42,18 +68,6 @@ resource "aws_security_group_rule" "autoscaler_ingress" {
4268
security_group_id = join("", aws_security_group.docker_autoscaler[*].id)
4369
}
4470

45-
resource "aws_security_group_rule" "extra_autoscaler_ingress" {
46-
count = var.runner_worker.type == "docker-autoscaler" ? length(var.runner_worker_docker_autoscaler_asg.sg_ingresses) : 0
47-
48-
description = var.runner_worker_docker_autoscaler_asg.sg_ingresses[count.index].description
49-
type = "ingress"
50-
from_port = var.runner_worker_docker_autoscaler_asg.sg_ingresses[count.index].from_port
51-
to_port = var.runner_worker_docker_autoscaler_asg.sg_ingresses[count.index].to_port
52-
protocol = var.runner_worker_docker_autoscaler_asg.sg_ingresses[count.index].protocol
53-
cidr_blocks = var.runner_worker_docker_autoscaler_asg.sg_ingresses[count.index].cidr_blocks
54-
security_group_id = join("", aws_security_group.docker_autoscaler[*].id)
55-
}
56-
5771
####################################
5872
###### Launch template Workers #####
5973
####################################

security_groups.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ resource "aws_security_group_rule" "runner_ping_group" {
6666

6767
resource "aws_security_group" "docker_machine" {
6868
# checkov:skip=CKV2_AWS_5:Security group is used within an template and assigned to the docker machines
69-
count = contains(["docker+machine", "docker-autoscaler"], var.runner_worker.type) ? 1 : 0
69+
count = var.runner_worker.type == "docker+machine" ? 1 : 0
7070

7171
name_prefix = "${local.name_sg}-docker-machine"
7272
vpc_id = var.vpc_id

variables.tf

+58-22
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,11 @@ variable "runner_worker_cache" {
428428
cache. To use the same cache across multiple Runner Worker disable the creation of the cache and provide a policy and
429429
bucket name. See the public runner example for more details."
430430
431-
For detailed documentation check https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-runnerscaches3-section
431+
For detailed documentation check https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-runnerscaches3-section.
432432
433433
access_log_bucker_id = The ID of the bucket where the access logs are stored.
434434
access_log_bucket_prefix = The bucket prefix for the access logs.
435-
authentication_type = A string that declares the AuthenticationType for [runners.cache.s3]. Can either be 'iam' or 'credentials'
435+
authentication_type = A string that declares the AuthenticationType for [runners.cache.s3]. Can either be 'iam' or 'credentials'.
436436
bucket = Name of the cache bucket. Requires `create = false`.
437437
bucket_prefix = Prefix for s3 cache bucket name. Requires `create = true`.
438438
create = Boolean used to enable or disable the creation of the cache bucket.
@@ -609,11 +609,11 @@ variable "runner_worker_docker_machine_fleet" {
609609

610610
variable "runner_worker_docker_autoscaler" {
611611
description = <<-EOT
612-
fleeting_plugin_version = The version of aws fleeting plugin
613-
connector_config_user = User to connect to worker machine
612+
fleeting_plugin_version = The version of aws fleeting plugin.
613+
connector_config_user = User to connect to worker machine.
614614
key_pair_name = The name of the key pair used by the Runner to connect to the docker-machine Runner Workers. This variable is only supported when `enables` is set to `true`.
615615
capacity_per_instance = The number of jobs that can be executed concurrently by a single instance.
616-
max_use_count = Max job number that can run on a worker
616+
max_use_count = Max job number that can run on a worker.
617617
update_interval = The interval to check with the fleeting plugin for instance updates.
618618
update_interval_when_expecting = The interval to check with the fleeting plugin for instance updates when expecting a state change.
619619
instance_ready_command = Executes this command on each instance provisioned by the autoscaler to ensure that it is ready for use. A failure results in the instance being removed.
@@ -634,14 +634,14 @@ variable "runner_worker_docker_autoscaler" {
634634
variable "runner_worker_docker_autoscaler_instance" {
635635
description = <<-EOT
636636
ebs_optimized = Enable EBS optimization for the Runner Worker.
637-
http_tokens = Whether or not the metadata service requires session tokens
637+
http_tokens = Whether or not the metadata service requires session tokens.
638638
http_put_response_hop_limit = The desired HTTP PUT response hop limit for instance metadata requests. The larger the number, the further instance metadata requests can travel.
639639
monitoring = Enable detailed monitoring for the Runner Worker.
640640
private_address_only = Restrict Runner Worker to the use of a private IP address. If `runner_instance.use_private_address_only` is set to `true` (default),
641641
root_device_name = The name of the root volume for the Runner Worker.
642642
root_size = The size of the root volume for the Runner Worker.
643643
start_script = Cloud-init user data that will be passed to the Runner Worker. Should not be base64 encrypted.
644-
volume_type = The type of volume to use for the Runner Worker. `gp2`, `gp3`, `io1` or `io2` are supported
644+
volume_type = The type of volume to use for the Runner Worker. `gp2`, `gp3`, `io1` or `io2` are supported.
645645
volume_iops = Guaranteed IOPS for the volume. Only supported when using `gp3`, `io1` or `io2` as `volume_type`.
646646
volume_throughput = Throughput in MB/s for the volume. Only supported when using `gp3` as `volume_type`.
647647
EOT
@@ -664,25 +664,25 @@ EOT
664664

665665
variable "runner_worker_docker_autoscaler_asg" {
666666
description = <<-EOT
667+
enabled_metrics = List of metrics to collect.
667668
enable_mixed_instances_policy = Make use of autoscaling-group mixed_instances_policy capacities to leverage pools and spot instances.
668-
health_check_grace_period = Time (in seconds) after instance comes into service before checking health
669-
health_check_type = Controls how health checking is done. Values are - EC2 and ELB
669+
health_check_grace_period = Time (in seconds) after instance comes into service before checking health.
670+
health_check_type = Controls how health checking is done. Values are - EC2 and ELB.
670671
instance_refresh_min_healthy_percentage = The amount of capacity in the Auto Scaling group that must remain healthy during an instance refresh to allow the operation to continue, as a percentage of the desired capacity of the Auto Scaling group.
671672
instance_refresh_triggers = Set of additional property names that will trigger an Instance Refresh. A refresh will always be triggered by a change in any of launch_configuration, launch_template, or mixed_instances_policy.
672673
max_growth_rate = The maximum number of machines that can be added to the runner in parallel.
673674
on_demand_base_capacity = Absolute minimum amount of desired capacity that must be fulfilled by on-demand instances.
674675
on_demand_percentage_above_base_capacity = Percentage split between on-demand and Spot instances above the base on-demand capacity.
675-
override_instance_types = List to override the instance type in the Launch Template. Allow to spread spot instances on several types, to reduce interruptions
676+
override_instance_types = List to override the instance type in the Launch Template. Allow to spread spot instances on several types, to reduce interruptions.
676677
profile_name = profile_name = Name of the IAM profile to attach to the Runner Workers.
677-
sg_ingresses = Extra security group rule for workers
678-
spot_allocation_strategy = How to allocate capacity across the Spot pools. 'lowest-price' to optimize cost, 'capacity-optimized' to reduce interruptions
678+
spot_allocation_strategy = How to allocate capacity across the Spot pools. 'lowest-price' to optimize cost, 'capacity-optimized' to reduce interruptions.
679679
spot_instance_pools = Number of Spot pools per availability zone to allocate capacity. EC2 Auto Scaling selects the cheapest Spot pools and evenly allocates Spot capacity across the number of Spot pools that you specify.
680680
subnet_ids = The list of subnet IDs to use for the Runner Worker when the fleet mode is enabled.
681681
types = The type of instance to use for the Runner Worker. In case of fleet mode, multiple instance types are supported.
682-
upgrade_strategy = Auto deploy new instances when launch template changes. Can be either 'bluegreen', 'rolling' or 'off'
683-
enabled_metrics = List of metrics to collect.
682+
upgrade_strategy = Auto deploy new instances when launch template changes. Can be either 'bluegreen', 'rolling' or 'off'.
684683
EOT
685684
type = object({
685+
enabled_metrics = optional(list(string), [])
686686
enable_mixed_instances_policy = optional(bool, false)
687687
health_check_grace_period = optional(number, 300)
688688
health_check_type = optional(string, "EC2")
@@ -697,14 +697,6 @@ variable "runner_worker_docker_autoscaler_asg" {
697697
subnet_ids = optional(list(string), [])
698698
types = optional(list(string), ["m5.large"])
699699
upgrade_strategy = optional(string, "rolling")
700-
enabled_metrics = optional(list(string), [])
701-
sg_ingresses = optional(list(object({
702-
description = string
703-
from_port = number
704-
to_port = number
705-
protocol = string
706-
cidr_blocks = list(string)
707-
})), [])
708700
})
709701
default = {}
710702
}
@@ -741,6 +733,50 @@ variable "runner_worker_docker_autoscaler_role" {
741733
default = {}
742734
}
743735

736+
variable "runner_worker_docker_autoscaler_ingress_rules" {
737+
description = "List of ingress rules for the Docker-autoscaler Runner workers"
738+
type = list(object({
739+
cidr_blocks = list(string)
740+
ipv6_cidr_blocks = list(string)
741+
prefix_list_ids = list(string)
742+
from_port = number
743+
protocol = string
744+
security_groups = list(string)
745+
self = bool
746+
to_port = number
747+
description = string
748+
}))
749+
default = []
750+
}
751+
752+
variable "runner_worker_docker_autoscaler_egress_rules" {
753+
description = "List of egress rules for the Docker-autoscaler Runner workers"
754+
type = list(object({
755+
cidr_blocks = list(string)
756+
ipv6_cidr_blocks = list(string)
757+
prefix_list_ids = list(string)
758+
from_port = number
759+
protocol = string
760+
security_groups = list(string)
761+
self = bool
762+
to_port = number
763+
description = string
764+
}))
765+
default = [
766+
{
767+
cidr_blocks = ["0.0.0.0/0"]
768+
ipv6_cidr_blocks = ["::/0"]
769+
prefix_list_ids = null
770+
from_port = 0
771+
protocol = "-1"
772+
security_groups = null
773+
self = null
774+
to_port = 0
775+
description = "Allow all egress traffic for Docker-autoscaler runner workers."
776+
}
777+
]
778+
}
779+
744780
variable "runner_worker_docker_machine_extra_egress_rules" {
745781
description = "List of egress rules for the Runner Workers."
746782
type = list(object({

0 commit comments

Comments
 (0)