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