-
-
Notifications
You must be signed in to change notification settings - Fork 331
/
docker_autoscaler.tf
199 lines (169 loc) · 7.34 KB
/
docker_autoscaler.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#
# This file is responsible for creating the resources needed to run the docker autoscaler plugin from GitLab. It replaces the
# outdated docker+machine driver. The docker+machine driver is a legacy driver that is no longer maintained by GitLab.
#
resource "aws_security_group" "docker_autoscaler" {
count = var.runner_worker.type == "docker-autoscaler" ? 1 : 0
description = "Docker autoscaler security group"
vpc_id = var.vpc_id
name = "${local.name_sg}-docker-autoscaler"
tags = merge(
local.tags,
{
"Name" = format("%s", local.name_sg)
},
)
}
resource "aws_security_group_rule" "autoscaler_egress" {
count = var.runner_worker.type == "docker-autoscaler" ? 1 : 0
description = "All egress traffic docker autoscaler"
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = join("", aws_security_group.docker_autoscaler[*].id)
}
resource "aws_security_group_rule" "autoscaler_ingress" {
count = var.runner_worker.type == "docker-autoscaler" ? 1 : 0
description = "All ingress traffic from runner security group"
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
source_security_group_id = aws_security_group.runner.id
security_group_id = join("", aws_security_group.docker_autoscaler[*].id)
}
resource "aws_security_group_rule" "extra_autoscaler_ingress" {
count = var.runner_worker.type == "docker-autoscaler" ? length(var.runner_worker_docker_autoscaler_asg.sg_ingresses) : 0
description = var.runner_worker_docker_autoscaler_asg.sg_ingresses[count.index].description
type = "ingress"
from_port = var.runner_worker_docker_autoscaler_asg.sg_ingresses[count.index].from_port
to_port = var.runner_worker_docker_autoscaler_asg.sg_ingresses[count.index].to_port
protocol = var.runner_worker_docker_autoscaler_asg.sg_ingresses[count.index].protocol
cidr_blocks = var.runner_worker_docker_autoscaler_asg.sg_ingresses[count.index].cidr_blocks
security_group_id = join("", aws_security_group.docker_autoscaler[*].id)
}
####################################
###### Launch template Workers #####
####################################
resource "aws_launch_template" "this" {
count = var.runner_worker.type == "docker-autoscaler" ? 1 : 0
name = "${local.name_runner_agent_instance}-worker-launch-template"
user_data = base64gzip(var.runner_worker_docker_autoscaler_instance.start_script)
image_id = data.aws_ami.docker-autoscaler[0].id
instance_type = var.runner_worker_docker_autoscaler_asg.types[0]
key_name = aws_key_pair.autoscaler[0].key_name
ebs_optimized = var.runner_worker_docker_autoscaler_instance.ebs_optimized
monitoring {
enabled = var.runner_worker_docker_autoscaler_instance.monitoring
}
iam_instance_profile {
name = aws_iam_instance_profile.docker_autoscaler[0].name
}
network_interfaces {
security_groups = [aws_security_group.docker_autoscaler[0].id]
associate_public_ip_address = !var.runner_worker_docker_autoscaler_instance.private_address_only
}
block_device_mappings {
device_name = var.runner_worker_docker_autoscaler_instance.root_device_name
ebs {
volume_size = var.runner_worker_docker_autoscaler_instance.root_size
volume_type = var.runner_worker_docker_autoscaler_instance.volume_type
iops = contains(["gp3", "io1", "io2"], var.runner_worker_docker_autoscaler_instance.volume_type) ? var.runner_worker_docker_autoscaler_instance.volume_iops : null
throughput = var.runner_worker_docker_autoscaler_instance.volume_type == "gp3" ? var.runner_worker_docker_autoscaler_instance.volume_throughput : null
}
}
tag_specifications {
resource_type = "instance"
tags = local.tags
}
tag_specifications {
resource_type = "volume"
tags = local.tags
}
tags = local.tags
metadata_options {
http_tokens = var.runner_worker_docker_autoscaler_instance.http_tokens
http_put_response_hop_limit = var.runner_worker_docker_autoscaler_instance.http_put_response_hop_limit
instance_metadata_tags = "enabled"
}
lifecycle {
create_before_destroy = true
}
}
#########################################
# Autoscaling group with launch template
#########################################
# false positive, tags are created with "dynamic" block
# kics-scan ignore-line
resource "aws_autoscaling_group" "autoscaler" {
count = var.runner_worker.type == "docker-autoscaler" ? 1 : 0
name = "${local.name_runner_agent_instance}-asg"
capacity_rebalance = false
protect_from_scale_in = true
dynamic "launch_template" {
for_each = var.runner_worker_docker_autoscaler_asg.enable_mixed_instances_policy ? [] : [1]
content {
id = aws_launch_template.this[0].id
version = aws_launch_template.this[0].latest_version
}
}
dynamic "mixed_instances_policy" {
for_each = var.runner_worker_docker_autoscaler_asg.enable_mixed_instances_policy ? [1] : []
content {
instances_distribution {
on_demand_base_capacity = var.runner_worker_docker_autoscaler_asg.on_demand_base_capacity
on_demand_percentage_above_base_capacity = var.runner_worker_docker_autoscaler_asg.on_demand_percentage_above_base_capacity
spot_allocation_strategy = var.runner_worker_docker_autoscaler_asg.spot_allocation_strategy
spot_instance_pools = var.runner_worker_docker_autoscaler_asg.spot_instance_pools
}
launch_template {
launch_template_specification {
launch_template_id = aws_launch_template.this[0].id
version = aws_launch_template.this[0].latest_version
}
dynamic "override" {
for_each = var.runner_worker_docker_autoscaler_asg.types
content {
instance_type = override.value
}
}
}
}
}
dynamic "instance_refresh" {
for_each = var.runner_worker_docker_autoscaler_asg.upgrade_strategy == "rolling" ? [1] : []
content {
strategy = "Rolling"
preferences {
min_healthy_percentage = var.runner_worker_docker_autoscaler_asg.instance_refresh_min_healthy_percentage
}
triggers = var.runner_worker_docker_autoscaler_asg.instance_refresh_triggers
}
}
vpc_zone_identifier = var.runner_worker_docker_autoscaler_asg.subnet_ids
max_size = var.runner_worker.max_jobs
min_size = 0
desired_capacity = 0 # managed by the fleeting plugin
health_check_grace_period = var.runner_worker_docker_autoscaler_asg.health_check_grace_period
health_check_type = var.runner_worker_docker_autoscaler_asg.health_check_type
force_delete = true
enabled_metrics = var.runner_worker_docker_autoscaler_asg.enabled_metrics
dynamic "tag" {
for_each = local.tags
content {
key = tag.key
value = tag.value
propagate_at_launch = true
}
}
lifecycle {
# do not change these values as we would immediately scale up/down, which is not wanted
ignore_changes = [
desired_capacity,
min_size,
max_size
]
}
}