Skip to content

Commit

Permalink
FAST plugin system (#1266)
Browse files Browse the repository at this point in the history
* plugin folder, gitignore, serverless connector example

* add support to fast plugin variables and outputs to tfdoc

* rename folder, READMEs

* add variable description

* show diffs

* check documentation, use multiple files

* debug check doc

* try a different glob

* debug tfdoc names

* more debug

* and even more debug

* fix gitignore

* fix links

* support extra files in tests

* fix fixture, switch stage 2 peering to new tests

* tfdoc

* Allow globs in extra files

---------

Co-authored-by: Julio Castillo <jccb@google.com>
  • Loading branch information
2 people authored and lcaggio committed May 5, 2023
1 parent 83296f9 commit 1057dcd
Show file tree
Hide file tree
Showing 40 changed files with 401 additions and 145 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
- name: Check documentation
id: documentation-fabric
run: |
python3 tools/check_documentation.py modules fast blueprints
python3 tools/check_documentation.py --show-diffs modules fast blueprints
- name: Check documentation links
id: documentation-links-fabric
Expand Down
1 change: 0 additions & 1 deletion blueprints/serverless/api-gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Clone this repository or [open it in cloud shell](https://ssh.cloud.google.com/c

Once done testing, you can clean up resources by running `terraform destroy`.


<!-- BEGIN TFDOC -->

## Variables
Expand Down
2 changes: 2 additions & 0 deletions fast/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
stages/[0-9]*/local-*.tf
stages-multitenant/[0-9]*/local-*.tf
40 changes: 40 additions & 0 deletions fast/plugins/2-networking-serverless-connector/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# VPC Serverless Connector

This FAST plugin adds centralized [Serverless VPC Access Connectors](https://cloud.google.com/vpc/docs/serverless-vpc-access) to network stages.

This plugin does not manage

- IAM bindings for the connectors, which should be added via the stage project-level variables
- firewall rules for the connectors, which should be added via the stage factory

The plugin only requires a specific configuration if the defaults it uses need to be changed:

- the connector-specific subnets default to the `10.255.255.0` range
- the machine type, number of instances and thoughput use the API defaults

To enable the plugin, simply copy or link its files in the networking stage.

<!-- TFDOC OPTS files:1 show_extra:1 -->
<!-- BEGIN TFDOC -->

## Files

| name | description | modules | resources |
|---|---|---|---|
| [local-serverless-connector-outputs.tf](./local-serverless-connector-outputs.tf) | Serverless Connector outputs. | | <code>google_storage_bucket_object</code> · <code>local_file</code> |
| [local-serverless-connector-variables.tf](./local-serverless-connector-variables.tf) | Serverless Connector variables. | | |
| [local-serverless-connector.tf](./local-serverless-connector.tf) | Serverless Connector resources. | <code>net-vpc</code> | <code>google_vpc_access_connector</code> |

## Variables

| name | description | type | required | default | producer |
|---|---|:---:|:---:|:---:|:---:|
| [serverless_connector_config](local-serverless-connector-variables.tf#L19) | VPC Access Serverless Connectors configuration. | <code title="object&#40;&#123;&#10; dev-primary &#61; object&#40;&#123;&#10; ip_cidr_range &#61; optional&#40;string, &#34;10.255.255.128&#47;28&#34;&#41;&#10; machine_type &#61; optional&#40;string&#41;&#10; instances &#61; optional&#40;object&#40;&#123;&#10; max &#61; optional&#40;number&#41;&#10; min &#61; optional&#40;number&#41;&#10; &#125;&#41;, &#123;&#125;&#41;&#10; throughput &#61; optional&#40;object&#40;&#123;&#10; max &#61; optional&#40;number&#41;&#10; min &#61; optional&#40;number&#41;&#10; &#125;&#41;, &#123;&#125;&#41;&#10; &#125;&#41;&#10; prod-primary &#61; object&#40;&#123;&#10; ip_cidr_range &#61; optional&#40;string, &#34;10.255.255.0&#47;28&#34;&#41;&#10; machine_type &#61; optional&#40;string&#41;&#10; instances &#61; optional&#40;object&#40;&#123;&#10; max &#61; optional&#40;number&#41;&#10; min &#61; optional&#40;number&#41;&#10; &#125;&#41;, &#123;&#125;&#41;&#10; throughput &#61; optional&#40;object&#40;&#123;&#10; max &#61; optional&#40;number&#41;&#10; min &#61; optional&#40;number&#41;&#10; &#125;&#41;, &#123;&#125;&#41;&#10; &#125;&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; dev-primary &#61; &#123;&#125;&#10; prod-primary &#61; &#123;&#125;&#10;&#125;">&#123;&#8230;&#125;</code> | |

## Outputs

| name | description | sensitive | consumers |
|---|---|:---:|---|
| [plugin_sc_connectors](local-serverless-connector-outputs.tf#L43) | VPC Access Connectors. | | |

<!-- END TFDOC -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright 2023 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.
*/

# tfdoc:file:description Serverless Connector outputs.

locals {
plugin_sc_tfvars = {
dev = google_vpc_access_connector.dev-primary.0.id
prod = google_vpc_access_connector.prod-primary.0.id
}
}

# generate tfvars file for subsequent stages

resource "local_file" "plugin_sc_tfvars" {
for_each = var.outputs_location == null ? {} : { 1 = 1 }
file_permission = "0644"
filename = "${try(pathexpand(var.outputs_location), "")}/tfvars/2-networking-serverless-connnector.auto.tfvars.json"
content = jsonencode(local.plugin_sc_tfvars)
}

resource "google_storage_bucket_object" "plugin_sc_tfvars" {
bucket = var.automation.outputs_bucket
name = "tfvars/2-networking-serverless-connnector.auto.tfvars.json"
content = jsonencode(local.plugin_sc_tfvars)
}

# outputs

output "plugin_sc_connectors" {
description = "VPC Access Connectors."
value = local.plugin_sc_tfvars
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2023 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.
*/

# tfdoc:file:description Serverless Connector variables.

variable "serverless_connector_config" {
description = "VPC Access Serverless Connectors configuration."
type = object({
dev-primary = object({
ip_cidr_range = optional(string, "10.255.255.128/28")
machine_type = optional(string)
instances = optional(object({
max = optional(number)
min = optional(number)
}), {})
throughput = optional(object({
max = optional(number)
min = optional(number)
}), {})
})
prod-primary = object({
ip_cidr_range = optional(string, "10.255.255.0/28")
machine_type = optional(string)
instances = optional(object({
max = optional(number)
min = optional(number)
}), {})
throughput = optional(object({
max = optional(number)
min = optional(number)
}), {})
})
})
default = {
dev-primary = {}
prod-primary = {}
}
nullable = false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Copyright 2023 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.
*/

# tfdoc:file:description Serverless Connector resources.

locals {
plugin_sc_subnets = {
dev = {
for k, v in module.dev-spoke-vpc-serverless.subnets : k => v.name
}
prod = {
for k, v in module.prod-spoke-vpc-serverless.subnets : k => v.name
}
}
}

module "dev-spoke-vpc-serverless" {
source = "../../../modules/net-vpc"
project_id = module.dev-spoke-project.project_id
name = module.dev-spoke-vpc.name
vpc_create = false
subnets = [{
name = "access-connector"
description = "VPC Serverless Connector for the primary region."
ip_cidr_range = var.serverless_connector_config.dev-primary.ip_cidr_range
region = var.regions.primary
}]
}

module "prod-spoke-vpc-serverless" {
source = "../../../modules/net-vpc"
project_id = module.prod-spoke-project.project_id
name = module.prod-spoke-vpc.name
vpc_create = false
subnets = [{
name = "access-connector"
description = "VPC Serverless Connector for the primary region."
ip_cidr_range = var.serverless_connector_config.prod-primary.ip_cidr_range
region = var.regions.primary
}]
}

resource "google_vpc_access_connector" "dev-primary" {
count = var.serverless_connector_config.dev-primary == null ? 0 : 1
project = module.dev-spoke-project.project_id
region = var.regions.primary
name = "access-connector-${local.region_shortnames[var.regions.primary]}"
subnet {
name = local.plugin_sc_subnets.dev["${var.regions.primary}/access-connector"]
}
machine_type = var.serverless_connector_config.dev-primary.machine_type
max_instances = var.serverless_connector_config.dev-primary.instances.max
max_throughput = var.serverless_connector_config.dev-primary.throughput.max
min_instances = var.serverless_connector_config.dev-primary.instances.min
min_throughput = var.serverless_connector_config.dev-primary.throughput.min
}

resource "google_vpc_access_connector" "prod-primary" {
count = var.serverless_connector_config.prod-primary == null ? 0 : 1
project = module.prod-spoke-project.project_id
region = var.regions.primary
name = "access-connector-${local.region_shortnames[var.regions.primary]}"
subnet {
name = local.plugin_sc_subnets.prod["${var.regions.primary}/access-connector"]
}
machine_type = var.serverless_connector_config.prod-primary.machine_type
max_instances = var.serverless_connector_config.prod-primary.instances.max
max_throughput = var.serverless_connector_config.prod-primary.throughput.max
min_instances = var.serverless_connector_config.prod-primary.instances.min
min_throughput = var.serverless_connector_config.prod-primary.throughput.min
}
25 changes: 25 additions & 0 deletions fast/plugins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# FAST plugin system

This folders details a simple mechanism that can be used to add extra functionality to FAST stages, and a few examples that implement simple plugins that can be used as-is.

## Available plugins

### Networking

- [Serverless VPC Access Connector](./2-networking-serverless-connector/)

## Anatomy of a plugin

FAST plugins are much simpler and easier to code than full-blown stages: each plugin is meant to add a single feature using a small set of resources, and interacting directly with stage modules and variables.

A simple plugin might be composed of a single file with one resource, and grow up to the canonical set of one "main" (resources), one variables, and outputs file.

Plugin file names start with the `local-` prefix which is purposefully excluded in FAST stages via Git ignore, so that plugins are not accidentally committed to stages during development and staying aligned with our master branch is possible.

Plugins are structured here as individual folders, organized in top-level folders according to the FAST stage they are designed to work with.

As an example, the [`2-networking/serverless-connector` plugin](./2-networking-serverless-connector/) implements centralized [Serverless VPC Access Connectors](https://cloud.google.com/vpc/docs/serverless-vpc-access) for our networking stages, and is composed of three files:

- [`local-serverless-connector.tf`](./2-networking-serverless-connector/local-serverless-connector.tf) managing resources including the subnets needed in each VPC and the connectors themselves
- [`local-serverless-connector-outputs.tf`](./2-networking-serverless-connector/local-serverless-connector-outputs.tf) defining a single `serverless_connectors` output for the plugin, and optional output files
- [`local-serverless-connector-variables.tf`](./2-networking-serverless-connector/local-serverless-connector-variables.tf) defining a single `serverless_connector_config` variable used to configure the plugin
1 change: 1 addition & 0 deletions fast/stages/2-networking-a-peering/spoke-dev.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module "dev-spoke-project" {
"networkmanagement.googleapis.com",
"servicenetworking.googleapis.com",
"stackdriver.googleapis.com",
"vpcaccess.googleapis.com"
]
shared_vpc_host_config = {
enabled = true
Expand Down
1 change: 1 addition & 0 deletions fast/stages/2-networking-a-peering/spoke-prod.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module "prod-spoke-project" {
"networkmanagement.googleapis.com",
"servicenetworking.googleapis.com",
"stackdriver.googleapis.com",
"vpcaccess.googleapis.com"
]
shared_vpc_host_config = {
enabled = true
Expand Down
1 change: 1 addition & 0 deletions fast/stages/2-networking-b-vpn/spoke-dev.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module "dev-spoke-project" {
"networkmanagement.googleapis.com",
"servicenetworking.googleapis.com",
"stackdriver.googleapis.com",
"vpcaccess.googleapis.com"
]
shared_vpc_host_config = {
enabled = true
Expand Down
1 change: 1 addition & 0 deletions fast/stages/2-networking-b-vpn/spoke-prod.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module "prod-spoke-project" {
"networkmanagement.googleapis.com",
"servicenetworking.googleapis.com",
"stackdriver.googleapis.com",
"vpcaccess.googleapis.com"
]
shared_vpc_host_config = {
enabled = true
Expand Down
1 change: 1 addition & 0 deletions fast/stages/2-networking-c-nva/spoke-dev.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module "dev-spoke-project" {
"networkmanagement.googleapis.com",
"servicenetworking.googleapis.com",
"stackdriver.googleapis.com",
"vpcaccess.googleapis.com"
]
shared_vpc_host_config = {
enabled = true
Expand Down
1 change: 1 addition & 0 deletions fast/stages/2-networking-c-nva/spoke-prod.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module "prod-spoke-project" {
"networkmanagement.googleapis.com",
"servicenetworking.googleapis.com",
"stackdriver.googleapis.com",
"vpcaccess.googleapis.com"
]
shared_vpc_host_config = {
enabled = true
Expand Down
1 change: 1 addition & 0 deletions fast/stages/2-networking-d-separate-envs/spoke-dev.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module "dev-spoke-project" {
"networkmanagement.googleapis.com",
"servicenetworking.googleapis.com",
"stackdriver.googleapis.com",
"vpcaccess.googleapis.com"
]
shared_vpc_host_config = {
enabled = true
Expand Down
1 change: 1 addition & 0 deletions fast/stages/2-networking-d-separate-envs/spoke-prod.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module "prod-spoke-project" {
"networkmanagement.googleapis.com",
"servicenetworking.googleapis.com",
"stackdriver.googleapis.com",
"vpcaccess.googleapis.com"
]
shared_vpc_host_config = {
enabled = true
Expand Down
18 changes: 13 additions & 5 deletions tests/collectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def collect(self):
have the following structure:
test-name:
extra_files:
- bar.tf
- foo.tf
tfvars:
- tfvars1.tfvars
- tfvars2.tfvars
Expand All @@ -60,27 +63,32 @@ def collect(self):
common = raw.pop('common_tfvars', [])
for test_name, spec in raw.get('tests', {}).items():
spec = {} if spec is None else spec
extra_files = spec.get('extra_files')
inventories = spec.get('inventory', [f'{test_name}.yaml'])
tfvars = common + [f'{test_name}.tfvars'] + spec.get('tfvars', [])
tf_var_files = common + [f'{test_name}.tfvars'] + spec.get('tfvars', [])
for i in inventories:
name = test_name
if isinstance(inventories, list) and len(inventories) > 1:
name = f'{test_name}[{i}]'
yield FabricTestItem.from_parent(self, name=name, module=module,
inventory=[i], tfvars=tfvars)
inventory=[i],
tf_var_files=tf_var_files,
extra_files=extra_files)


class FabricTestItem(pytest.Item):

def __init__(self, name, parent, module, inventory, tfvars):
def __init__(self, name, parent, module, inventory, tf_var_files,
extra_files=None):
super().__init__(name, parent)
self.module = module
self.inventory = inventory
self.tfvars = tfvars
self.tf_var_files = tf_var_files
self.extra_files = extra_files

def runtest(self):
s = plan_validator(self.module, self.inventory, self.parent.path.parent,
self.tfvars)
self.tf_var_files, self.extra_files)

def reportinfo(self):
return self.path, None, self.name
Expand Down
1 change: 0 additions & 1 deletion tests/fast/stages/s2_networking_a_peering/common.tfvars
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
data_dir = "../../../fast/stages/2-networking-a-peering/data/"
automation = {
outputs_bucket = "test"
}
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@
# 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.

counts:
modules: 31
resources: 122

0 comments on commit 1057dcd

Please sign in to comment.