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

Use fixtures in net-lb-ext #1964

Merged
merged 2 commits into from
Jan 6, 2024
Merged
Changes from 1 commit
Commits
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
Next Next commit
Use fixtures in net-lb-ext
  • Loading branch information
wiktorn committed Jan 6, 2024
commit 64b6bce14b45e85f76ffe0a282e7020477708bd3
88 changes: 14 additions & 74 deletions modules/net-lb-ext/README.md
Original file line number Diff line number Diff line change
@@ -13,69 +13,28 @@ 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 = {
port = 80
}
}
}
# 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

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)
<!-- END TFDOC -->
42 changes: 42 additions & 0 deletions tests/fixtures/compute-mig.tf
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion tests/modules/net_lb_ext/examples/dual_stack.yaml
Original file line number Diff line number Diff line change
@@ -100,4 +100,4 @@ counts:
google_compute_region_backend_service: 1
google_compute_region_health_check: 1
modules: 3
resources: 7
resources: 9
4 changes: 2 additions & 2 deletions tests/modules/net_lb_ext/examples/ext_migs.yaml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions tests/modules/net_lb_ext/examples/fwd_rules.yaml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions tests/modules/net_lb_ext/examples/migs.yaml
Original file line number Diff line number Diff line change
@@ -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
Loading