Skip to content

ci: Initial Terraform configurations (Work in Progress) #8436

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

Closed
wants to merge 111 commits into from
Closed
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
111 commits
Select commit Hold shift + click to select a range
1d3f47c
ci: Initial Terraform configurations (Work in Progress)
burkedavison Sep 21, 2022
fb6b240
ci: Add Terraform scripts and configurations for java-accessapproval,…
burkedavison Sep 21, 2022
fe40718
fix: Remove hardcoded project prefix, and replace with environment va…
burkedavison Sep 21, 2022
65ff848
fix: Ensure 'set-quota-project' is set to the created GCP project. De…
burkedavison Sep 21, 2022
6f89ea6
fix: Workaround set-quota-project not working when previous quota pro…
burkedavison Sep 22, 2022
dad488e
chore: Add 'auto_create_subnetworks' to container network configuration.
burkedavison Sep 22, 2022
3959735
fix: Add predestroy stage. Remove container network from tfstate to a…
burkedavison Sep 22, 2022
3fbf756
chore: Refactor script to split project and module provisioning. Supp…
burkedavison Sep 22, 2022
061b666
chore: Add java-compute terraform config
burkedavison Sep 22, 2022
9eaac54
chore: Add java-containeranalysis terraform config
burkedavison Sep 22, 2022
1ddad88
chore: Add java-datacatalog terraform config
burkedavison Sep 22, 2022
638566e
chore: Add java-datalabeling terraform config
burkedavison Sep 22, 2022
45dfae5
chore: Add java-errorreporting terraform config
burkedavison Sep 22, 2022
1aee4e1
chore: Add java-game-servers terraform config
burkedavison Sep 22, 2022
57a1ef3
chore: Add java-iot terraform config
burkedavison Sep 22, 2022
52b4884
chore: Add java-kms terraform config
burkedavison Sep 22, 2022
fdf8198
chore: Add java-monitoring terraform config
burkedavison Sep 22, 2022
4c93bcb
chore: Add java-resourcemanager terraform config
burkedavison Sep 22, 2022
d8d5727
chore: Add java-secretmanager terraform config
burkedavison Sep 22, 2022
cbe9b98
chore: Add java-speech terraform config
burkedavison Sep 22, 2022
cf3c219
chore: Add java-trace terraform config
burkedavison Sep 22, 2022
f7543ae
chore: Add java-translate terraform config
burkedavison Sep 22, 2022
d5b11ca
chore: Add java-video-intelligence terraform config
burkedavison Sep 22, 2022
57aebd8
chore: Add java-vision terraform config
burkedavison Sep 22, 2022
5e8a943
chore: Refactor solution to use single generated Terraform root modul…
burkedavison Sep 26, 2022
3a817cf
chore: Add java-asset
burkedavison Sep 26, 2022
85d4ace
chore: Add java-iam-admin
burkedavison Sep 26, 2022
e13a6e9
chore: Add java-notebooks
burkedavison Sep 26, 2022
72a0d60
chore: Add java-texttospeech
burkedavison Sep 26, 2022
8594ff7
chore: Add java-dataproc
burkedavison Sep 26, 2022
51f1fd8
chore: Add java-tasks
burkedavison Sep 26, 2022
8f27d2a
fix: Remove unintentional .kokoro/build.sh modification.
burkedavison Sep 26, 2022
7efd5d4
fix: Ensure compute API is enabled prior to requesting compute defaul…
burkedavison Sep 27, 2022
cacbcec
chore: Create and Impersonate a Service Account on the Terraform-crea…
burkedavison Sep 27, 2022
13c2f1b
chore: Add java-dns terraform configuration
burkedavison Sep 27, 2022
329748c
chore: Add java-dialogflow and java-dialogflow-cx terraform configura…
burkedavison Sep 27, 2022
c75f5c8
fix: Ensure environment variables are correctly added when performing…
burkedavison Sep 28, 2022
41f1325
fix: Fail fast if terraform apply fails.
burkedavison Sep 28, 2022
8cecc8d
chore: Rework module output and env.sh design to eliminate need for f…
burkedavison Sep 28, 2022
ecfdd45
chore: Add java-notification support
burkedavison Sep 28, 2022
327c613
fix: Ensure environment variables are set with script if not already …
burkedavison Sep 28, 2022
977ce54
fix: Add time delay to allow role/permissions to apply to service acc…
burkedavison Sep 28, 2022
6c25de7
fix: Relocate invocation of generated-env.sh to ensure consistent app…
burkedavison Sep 28, 2022
3d4cc93
chore: Support 'y' argument to destroy.sh to destroy the project non-…
burkedavison Sep 28, 2022
5b71c3f
fix: Destroy modules before project to ensure clean starting point fo…
burkedavison Sep 28, 2022
36f146d
chore: Add test-individually.sh, which iterates through each terrafor…
burkedavison Sep 28, 2022
1f86837
fix: Apply predestroy.sh to <root>/.terraform state.
burkedavison Sep 28, 2022
cd1b082
chore: Add java-scheduler, although not yet reliable.
burkedavison Sep 29, 2022
03ef514
chore: Add java-oslogin. Not currently working.
burkedavison Sep 29, 2022
9ae6b1a
fix: Rework of Terraform logic to use single root module. Project and…
burkedavison Sep 30, 2022
74a6728
fix: cleanup
burkedavison Sep 30, 2022
040d9a3
fix: Ensure cloudresourcemanager.googleapis.com is enabled prior to s…
burkedavison Oct 3, 2022
be9b07c
Merge branch 'main' into terraform
burkedavison Oct 3, 2022
a50e5fd
fix: Workaround for 'Resource Exhausted' error given first time runni…
burkedavison Oct 3, 2022
01b0570
fix: Re-enable job and topic deletions during Scheduler IT clean up.
burkedavison Oct 3, 2022
5e05e29
fix: Re-enable application-default login and add delay after enabling…
burkedavison Oct 3, 2022
8d3ded0
fix: Apply retry to v1beta1 Scheduler IT
burkedavison Oct 3, 2022
42e677d
fix: Sleep immediately after gcloud project creation to allow default…
burkedavison Oct 3, 2022
30da4a1
fix: Add 10s delay after creating service account and before assignin…
burkedavison Oct 3, 2022
6a8b09a
fix: Replace compound assertion statement with fine-grain assertions …
burkedavison Oct 3, 2022
2529c10
Merge remote-tracking branch 'origin/terraform' into terraform
burkedavison Oct 3, 2022
7ee1e2a
fix: Add delay to allow service account permissions to settle.
burkedavison Oct 3, 2022
70ce173
chore: Handle generated-outputs.template.tf same as generated-main+va…
burkedavison Oct 3, 2022
e1f2f03
fix: Assign gcloud_account local variable before using it. Comment im…
burkedavison Oct 3, 2022
eb9a2f1
fix: Synchronize environment using common script when entering indivi…
burkedavison Oct 3, 2022
68e6e14
fix: Fail ITHeadersTest if server does not respond to request, rather…
burkedavison Oct 3, 2022
854dac7
fix: Sleep syntax, and no longer sync environment before apply.sh
burkedavison Oct 3, 2022
c800cdc
fix: DRY and remove unnecessary resources from template.
burkedavison Oct 3, 2022
5c204bd
Revert "fix: Fail ITHeadersTest if server does not respond to request…
burkedavison Oct 4, 2022
82de4ef
chore: Explicit error when headers is null.
burkedavison Oct 4, 2022
3b8c01b
chore: Make API enabling and disabling behaviors variables, following…
burkedavison Oct 4, 2022
ff29a16
fix: Increase retries and retry delay in Scheduler IT to ensure enoug…
burkedavison Oct 4, 2022
7eb3309
fix: Add region to submodule input object. Fix typo.
burkedavison Oct 4, 2022
dd7a68e
Merge branch 'main' into terraform
burkedavison Oct 4, 2022
acc1c28
fix: When running ITs with terraform, ignore os-login, recommender, a…
burkedavison Oct 4, 2022
77fec0e
chore: DRY friendly output module name, and module output value parsing.
burkedavison Oct 4, 2022
41bc320
fix: Randomize Dialogflow Agent name.
burkedavison Oct 4, 2022
8629ecd
fix: Randomize Container Network name.
burkedavison Oct 4, 2022
cacb6ed
fix: Attempt to refresh Terraform's state if no project ID is in its …
burkedavison Oct 4, 2022
69a760d
chore: Add java-recommender Terraform configuration, and adjust IT to…
burkedavison Oct 4, 2022
75c031d
chore: Improve ./.terraform/README.md
burkedavison Oct 4, 2022
92fa513
fix: Only invoke predestroy.sh for active modules.
burkedavison Oct 5, 2022
d6a936f
chore: Refactor all Terraform project setup steps into setup.sh
burkedavison Oct 5, 2022
fb6b69a
chore: Add java-talent Terraform configuration
burkedavison Oct 5, 2022
09cf5cb
chore: Simplify
burkedavison Oct 6, 2022
1a06ff9
fix: Consistent application of variable rename
burkedavison Oct 6, 2022
8611e97
fix: Wait 60s after service account creation to mitigate errors due t…
burkedavison Oct 6, 2022
3f18991
chore: Slightly lengthen initialization waiting time for service acco…
burkedavison Oct 6, 2022
b891f4d
fix: Additional delay and comment around scheduler retry.
burkedavison Oct 6, 2022
5778d9b
chore: Remove ./.terraform/README.md while offline review continues.
burkedavison Oct 6, 2022
8604095
Merge branch 'main' into terraform
burkedavison Oct 6, 2022
0c917af
fix: Don't prepend "serviceAccount:" when using member "allAuthentica…
burkedavison Oct 7, 2022
f479750
Merge branch 'main' into terraform
burkedavison Oct 7, 2022
f8277b8
chore: Change module delimiter from ':' to ',' for consistency with .…
burkedavison Oct 7, 2022
843c440
Merge branch 'main' into terraform
burkedavison Oct 7, 2022
0fef18c
chore: Split 'gcloud' steps from 'terraform' steps in preparation for…
burkedavison Oct 7, 2022
483dea6
fix: Return to previous working directory after plan.sh
burkedavison Oct 7, 2022
c5264f9
chore: Use set -eo pipefail rather than explicit "|| exit"
burkedavison Oct 10, 2022
42b099b
Merge branch 'main' into terraform
burkedavison Oct 11, 2022
70527ff
chore: Remove 'test-individually.sh'
burkedavison Oct 11, 2022
bc34548
fix: Ignore 'java-recommender' IT rather than modifying logic.
burkedavison Oct 11, 2022
56abe0c
Merge branch 'main' into terraform
burkedavison Oct 14, 2022
0db282c
chore: release main (#8610)
release-please[bot] Oct 14, 2022
6420712
build: have release-please tag releases (#8615)
chingor13 Oct 17, 2022
023793d
Merge branch 'main' into terraform
burkedavison Oct 17, 2022
ddae556
Merge branch 'main' into terraform
burkedavison Oct 19, 2022
3f9b482
fix: Revert ITHeadersTest changes to same as main
burkedavison Oct 19, 2022
5ac7c07
fix: Remove unused import
burkedavison Oct 19, 2022
a6dfc72
fix: Apply mvn com.coveo:fmt-maven-plugin:format
burkedavison Oct 19, 2022
09abe52
ci: Terraform now used in Kokoro integration tests
burkedavison Oct 24, 2022
23c4a91
ci: Temporarily disable Terraform in integration tests
burkedavison Oct 24, 2022
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
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,15 @@ api_key

# Python utilities
*.pyc

# Terraform
*.hcl
**/.terraform/.terraform/
**/.terraform/plugins/
**/.terraform/providers/
**/.terraform/plugin_path
*.lock.
*.tfstate
*.tfstate.backup
*.tfstate.*.backup
*.tfstate.lock.info
40 changes: 40 additions & 0 deletions .terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Terraform Integration Testing Script

### Command:

`test.sh [module]`

#### Examples:

```shell
test.sh java-accessapproval
```

Performs integration testing on the `java-accessapproval` submodule only.

```shell
test.sh
```

Performs integration testing on all modules defined in `modules-under-test.txt`.

### GCP Project Selection or Creation+Deletion

#### Using an existing project

If the environment variable `GOOGLE_CLOUD_PROJECT` is defined with a
GCP Project ID, that project will be used for all integration tests and
will not be deleted after use.

#### Automatic creation and deletion of single-use project

If the environment variable `GOOGLE_CLOUD_PROJECT` is not defined,
a single-use project will be created with a random suffix. In this
situation, the following environment variables will be used or
values will be prompted for them:

* `TF_VAR_folder_id` : [GCP Folder](https://cloud.google.com/resource-manager/docs/creating-managing-folders)
in which new GCP projects will be created
* `TF_VAR_billing_account` : [Billing account](https://cloud.google.com/billing/docs/concepts) to be
used for created GCP projects
* `TF_VAR_project_prefix` : Prefix to use for all created GCP projects
62 changes: 62 additions & 0 deletions .terraform/helpers/create-project.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash
#
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#####################
# Expected Current Directory: Root repo directory (/google-cloud-java)
# Expected Environment Variables:
# TF_VAR_folder_id : Folder in which new GCP projects will be created
# TF_VAR_billing_account : Billing account to be used for created GCP projects
# TF_VAR_project_prefix : Prefix to use for all created GCP projects
#####################

create_project_dir="./.terraform/modules/create-project"

function createProject() {
# Ensure required environment variables are set.
if [ -z "${TF_VAR_folder_id+x}" ]; then
echo -n "Which GCP folder should be used when creating new GCP projects? Set TF_VAR_folder_id environment variable: "
read -r folder_id
export TF_VAR_folder_id="${folder_id}"
fi
if [ -z "${TF_VAR_billing_account+x}" ]; then
echo -n "Which GCP billing account should be assigned to created GCP projects? Set TF_VAR_billing_account environment variable: "
read -r billing_acct
export TF_VAR_billing_account="${billing_acct}"
fi
if [ -z "${TF_VAR_project_prefix+x}" ]; then
echo -n "What should be the project prefix for any created GCP project? Set TF_VAR_project_prefix environment variable: "
read -r prefix
export TF_VAR_project_prefix="${prefix}"
fi

# Provision GCP Project
pushd "$create_project_dir" || exit

terraform init || exit
terraform plan || exit
terraform apply -auto-approve || exit
GOOGLE_CLOUD_PROJECT=$(terraform output -raw project_id)
export GOOGLE_CLOUD_PROJECT

popd || exit
}

function destroyProject() {
pushd "$create_project_dir" || exit
terraform destroy -auto-approve || exit
popd || exit
}
32 changes: 32 additions & 0 deletions .terraform/helpers/gcloud-login.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
#
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Perform gcloud auth login if no current credentials are available.
if gcloud auth application-default print-access-token &>/dev/null; then
true
else
if ! gcloud auth application-default login; then
exit
fi
fi
if gcloud auth print-access-token &>/dev/null; then
true
else
if ! gcloud auth login; then
exit
fi
fi
62 changes: 62 additions & 0 deletions .terraform/helpers/test-module.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash
#
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

function performTest() {
if [ -z "${GOOGLE_CLOUD_PROJECT+x}" ]; then
echo "Error: GOOGLE_CLOUD_PROJECT environment variable not set."
exit
fi

# Provision resources for module with configuration found in './<module>/.terraform' directory
pushd ./"$1"/.terraform || exit
terraform init || exit
terraform plan -var="project_id=$GOOGLE_CLOUD_PROJECT" || exit
terraform apply -auto-approve -var="project_id=$GOOGLE_CLOUD_PROJECT" || exit

# Set module-specific environment variables for upcoming integration test(s)
if [[ -f "./env.sh" ]]; then
source "./env.sh"
fi

# Perform integration testing
pushd ../ || exit
mvn -B ${INTEGRATION_TEST_ARGS} \
-ntp \
-Penable-integration-tests \
-DtrimStackTrace=false \
-Dclirr.skip=true \
-Denforcer.skip=true \
-Dcheckstyle.skip=true \
-fae \
verify

exit_code=$?
popd || exit

# Clean up provisioned resources regardless of exit code
if [[ -f "./predestroy.sh" ]]; then
source "./predestroy.sh"
fi
terraform destroy -auto-approve -var="project_id=$GOOGLE_CLOUD_PROJECT" || exit
popd || exit

# Stop execution if integration test failed
if [[ $exit_code != 0 ]]; then
echo "Execution stopped with error in $1"
exit
fi
}
20 changes: 20 additions & 0 deletions .terraform/modules-under-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Modules to be tested when no specific module is given to test.sh as an argument
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this file can be replaced by a command to find directories that contain Terraform configuration. With that we can eliminate a possibility of having discrepancy (Terraform files are added to modules but forget updating this file). What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion. TODO

java-accessapproval
java-bigqueryconnection
java-container
java-compute
java-containeranalysis
java-datacatalog
java-datalabeling
java-errorreporting
java-game-servers
java-iot
java-kms
java-monitoring
java-resourcemanager
java-secretmanager
java-speech
java-trace
java-translate
java-video-intelligence
java-vision
5 changes: 5 additions & 0 deletions .terraform/modules/create-project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 'create-project' Root Module

This 'root' module creates a GCP project with a given prefix
and a random ID suffix.

31 changes: 31 additions & 0 deletions .terraform/modules/create-project/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
terraform {
required_providers {
google = {
source = "hashicorp/google"
}
}
}

provider "google" {
region = var.region
zone = var.zone
}

resource "random_id" "id" {
byte_length = 3
keepers = {
folder_id = var.folder_id
billing_account = var.billing_account
}
}

locals {
project_id = lower("${var.project_prefix}-${random_id.id.hex}")
}

resource "google_project" "project" {
name = local.project_id
project_id = local.project_id
folder_id = random_id.id.keepers.folder_id
billing_account = random_id.id.keepers.billing_account
}
3 changes: 3 additions & 0 deletions .terraform/modules/create-project/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "project_id" {
value = google_project.project.project_id
}
28 changes: 28 additions & 0 deletions .terraform/modules/create-project/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
variable "project_prefix" {
type = string
description = "Prefix to use when creating the GCP project"
}

variable "folder_id" {
type = string
description = "GCP folder ID in which to create the project"
sensitive = true
}

variable "billing_account" {
type = string
description = "GCP Billing Account ID for the test project"
sensitive = true
}

variable "region" {
type = string
description = "GCP region to deploy resources to."
default = "us-central1"
}

variable "zone" {
type = string
description = "GCP zone to deploy resources to. Must be a zone in the chosen region."
default = "us-central1-c"
}
83 changes: 83 additions & 0 deletions .terraform/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/bash
#
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#####################
# This script uses the Terraform configurations found in the submodules
# of this repository to provision a given GCP project with the correct
# resources, perform integration testing, and then destroying the used
# resources.
#
# Expected Environment Variables:
# GOOGLE_CLOUD_PROJECT :
# If defined, no GCP project will be created or destroyed.
# If not defined, a GCP project will be created for single-use, then destroyed.
#
# If GOOGLE_CLOUD_PROJECT is not defined, the following environment variables
# are used. If not available, their values will be requested by prompt:
# TF_VAR_folder_id : Folder in which new GCP projects will be created
# TF_VAR_billing_account : Billing account to be used for created GCP projects
# TF_VAR_project_prefix : Prefix to use for all created GCP projects
#
#####################
# Single Module Usage:
# ./.terraform/test.sh <module> :: Tests only the given module
# ex: ./.terraform/test.sh java-accessapproval
#
# All Module Usage:
# ./.terraform/test.sh :: Tests all modules in modules-under-test.txt

# Ensure current directory is root repo folder
scriptDir="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
cd "$scriptDir/.." || exit

source ./.terraform/helpers/gcloud-login.sh
source ./.terraform/helpers/create-project.sh
source ./.terraform/helpers/test-module.sh

# Create a single-use project if one is not already provided via GOOGLE_CLOUD_PROJECT env var.
if [ -z "${GOOGLE_CLOUD_PROJECT+x}" ]; then
echo "GOOGLE_CLOUD_PROJECT environment variable not set. Creating single-use project!"
createProject
createdProject=true
else
createdProject=false
fi

# Use the project ID in gcloud set-quota-project. Clear the existing quota project directly from
# the configuration, and re-set.
gcloud config set project "$GOOGLE_CLOUD_PROJECT"
sed -i.bak '/quota_project_id/d' ~/.config/gcloud/application_default_credentials.json
gcloud auth application-default set-quota-project "$GOOGLE_CLOUD_PROJECT"

if [ -n "$1" ]; then
# If shell script given a specific module as its argument
performTest "$1"
else
# Otherwise, iterate through the list found in modules-under-test.txt
while IFS= read -r module; do
# Ignore lines starting with '#'
[[ "$module" =~ ^#.* ]] && continue

performTest "$module"
done <"./.terraform/modules-under-test.txt"
fi

if [ $createdProject ]; then
echo "Destroying single-use project $GOOGLE_CLOUD_PROJECT created at start."
destroyProject
export -n GOOGLE_CLOUD_PROJECT
fi
Loading