Skip to content

Commit 30000d3

Browse files
Merge branch 'master' into dns-refactory
2 parents cc17b6a + 1b08c16 commit 30000d3

File tree

91 files changed

+861
-297
lines changed

Some content is hidden

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

91 files changed

+861
-297
lines changed

.github/workflows/go-lint.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ jobs:
3636
matrix:
3737
folder: [helpers/foundation-deployer]
3838
steps:
39-
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
40-
- uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2
39+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
40+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
4141
with:
4242
go-version-file: ${{ matrix.folder }}/go.mod
4343
cache-dependency-path: ${{ matrix.folder }}/go.sum
4444
- name: golangci-lint
45-
uses: golangci/golangci-lint-action@aaa42aa0628b4ae2578232a66b541047968fac86 # v6.1.0
45+
uses: golangci/golangci-lint-action@971e284b6050e8a5849b72094c50ab08da042db8 # v6.1.1
4646
with:
4747
version: latest
4848
working-directory: ${{ matrix.folder }}

.github/workflows/go-test.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ jobs:
3838
matrix:
3939
folder: [helpers/foundation-deployer]
4040
steps:
41-
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
42-
- uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2
41+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
42+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
4343
with:
4444
go-version-file: ${{ matrix.folder }}/go.mod
4545
cache-dependency-path: ${{ matrix.folder }}/go.sum

0-bootstrap/README.md

Lines changed: 174 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ This repository is intended as an example to be forked, tweaked, and maintained
6464
Though this blueprint can help accelerate your foundation design and build, we assume that you have the engineering skills and teams to deploy and customize your own foundation based on your own requirements.
6565

6666
We will support:
67-
- Code is semantically valid, pinned to known good versions, and passes terraform validate and lint checks
68-
- All PR to this repo must pass integration tests to deploy all resources into a test environment before being merged
69-
- Feature requests about ease of use of the code, or feature requests that generally apply to all users, are welcome
67+
68+
- Code is semantically valid, pinned to known good versions, and passes terraform validate and lint checks
69+
- All PR to this repo must pass integration tests to deploy all resources into a test environment before being merged
70+
- Feature requests about ease of use of the code, or feature requests that generally apply to all users, are welcome
7071

7172
We will not support:
72-
- In-place upgrades from a foundation deployed with an earlier version to a more recent version, even for minor version changes, might not be feasible. Repository maintainers do not have visibility to what resources a user deploys on top of their foundation or how the foundation was customized in deployment, so we make no guarantee about avoiding breaking changes.
73-
- Feature requests that are specific to a single user's requirement and not representative of general best practices
73+
74+
- In-place upgrades from a foundation deployed with an earlier version to a more recent version, even for minor version changes, might not be feasible. Repository maintainers do not have visibility to what resources a user deploys on top of their foundation or how the foundation was customized in deployment, so we make no guarantee about avoiding breaking changes.
75+
- Feature requests that are specific to a single user's requirement and not representative of general best practices
7476

7577
## Prerequisites
7678

@@ -86,21 +88,25 @@ To run the commands described in this document, install the following:
8688
Version 1.5.7 is the last version before the license model change. To use a later version of Terraform, ensure that the Terraform version used in the Operational System to manually execute part of the steps in `3-networks` and `4-projects` is the same version configured in the following code
8789

8890
- 0-bootstrap/modules/jenkins-agent/variables.tf
91+
8992
```
9093
default = "1.5.7"
9194
```
9295

9396
- 0-bootstrap/cb.tf
97+
9498
```
9599
terraform_version = "1.5.7"
96100
```
97101

98102
- scripts/validate-requirements.sh
103+
99104
```
100105
TF_VERSION="1.5.7"
101106
```
102107

103108
- build/github-tf-apply.yaml
109+
104110
```
105111
terraform_version: '1.5.7'
106112
```
@@ -112,6 +118,7 @@ Version 1.5.7 is the last version before the license model change. To use a late
112118
```
113119

114120
- 0-bootstrap/Dockerfile
121+
115122
```
116123
ARG TERRAFORM_VERSION=1.5.7
117124
```
@@ -136,7 +143,9 @@ Set the variables in **terraform.tfvars** (`groups` block) to use the specific g
136143
# example:
137144
gcloud organizations add-iam-policy-binding ${ORG_ID} --member=user:$SUPER_ADMIN_EMAIL --role=roles/securitycenter.admin --quiet > /dev/null 1>&1
138145
```
146+
139147
1. Enable the following additional services on your current bootstrap project:
148+
140149
```bash
141150
gcloud services enable cloudresourcemanager.googleapis.com
142151
gcloud services enable cloudbilling.googleapis.com
@@ -342,11 +351,164 @@ The following steps introduce the steps to deploy with Cloud Build Alternatively
342351

343352
## Running Terraform locally
344353

345-
If you deploy using Cloud Build, the bucket information is replaced in the state
346-
backends as part of the build process when the build is executed by Cloud Build.
347-
If you want to execute Terraform locally, you need to add your Cloud
348-
Storage bucket to the `backend.tf` files.
349-
Each step has instructions for this change.
354+
The following steps will guide you through deploying without using Cloud Build.
355+
356+
1. Clone [terraform-example-foundation](https://github.com/terraform-google-modules/terraform-example-foundation) into your local environment and create to the `gcp-bootstrap` folder at the same level. Copy the `0-bootstrap` content and `.gitignore` to `gcp-bootstrap`.
357+
358+
```bash
359+
git clone https://github.com/terraform-google-modules/terraform-example-foundation.git
360+
361+
mkdir gcp-bootstrap
362+
363+
cp -R terraform-example-foundation/0-bootstrap/* gcp-bootstrap/
364+
365+
cp terraform-example-foundation/.gitignore gcp-bootstrap
366+
```
367+
368+
1. Navigate to `gcp-bootstrap` and initialize a local Git repository to manage versions locally. Then, Create the environment branches.
369+
370+
```bash
371+
cd gcp-bootstrap
372+
373+
git init
374+
git commit -m "initialize empty directory" --allow-empty
375+
git checkout -b plan
376+
377+
git checkout -b shared
378+
```
379+
380+
1. Rename `terraform.example.tfvars` to `terraform.tfvars` and update the file with values from your environment:
381+
382+
```bash
383+
mv terraform.example.tfvars terraform.tfvars
384+
```
385+
386+
1. Rename `cb.tf` to `cb.tf.example`:
387+
388+
```bash
389+
mv cb.tf cb.tf.example
390+
```
391+
392+
1. Comment Cloud Build related outputs at `outputs.tf`.
393+
394+
1. In `sa.tf` file, comment out lines related to Cloud Build. Specifically, search for `cicd_project_iam_member` and comment out the corresponding module, as well as the "depends_on" meta-argument in any modules that depend on the commented module.
395+
396+
1. In `sa.tf` file, search for `local.cicd_project_id` and comment out the corresponding code.
397+
398+
1. Use the helper script [validate-requirements.sh](../scripts/validate-requirements.sh) to validate your environment:
399+
400+
```bash
401+
../terraform-example-foundation/scripts/validate-requirements.sh -o <ORGANIZATION_ID> -b <BILLING_ACCOUNT_ID> -u <END_USER_EMAIL>
402+
```
403+
404+
**Note:** The script is not able to validate if the user is in a Cloud Identity or Google Workspace group with the required roles.
405+
406+
1. Run `terraform init` and `terraform plan` and review the output.
407+
408+
```bash
409+
git checkout plan
410+
terraform init
411+
terraform plan -input=false -out bootstrap.tfplan
412+
```
413+
414+
1. Create a new folder called gcp-policies at the same directory level as the `terraform-example-foundation` folder. Initialize a Git repository, create a branch called `main`, and copy the contents of the `policy-library` directory from the `terraform-example-foundation` folder into the gcp-policies folder.
415+
416+
```bash
417+
cd ../
418+
419+
mkdir gcp-policies
420+
421+
cd gcp-policies
422+
git init
423+
git checkout -b main
424+
cp -RT ../terraform-example-foundation/policy-library/ .
425+
```
426+
427+
1. Commit changes to the main branch of the policy repo. This way you can manage versions locally.
428+
429+
```bash
430+
git add .
431+
git commit -m 'Initialize policy library repo'
432+
```
433+
434+
1. Navigate back to `gcp-bootstrap` repo.
435+
436+
```bash
437+
cd ../gcp-bootstrap
438+
```
439+
440+
1. To validate your policies, run `gcloud beta terraform vet`. For installation instructions, see [Install Google Cloud CLI](https://cloud.google.com/docs/terraform/policy-validation/validate-policies#install).
441+
442+
1. Run the following commands and check for violations:
443+
444+
```bash
445+
export VET_PROJECT_ID=A-VALID-PROJECT-ID
446+
terraform show -json bootstrap.tfplan > bootstrap.json
447+
gcloud beta terraform vet bootstrap.json --policy-library="$(pwd)/../gcp-policies" --project ${VET_PROJECT_ID}
448+
```
449+
450+
*`A-VALID-PROJECT-ID`* must be an existing project you have access to. This is necessary because `gcloud beta terraform vet` needs to link resources to a valid Google Cloud Platform project.
451+
452+
1. Commit validated code in plan branch.
453+
454+
```bash
455+
git add .
456+
git commit -m "Initial version os gcp-bootstrap."
457+
```
458+
459+
1. Checkout `shared` branch and merge the `plan` branch into it. Then, Run `terraform apply`.
460+
461+
```bash
462+
git checkout shared
463+
git merge plan
464+
465+
terraform apply bootstrap.tfplan
466+
```
467+
468+
1. Run `terraform output` to get the email address of the terraform service accounts that will be used to run steps manually and the state bucket that will be used by step `4-projects`.
469+
470+
```bash
471+
export network_step_sa=$(terraform output -raw networks_step_terraform_service_account_email)
472+
export projects_step_sa=$(terraform output -raw projects_step_terraform_service_account_email)
473+
export projects_gcs_bucket_tfstate=$(terraform output -raw projects_gcs_bucket_tfstate)
474+
475+
echo "network step service account = ${network_step_sa}"
476+
echo "projects step service account = ${projects_step_sa}"
477+
echo "projects gcs bucket tfstate = ${projects_gcs_bucket_tfstate}"
478+
```
479+
480+
1. Copy the backend and update `backend.tf` with the name of your Google Cloud Storage bucket for Terraform's state. Also update the `backend.tf` of all steps.
481+
482+
```bash
483+
export backend_bucket=$(terraform output -raw gcs_bucket_tfstate)
484+
echo "backend_bucket = ${backend_bucket}"
485+
486+
export backend_bucket_projects=$(terraform output -raw projects_gcs_bucket_tfstate)
487+
echo "backend_bucket_projects = ${backend_bucket_projects}"
488+
489+
cp backend.tf.example backend.tf
490+
491+
cd ../
492+
493+
for i in `find . -name 'backend.tf'`; do sed -i'' -e "s/UPDATE_ME/${backend_bucket}/" $i; done
494+
for i in `find . -name 'backend.tf'`; do sed -i'' -e "s/UPDATE_PROJECTS_BACKEND/${backend_bucket_projects}/" $i; done
495+
496+
cd gcp-bootstrap
497+
```
498+
499+
1. Re-run `terraform init`. When you're prompted, agree to copy Terraform state to Cloud Storage.
500+
501+
```bash
502+
terraform init
503+
```
504+
505+
1. Commit the new code version, so you can manage versions locally.
506+
507+
```sh
508+
git add backend.tf
509+
git commit -m "Init gcs backend."
510+
cd ../
511+
```
350512

351513
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
352514
## Inputs
@@ -361,12 +523,14 @@ Each step has instructions for this change.
361523
| default\_region\_2 | Secondary default region to create resources where applicable. | `string` | `"us-west1"` | no |
362524
| default\_region\_gcs | Case-Sensitive default region to create gcs resources where applicable. | `string` | `"US"` | no |
363525
| default\_region\_kms | Secondary default region to create kms resources where applicable. | `string` | `"us"` | no |
526+
| folder\_deletion\_protection | Prevent Terraform from destroying or recreating the folder. | `string` | `true` | no |
364527
| folder\_prefix | Name prefix to use for folders created. Should be the same in all steps. | `string` | `"fldr"` | no |
365528
| groups | Contain the details of the Groups to be created. | <pre>object({<br> create_required_groups = optional(bool, false)<br> create_optional_groups = optional(bool, false)<br> billing_project = optional(string, null)<br> required_groups = object({<br> group_org_admins = string<br> group_billing_admins = string<br> billing_data_users = string<br> audit_data_users = string<br> })<br> optional_groups = optional(object({<br> gcp_security_reviewer = optional(string, "")<br> gcp_network_viewer = optional(string, "")<br> gcp_scc_admin = optional(string, "")<br> gcp_global_secrets_admin = optional(string, "")<br> gcp_kms_admin = optional(string, "")<br> }), {})<br> })</pre> | n/a | yes |
366529
| initial\_group\_config | Define the group configuration when it is initialized. Valid values are: WITH\_INITIAL\_OWNER, EMPTY and INITIAL\_GROUP\_CONFIG\_UNSPECIFIED. | `string` | `"WITH_INITIAL_OWNER"` | no |
367530
| org\_id | GCP Organization ID | `string` | n/a | yes |
368531
| org\_policy\_admin\_role | Additional Org Policy Admin role for admin group. You can use this for testing purposes. | `bool` | `false` | no |
369532
| parent\_folder | Optional - for an organization with existing projects or for development/validation. It will place all the example foundation resources under the provided folder instead of the root organization. The value is the numeric folder ID. The folder must already exist. | `string` | `""` | no |
533+
| project\_deletion\_policy | The deletion policy for the project created. | `string` | `"PREVENT"` | no |
370534
| project\_prefix | Name prefix to use for projects created. Should be the same in all steps. Max size is 3 characters. | `string` | `"prj"` | no |
371535

372536
## Outputs

0-bootstrap/cb.tf

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ locals {
2222

2323
cicd_project_id = module.tf_source.cloudbuild_project_id
2424

25-
state_bucket_kms_key = "projects/${module.seed_bootstrap.seed_project_id}/locations/${var.default_region}/keyRings/${var.project_prefix}-keyring/cryptoKeys/${var.project_prefix}-key"
26-
2725
bucket_self_link_prefix = "https://www.googleapis.com/storage/v1/b/"
2826
default_state_bucket_self_link = "${local.bucket_self_link_prefix}${module.seed_bootstrap.gcs_bucket_tfstate}"
2927
gcp_projects_state_bucket_self_link = module.gcp_projects_state_bucket.bucket.self_link
@@ -70,7 +68,7 @@ resource "random_string" "suffix" {
7068

7169
module "gcp_projects_state_bucket" {
7270
source = "terraform-google-modules/cloud-storage/google//modules/simple_bucket"
73-
version = "~> 6.0"
71+
version = "~> 8.0"
7472

7573
name = "${var.bucket_prefix}-${module.seed_bootstrap.seed_project_id}-gcp-projects-tfstate"
7674
project_id = module.seed_bootstrap.seed_project_id
@@ -86,7 +84,7 @@ module "gcp_projects_state_bucket" {
8684

8785
module "tf_source" {
8886
source = "terraform-google-modules/bootstrap/google//modules/tf_cloudbuild_source"
89-
version = "~> 8.0"
87+
version = "~> 9.0"
9088

9189
org_id = var.org_id
9290
folder_id = google_folder.bootstrap.id
@@ -96,6 +94,8 @@ module "tf_source" {
9694
group_org_admins = var.groups.required_groups.group_org_admins
9795
buckets_force_destroy = var.bucket_force_destroy
9896

97+
project_deletion_policy = var.project_deletion_policy
98+
9999
activate_apis = [
100100
"serviceusage.googleapis.com",
101101
"servicenetworking.googleapis.com",
@@ -134,6 +134,15 @@ module "tf_source" {
134134
depends_on = [module.seed_bootstrap]
135135
}
136136

137+
resource "google_project_service_identity" "workflows_identity" {
138+
provider = google-beta
139+
140+
project = module.tf_source.cloudbuild_project_id
141+
service = "workflows.googleapis.com"
142+
143+
depends_on = [module.tf_source]
144+
}
145+
137146
module "tf_private_pool" {
138147
source = "./modules/cb-private-pool"
139148

@@ -155,7 +164,7 @@ module "tf_private_pool" {
155164

156165
module "tf_cloud_builder" {
157166
source = "terraform-google-modules/bootstrap/google//modules/tf_cloudbuild_builder"
158-
version = "~> 8.0"
167+
version = "~> 9.0"
159168

160169
project_id = module.tf_source.cloudbuild_project_id
161170
dockerfile_repo_uri = module.tf_source.csr_repos[local.cloudbuilder_repo].url
@@ -206,7 +215,7 @@ module "build_terraform_image" {
206215

207216
module "tf_workspace" {
208217
source = "terraform-google-modules/bootstrap/google//modules/tf_cloudbuild_workspace"
209-
version = "~> 8.0"
218+
version = "~> 9.0"
210219
for_each = local.granular_sa
211220

212221
project_id = module.tf_source.cloudbuild_project_id

0-bootstrap/github.tf.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ locals {
7070

7171
module "gh_cicd" {
7272
source = "terraform-google-modules/project-factory/google"
73-
version = "~> 15.0"
73+
version = "~> 17.0"
7474

7575
name = "${var.project_prefix}-b-cicd-wif-gh"
7676
random_project_id = true
@@ -87,6 +87,8 @@ module "gh_cicd" {
8787
"cloudresourcemanager.googleapis.com",
8888
"iamcredentials.googleapis.com",
8989
]
90+
91+
deletion_policy = var.project_deletion_policy
9092
}
9193

9294
module "gh_oidc" {

0-bootstrap/gitlab.tf.example

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ provider "gitlab" {
8181

8282
module "gitlab_cicd" {
8383
source = "terraform-google-modules/project-factory/google"
84-
version = "~> 15.0"
84+
version = "~> 17.0"
8585

8686
name = "${var.project_prefix}-b-cicd-wif-gl"
8787
random_project_id = true
@@ -100,8 +100,9 @@ module "gitlab_cicd" {
100100
"sts.googleapis.com",
101101
"dns.googleapis.com",
102102
"secretmanager.googleapis.com",
103-
104103
]
104+
105+
deletion_policy = var.project_deletion_policy
105106
}
106107

107108
module "gitlab_oidc" {

0 commit comments

Comments
 (0)