Enable managed connection pooling in modules/alloydb (#4009)
* Enable managed connection pooling * Add validation to connection_pool_flags * Update README --------- Co-authored-by: Julio Castillo <jccb@google.com>
This commit is contained in:
@@ -19,6 +19,7 @@ Note that this module assumes that some options are the same for both the primar
|
||||
- [PSC instance with auto connections](#psc-instance-with-auto-connections)
|
||||
- [Custom flags and users definition](#custom-flags-and-users-definition)
|
||||
- [CMEK encryption](#cmek-encryption)
|
||||
- [Managed connection pooling](#managed-connection-pooling)
|
||||
- [Tag bindings](#tag-bindings)
|
||||
- [Variables](#variables)
|
||||
- [Outputs](#outputs)
|
||||
@@ -406,6 +407,32 @@ module "alloydb" {
|
||||
# tftest inventory=cmek.yaml e2e
|
||||
```
|
||||
|
||||
### Managed connection pooling
|
||||
|
||||
```hcl
|
||||
module "alloydb" {
|
||||
source = "./fabric/modules/alloydb"
|
||||
project_id = var.project_id
|
||||
project_number = var.project_number
|
||||
cluster_name = "db"
|
||||
location = var.region
|
||||
instance_name = "db"
|
||||
network_config = {
|
||||
psa_config = {
|
||||
network = var.vpc.id
|
||||
}
|
||||
}
|
||||
|
||||
connection_pool_flags = {
|
||||
"pool_mode" = "transaction"
|
||||
"max_pool_size" = "240"
|
||||
}
|
||||
|
||||
deletion_protection = false
|
||||
}
|
||||
# tftest modules=1 resources=2 inventory=connection_pool.yaml e2e
|
||||
```
|
||||
|
||||
## Tag bindings
|
||||
|
||||
Refer to the [Creating and managing tags](https://cloud.google.com/resource-manager/docs/tags/tags-creating-and-managing) documentation for details on usage.
|
||||
@@ -451,37 +478,38 @@ module "alloydb" {
|
||||
| name | description | type | required | default |
|
||||
|---|---|:---:|:---:|:---:|
|
||||
| [cluster_name](variables.tf#L84) | Name of the primary cluster. | <code>string</code> | ✓ | |
|
||||
| [instance_name](variables.tf#L211) | Name of primary instance. | <code>string</code> | ✓ | |
|
||||
| [location](variables.tf#L223) | Region or zone of the cluster and instance. | <code>string</code> | ✓ | |
|
||||
| [network_config](variables.tf#L268) | Network configuration for cluster and instance. Only one between psa_config and psc_config can be used. | <code>object({…})</code> | ✓ | |
|
||||
| [project_id](variables.tf#L324) | The ID of the project where this instances will be created. | <code>string</code> | ✓ | |
|
||||
| [instance_name](variables.tf#L224) | Name of primary instance. | <code>string</code> | ✓ | |
|
||||
| [location](variables.tf#L236) | Region or zone of the cluster and instance. | <code>string</code> | ✓ | |
|
||||
| [network_config](variables.tf#L281) | Network configuration for cluster and instance. Only one between psa_config and psc_config can be used. | <code>object({…})</code> | ✓ | |
|
||||
| [project_id](variables.tf#L337) | The ID of the project where this instances will be created. | <code>string</code> | ✓ | |
|
||||
| [annotations](variables.tf#L17) | Map FLAG_NAME=>VALUE for annotations which allow client tools to store small amount of arbitrary data. | <code>map(string)</code> | | <code>null</code> |
|
||||
| [automated_backup_configuration](variables.tf#L23) | Automated backup settings for cluster. | <code>object({…})</code> | | <code>{}</code> |
|
||||
| [availability_type](variables.tf#L61) | Availability type for the primary replica. Either `ZONAL` or `REGIONAL`. | <code>string</code> | | <code>"REGIONAL"</code> |
|
||||
| [client_connection_config](variables.tf#L67) | Client connection config. | <code>object({…})</code> | | <code>null</code> |
|
||||
| [cluster_display_name](variables.tf#L78) | Display name of the primary cluster. | <code>string</code> | | <code>null</code> |
|
||||
| [continuous_backup_configuration](variables.tf#L90) | Continuous backup settings for cluster. | <code>object({…})</code> | | <code>{}</code> |
|
||||
| [cross_region_replication](variables.tf#L100) | Cross region replication config. | <code>object({…})</code> | | <code>{}</code> |
|
||||
| [database_version](variables.tf#L157) | Database type and version to create. | <code>string</code> | | <code>"POSTGRES_15"</code> |
|
||||
| [deletion_policy](variables.tf#L163) | AlloyDB cluster and instance deletion policy. | <code>string</code> | | <code>null</code> |
|
||||
| [deletion_protection](variables.tf#L169) | Whether Terraform will be prevented from destroying the cluster. When the field is set to true or unset in Terraform state, a terraform apply or terraform destroy that would delete the cluster will fail. When the field is set to false, deleting the cluster is allowed. | <code>bool</code> | | <code>null</code> |
|
||||
| [display_name](variables.tf#L175) | AlloyDB instance display name. | <code>string</code> | | <code>null</code> |
|
||||
| [encryption_config](variables.tf#L181) | Set encryption configuration. KMS name format: 'projects/[PROJECT]/locations/[REGION]/keyRings/[RING]/cryptoKeys/[KEY_NAME]'. | <code>object({…})</code> | | <code>null</code> |
|
||||
| [flags](variables.tf#L190) | Map FLAG_NAME=>VALUE for database-specific tuning. | <code>map(string)</code> | | <code>null</code> |
|
||||
| [gce_zone](variables.tf#L196) | The GCE zone that the instance should serve from. This can ONLY be specified for ZONAL instances. If present for a REGIONAL instance, an error will be thrown. | <code>string</code> | | <code>null</code> |
|
||||
| [initial_user](variables.tf#L202) | AlloyDB cluster initial user credentials. | <code>object({…})</code> | | <code>null</code> |
|
||||
| [labels](variables.tf#L217) | Labels to be attached to all instances. | <code>map(string)</code> | | <code>null</code> |
|
||||
| [machine_config](variables.tf#L229) | AlloyDB machine config. | <code>object({…})</code> | | <code>{}</code> |
|
||||
| [maintenance_config](variables.tf#L243) | Set maintenance window configuration. | <code>object({…})</code> | | <code>{}</code> |
|
||||
| [observability_config](variables.tf#L298) | Advanced query insights config for AlloyDB. Mutually exclusive with query_insights_config. | <code>object({…})</code> | | <code>null</code> |
|
||||
| [prefix](variables.tf#L314) | Optional prefix used to generate instance names. | <code>string</code> | | <code>null</code> |
|
||||
| [project_number](variables.tf#L329) | The project number of the project where this instances will be created. Only used for testing purposes. | <code>string</code> | | <code>null</code> |
|
||||
| [query_insights_config](variables.tf#L335) | Query insights config. Mutually exclusive with observability_config. It will be ignored if observability_config is enabled. | <code>object({…})</code> | | <code>{}</code> |
|
||||
| [read_pool](variables.tf#L346) | Map of read pool instances to create in the primary cluster. | <code>map(object({…}))</code> | | <code>{}</code> |
|
||||
| [skip_await_major_version_upgrade](variables.tf#L402) | Set to true to skip awaiting on the major version upgrade of the cluster. | <code>bool</code> | | <code>true</code> |
|
||||
| [subscription_type](variables.tf#L408) | The subscription type of cluster. Possible values are: 'STANDARD' or 'TRIAL'. | <code>string</code> | | <code>"STANDARD"</code> |
|
||||
| [tag_bindings](variables.tf#L414) | Tag bindings for this service, in key => tag value id format. | <code>map(string)</code> | | <code>{}</code> |
|
||||
| [users](variables.tf#L421) | Map of users to create in the primary instance (and replicated to other replicas). Set PASSWORD to null if you want to get an autogenerated password. The user types available are: 'ALLOYDB_BUILT_IN' or 'ALLOYDB_IAM_USER'. | <code>map(object({…}))</code> | | <code>{}</code> |
|
||||
| [connection_pool_flags](variables.tf#L90) | Map FLAG_NAME=>VALUE for managed connection pooling. | <code>map(string)</code> | | <code>null</code> |
|
||||
| [continuous_backup_configuration](variables.tf#L103) | Continuous backup settings for cluster. | <code>object({…})</code> | | <code>{}</code> |
|
||||
| [cross_region_replication](variables.tf#L113) | Cross region replication config. | <code>object({…})</code> | | <code>{}</code> |
|
||||
| [database_version](variables.tf#L170) | Database type and version to create. | <code>string</code> | | <code>"POSTGRES_15"</code> |
|
||||
| [deletion_policy](variables.tf#L176) | AlloyDB cluster and instance deletion policy. | <code>string</code> | | <code>null</code> |
|
||||
| [deletion_protection](variables.tf#L182) | Whether Terraform will be prevented from destroying the cluster. When the field is set to true or unset in Terraform state, a terraform apply or terraform destroy that would delete the cluster will fail. When the field is set to false, deleting the cluster is allowed. | <code>bool</code> | | <code>null</code> |
|
||||
| [display_name](variables.tf#L188) | AlloyDB instance display name. | <code>string</code> | | <code>null</code> |
|
||||
| [encryption_config](variables.tf#L194) | Set encryption configuration. KMS name format: 'projects/[PROJECT]/locations/[REGION]/keyRings/[RING]/cryptoKeys/[KEY_NAME]'. | <code>object({…})</code> | | <code>null</code> |
|
||||
| [flags](variables.tf#L203) | Map FLAG_NAME=>VALUE for database-specific tuning. | <code>map(string)</code> | | <code>null</code> |
|
||||
| [gce_zone](variables.tf#L209) | The GCE zone that the instance should serve from. This can ONLY be specified for ZONAL instances. If present for a REGIONAL instance, an error will be thrown. | <code>string</code> | | <code>null</code> |
|
||||
| [initial_user](variables.tf#L215) | AlloyDB cluster initial user credentials. | <code>object({…})</code> | | <code>null</code> |
|
||||
| [labels](variables.tf#L230) | Labels to be attached to all instances. | <code>map(string)</code> | | <code>null</code> |
|
||||
| [machine_config](variables.tf#L242) | AlloyDB machine config. | <code>object({…})</code> | | <code>{}</code> |
|
||||
| [maintenance_config](variables.tf#L256) | Set maintenance window configuration. | <code>object({…})</code> | | <code>{}</code> |
|
||||
| [observability_config](variables.tf#L311) | Advanced query insights config for AlloyDB. Mutually exclusive with query_insights_config. | <code>object({…})</code> | | <code>null</code> |
|
||||
| [prefix](variables.tf#L327) | Optional prefix used to generate instance names. | <code>string</code> | | <code>null</code> |
|
||||
| [project_number](variables.tf#L342) | The project number of the project where this instances will be created. Only used for testing purposes. | <code>string</code> | | <code>null</code> |
|
||||
| [query_insights_config](variables.tf#L348) | Query insights config. Mutually exclusive with observability_config. It will be ignored if observability_config is enabled. | <code>object({…})</code> | | <code>{}</code> |
|
||||
| [read_pool](variables.tf#L359) | Map of read pool instances to create in the primary cluster. | <code>map(object({…}))</code> | | <code>{}</code> |
|
||||
| [skip_await_major_version_upgrade](variables.tf#L415) | Set to true to skip awaiting on the major version upgrade of the cluster. | <code>bool</code> | | <code>true</code> |
|
||||
| [subscription_type](variables.tf#L421) | The subscription type of cluster. Possible values are: 'STANDARD' or 'TRIAL'. | <code>string</code> | | <code>"STANDARD"</code> |
|
||||
| [tag_bindings](variables.tf#L427) | Tag bindings for this service, in key => tag value id format. | <code>map(string)</code> | | <code>{}</code> |
|
||||
| [users](variables.tf#L434) | Map of users to create in the primary instance (and replicated to other replicas). Set PASSWORD to null if you want to get an autogenerated password. The user types available are: 'ALLOYDB_BUILT_IN' or 'ALLOYDB_IAM_USER'. | <code>map(object({…}))</code> | | <code>{}</code> |
|
||||
|
||||
## Outputs
|
||||
|
||||
|
||||
@@ -218,6 +218,14 @@ resource "google_alloydb_instance" "primary" {
|
||||
}
|
||||
}
|
||||
|
||||
dynamic "connection_pool_config" {
|
||||
for_each = var.connection_pool_flags != null ? [""] : []
|
||||
content {
|
||||
enabled = true
|
||||
flags = var.connection_pool_flags
|
||||
}
|
||||
}
|
||||
|
||||
machine_config {
|
||||
cpu_count = var.machine_config.cpu_count
|
||||
machine_type = var.machine_config.machine_type
|
||||
@@ -439,6 +447,14 @@ resource "google_alloydb_instance" "secondary" {
|
||||
}
|
||||
}
|
||||
|
||||
dynamic "connection_pool_config" {
|
||||
for_each = var.connection_pool_flags != null ? [""] : []
|
||||
content {
|
||||
enabled = true
|
||||
flags = var.connection_pool_flags
|
||||
}
|
||||
}
|
||||
|
||||
machine_config {
|
||||
cpu_count = coalesce(try(var.cross_region_replication.secondary_machine_config.cpu_count, null), var.machine_config.cpu_count)
|
||||
machine_type = local.secondary_machine_type
|
||||
@@ -526,6 +542,14 @@ resource "google_alloydb_instance" "read_pool_primary" {
|
||||
}
|
||||
}
|
||||
|
||||
dynamic "connection_pool_config" {
|
||||
for_each = var.connection_pool_flags != null ? [""] : []
|
||||
content {
|
||||
enabled = true
|
||||
flags = var.connection_pool_flags
|
||||
}
|
||||
}
|
||||
|
||||
machine_config {
|
||||
cpu_count = each.value.machine_config.cpu_count
|
||||
machine_type = each.value.machine_config.machine_type
|
||||
@@ -623,6 +647,14 @@ resource "google_alloydb_instance" "read_pool_secondary" {
|
||||
}
|
||||
}
|
||||
|
||||
dynamic "connection_pool_config" {
|
||||
for_each = var.connection_pool_flags != null ? [""] : []
|
||||
content {
|
||||
enabled = true
|
||||
flags = var.connection_pool_flags
|
||||
}
|
||||
}
|
||||
|
||||
machine_config {
|
||||
cpu_count = each.value.machine_config.cpu_count
|
||||
machine_type = each.value.machine_config.machine_type
|
||||
|
||||
@@ -87,6 +87,19 @@ variable "cluster_name" {
|
||||
nullable = false
|
||||
}
|
||||
|
||||
variable "connection_pool_flags" {
|
||||
description = "Map FLAG_NAME=>VALUE for managed connection pooling."
|
||||
type = map(string)
|
||||
default = null
|
||||
validation {
|
||||
condition = var.connection_pool_flags == null || alltrue([
|
||||
for k in keys(var.connection_pool_flags) :
|
||||
(!startswith(k, "connection-pooling-") && length(regexall("-", k)) == 0)
|
||||
])
|
||||
error_message = "Remove the \"connection-pooling-\" prefix and use underscores instead of dashes in the name. For example, \"connection-pooling-pool-mode\" would be \"pool_mode\"."
|
||||
}
|
||||
}
|
||||
|
||||
variable "continuous_backup_configuration" {
|
||||
description = "Continuous backup settings for cluster."
|
||||
type = object({
|
||||
|
||||
85
tests/modules/alloydb/examples/connection_pool.yaml
Normal file
85
tests/modules/alloydb/examples/connection_pool.yaml
Normal file
@@ -0,0 +1,85 @@
|
||||
# Copyright 2024 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
|
||||
#
|
||||
# http://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.
|
||||
|
||||
values:
|
||||
module.alloydb.google_alloydb_cluster.primary:
|
||||
annotations: null
|
||||
cluster_id: db
|
||||
cluster_type: PRIMARY
|
||||
continuous_backup_config:
|
||||
- enabled: true
|
||||
encryption_config: []
|
||||
recovery_window_days: 14
|
||||
database_version: POSTGRES_15
|
||||
deletion_policy: DEFAULT
|
||||
deletion_protection: false
|
||||
display_name: db
|
||||
effective_labels:
|
||||
goog-terraform-provisioned: 'true'
|
||||
encryption_config: []
|
||||
etag: null
|
||||
initial_user: []
|
||||
labels: null
|
||||
location: europe-west8
|
||||
maintenance_update_policy: []
|
||||
network_config:
|
||||
- allocated_ip_range: null
|
||||
network: projects/xxx/global/networks/aaa
|
||||
project: project-id
|
||||
psc_config: []
|
||||
restore_backup_source: []
|
||||
restore_backupdr_backup_source: []
|
||||
restore_backupdr_pitr_source: []
|
||||
restore_continuous_backup_source: []
|
||||
secondary_config: []
|
||||
skip_await_major_version_upgrade: true
|
||||
subscription_type: STANDARD
|
||||
terraform_labels:
|
||||
goog-terraform-provisioned: 'true'
|
||||
timeouts: null
|
||||
module.alloydb.google_alloydb_instance.primary:
|
||||
annotations: null
|
||||
availability_type: REGIONAL
|
||||
connection_pool_config:
|
||||
- enabled: true
|
||||
flags:
|
||||
max_pool_size: '240'
|
||||
pool_mode: transaction
|
||||
deletion_policy: DELETE
|
||||
display_name: db
|
||||
effective_labels:
|
||||
goog-terraform-provisioned: 'true'
|
||||
gce_zone: null
|
||||
instance_id: db
|
||||
instance_type: PRIMARY
|
||||
labels: null
|
||||
machine_config:
|
||||
- cpu_count: 2
|
||||
query_insights_config:
|
||||
- query_plans_per_minute: 5
|
||||
query_string_length: 1024
|
||||
record_application_tags: true
|
||||
record_client_address: true
|
||||
read_pool_config: []
|
||||
terraform_labels:
|
||||
goog-terraform-provisioned: 'true'
|
||||
timeouts: null
|
||||
|
||||
counts:
|
||||
google_alloydb_cluster: 1
|
||||
google_alloydb_instance: 1
|
||||
modules: 1
|
||||
resources: 2
|
||||
|
||||
outputs: {}
|
||||
Reference in New Issue
Block a user