Skip to content

Commit ff97d86

Browse files
authored
Merge pull request #8 from FairwindsOps/nb/cloud-nat-manual-option
cloud nat manual option
2 parents a11c19b + 276ed65 commit ff97d86

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

cloud-nat/CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7-
## [next version]
7+
## 1.1.0
8+
### Added
9+
* Ability to configure nat router with `var.nat_ip_allocate_option`
10+
* Added `var.cloud_nat_address_count` to specify the number of public NAT IP addresses
11+
12+
13+
## 1.0.0
814

915
### deprecations
1016

cloud-nat/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ The `cloud-nat` module is similar to the `default` module, but it additionally c
33

44
The set up is the same as for the default module. You'd fill out the network.tf like so, specifying the path of the cloud-nat module instead:
55

6-
```
7-
```
6+
```terraform
87
module "network" {
9-
source = "git@github.com:FairwindsOps/terraform-gcp-vpc-native.git//cloud-nat?ref=v0.0.1"
8+
source = "git@github.com:FairwindsOps/terraform-gcp-vpc-native.git//cloud-nat?ref=cloud-nat-v1.1.0"
109
// base network parameters
1110
network_name = "project-kube-staging-1"
1211
subnetwork_name = "project-staging-1"
@@ -18,5 +17,9 @@ module "network" {
1817
subnetwork_pods = "10.128.64.0/18"
1918
subnetwork_services = "10.128.32.0/20"
2019
20+
// Optional Variables
21+
// AUTO_ONLY or MANUAL_ONLY NAT allocation
22+
nat_ip_allocate_option = "MANUAL_ONLY"
23+
cloud_nat_address_count = 2
2124
}
2225
```

cloud-nat/main.tf

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ variable "enable_flow_logs" {
3030
description = "whether to turn on flow logs or not"
3131
}
3232

33+
variable "nat_ip_allocate_option" {
34+
# https://cloud.google.com/nat/docs/overview#ip_address_allocation
35+
description = "AUTO_ONLY or MANUAL_ONLY"
36+
default = "AUTO_ONLY"
37+
}
38+
39+
variable "cloud_nat_address_count" {
40+
# https://cloud.google.com/nat/docs/overview#number_of_nat_ports_and_connections
41+
description = "the count of external ip address to assign to the cloud-nat object"
42+
default = 1
43+
}
44+
45+
locals {
46+
## the following locals modify resource creation behavior depending on var.nat_ip_allocate_option
47+
cloud_nat_address_count = "${var.nat_ip_allocate_option == "AUTO_ONLY" ? 0 : var.cloud_nat_address_count}"
48+
nat_ips = "${var.nat_ip_allocate_option == "AUTO_ONLY" ? "" : join(",", google_compute_address.ip_address.*.self_link)}"
49+
manual_nat_router = "${var.nat_ip_allocate_option == "AUTO_ONLY" ? 0 : 1}"
50+
auto_nat_router = "${var.nat_ip_allocate_option == "AUTO_ONLY" ? 1 : 0}"
51+
}
52+
3353
#######################
3454
# Create the network and subnetworks, including secondary IP ranges on subnetworks
3555
#######################
@@ -61,7 +81,7 @@ resource "google_compute_subnetwork" "subnetwork" {
6181
}
6282

6383
/* We ignore changes on secondary_ip_range because terraform doesn't list
64-
them in the same order every time during runs. */
84+
them in the same order every time during runs. */
6585
lifecycle {
6686
ignore_changes = ["secondary_ip_range"]
6787
}
@@ -73,11 +93,31 @@ resource "google_compute_router" "router" {
7393
region = "${var.region}"
7494
}
7595

96+
resource "google_compute_address" "ip_address" {
97+
# resource only created if var.nat_allocate_option != AUTO_ONLY
98+
count = "${local.cloud_nat_address_count}"
99+
name = "nat-external-address-${count.index}"
100+
region = "${var.region}"
101+
}
102+
76103
resource "google_compute_router_nat" "nat_router" {
104+
# resource only created if local.auto_nat_router evaulates to TRUE
105+
count = "${local.auto_nat_router}"
106+
name = "${var.network_name}"
107+
router = "${google_compute_router.router.name}"
108+
region = "${var.region}"
109+
nat_ip_allocate_option = "${var.nat_ip_allocate_option}"
110+
source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"
111+
}
112+
113+
resource "google_compute_router_nat" "manual_nat_router" {
114+
# resource only created if local.manual_nat_router evaulates to TRUE
115+
count = "${local.manual_nat_router}"
77116
name = "${var.network_name}"
78117
router = "${google_compute_router.router.name}"
79118
region = "${var.region}"
80-
nat_ip_allocate_option = "AUTO_ONLY"
119+
nat_ip_allocate_option = "${var.nat_ip_allocate_option}"
120+
nat_ips = ["${split(",", local.nat_ips)}"]
81121
source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"
82122
}
83123

0 commit comments

Comments
 (0)