diff --git a/modules/net-lb-ext/README.md b/modules/net-lb-ext/README.md index 674848cc..67e5c7bd 100644 --- a/modules/net-lb-ext/README.md +++ b/modules/net-lb-ext/README.md @@ -13,42 +13,13 @@ This module allows managing a GCE Network Load Balancer and integrates the forwa This example shows how to reference existing Managed Infrastructure Groups (MIGs). ```hcl -module "instance_template" { - source = "./fabric/modules/compute-vm" - project_id = var.project_id - zone = "${var.region}-b" - name = "vm-test" - create_template = true - service_account = { - auto_create = true - } - network_interfaces = [ - { - network = var.vpc.self_link - subnetwork = var.subnet.self_link - } - ] - tags = [ - "http-server" - ] -} - -module "mig" { - source = "./fabric/modules/compute-mig" - project_id = var.project_id - location = var.region - name = "mig-test" - target_size = 1 - instance_template = module.instance_template.template.self_link -} - module "nlb" { source = "./fabric/modules/net-lb-ext" project_id = var.project_id region = var.region name = "nlb-test" backends = [{ - group = module.mig.group_manager.instance_group + group = module.compute-mig.group_manager.instance_group }] health_check_config = { http = { @@ -56,7 +27,7 @@ module "nlb" { } } } -# tftest modules=3 resources=6 inventory=migs.yaml e2e +# tftest modules=3 resources=5 fixtures=fixtures/compute-mig.tf inventory=migs.yaml e2e ``` ### Externally managed instances @@ -64,18 +35,6 @@ module "nlb" { This examples shows how to create an NLB by combining externally managed instances (in a custom module or even outside of the current root module) in an unmanaged group. When using internally managed groups, remember to run `terraform apply` each time group instances change. ```hcl -module "instance" { - source = "./fabric/modules/compute-vm" - for_each = toset(["b", "c"]) - name = "instance-${each.key}" - project_id = var.project_id - zone = "${var.region}-b" - network_interfaces = [{ - network = var.vpc.self_link - subnetwork = var.subnet.self_link - }] -} - module "nlb" { source = "./fabric/modules/net-lb-ext" project_id = var.project_id @@ -85,7 +44,7 @@ module "nlb" { my-group = { zone = "${var.region}-b" instances = [ - for z in ["b", "c"] : module.instance[z].id + module.compute-vm-group-b.id, ] } } @@ -98,10 +57,10 @@ module "nlb" { } } } -# tftest modules=3 resources=6 inventory=ext_migs.yaml e2e +# tftest modules=3 resources=8 fixtures=fixtures/compute-vm-group-bc.tf inventory=ext_migs.yaml e2e ``` -### Mutiple forwarding rules +### Multiple forwarding rules You can add more forwarding rules to your load balancer and override some forwarding rules defaults, including the global access policy, the IP protocol, the IP version and ports. @@ -111,18 +70,6 @@ The example adds two forwarding rules: - the second one, called `nlb-test-vip-two` exposes an IPv4 address, it listens on port 80 and allows connections from the same region only. ```hcl -module "instance" { - source = "./fabric/modules/compute-vm" - for_each = toset(["b", "c"]) - name = "instance-${each.key}" - project_id = var.project_id - zone = "${var.region}-b" - network_interfaces = [{ - network = var.vpc.self_link - subnetwork = var.subnet.self_link - }] -} - module "nlb" { source = "./fabric/modules/net-lb-ext" project_id = var.project_id @@ -141,12 +88,12 @@ module "nlb" { my-group = { zone = "${var.region}-b" instances = [ - for z in ["b", "c"] : module.instance[z].id + module.compute-vm-group-b.id, ] } } } -# tftest modules=3 resources=7 inventory=fwd_rules.yaml e2e +# tftest modules=3 resources=9 fixtures=fixtures/compute-vm-group-bc.tf inventory=fwd_rules.yaml e2e ``` ### Dual stack (IPv4 and IPv6) @@ -155,18 +102,6 @@ Your load balancer can use a combination of either or both IPv4 and IPv6 forward In this example we set the load balancer to work as dual stack, meaning it exposes both an IPv4 and an IPv6 address. ```hcl -module "instance" { - source = "./fabric/modules/compute-vm" - for_each = toset(["b", "c"]) - name = "instance-${each.key}" - project_id = var.project_id - zone = "${var.region}-b" - network_interfaces = [{ - network = var.vpc.self_link - subnetwork = var.subnet.self_link - }] -} - module "nlb" { source = "./fabric/modules/net-lb-ext" project_id = var.project_id @@ -187,12 +122,12 @@ module "nlb" { my-group = { zone = "${var.region}-b" instances = [ - for z in ["b", "c"] : module.instance[z].id + module.compute-vm-group-b.id, ] } } } -# tftest modules=3 resources=7 inventory=dual_stack.yaml e2e +# tftest modules=3 resources=9 fixtures=fixtures/compute-vm-group-bc.tf inventory=dual_stack.yaml e2e ``` ### End to end example @@ -293,4 +228,9 @@ module "nlb" { | [health_check_id](outputs.tf#L70) | Auto-created health-check id. | | | [health_check_self_link](outputs.tf#L75) | Auto-created health-check self link. | | | [id](outputs.tf#L80) | Fully qualified forwarding rule ids. | | + +## Fixtures + +- [compute-mig.tf](../../tests/fixtures/compute-mig.tf) +- [compute-vm-group-bc.tf](../../tests/fixtures/compute-vm-group-bc.tf) diff --git a/tests/fixtures/compute-mig.tf b/tests/fixtures/compute-mig.tf new file mode 100644 index 00000000..bf7d6a0a --- /dev/null +++ b/tests/fixtures/compute-mig.tf @@ -0,0 +1,42 @@ +# 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. + +module "_instance-template" { + source = "./fabric/modules/compute-vm" + project_id = var.project_id + name = "mig-e2e-template" + zone = "${var.region}-b" + tags = ["http-server", "ssh"] + network_interfaces = [{ + network = var.vpc.self_link + subnetwork = var.subnet.self_link + nat = false + addresses = null + }] + boot_disk = { + initialize_params = { + image = "projects/cos-cloud/global/images/family/cos-stable" + } + } + create_template = true +} + +module "compute-mig" { + source = "./fabric/modules/compute-mig" + project_id = var.project_id + location = "${var.region}-b" + name = "mig-e2e-tests" + # target_size = 0 + instance_template = module._instance-template.template.self_link +} diff --git a/tests/modules/net_lb_ext/examples/dual_stack.yaml b/tests/modules/net_lb_ext/examples/dual_stack.yaml index d1f4f9d1..370b215a 100644 --- a/tests/modules/net_lb_ext/examples/dual_stack.yaml +++ b/tests/modules/net_lb_ext/examples/dual_stack.yaml @@ -100,4 +100,4 @@ counts: google_compute_region_backend_service: 1 google_compute_region_health_check: 1 modules: 3 - resources: 7 + resources: 9 diff --git a/tests/modules/net_lb_ext/examples/ext_migs.yaml b/tests/modules/net_lb_ext/examples/ext_migs.yaml index 6fc686d2..c4f5b182 100644 --- a/tests/modules/net_lb_ext/examples/ext_migs.yaml +++ b/tests/modules/net_lb_ext/examples/ext_migs.yaml @@ -86,8 +86,8 @@ values: counts: google_compute_forwarding_rule: 1 - google_compute_instance_group: 1 + google_compute_instance_group: 3 google_compute_region_backend_service: 1 google_compute_region_health_check: 1 modules: 3 - resources: 6 + resources: 8 diff --git a/tests/modules/net_lb_ext/examples/fwd_rules.yaml b/tests/modules/net_lb_ext/examples/fwd_rules.yaml index 73321ec3..3c1fafdc 100644 --- a/tests/modules/net_lb_ext/examples/fwd_rules.yaml +++ b/tests/modules/net_lb_ext/examples/fwd_rules.yaml @@ -105,8 +105,8 @@ values: counts: google_compute_forwarding_rule: 2 - google_compute_instance_group: 1 + google_compute_instance_group: 3 google_compute_region_backend_service: 1 google_compute_region_health_check: 1 modules: 3 - resources: 7 + resources: 9 diff --git a/tests/modules/net_lb_ext/examples/migs.yaml b/tests/modules/net_lb_ext/examples/migs.yaml index 9c6fde6a..f1567747 100644 --- a/tests/modules/net_lb_ext/examples/migs.yaml +++ b/tests/modules/net_lb_ext/examples/migs.yaml @@ -81,6 +81,5 @@ counts: google_compute_forwarding_rule: 1 google_compute_region_backend_service: 1 google_compute_region_health_check: 1 - google_service_account: 1 modules: 3 - resources: 6 + resources: 5