Skip to content

Commit 7380b7a

Browse files
feat: add optional worker pools and add support to self-signed certificates (GoogleCloudPlatform#399)
1 parent ed7cdcb commit 7380b7a

69 files changed

Lines changed: 1175 additions & 92 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1-bootstrap/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ Within the repository, you'll find `backend.tf` files that define the GCS bucket
275275
| project\_id | Project ID for initial resources | `string` | n/a | yes |
276276
| tf\_apply\_branches | List of git branches configured to run terraform apply Cloud Build trigger. All other branches will run plan by default. | `list(string)` | <pre>[<br> "development",<br> "nonproduction",<br> "production"<br>]</pre> | no |
277277
| trigger\_location | Location of for Cloud Build triggers created in the workspace. If using private pools should be the same location as the pool. | `string` | `"us-central1"` | no |
278+
| worker\_pool\_id | Specifies the Cloud Build Worker Pool that will be utilized for triggers created in this step.<br><br>The expected format is:<br>`projects/PROJECT/locations/LOCATION/workerPools/POOL_NAME`.<br><br>If you are using worker pools from a different project, ensure that you grant the<br>`roles/cloudbuild.workerPoolUser` role to the Cloud Build Service Agent and the Cloud Build Service Account of the trigger project:<br>`service-PROJECT_NUMBER@gcp-sa-cloudbuild.iam.gserviceaccount.com`, `PROJECT_NUMBER@cloudbuild.gserviceaccount.com`<br><br>If this variable is left undefined, Worker Pool will not be used for the Cloud Build Triggers. | `string` | `""` | no |
278279

279280
## Outputs
280281

1-bootstrap/main.tf

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ locals {
3434
use_csr = var.cloudbuildv2_repository_config.repo_type == "CSR"
3535
csr_repos = local.use_csr ? { for k, v in var.cloudbuildv2_repository_config.repositories : k => v.repository_name } : {}
3636
cb_service_accounts_emails = { for k, v in module.tf_cloudbuild_workspace : k => reverse(split("/", v.cloudbuild_sa))[0] }
37+
38+
// If the user specify a Cloud Build Worker Pool, utilize it in the trigger
39+
optional_worker_pool = var.worker_pool_id != "" ? { "_PRIVATE_POOL" = var.worker_pool_id } : {}
3740
}
3841

3942
resource "google_sourcerepo_repository" "gcp_repo" {
@@ -102,12 +105,12 @@ module "tf_cloudbuild_workspace" {
102105
roles = local.cb_config[each.key].roles }
103106
}
104107

105-
substitutions = {
108+
substitutions = merge({
106109
"_GAR_REGION" = var.location
107110
"_GAR_PROJECT_ID" = google_artifact_registry_repository.tf_image.project
108111
"_GAR_REPOSITORY" = google_artifact_registry_repository.tf_image.name
109112
"_DOCKER_TAG_VERSION_TERRAFORM" = local.docker_tag_version_terraform
110-
}
113+
}, local.optional_worker_pool)
111114

112115
# Branches to run the build
113116
tf_apply_branches = var.tf_apply_branches

1-bootstrap/terraform.tfvars

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ cloudbuildv2_repository_config = {
2020
gitlab_webhook_secret_id = "REPLACE_WITH_WEBHOOK_SECRET_ID"
2121
# If you are using a self-hosted instance, you may change the URL below accordingly
2222
gitlab_enterprise_host_uri = "https://gitlab.com"
23+
# Format is projects/PROJECT/locations/LOCATION/namespaces/NAMESPACE/services/SERVICE
24+
gitlab_enterprise_service_directory = "REPLACE_WITH_SERVICE_DIRECTORY"
25+
# .pem string
26+
gitlab_enterprise_ca_certificate = <<EOF
27+
REPLACE_WITH_SSL_CERT
28+
EOF
2329
}

1-bootstrap/variables.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,20 @@ variable "cloudbuildv2_repository_config" {
154154
}
155155

156156
}
157+
158+
variable "worker_pool_id" {
159+
description = <<-EOT
160+
Specifies the Cloud Build Worker Pool that will be utilized for triggers created in this step.
161+
162+
The expected format is:
163+
`projects/PROJECT/locations/LOCATION/workerPools/POOL_NAME`.
164+
165+
If you are using worker pools from a different project, ensure that you grant the
166+
`roles/cloudbuild.workerPoolUser` role to the Cloud Build Service Agent and the Cloud Build Service Account of the trigger project:
167+
`service-PROJECT_NUMBER@gcp-sa-cloudbuild.iam.gserviceaccount.com`, `PROJECT_NUMBER@cloudbuild.gserviceaccount.com`
168+
169+
If this variable is left undefined, Worker Pool will not be used for the Cloud Build Triggers.
170+
EOT
171+
type = string
172+
default = ""
173+
}

4-appfactory/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ It will also create an Application Folder to group your admin projects under it,
3131

3232
## Usage
3333

34+
### Git Provider
35+
3436
You have 3 Git provider options for this step: Cloud Source Repositories (CSR), Github, and Gitlab. If you are using Github or Gitlab you will need to take additional steps that are described in the following sections:
3537

3638
- [Cloud Build with Github Pre-requisites](#cloud-build-with-github-pre-requisites)
@@ -107,6 +109,20 @@ To proceed with Gitlab as your git provider you will need:
107109
gcloud projects add-iam-policy-binding $GIT_SECRET_PROJECT --role=roles/secretmanager.admin --member=serviceAccount:tf-cb-eab-applicationfactory@YOUR-CLOUDBUILD-PROJECT.iam.gserviceaccount.com
108110
```
109111

112+
### Worker Pool Requirements
113+
114+
If you are not using Worker Pools you can skip this step. If you are using Worker Pools, an additional step must be taken before deploying.
115+
116+
There is a terraform script that will assign required permissions on the Worker Pool Host Project and requires `var.worker_pool_id` to be specified on the 4-appfactory `terraform.tfvars` file. The script is located at [./modules/app-group-baseline/additional_workerpool_permissions.tf.example](./modules/app-group-baseline/additional_workerpool_permissions.tf.example).
117+
118+
1. Enable the permission assignment terraform script on `app-group-baseline` module.
119+
120+
```bash
121+
mv ./modules/app-group-baseline/additional_workerpool_permissions.tf.example ./modules/app-group-baseline/additional_workerpool_permissions.tf
122+
```
123+
124+
After renaming the file to `additional_workerpool_permissions.tf`, when you run the pipeline, the required permissions will automatically be assigned on the Worker Pool Host Project.
125+
110126
### Deploying with Google Cloud Build
111127

112128
The steps below assume that you are checked out on the same level as `terraform-google-enterprise-application` and `terraform-example-foundation` directories.

4-appfactory/envs/shared/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
| remote\_state\_bucket | Backend bucket to load Terraform Remote State Data from previous steps. | `string` | n/a | yes |
1616
| tf\_apply\_branches | List of git branches configured to run terraform apply Cloud Build trigger. All other branches will run plan by default. | `list(string)` | <pre>[<br> "development",<br> "nonproduction",<br> "production"<br>]</pre> | no |
1717
| trigger\_location | Location of for Cloud Build triggers created in the workspace. If using private pools should be the same location as the pool. | `string` | `"us-central1"` | no |
18+
| worker\_pool\_id | Specifies the Cloud Build Worker Pool that will be utilized for triggers created in this step.<br><br>The expected format is:<br>`projects/PROJECT/locations/LOCATION/workerPools/POOL_NAME`.<br><br>If you are using worker pools from a different project, ensure that you grant the<br>`roles/cloudbuild.workerPoolUser` role to the Cloud Build Service Agent and the Cloud Build Service Account of the trigger project:<br>`service-PROJECT_NUMBER@gcp-sa-cloudbuild.iam.gserviceaccount.com`, `PROJECT_NUMBER@cloudbuild.gserviceaccount.com`<br><br>If this variable is left undefined, Worker Pool will not be used for the Cloud Build Triggers. | `string` | `""` | no |
1819

1920
## Outputs
2021

4-appfactory/envs/shared/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@ module "components" {
7979
create_admin_project = each.value.service.create_admin_project
8080
create_infra_project = each.value.service.create_infra_project
8181

82-
8382
cloudbuildv2_repository_config = var.cloudbuildv2_repository_config
83+
worker_pool_id = var.worker_pool_id
8484
}

4-appfactory/envs/shared/variables.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,20 @@ variable "cloudbuildv2_repository_config" {
169169
}
170170

171171
}
172+
173+
variable "worker_pool_id" {
174+
description = <<-EOT
175+
Specifies the Cloud Build Worker Pool that will be utilized for triggers created in this step.
176+
177+
The expected format is:
178+
`projects/PROJECT/locations/LOCATION/workerPools/POOL_NAME`.
179+
180+
If you are using worker pools from a different project, ensure that you grant the
181+
`roles/cloudbuild.workerPoolUser` role to the Cloud Build Service Agent and the Cloud Build Service Account of the trigger project:
182+
`service-PROJECT_NUMBER@gcp-sa-cloudbuild.iam.gserviceaccount.com`, `PROJECT_NUMBER@cloudbuild.gserviceaccount.com`
183+
184+
If this variable is left undefined, Worker Pool will not be used for the Cloud Build Triggers.
185+
EOT
186+
type = string
187+
default = ""
188+
}

4-appfactory/modules/app-group-baseline/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
| service\_name | The name of a single service application. | `string` | `"demo-app"` | no |
2525
| tf\_apply\_branches | List of git branches configured to run terraform apply Cloud Build trigger. All other branches will run plan by default. | `list(string)` | <pre>[<br> "development",<br> "nonproduction",<br> "production"<br>]</pre> | no |
2626
| trigger\_location | Location of for Cloud Build triggers created in the workspace. If using private pools should be the same location as the pool. | `string` | `"global"` | no |
27+
| worker\_pool\_id | Specifies the Cloud Build Worker Pool that will be utilized for triggers created in this step.<br><br>The expected format is:<br>`projects/PROJECT/locations/LOCATION/workerPools/POOL_NAME`.<br><br>If you are using worker pools from a different project, ensure that you grant the<br>`roles/cloudbuild.workerPoolUser` role to the Cloud Build Service Agent and the Cloud Build Service Account of the trigger project:<br>`service-PROJECT_NUMBER@gcp-sa-cloudbuild.iam.gserviceaccount.com`, `PROJECT_NUMBER@cloudbuild.gserviceaccount.com`<br><br>If this variable is left undefined, Worker Pool will not be used for the Cloud Build Triggers. | `string` | `""` | no |
2728

2829
## Outputs
2930

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
// ============== TERRAFOM SCRIPT - CLOUD BUILD WORKER POOL PERMISSIONS ==============
3+
// This is an optional terraform script
4+
// - Assigns workerPoolUser to Cloud Build Service Agent and Service Account
5+
// - Allows the use of worker pool in separate project
6+
// - Admin projects will be able to build images using workerpool
7+
// ******************
8+
// ** REQUIREMENTS **
9+
// ******************
10+
// To run this script in AppFactory Pipeline:
11+
// - Application Factory Pipeline SA must have `roles/resourcemanager.projectIamAdmin` on the workerpool project
12+
13+
locals {
14+
projects_re = "projects/([^/]+)/"
15+
worker_pool_project = regex(local.projects_re, var.worker_pool_id)[0]
16+
}
17+
18+
data "google_project" "admin_projects" {
19+
project_id = local.admin_project_id
20+
}
21+
22+
resource "google_project_iam_member" "assign_permissions" {
23+
project = local.worker_pool_project
24+
role = "roles/cloudbuild.workerPoolUser"
25+
member = "serviceAccount:service-${data.google_project.admin_projects.number}@gcp-sa-cloudbuild.iam.gserviceaccount.com"
26+
}
27+
28+
resource "google_project_iam_member" "assign_permissions_service_agent" {
29+
project = local.worker_pool_project
30+
role = "roles/cloudbuild.workerPoolUser"
31+
member = "serviceAccount:${data.google_project.admin_projects.number}@cloudbuild.gserviceaccount.com"
32+
}
33+
34+
resource "google_project_iam_member" "sd_viewer" {
35+
project = local.worker_pool_project
36+
role = "roles/servicedirectory.viewer"
37+
member = "serviceAccount:service-${data.google_project.admin_projects.number}@gcp-sa-cloudbuild.iam.gserviceaccount.com"
38+
}
39+
40+
resource "google_project_iam_member" "access_network" {
41+
project = local.worker_pool_project
42+
role = "roles/servicedirectory.pscAuthorizedService"
43+
member = "serviceAccount:service-${data.google_project.admin_projects.number}@gcp-sa-cloudbuild.iam.gserviceaccount.com"
44+
}

0 commit comments

Comments
 (0)