Skip to content

Commit 0463fc9

Browse files
feat: Alias IP Ranges (#207)
* Added support for setting alias-ip-range on an instance template There is now a new variable 'alias_ip_range' which is passed verbatim to the underlying google_compute_instance_template resource. Added a simple test, verifying an alias_ip_range setting gets through. Updated the Readme.md, removed the testing part, it is covered by the CONTRIBUTING.md * remove unused vars Co-authored-by: bharathkkb <[email protected]>
1 parent 2cc11ee commit 0463fc9

File tree

31 files changed

+370
-68
lines changed

31 files changed

+370
-68
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ terraform.tfvars
88
.terraform
99
.terraform.tfstate.d
1010
*.pyc
11+
credentials*.json
1112

1213
# JetBrains - PyCharm, IntelliJ, etc.
1314
.idea/
1415
__pycache__/
1516
*.iml
17+
.project
1618

1719
# Kitchen files
1820
**/inspec.lock

.kitchen.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ suites:
4040
name: terraform
4141
command_timeout: 1800
4242
root_module_directory: test/fixtures/instance_template/additional_disks
43+
- name: it_alias_ip_range
44+
driver:
45+
name: terraform
46+
command_timeout: 1800
47+
root_module_directory: test/fixtures/instance_template/alias_ip_range
4348
- name: preemptible_and_regular_instance_templates_simple
4449
driver:
4550
name: terraform

README.md

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -30,49 +30,9 @@ See also the [project_services](modules/project_services) module (optional).
3030
`distribution_policy_zones` cannot be changed during use.
3131
If you have changed them yourself or used to have a default value, then you'll have to force recreate a MIG group yourself.
3232

33-
## Test Configuration
33+
## Tests
3434

35-
1. Create a `terraform.tfvars` file, using `terraform.tfvars.example` as an example
36-
37-
```shell
38-
cp test/fixtures/shared/terraform.tfvars.example test/fixtures/shared/terraform.tfvars
39-
```
40-
41-
The `terraform.tfvars` in each fixture directory is already symlinked to this one shared file.
42-
43-
2. Populate the variables with values appropriate for your test environment (i.e. `project_id`, `service_account.email`)
44-
3. Download a Service Account key with the necessary [permissions](#permissions) and put it in the module's root directory with the name credentials.json.
45-
46-
## Running Tests
47-
48-
From the root of the module, run
49-
50-
```
51-
make test_integration_docker
52-
```
53-
54-
to build the container and run through all the test suites. Note that this will take some time (> 20 minutes).
55-
56-
You can also run each test case individually and interactively in the Docker container:
57-
58-
```
59-
make docker_run
60-
```
61-
62-
The root directory of the module will be mounted to `/cft/workdir` in the container. For example, to run the `mig-autoscaler` test suite:
63-
64-
```
65-
bundle exec kitchen test mig-autosaler
66-
```
67-
68-
or
69-
70-
```
71-
bundle exec kitchen create mig-autoscaler
72-
bundle exec kitchen converge mig-autoscaler
73-
bundle exec kitchen verify mig-autoscaler
74-
bundle exec kitchen destroy mig-autoscaler
75-
```
35+
For running the integration test cases, please refer to the [CONTRIBUTING](CONTRIBUTING.md) documentation.
7636

7737
## Permissions
7838

examples/compute_instance/disk_snapshot/main.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ provider "google" {
2121
# Building the list of disk names in the required format.
2222
# Usually you would build this list from the outputs of the compute_instance module
2323
locals {
24-
instance_disks = [for i in range(2) : "projects/${var.project_id}/disks/instance-simple-001-${i + 1}/zones/${data.google_compute_zones.available.names[0]}"]
24+
instance_disks = [for i in range(2) : "projects/${var.project_id}/disks/instance-disk-snapshot-001-${i + 1}/zones/${data.google_compute_zones.available.names[0]}"]
2525
}
2626

2727
data "google_compute_zones" "available" {
@@ -34,6 +34,7 @@ module "instance_template" {
3434
region = var.region
3535
project_id = var.project_id
3636
subnetwork = var.subnetwork
37+
name_prefix = "instance-disk-snapshot"
3738
service_account = null
3839

3940
additional_disks = [
@@ -63,7 +64,7 @@ module "compute_instance" {
6364
region = var.region
6465
subnetwork = var.subnetwork
6566
num_instances = 1
66-
hostname = "instance-simple"
67+
hostname = "instance-disk-snapshot"
6768
instance_template = module.instance_template.self_link
6869
}
6970

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# instance-template-alias-ip-range
2+
3+
This example demonstrates how to use an alias IP range.
4+
5+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
6+
## Inputs
7+
8+
| Name | Description | Type | Default | Required |
9+
|------|-------------|------|---------|:--------:|
10+
| project\_id | The GCP project to use for integration tests | `string` | n/a | yes |
11+
| region | The GCP region to create and test resources in | `string` | `"us-central1"` | no |
12+
| service\_account | Service account to attach to the instance. See https://www.terraform.io/docs/providers/google/r/compute_instance_template.html#service_account. | <pre>object({<br> email = string<br> scopes = set(string)<br> })</pre> | `null` | no |
13+
| subnetwork | The name of the subnetwork create this instance in. | `string` | `""` | no |
14+
15+
## Outputs
16+
17+
| Name | Description |
18+
|------|-------------|
19+
| name | Name of the instance templates |
20+
| self\_link | Self-link to the instance template |
21+
22+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
provider "google" {
18+
19+
project = var.project_id
20+
region = var.region
21+
version = "~> 3.0"
22+
}
23+
24+
resource "google_compute_address" "ip_address" {
25+
name = "external-ip-alias-ip-range"
26+
}
27+
28+
module "instance_template" {
29+
source = "../../../modules/instance_template"
30+
project_id = var.project_id
31+
subnetwork = var.subnetwork
32+
service_account = var.service_account
33+
name_prefix = "alias-ip-range"
34+
35+
alias_ip_range = {
36+
ip_cidr_range = "/24"
37+
subnetwork_range_name = var.subnetwork
38+
}
39+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
output "self_link" {
18+
description = "Self-link to the instance template"
19+
value = module.instance_template.self_link
20+
}
21+
22+
output "name" {
23+
description = "Name of the instance templates"
24+
value = module.instance_template.name
25+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable "project_id" {
18+
description = "The GCP project to use for integration tests"
19+
type = string
20+
}
21+
22+
variable "region" {
23+
description = "The GCP region to create and test resources in"
24+
type = string
25+
default = "us-central1"
26+
}
27+
28+
variable "subnetwork" {
29+
description = "The name of the subnetwork create this instance in."
30+
default = ""
31+
}
32+
33+
variable "service_account" {
34+
default = null
35+
type = object({
36+
email = string
37+
scopes = set(string)
38+
})
39+
description = "Service account to attach to the instance. See https://www.terraform.io/docs/providers/google/r/compute_instance_template.html#service_account."
40+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
terraform {
18+
required_version = ">=0.12.6"
19+
}

modules/instance_template/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ See the [simple](../../examples/instance_template/simple) for a usage example.
1616
| access\_config | Access configurations, i.e. IPs via which the VM instance can be accessed via the Internet. | <pre>list(object({<br> nat_ip = string<br> network_tier = string<br> }))</pre> | `[]` | no |
1717
| additional\_disks | List of maps of additional disks. See https://www.terraform.io/docs/providers/google/r/compute_instance_template.html#disk_name | <pre>list(object({<br> disk_name = string<br> device_name = string<br> auto_delete = bool<br> boot = bool<br> disk_size_gb = number<br> disk_type = string<br> disk_labels = map(string)<br> }))</pre> | `[]` | no |
1818
| additional\_networks | Additional network interface details for GCE, if any. | <pre>list(object({<br> network = string<br> subnetwork = string<br> subnetwork_project = string<br> network_ip = string<br> access_config = list(object({<br> nat_ip = string<br> network_tier = string<br> }))<br> }))</pre> | `[]` | no |
19+
| alias\_ip\_range | An array of alias IP ranges for this network interface. Can only be specified for network interfaces on subnet-mode networks.<br>ip\_cidr\_range: The IP CIDR range represented by this alias IP range. This IP CIDR range must belong to the specified subnetwork and cannot contain IP addresses reserved by system or used by other network interfaces. At the time of writing only a netmask (e.g. /24) may be supplied, with a CIDR format resulting in an API error.<br>subnetwork\_range\_name: The subnetwork secondary range name specifying the secondary range from which to allocate the IP CIDR range for this alias IP range. If left unspecified, the primary range of the subnetwork will be used. | <pre>object({<br> ip_cidr_range = string<br> subnetwork_range_name = string<br> })</pre> | `null` | no |
1920
| auto\_delete | Whether or not the boot disk should be auto-deleted | `string` | `"true"` | no |
2021
| can\_ip\_forward | Enable IP forwarding, for NAT instances for example | `string` | `"false"` | no |
2122
| disk\_encryption\_key | The self link of the encryption key that is stored in Google Cloud KMS to use to encrypt all the disks on this instance | `string` | `null` | no |

modules/instance_template/main.tf

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ locals {
4343
shielded_vm_configs = var.enable_shielded_vm ? [true] : []
4444
confidential_instance_config = var.enable_confidential_vm ? [true] : []
4545

46-
gpu_enabled = var.gpu != null
46+
gpu_enabled = var.gpu != null
47+
alias_ip_range_enabled = var.alias_ip_range != null
4748
on_host_maintenance = (
4849
var.preemptible || var.enable_confidential_vm || local.gpu_enabled
4950
? "TERMINATE"
@@ -110,6 +111,13 @@ resource "google_compute_instance_template" "tpl" {
110111
network_tier = access_config.value.network_tier
111112
}
112113
}
114+
dynamic "alias_ip_range" {
115+
for_each = local.alias_ip_range_enabled ? [var.alias_ip_range] : []
116+
content {
117+
ip_cidr_range = alias_ip_range.value.ip_cidr_range
118+
subnetwork_range_name = alias_ip_range.value.subnetwork_range_name
119+
}
120+
}
113121
}
114122

115123
dynamic "network_interface" {

modules/instance_template/variables.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,19 @@ variable "gpu" {
249249
})
250250
default = null
251251
}
252+
253+
##################
254+
# alias IP range
255+
##################
256+
variable "alias_ip_range" {
257+
description = <<EOF
258+
An array of alias IP ranges for this network interface. Can only be specified for network interfaces on subnet-mode networks.
259+
ip_cidr_range: The IP CIDR range represented by this alias IP range. This IP CIDR range must belong to the specified subnetwork and cannot contain IP addresses reserved by system or used by other network interfaces. At the time of writing only a netmask (e.g. /24) may be supplied, with a CIDR format resulting in an API error.
260+
subnetwork_range_name: The subnetwork secondary range name specifying the secondary range from which to allocate the IP CIDR range for this alias IP range. If left unspecified, the primary range of the subnetwork will be used.
261+
EOF
262+
type = object({
263+
ip_cidr_range = string
264+
subnetwork_range_name = string
265+
})
266+
default = null
267+
}

test/fixtures/instance_template/additional_disks/terraform.tfvars

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
module "instance_template_alias_ip_range" {
18+
source = "../../../../examples/instance_template/alias_ip_range"
19+
project_id = var.project_id
20+
subnetwork = google_compute_subnetwork.main.name
21+
service_account = var.service_account
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../shared/network.tf
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
output "self_link" {
18+
description = "Self-link to instance template"
19+
value = module.instance_template_alias_ip_range.self_link
20+
}
21+
22+
output "name" {
23+
description = "Name of instance template"
24+
value = module.instance_template_alias_ip_range.name
25+
}
26+
27+
output "project_id" {
28+
description = "The GCP project to use for integration tests"
29+
value = var.project_id
30+
}
31+
32+
output "subnetwork_name" {
33+
description = "The GCP subnetwork name to use for integration tests"
34+
value = google_compute_subnetwork.main.name
35+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable "project_id" {
18+
description = "The GCP project to use for integration tests"
19+
}
20+
21+
variable "service_account" {
22+
default = null
23+
type = object({
24+
email = string
25+
scopes = list(string)
26+
})
27+
}

0 commit comments

Comments
 (0)