Skip to content

Commit d1a56d4

Browse files
iyabchenbharathkkb
andauthored
feat: update document and script to use gcloud beta terraform vet (#729)
* update document and script to use gcloud beta terraform vet * sync bootstrap change * generate docs * use bootstrap master * use git branch for bootstrap module * Change seed source to bootstrap github branch * Fix mismatch module * move bootstrap to use 6.0 * update gcloud image version to 393.0.0 Co-authored-by: Bharath KKB <[email protected]>
1 parent 4c84d80 commit d1a56d4

File tree

15 files changed

+42
-51
lines changed

15 files changed

+42
-51
lines changed

0-bootstrap/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ your current Jenkins manager (controller) environment.
115115
```
116116
1. Run `terraform init`.
117117
1. Run `terraform plan` and review the output.
118-
1. To run terraform-validator steps please follow the [instructions](https://github.com/GoogleCloudPlatform/terraform-validator/blob/main/docs/install.md) in the **Install Terraform Validator** section and install version `v0.4.0`. You will also need to rename the binary from `terraform-validator-<your-platform>` to `terraform-validator` and the terraform-validator binary must be in your PATH.
118+
1. To run terraform-validator steps please follow the [instructions](https://cloud.google.com/docs/terraform/policy-validation/validate-policies#install) to install the terraform-tools component.
119119
1. Run `terraform plan -input=false -out bootstrap.tfplan`
120120
1. Run `terraform show -json bootstrap.tfplan > bootstrap.json`
121-
1. Run `terraform-validator validate bootstrap.json --policy-path="../policy-library" --project <A-VALID-PROJECT-ID>` and check for violations (`<A-VALID-PROJECT-ID>` must be an existing project you have access to, this is necessary because Terraform-validator needs to link resources to a valid Google Cloud Platform project).
121+
1. Run `gcloud beta terraform vet bootstrap.json --policy-library="../policy-library" --project <A-VALID-PROJECT-ID>` and check for violations (`<A-VALID-PROJECT-ID>` must be an existing project you have access to, this is necessary because Terraform-validator needs to link resources to a valid Google Cloud Platform project).
122122
1. Run `terraform apply`.
123123
1. Run `terraform output terraform_service_account` to get the email address of the admin. You need this address in a later procedure.
124124
1. Run `terraform output gcs_bucket_tfstate` to get your Google Cloud bucket name from Terraform's state.

0-bootstrap/main.tf

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ resource "google_folder" "bootstrap" {
4141

4242
module "seed_bootstrap" {
4343
source = "terraform-google-modules/bootstrap/google"
44-
version = "~> 5.0"
44+
version = "~> 6.0"
4545
org_id = var.org_id
4646
folder_id = google_folder.bootstrap.id
4747
project_id = "${var.project_prefix}-b-seed"
@@ -102,7 +102,7 @@ resource "google_billing_account_iam_member" "tf_billing_admin" {
102102
// Comment-out the cloudbuild_bootstrap module and its outputs if you want to use Jenkins instead of Cloud Build
103103
module "cloudbuild_bootstrap" {
104104
source = "terraform-google-modules/bootstrap/google//modules/cloudbuild"
105-
version = "~> 5.0"
105+
version = "~> 6.0"
106106
org_id = var.org_id
107107
folder_id = google_folder.bootstrap.id
108108
project_id = "${var.project_prefix}-b-cicd"
@@ -117,7 +117,6 @@ module "cloudbuild_bootstrap" {
117117
cloudbuild_apply_filename = "cloudbuild-tf-apply.yaml"
118118
project_prefix = var.project_prefix
119119
cloud_source_repos = var.cloud_source_repos
120-
terraform_validator_release = "v0.6.0"
121120
terraform_version = "0.13.7"
122121
terraform_version_sha256sum = "4a52886e019b4fdad2439da5ff43388bbcc6cce9784fde32c53dcd0e28ca9957"
123122

0-bootstrap/modules/jenkins-agent/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ module "jenkins_bootstrap" {
114114

115115
- **VPN Connectivity with on-prem:** Once you run this module, a Jenkins Agent is created in the CICD project in GCP. Please add VPN connectivity manually by following our user guide about [how to deploy a VPN tunnel in GCP](https://cloud.google.com/network-connectivity/docs/vpn/how-to). This VPN is necessary to allow communication between the Jenkins Controller (on prem or in a cloud environment) with the Jenkins Agent in the CICD project.
116116

117-
- **Binaries and packages for the Jenkins Agent:** The Jenkins Agent is a new GCE instance created by this module. After creation, the startup script needs to fetch several binaries for later use, during pipelines execution. These binaries include `java`, `terraform`, `terraform-validator` and any other binary you use in your own scripts. You have several options to make these binaries and libraries available to the Jenkins Agent:
117+
- **Binaries and packages for the Jenkins Agent:** The Jenkins Agent is a new GCE instance created by this module. After creation, the startup script needs to fetch several binaries for later use, during pipelines execution. These binaries include `java`, `terraform` and any other binary you use in your own scripts. You have several options to make these binaries and libraries available to the Jenkins Agent:
118118
- allow the Jenkins Agent Internet access (ideally through Cloud NAT, implemented by default).
119119
- allow the Jenkins Agent access to local package repositories on your premises, ideally through the VPN connection.
120120
- preparing a golden image for the Jenkins Agent (and assign the image to the `jenkins_agent_gce_instance.boot_disk.initialize_params.image` terraform variable). You can create the golden images with tools like Packer. Although, you might still need network access to download dependencies while running a pipeline.

0-bootstrap/modules/jenkins-agent/files/jenkins_gce_startup_script.sh

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,12 @@ wget "https://releases.hashicorp.com/terraform/${tpl_TERRAFORM_VERSION}/terrafor
4141
apt-get clean && \
4242
rm -rf /var/lib/apt/lists/*
4343

44-
echo "**** Startup Step 6/9: Download and install the Terraform validator ****"
45-
gsutil cp gs://terraform-validator/releases/v0.4.0/terraform-validator-linux-amd64 .
46-
chmod 755 "${tpl_TERRAFORM_DIR}terraform-validator-linux-amd64"
47-
mv "${tpl_TERRAFORM_DIR}terraform-validator-linux-amd64" "${tpl_TERRAFORM_DIR}terraform-validator"
44+
echo "**** Startup Step 6/9: Download and install the Terraform tools. ****"
45+
gcloud components update
46+
gcloud components install terraform-tools
4847

49-
echo "**** Startup Step 7/9: Set the Linux PATH to point to the Terraform directory. ****"
50-
export PATH=$PATH:${tpl_TERRAFORM_DIR}
48+
echo "**** Startup Step 7/9: Verify that the tool is installed. ****"
49+
gcloud beta terraform vet --help
5150

5251
echo "**** Startup Step 8/9: Create jenkins user. ****"
5352
# Home directory for the jenkins user

1-org/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,11 @@ to run the command as the Terraform service account.
282282
We will now deploy our environment (production) using this script.
283283
When using Cloud Build or Jenkins as your CI/CD tool each environment corresponds to a branch is the repository for 1-org step and only the corresponding environment is applied.
284284
285-
To use the `validate` option of the `tf-wrapper.sh` script, please follow the [instructions](https://github.com/GoogleCloudPlatform/terraform-validator/blob/main/docs/install.md) in the **Install Terraform Validator** section and install version `v0.4.0` in your system. You will also need to rename the binary from `terraform-validator-<your-platform>` to `terraform-validator` and the `terraform-validator` binary must be in your `PATH`.
285+
To use the `validate` option of the `tf-wrapper.sh` script, please follow the [instructions](https://cloud.google.com/docs/terraform/policy-validation/validate-policies#install) to install the terraform-tools component.
286286
287287
1. Run `./tf-wrapper.sh init production`.
288-
1. Run `./tf-wrapper.sh plan production` and review output.
289-
1. Run `./tf-wrapper.sh validate production $(pwd)/../policy-library <YOUR_CLOUD_BUILD_PROJECT_ID>` and check for violations.
290-
1. Run `./tf-wrapper.sh apply production`.
288+
2. Run `./tf-wrapper.sh plan production` and review output.
289+
3. Run `./tf-wrapper.sh validate production $(pwd)/../policy-library <YOUR_CLOUD_BUILD_PROJECT_ID>` and check for violations.
290+
4. Run `./tf-wrapper.sh apply production`.
291291
292292
If you received any errors or made any changes to the Terraform config or `terraform.tfvars` you must re-run `./tf-wrapper.sh plan production` before run `./tf-wrapper.sh apply production`.

2-environments/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -222,19 +222,19 @@ You can run `terraform output gcs_bucket_tfstate` in the 0-bootstrap folder to o
222222
We will now deploy each of our environments(development/production/non-production) using this script.
223223
When using Cloud Build or Jenkins as your CI/CD tool each environment corresponds to a branch is the repository for 2-environments step and only the corresponding environment is applied.
224224
225-
To use the `validate` option of the `tf-wrapper.sh` script, please follow the [instructions](https://github.com/GoogleCloudPlatform/terraform-validator/blob/main/docs/install.md) in the **Install Terraform Validator** section and install version `v0.4.0` in your system. You will also need to rename the binary from `terraform-validator-<your-platform>` to `terraform-validator` and the `terraform-validator` binary must be in your `PATH`.
225+
To use the `validate` option of the `tf-wrapper.sh` script, please follow the [instructions](https://cloud.google.com/docs/terraform/policy-validation/validate-policies#install) to install the terraform-tools component.
226226
227227
1. Run `./tf-wrapper.sh init development`.
228-
1. Run `./tf-wrapper.sh plan development` and review output.
229-
1. Run `./tf-wrapper.sh validate development $(pwd)/../policy-library <YOUR_CLOUD_BUILD_PROJECT_ID>` and check for violations.
230-
1. Run `./tf-wrapper.sh apply development`.
231-
1. Run `./tf-wrapper.sh init non-production`.
232-
1. Run `./tf-wrapper.sh plan non-production` and review output.
233-
1. Run `./tf-wrapper.sh validate non-production $(pwd)/../policy-library <YOUR_CLOUD_BUILD_PROJECT_ID>` and check for violations.
234-
1. Run `./tf-wrapper.sh apply non-production`.
235-
1. Run `./tf-wrapper.sh init production`.
236-
1. Run `./tf-wrapper.sh plan production` and review output.
237-
1. Run `./tf-wrapper.sh validate production $(pwd)/../policy-library <YOUR_CLOUD_BUILD_PROJECT_ID>` and check for violations.
238-
1. Run `./tf-wrapper.sh apply production`.
228+
2. Run `./tf-wrapper.sh plan development` and review output.
229+
3. Run `./tf-wrapper.sh validate development $(pwd)/../policy-library <YOUR_CLOUD_BUILD_PROJECT_ID>` and check for violations.
230+
4. Run `./tf-wrapper.sh apply development`.
231+
5. Run `./tf-wrapper.sh init non-production`.
232+
6. Run `./tf-wrapper.sh plan non-production` and review output.
233+
7. Run `./tf-wrapper.sh validate non-production $(pwd)/../policy-library <YOUR_CLOUD_BUILD_PROJECT_ID>` and check for violations.
234+
8. Run `./tf-wrapper.sh apply non-production`.
235+
9. Run `./tf-wrapper.sh init production`.
236+
10. Run `./tf-wrapper.sh plan production` and review output.
237+
11. Run `./tf-wrapper.sh validate production $(pwd)/../policy-library <YOUR_CLOUD_BUILD_PROJECT_ID>` and check for violations.
238+
12. Run `./tf-wrapper.sh apply production`.
239239
240240
If you received any errors or made any changes to the Terraform config or `terraform.tfvars` you must re-run `./tf-wrapper.sh plan <env>` before running `./tf-wrapper.sh apply <env>`.

3-networks-dual-svpc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ We will now deploy each of our environments(development/production/non-productio
291291
When using Cloud Build or Jenkins as your CI/CD tool each environment corresponds to a branch in the repository for 3-networks-dual-svpc step
292292
and only the corresponding environment is applied.
293293
294-
To use the `validate` option of the `tf-wrapper.sh` script, please follow the [instructions](https://github.com/GoogleCloudPlatform/terraform-validator/blob/main/docs/install.md) in the **Install Terraform Validator** section and install version `v0.4.0` in your system. You will also need to rename the binary from `terraform-validator-<your-platform>` to `terraform-validator` and the `terraform-validator` binary must be in your `PATH`.
294+
To use the `validate` option of the `tf-wrapper.sh` script, please follow the [instructions](https://cloud.google.com/docs/terraform/policy-validation/validate-policies#install) to install the terraform-tools component.
295295
296296
1. Run `./tf-wrapper.sh init shared`.
297297
1. Run `./tf-wrapper.sh plan shared` and review output.

4-projects/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ commands. The `-T` flag is needed for Linux, but causes problems for MacOS.
303303
We will now deploy each of our environments(development/production/non-production) using this script.
304304
When using Cloud Build or Jenkins as your CI/CD tool each environment corresponds to a branch is the repository for 4-projects step and only the corresponding environment is applied. Environment shared must be applied first because development, non-production, and production depend on it.
305305
306-
To use the `validate` option of the `tf-wrapper.sh` script, please follow the [instructions](https://github.com/GoogleCloudPlatform/terraform-validator/blob/main/docs/install.md) in the **Install Terraform Validator** section and install version `v0.4.0` in your system. You will also need to rename the binary from `terraform-validator-<your-platform>` to `terraform-validator` and the `terraform-validator` binary must be in your `PATH`.
306+
To use the `validate` option of the `tf-wrapper.sh` script, please follow the [instructions](https://cloud.google.com/docs/terraform/policy-validation/validate-policies#install) to install the terraform-tools component.
307307
308308
1. Run `./tf-wrapper.sh init shared`.
309309
1. Run `./tf-wrapper.sh plan shared` and review output.

4-projects/modules/infra_pipelines/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
| default\_region | Default region to create resources where applicable. | `string` | n/a | yes |
1313
| folders\_to\_grant\_browser\_role | List of folders to grant browser role to the cloud build service account. Used by terraform validator to able to load IAM policies. | `list(string)` | `[]` | no |
1414
| gar\_repo\_name | Custom name to use for GAR repo. | `string` | `""` | no |
15+
| gcloud\_version | Default gcloud image version. | `string` | `"393.0.0-slim"` | no |
1516
| impersonate\_service\_account | Service account email of the account to impersonate to run Terraform | `string` | n/a | yes |
1617
| project\_prefix | Name prefix to use for projects created. | `string` | `"prj"` | no |
1718
| terraform\_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> "non-production",<br> "production"<br>]</pre> | no |
18-
| terraform\_validator\_release | Default terraform-validator release. | `string` | `"v0.4.0"` | no |
1919
| terraform\_version | Default terraform version. | `string` | `"0.13.7"` | no |
2020
| terraform\_version\_sha256sum | sha256sum for default terraform version. | `string` | `"4a52886e019b4fdad2439da5ff43388bbcc6cce9784fde32c53dcd0e28ca9957"` | no |
2121

4-projects/modules/infra_pipelines/cloudbuild_builder/Dockerfile

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,25 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
FROM gcr.io/cloud-builders/gcloud-slim
15+
ARG GCLOUD_VERSION=slim
16+
FROM google/cloud-sdk:${GCLOUD_VERSION}
1617

1718
# Use ARG so that values can be overriden by user/cloudbuild
1819
ARG TERRAFORM_VERSION=0.13.7
1920
ARG TERRAFORM_VERSION_SHA256SUM=4a52886e019b4fdad2439da5ff43388bbcc6cce9784fde32c53dcd0e28ca9957
20-
ARG TERRAFORM_VALIDATOR_RELEASE=v0.4.0
2121

2222
ENV ENV_TERRAFORM_VERSION=$TERRAFORM_VERSION
2323
ENV ENV_TERRAFORM_VERSION_SHA256SUM=$TERRAFORM_VERSION_SHA256SUM
24-
ENV ENV_TERRAFORM_VALIDATOR_RELEASE=$TERRAFORM_VALIDATOR_RELEASE
2524

2625
RUN apt-get update && \
27-
/builder/google-cloud-sdk/bin/gcloud -q components install alpha beta && \
2826
apt-get -y install curl jq unzip git ca-certificates && \
2927
curl https://releases.hashicorp.com/terraform/${ENV_TERRAFORM_VERSION}/terraform_${ENV_TERRAFORM_VERSION}_linux_amd64.zip \
3028
> terraform_linux_amd64.zip && \
3129
echo "${ENV_TERRAFORM_VERSION_SHA256SUM} terraform_linux_amd64.zip" > terraform_SHA256SUMS && \
3230
sha256sum -c terraform_SHA256SUMS --status && \
31+
mkdir -p /builder && \
3332
unzip terraform_linux_amd64.zip -d /builder/terraform && \
34-
rm -f terraform_linux_amd64.zip && \
35-
gsutil cp gs://terraform-validator/releases/${ENV_TERRAFORM_VALIDATOR_RELEASE}/terraform-validator-linux-amd64 /builder/terraform/terraform-validator && \
36-
chmod +x /builder/terraform/terraform-validator && \
37-
apt-get remove --purge -y curl unzip && \
33+
rm -f terraform_linux_amd64.zip terraform_SHA256SUMS && \
3834
apt-get --purge -y autoremove && \
3935
apt-get clean && \
4036
rm -rf /var/lib/apt/lists/*

0 commit comments

Comments
 (0)