Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add session_server configuration #243

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e5da110
Merge remote-tracking branch 'origin/develop' into kayman-mk/92-add-s…
Jul 27, 2020
be2c5a9
Add [session_server] section.
Jul 27, 2020
4d1940f
Add session_server configuration variables.
Jul 28, 2020
8126edb
Merge branch 'develop' of github.com:npalm/terraform-aws-gitlab-runne…
Jul 28, 2020
60fa022
Format issue
Jul 28, 2020
52183fb
Add new parameters
Aug 13, 2020
231f5f4
Merge remote-tracking branch 'main/develop' into kayman-mk/92-add-sup…
Aug 13, 2020
acfd352
Merge branch 'develop' of github.com:npalm/terraform-aws-gitlab-runne…
Sep 4, 2020
14f998c
Add support for session server ALB
Sep 4, 2020
2ede6f5
Add security groups to protect the session server
Sep 4, 2020
c53f8d0
Fix verification errors
Sep 4, 2020
ae6e0c7
Correct formatting
Sep 4, 2020
fb898be
Correct string comparison
Sep 4, 2020
ed780bf
Use correct format
Sep 4, 2020
e056c36
Fix errors
Sep 4, 2020
09e4778
Merge branch 'master' of github.com:npalm/terraform-aws-gitlab-runner…
kayman-mk Nov 12, 2020
27bf9cd
Rename variable to match 'session_server' prefix
kayman-mk Nov 12, 2020
9d1988d
Refactor variables and use a map instead of single variables
kayman-mk Nov 12, 2020
6a8fcca
Fix syntax error
kayman-mk Nov 12, 2020
1392baa
Fix indentation
kayman-mk Nov 12, 2020
8e90692
Fix syntax errors
kayman-mk Nov 19, 2020
d932063
Add port to listen/advertise address
kayman-mk Nov 19, 2020
aa4acd1
Check if session_server is set
kayman-mk Nov 19, 2020
7d927b7
Quote session server settings
kayman-mk Nov 19, 2020
63b8e80
Replace map by object reference
kayman-mk Nov 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ terraform destroy
| runners\_volumes\_tmpfs | n/a | <pre>list(object({<br> volume = string<br> options = string<br> }))</pre> | `[]` | no |
| schedule\_config | Map containing the configuration of the ASG scale-in and scale-up for the runner instance. Will only be used if enable\_schedule is set to true. | `map` | <pre>{<br> "scale_in_count": 0,<br> "scale_in_recurrence": "0 18 * * 1-5",<br> "scale_out_count": 1,<br> "scale_out_recurrence": "0 8 * * 1-5"<br>}</pre> | no |
| secure\_parameter\_store\_runner\_token\_key | The key name used store the Gitlab runner token in Secure Parameter Store | `string` | `"runner-token"` | no |
| session\_server\_advertise\_address | The URL exposed to Gitlab used to access the session server, e.g. runner-host-name.tld:8093 | `string` | `""` | no |
| session\_server\_listen\_address | Listen address of the session server, e.g. [::]:8093. Don't forget to expose this port if you use the docker runner image. | `string` | `""` | no |
| session\_server\_session\_timeout | Time in seconds how long the session stays active after the job completes. | `number` | `1800` | no |
| ssh\_key\_pair | Set this to use existing AWS key pair | `string` | `null` | no |
| subnet\_id\_runners | List of subnets used for hosting the gitlab-runners. | `string` | n/a | yes |
| subnet\_ids\_gitlab\_runner | Subnet used for hosting the GitLab runner. | `list(string)` | n/a | yes |
Expand Down
11 changes: 11 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ locals {
join(",", formatlist("%q", var.docker_machine_options)),
)

// convert the options for the session server
session_server_string = var.session_server == null ? "" : join("",
formatlist("%s", [
"[session_server]\n",
format("listen_address = \"%s:%d\"\n", var.session_server.listen_address, var.session_server.port),
format("advertise_address = \"%q:%d\"\n", var.session_server.advertise_address, var.session_server.port),
format("session_timeout = %s\n", var.session_server.timeout)
]
)
)

// Ensure max builds is optional
runners_max_builds_string = var.runners_max_builds == 0 ? "" : format("MaxBuilds = %d", var.runners_max_builds)

Expand Down
31 changes: 31 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ locals {
runners_services_volumes_tmpfs = join(",", [for v in var.runners_services_volumes_tmpfs : format("\"%s\" = \"%s\"", v.volume, v.options)])
bucket_name = local.bucket_name
shared_cache = var.cache_shared
session_server_string = var.session_server == null ? "" : local.session_server_string
}
)
}
Expand All @@ -158,6 +159,7 @@ resource "aws_autoscaling_group" "gitlab_runner_instance" {
max_size = "1"
desired_capacity = "1"
health_check_grace_period = 0
target_group_arns = [var.session_server != null && var.session_server.listener_arn != "" ? aws_alb_target_group.session_server[0].arn : null]
launch_configuration = aws_launch_configuration.gitlab_runner_instance.name
enabled_metrics = var.metrics_autoscaling
tags = data.null_data_source.agent_tags.*.outputs
Expand Down Expand Up @@ -406,3 +408,32 @@ resource "aws_iam_role_policy_attachment" "eip" {
role = aws_iam_role.instance.name
policy_arn = aws_iam_policy.eip[0].arn
}

################################################################################
### Session server ALB support
################################################################################
resource "aws_alb_listener_rule" "session_server" {
count = var.session_server != null && var.session_server.listener_arn != "" ? 1 : 0

listener_arn = var.session_server.listener_arn

action {
type = "forward"
target_group_arn = aws_alb_target_group.session_server[0].arn
}

condition {
host_header {
values = [var.session_server.advertise_address]
}
}
}

resource "aws_alb_target_group" "session_server" {
count = var.session_server != null && var.session_server.listener_arn != "" ? 1 : 0

name = "${var.environment}-session-server"
port = var.session_server.port
protocol = "HTTP"
vpc_id = var.vpc_id
}
26 changes: 26 additions & 0 deletions security_groups.tf
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ resource "aws_security_group_rule" "runner_ping" {
)
}

# Allow incoming traffic for the session server to gitlab-runner agent instances
resource "aws_security_group_rule" "runner_session_server" {
count = length(var.session_server) > 0 ? 1 : 0

type = "ingress"
from_port = var.session_server["port"]
to_port = var.session_server["port"]
protocol = "tcp"

cidr_blocks = var.session_server["incoming_cidr_blocks"]
security_group_id = aws_security_group.runner.id
}

########################################
## Security group IDs to runner agent ##
########################################
Expand All @@ -97,6 +110,19 @@ resource "aws_security_group_rule" "runner_ssh_group" {
)
}

# Allow incoming traffic for the session server from allowed security groups to gitlab-runner agent instances
resource "aws_security_group_rule" "runner_session_server_group" {
count = length(var.session_server) > 0 && var.session_server["listener_arn"] != "" ? 1 : 0

type = "ingress"
from_port = var.session_server["port"]
to_port = var.session_server["port"]
protocol = "tcp"

source_security_group_id = var.session_server["alb_security_group_id"]
security_group_id = aws_security_group.runner.id
}

# Allow ICMP traffic from allowed security group IDs to gitlab-runner agent instances
resource "aws_security_group_rule" "runner_ping_group" {
count = length(var.gitlab_runner_security_group_ids) > 0 && var.enable_ping ? length(var.gitlab_runner_security_group_ids) : 0
Expand Down
5 changes: 4 additions & 1 deletion template/runner-config.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
concurrent = ${runners_concurrent}
check_interval = 0

${session_server_string}

[[runners]]
name = "${runners_name}"
url = "${gitlab_url}"
Expand Down Expand Up @@ -63,4 +65,5 @@ check_interval = 0
${runners_off_peak_idle_count}
${runners_off_peak_idle_time}
${runners_off_peak_periods_string}
${runners_machine_autoscaling}

${runners_machine_autoscaling}
23 changes: 23 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,26 @@ variable "runner_iam_policy_arns" {
description = "List of policy ARNs to be added to the instance profile of the gitlab runner agent ec2 instance."
default = []
}

variable "session_server" {
description = "Enables the session server support."
type = object({
timeout = number
port = number
listen_address = string
advertise_address = string
listener_arn = string
alb_security_group_id = string
incoming_cidr_blocks = list(string)
}
)

default = null
# session_timeout - Time in seconds how long the session stays active after the job completes. (1800)
# port - Port which is used to connect to the session server. Don't forget to expose this port if you use the docker runner image. (8093)
# listen_address - Listen address of the session server, e.g. [::] without a port. Session_server_port is used for the port.
# advertise_address - The URL exposed to Gitlab used to access the session server, e.g. runner-host-name.tld. session_server_port is used for the port.
# listener_arn - ALB listener ARN to connect the session server to the outside. An EIP can be used instead (see enable_eip).
# alb_security_group_id - ID of the security group belonging to the ALB to restrict the traffic to the session_server.
# incoming_cidr_blocks - CIDR blocks which are allowed to connect to the session server.
}