Skip to content
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

enhancement#93 : Implemented TF code for firestore creation. #114

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c5225d1
108: Updated the readme.md files from [distributed], [per-project] an…
AkshayKalbhor Apr 23, 2023
bed6791
Merge branch 'cloudspannerecosystem:master' into master
AkshayKalbhor Apr 26, 2023
bfc396c
fix: 108 - Fix for Firestore database create cmd.
AkshayKalbhor May 4, 2023
67c8722
Merge branch 'cloudspannerecosystem:master' into master
AkshayKalbhor May 4, 2023
3a18be9
fix: 108 updated formatting
AkshayKalbhor May 4, 2023
a388d40
updated files formatting to resolve build issues.
AkshayKalbhor May 6, 2023
8331e3c
fix: 108 updated files according to rc's.
AkshayKalbhor May 10, 2023
e39203a
Merge branch 'cloudspannerecosystem:master' into master
AkshayKalbhor May 10, 2023
4fd5a58
Explained Firestore update command
davidcueva May 10, 2023
ae1b6e6
Fixed linter issue
davidcueva May 10, 2023
c2c6ae4
enhancement#93: Initial commit.
AkshayKalbhor May 12, 2023
610c4ec
Merge branch 'cloudspannerecosystem:master' into master
AkshayKalbhor May 12, 2023
7865d12
updated poller SA variables.
AkshayKalbhor May 13, 2023
050a4a2
deleted unused variable
AkshayKalbhor May 15, 2023
d002716
Updated the flow of setting up firestore by providing an option to set
AkshayKalbhor May 15, 2023
fc0bf7f
updated file
AkshayKalbhor May 15, 2023
dbfdc36
updated missing variables
AkshayKalbhor May 15, 2023
2867582
commented condition.
AkshayKalbhor May 16, 2023
5e37716
updated condition for creating firestore
AkshayKalbhor May 16, 2023
ddd9108
updated the readme steps, included the firestore variable to be set
AkshayKalbhor May 16, 2023
a9213e7
updated cloud function files
AkshayKalbhor May 17, 2023
f234ca3
updated formatting
AkshayKalbhor May 17, 2023
9f1432d
Updated files to remove formatting errors
AkshayKalbhor May 17, 2023
33ac88e
updated formatting
AkshayKalbhor May 18, 2023
4ed3dd6
updated text for resolving formatting errors
AkshayKalbhor May 18, 2023
a719493
Merge branch 'cloudspannerecosystem:master' into master
AkshayKalbhor Jul 6, 2023
58f3710
updated files as per the comments.
AkshayKalbhor Jul 6, 2023
0e0726b
Merge branch 'master' of https://github.com/AkshayKalbhor/autoscaler
AkshayKalbhor Jul 6, 2023
b673ec2
updated formatting.
AkshayKalbhor Jul 6, 2023
d6566ae
updated line length to resolve markdown failures.
AkshayKalbhor Jul 7, 2023
8764752
Merge branch 'cloudspannerecosystem:master' into master
AkshayKalbhor Jul 7, 2023
d29fb0e
Merge branch 'master' of https://github.com/AkshayKalbhor/autoscaler
AkshayKalbhor Jul 7, 2023
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
29 changes: 6 additions & 23 deletions terraform/cloud-functions/distributed/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,33 +161,26 @@ Autoscaler infrastructure, with the exception of Cloud Scheduler, lives.
cloudresourcemanager.googleapis.com
```

6. Create a Google App Engine app, to enable the APIs for Cloud Scheduler and Firestore.
6. The Autoscaler state can be stored in either Firestore or Cloud Spanner.

```sh
gcloud app create --region="${AUTOSCALER_APP_ENGINE_LOCATION}"
```

7. The Autoscaler state can be stored in either Firestore or Cloud Spanner.

In case you want to use Firestore, update the database created with the
Google App Engine app to use [Firestore native mode][firestore-native].
In case you want to use Firestore, set the value of the following variable
to true.

```sh
gcloud firestore databases update --type=firestore-native
export TF_VAR_terraform_firestore_create=true
```

In case you want to use Cloud Spanner, no action is needed at this point.

### Deploying the Autoscaler

1. Set the project ID, region, zone and App Engine location in the
1. Set the project ID, region and zone in the
corresponding Terraform environment variables

```sh
export TF_VAR_project_id="${AUTOSCALER_PROJECT_ID}"
export TF_VAR_region="${AUTOSCALER_REGION}"
export TF_VAR_zone="${AUTOSCALER_ZONE}"
export TF_VAR_location="${AUTOSCALER_APP_ENGINE_LOCATION}"
```

2. Change directory into the Terraform scaler-project directory and initialize
Expand Down Expand Up @@ -229,14 +222,12 @@ topic and function in the project where the Spanner instances live.
gcloud config set project "${APP_PROJECT_ID}"
```

4. Choose the [region and zone][region-and-zone] and
[App Engine Location][app-engine-location] where the Application project
4. Choose the [region and zone][region-and-zone] where the Application project
will be located.

```sh
export APP_REGION=us-central1
export APP_ZONE=us-central1-c
export APP_APP_ENGINE_LOCATION=us-central
```

5. Use the following command to enable the Cloud APIs:
Expand All @@ -252,13 +243,6 @@ topic and function in the project where the Spanner instances live.
cloudbuild.googleapis.com
```

6. Create an App to enable Cloud Scheduler, but do not create a Firestore
database:

```sh
gcloud app create --region="${APP_APP_ENGINE_LOCATION}"
```

### Deploy the Application infrastructure

1. Set the project ID, region, zone and App Engine location in the
Expand All @@ -268,7 +252,6 @@ topic and function in the project where the Spanner instances live.
export TF_VAR_project_id="${APP_PROJECT_ID}"
export TF_VAR_region="${APP_REGION}"
export TF_VAR_zone="${APP_ZONE}"
export TF_VAR_location="${APP_APP_ENGINE_LOCATION}"
```

2. If you want to create a new Spanner instance for testing the Autoscaler, set
Expand Down
44 changes: 15 additions & 29 deletions terraform/cloud-functions/per-project/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,12 @@ In this section you prepare your project for deployment.
gcloud config set project "${PROJECT_ID}"
```

4. Choose the [region and zone][region-and-zone] and
[App Engine Location][app-engine-location] where the Autoscaler
4. Choose the [region and zone][region-and-zone] where the Autoscaler
infrastructure will be located.

```sh
export REGION=us-central1
export ZONE=us-central1-c
export APP_ENGINE_LOCATION=us-central
```

5. Enable the required Cloud APIs
Expand All @@ -155,24 +153,6 @@ In this section you prepare your project for deployment.
cloudresourcemanager.googleapis.com
```

6. Create a Google App Engine app, to enable the APIs
for Cloud Scheduler and Firestore

```sh
gcloud app create --region="${APP_ENGINE_LOCATION}"
```

7. The Autoscaler state can be stored in either Firestore or Cloud Spanner.

In case you want to use Firestore, update the database created with the
Google App Engine app to use [Firestore native mode][firestore-native].

```sh
gcloud firestore databases update --type=firestore-native
```

In case you want to use Cloud Spanner, no action is needed at this point.

## Deploying the Autoscaler

1. Set the project ID, region and zone in the corresponding Terraform
Expand Down Expand Up @@ -203,10 +183,16 @@ In this section you prepare your project for deployment.
For more information on how to make your Spanner instance to be managed by
Terraform, see [Importing your Spanner instances](#importing-your-spanner-instances)

3. If you chose to store the state in Firestore, skip this step. If you want
to store the state in Cloud Spanner and you don't have a Spanner
instance yet for that, then set the following variable so that Terraform
creates an instance for you named `autoscale-test-state`:
3. If you chose to store the state in Firestore, set the value of the following
variable to true and continue to step 5.

```sh
export TF_VAR_terraform_firestore_create=true
```

4. Alternatively, if you want to store the state in Cloud Spanner and you don't
have a Spanner instance yet for that, then set the following variable so that
Terraform creates an instance for you named `autoscale-test-state`.

```sh
export TF_VAR_terraform_spanner_state=true
Expand All @@ -223,7 +209,7 @@ In this section you prepare your project for deployment.
```

If you already have a Spanner instance where state must be stored,
only set the the name of your instance:
only set the the name of your instance.

```sh
export TF_VAR_state_spanner_name=<INSERT_YOUR_STATE_SPANNER_INSTANCE_NAME>
Expand All @@ -244,20 +230,20 @@ In this section you prepare your project for deployment.
For more information on how to make your existing Spanner instance to be
managed by Terraform, see [Importing your Spanner instances](../per-project/README.md#importing-your-spanner-instances)

4. Change directory into the Terraform per-project directory and initialize it.
5. Change directory into the Terraform per-project directory and initialize it.

```sh
cd "${AUTOSCALER_DIR}"
terraform init
```

5. Import the existing AppEngine application into Terraform state
6. Import the existing AppEngine application into Terraform state

```sh
terraform import module.scheduler.google_app_engine_application.app "${PROJECT_ID}"
```

6. Create the Autoscaler infrastructure. Answer `yes` when prompted, after
7. Create the Autoscaler infrastructure. Answer `yes` when prompted, after
reviewing the resources that Terraform intends to create.

```sh
Expand Down
25 changes: 25 additions & 0 deletions terraform/gke/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,31 @@ In this section you prepare your project for deployment.

## Using Firestore for Autoscaler state

### Setting up Firestore for storing Autoscaler state using terraform

1. If you want to create a new Firestore database for storing the state of the
Autoscaler, set the following variable.

```sh
export TF_VAR_terraform_firestore_create=true
```

2. Enable the additional APIs:

```sh
gcloud services enable \
appengine.googleapis.com \
firestore.googleapis.com
```

3. Next, continue to [Deploying the Autoscaler](#deploying-the-autoscaler)

On the other hand if you do not want to create the Firestore database using
terraform,then continue with the below steps to manually create the Firestore
database using gcloud commands.

### Manually setting up Firestore for storing Autoscaler state

1. To use Firestore for the Autoscaler state, choose the
[App Engine Location][app-engine-location] where the Autoscaler
infrastructure will be created, for example:
Expand Down
7 changes: 4 additions & 3 deletions terraform/gke/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ module "autoscaler-cluster" {
module "firestore" {
source = "../modules/firestore"

project_id = var.project_id
poller_sa_email = module.autoscaler-base.poller_sa_email
scaler_sa_email = module.autoscaler-base.scaler_sa_email
project_id = var.project_id
terraform_firestore_create = var.terraform_firestore_create
poller_sa_email = module.autoscaler-base.poller_sa_email
scaler_sa_email = module.autoscaler-base.scaler_sa_email
}

module "spanner" {
Expand Down
5 changes: 5 additions & 0 deletions terraform/gke/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ variable "terraform_dashboard" {
type = bool
default = true
}

variable "terraform_firestore_create" {
type = bool
default = false
}
18 changes: 18 additions & 0 deletions terraform/modules/firestore/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,21 @@ resource "google_project_iam_member" "scaler_sa_firestore" {
role = "roles/datastore.user"
member = "serviceAccount:${var.scaler_sa_email}"
}

resource "google_project_service" "firestore" {
count = var.terraform_firestore_create ? 1 : 0

project = var.project_id
service = "firestore.googleapis.com"
}

resource "google_firestore_database" "database" {
count = var.terraform_firestore_create ? 1 : 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this is in the Terraform module for Firestore, wouldn't using the module indicate we want to create the Firestore database. Also choosing to not create the database while using the module would break the setup, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello,

thanks for the review,
I sort of agree with you but if we do not provide an option then we will end up forcing the user to use Firestore. What in case if the user wants to use Spanner to store the Autoscaler state?
Should we still create the Firestore db as a default setup and let the user use Spanner without utilizing the provisioned Firestore db?

Let me know if my understanding is not accurate.

Thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Previously, if the user wanted to store state in Spanner the documentation instructed them to skip the gcloud command to create the Firestore database. Now with the module users in that situation should just not include the Firestore module in their Terraform.


project = var.project_id
name = "(default)"
location_id = var.location
type = var.firestore_database_type

depends_on = [google_project_service.firestore]
}
26 changes: 22 additions & 4 deletions terraform/modules/firestore/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ variable "project_id" {
type = string
}

variable "region" {
description = "The region where the infrastructure will be deployed."
type = string
default = "us-central1"
}

variable "location" {
description = "The location within the region where the infrastructure will be deployed."
type = string
default = "nam5"
}

variable "firestore_database_type" {
bgood marked this conversation as resolved.
Show resolved Hide resolved
description = "The database type. TYPE must be one of: datastore-mode, firestore-native"
type = string
default = "FIRESTORE_NATIVE"
}

variable "poller_sa_email" {
type = string
}
Expand All @@ -26,7 +44,7 @@ variable "scaler_sa_email" {
type = string
}

variable "region" {
type = string
default = "us-central1"
}
variable "terraform_firestore_create" {
type = bool
default = false
}