Skip to content

Commit 048156a

Browse files
authored
feat: add network project for svpc, expose cooldown period, default scripts (terraform-google-modules#15)
* add network project for svpc, cooldown period for mig autoscaler * move default scripts into module * fmt * add region to template * read default file
1 parent 5c01d64 commit 048156a

File tree

12 files changed

+67
-86
lines changed

12 files changed

+67
-86
lines changed

examples/gh-runner-mig-native-simple/main.tf

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
*/
1616

1717
module "runner-mig" {
18-
source = "../../modules/gh-runner-mig-vm"
19-
create_network = true
20-
project_id = var.project_id
21-
repo_name = var.repo_name
22-
repo_owner = var.repo_owner
23-
repo_url = var.repo_url
24-
gh_token = var.gh_token
25-
startup_script = file("${path.cwd}/startup.sh")
26-
shutdown_script = file("${path.cwd}/shutdown.sh")
18+
source = "../../modules/gh-runner-mig-vm"
19+
create_network = true
20+
project_id = var.project_id
21+
repo_name = var.repo_name
22+
repo_owner = var.repo_owner
23+
repo_url = var.repo_url
24+
gh_token = var.gh_token
2725
}

modules/gh-runner-gke/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ This example shows how to deploy a simple GKE Self Hosted Runner.
4343
| service\_account | Optional Service Account for the nodes | `string` | `""` | no |
4444
| subnet\_ip | IP range for the subnet | `string` | `"10.0.0.0/17"` | no |
4545
| subnet\_name | Name for the subnet | `string` | `"runner-subnet"` | no |
46+
| subnetwork\_project | The ID of the project in which the subnetwork belongs. If it is not provided, the project\_id is used. | `string` | `""` | no |
4647
| zones | The GCP zone to deploy gke into | `list(string)` | <pre>[<br> "us-east4-a"<br>]</pre> | no |
4748

4849
## Outputs

modules/gh-runner-gke/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ module "runner-cluster" {
5757
region = var.region
5858
zones = var.zones
5959
network = local.network_name
60+
network_project_id = var.subnetwork_project != "" ? var.subnetwork_project : var.project_id
6061
subnetwork = local.subnet_name
6162
ip_range_pods = var.ip_range_pods_name
6263
ip_range_services = var.ip_range_pods_name

modules/gh-runner-gke/variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,31 @@ variable "network_name" {
6060
description = "Name for the VPC network"
6161
default = "runner-network"
6262
}
63+
6364
variable "subnet_ip" {
6465
type = string
6566
description = "IP range for the subnet"
6667
default = "10.0.0.0/17"
6768
}
69+
6870
variable "subnet_name" {
6971
type = string
7072
description = "Name for the subnet"
7173
default = "runner-subnet"
7274
}
75+
7376
variable "create_network" {
7477
type = bool
7578
description = "When set to true, VPC will be auto created"
7679
default = true
7780
}
7881

82+
variable "subnetwork_project" {
83+
type = string
84+
description = "The ID of the project in which the subnetwork belongs. If it is not provided, the project_id is used."
85+
default = ""
86+
}
87+
7988
variable "machine_type" {
8089
type = string
8190
description = "Machine type for runner node pool"

modules/gh-runner-mig-container-vm/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This example shows how to deploy a Self Hosted Runner on MIG Container VMs.
2727
| Name | Description | Type | Default | Required |
2828
|------|-------------|------|---------|:--------:|
2929
| additional\_metadata | Additional metadata to attach to the instance | `map` | `{}` | no |
30+
| cooldown\_period | The number of seconds that the autoscaler should wait before it starts collecting information from a new instance. | `number` | `60` | no |
3031
| create\_network | When set to true, VPC,router and NAT will be auto created | `bool` | `true` | no |
3132
| dind | Flag to determine whether to expose dockersock | `bool` | `false` | no |
3233
| gh\_token | Github token that is used for generating Self Hosted Runner Token | `string` | n/a | yes |
@@ -43,6 +44,7 @@ This example shows how to deploy a Self Hosted Runner on MIG Container VMs.
4344
| service\_account | Service account email address | `string` | `""` | no |
4445
| subnet\_ip | IP range for the subnet | `string` | `"10.10.10.0/24"` | no |
4546
| subnet\_name | Name for the subnet | `string` | `"gh-runner-subnet"` | no |
47+
| subnetwork\_project | The ID of the project in which the subnetwork belongs. If it is not provided, the project\_id is used. | `string` | `""` | no |
4648
| target\_size | The number of runner instances | `number` | `2` | no |
4749
| zone | The GCP zone to deploy instances into | `string` | `"us-east4-b"` | no |
4850

modules/gh-runner-mig-container-vm/main.tf

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,13 @@ module "gce-container" {
149149

150150

151151
module "mig_template" {
152-
source = "terraform-google-modules/vm/google//modules/instance_template"
153-
version = "~> 5.0"
154-
project_id = var.project_id
155-
network = local.network_name
156-
subnetwork = local.subnet_name
152+
source = "terraform-google-modules/vm/google//modules/instance_template"
153+
version = "~> 5.0"
154+
project_id = var.project_id
155+
region = var.region
156+
network = local.network_name
157+
subnetwork = local.subnet_name
158+
subnetwork_project = var.subnetwork_project != "" ? var.subnetwork_project : var.project_id
157159
service_account = {
158160
email = local.service_account
159161
scopes = [
@@ -191,34 +193,5 @@ module "mig" {
191193

192194
/* autoscaler */
193195
autoscaling_enabled = true
194-
}
195-
/*****************************************
196-
FW
197-
*****************************************/
198-
resource "google_compute_firewall" "http-access" {
199-
name = "${local.instance_name}-http"
200-
project = var.project_id
201-
network = local.network_name
202-
203-
allow {
204-
protocol = "tcp"
205-
ports = ["8080"]
206-
}
207-
208-
source_ranges = ["0.0.0.0/0"]
209-
target_tags = ["gh-runner-vm"]
210-
}
211-
212-
resource "google_compute_firewall" "ssh-access" {
213-
name = "${local.instance_name}-ssh"
214-
project = var.project_id
215-
network = local.network_name
216-
217-
allow {
218-
protocol = "tcp"
219-
ports = ["22"]
220-
}
221-
222-
source_ranges = ["0.0.0.0/0"]
223-
target_tags = ["gh-runner-vm"]
196+
cooldown_period = var.cooldown_period
224197
}

modules/gh-runner-mig-container-vm/variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ variable "create_network" {
4242
default = true
4343
}
4444

45+
variable "subnetwork_project" {
46+
type = string
47+
description = "The ID of the project in which the subnetwork belongs. If it is not provided, the project_id is used."
48+
default = ""
49+
}
50+
4551
variable "subnet_ip" {
4652
type = string
4753
description = "IP range for the subnet"
@@ -118,3 +124,8 @@ variable "dind" {
118124
description = "Flag to determine whether to expose dockersock "
119125
default = false
120126
}
127+
128+
variable "cooldown_period" {
129+
description = "The number of seconds that the autoscaler should wait before it starts collecting information from a new instance."
130+
default = 60
131+
}

modules/gh-runner-mig-vm/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This example shows how to deploy a MIG Self Hosted Runner with an image pre-bake
2929
| Name | Description | Type | Default | Required |
3030
|------|-------------|------|---------|:--------:|
3131
| additional\_metadata | Additional metadata to attach to the instance | `map` | `{}` | no |
32+
| cooldown\_period | The number of seconds that the autoscaler should wait before it starts collecting information from a new instance. | `number` | `60` | no |
3233
| create\_network | When set to true, VPC,router and NAT will be auto created | `bool` | `true` | no |
3334
| custom\_metadata | User provided custom metadata | `map` | `{}` | no |
3435
| gh\_token | Github token that is used for generating Self Hosted Runner Token | `string` | n/a | yes |
@@ -49,6 +50,7 @@ This example shows how to deploy a MIG Self Hosted Runner with an image pre-bake
4950
| startup\_script | User startup script to run when instances spin up | `string` | `""` | no |
5051
| subnet\_ip | IP range for the subnet | `string` | `"10.10.10.0/24"` | no |
5152
| subnet\_name | Name for the subnet | `string` | `"gh-runner-subnet"` | no |
53+
| subnetwork\_project | The ID of the project in which the subnetwork belongs. If it is not provided, the project\_id is used. | `string` | `""` | no |
5254
| target\_size | The number of runner instances | `number` | `2` | no |
5355
| zone | The GCP zone to deploy instances into | `string` | `"us-east4-b"` | no |
5456

modules/gh-runner-mig-vm/main.tf

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ locals {
1818
network_name = var.create_network ? google_compute_network.gh-network[0].self_link : var.network_name
1919
subnet_name = var.create_network ? google_compute_subnetwork.gh-subnetwork[0].self_link : var.subnet_name
2020
service_account = var.service_account == "" ? google_service_account.runner_service_account[0].email : var.service_account
21-
shutdown_script = {
22-
"shutdown-script" = var.shutdown_script
23-
}
21+
startup_script = var.startup_script == "" ? file("${path.module}/scripts/startup.sh") : var.startup_script
22+
shutdown_script = var.shutdown_script == "" ? file("${path.module}/scripts/shutdown.sh") : var.shutdown_script
2423
}
2524

2625
/*****************************************
@@ -123,11 +122,13 @@ locals {
123122

124123

125124
module "mig_template" {
126-
source = "terraform-google-modules/vm/google//modules/instance_template"
127-
version = "~> 5.0"
128-
project_id = var.project_id
129-
network = local.network_name
130-
subnetwork = local.subnet_name
125+
source = "terraform-google-modules/vm/google//modules/instance_template"
126+
version = "~> 5.0"
127+
project_id = var.project_id
128+
network = local.network_name
129+
subnetwork = local.subnet_name
130+
region = var.region
131+
subnetwork_project = var.subnetwork_project != "" ? var.subnetwork_project : var.project_id
131132
service_account = {
132133
email = local.service_account
133134
scopes = [
@@ -140,11 +141,13 @@ module "mig_template" {
140141
name_prefix = "gh-runner"
141142
source_image_family = var.source_image_family
142143
source_image_project = var.source_image_project
143-
startup_script = var.startup_script
144+
startup_script = local.startup_script
144145
source_image = var.source_image
145146
metadata = merge({
146147
"secret-id" = google_secret_manager_secret_version.gh-secret-version.name
147-
}, local.shutdown_script, var.custom_metadata)
148+
}, {
149+
"shutdown-script" = local.shutdown_script
150+
}, var.custom_metadata)
148151
tags = [
149152
"gh-runner-vm"
150153
]
@@ -164,34 +167,5 @@ module "mig" {
164167

165168
/* autoscaler */
166169
autoscaling_enabled = true
167-
}
168-
/*****************************************
169-
FW
170-
*****************************************/
171-
resource "google_compute_firewall" "http-access" {
172-
name = "${local.instance_name}-http"
173-
project = var.project_id
174-
network = local.network_name
175-
176-
allow {
177-
protocol = "tcp"
178-
ports = ["8080"]
179-
}
180-
181-
source_ranges = ["0.0.0.0/0"]
182-
target_tags = ["gh-runner-vm"]
183-
}
184-
185-
resource "google_compute_firewall" "ssh-access" {
186-
name = "${local.instance_name}-ssh"
187-
project = var.project_id
188-
network = local.network_name
189-
190-
allow {
191-
protocol = "tcp"
192-
ports = ["22"]
193-
}
194-
195-
source_ranges = ["0.0.0.0/0"]
196-
target_tags = ["gh-runner-vm"]
170+
cooldown_period = var.cooldown_period
197171
}

examples/gh-runner-mig-native-simple/shutdown.sh renamed to modules/gh-runner-mig-vm/scripts/shutdown.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,3 @@ cd /runner || exit
3232
./svc.sh uninstall
3333
#remove the runner configuration
3434
RUNNER_ALLOW_RUNASROOT=1 /runner/config.sh remove --unattended --token "$(curl -sS --request POST --url "https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/actions/runners/remove-token" --header "authorization: Bearer ${GITHUB_TOKEN}" --header "content-type: application/json" | jq -r .token)"
35-

examples/gh-runner-mig-native-simple/startup.sh renamed to modules/gh-runner-mig-vm/scripts/startup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ secrets=$(gcloud secrets versions access "$SECRET_VERSION" --secret="$SECRET_NAM
3131
# we want to use wordsplitting
3232
export $(echo "$secrets" | jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]")
3333
#github runner version
34-
GH_RUNNER_VERSION="2.169.0"
34+
GH_RUNNER_VERSION="2.274.2"
3535
#get actions binary
3636
curl -o actions.tar.gz --location "https://github.com/actions/runner/releases/download/v${GH_RUNNER_VERSION}/actions-runner-linux-x64-${GH_RUNNER_VERSION}.tar.gz"
3737
mkdir /runner

modules/gh-runner-mig-vm/variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ variable "create_network" {
4242
default = true
4343
}
4444

45+
variable "subnetwork_project" {
46+
type = string
47+
description = "The ID of the project in which the subnetwork belongs. If it is not provided, the project_id is used."
48+
default = ""
49+
}
50+
4551
variable "subnet_ip" {
4652
type = string
4753
description = "IP range for the subnet"
@@ -143,3 +149,8 @@ variable "custom_metadata" {
143149
description = "User provided custom metadata"
144150
default = {}
145151
}
152+
153+
variable "cooldown_period" {
154+
description = "The number of seconds that the autoscaler should wait before it starts collecting information from a new instance."
155+
default = 60
156+
}

0 commit comments

Comments
 (0)