Skip to content

Commit 46258e3

Browse files
committed
terraform/OCI: Add a Kconfig switch to create a VCN on the fly
Make it simpler to use OCI: create a kdevops VCN if there isn't already a persistent VCN to use. Reviewed-by: Luis Chamberlain <[email protected]> Reviewed-by: Chandan Babu R <[email protected]> Signed-off-by: Chuck Lever <[email protected]>
1 parent 2b6580f commit 46258e3

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

playbooks/roles/gen_tfvars/defaults/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ terraform_private_net_prefix: ""
1818
terraform_private_net_mask: 0
1919

2020
terraform_oci_assign_public_ip: false
21+
terraform_oci_use_existing_vcn: false
2122

2223
terraform_openstack_cloud_name: "invalid"
2324
terraform_openstack_instance_prefix: "invalid"

playbooks/roles/gen_tfvars/templates/oci/terraform.tfvars.j2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ oci_instance_flex_memory_in_gbs = {{ terraform_oci_instance_flex_memory_in_gbs }
1212
{% endif %}
1313
oci_os_image_ocid = "{{ terraform_oci_os_image_ocid }}"
1414
oci_assign_public_ip = {{ terraform_oci_assign_public_ip | lower }}
15+
oci_use_existing_vcn = {{ terraform_oci_use_existing_vcn | lower }}
16+
{% if terraform_oci_use_existing_vcn %}
1517
oci_subnet_ocid = "{{ terraform_oci_subnet_ocid }}"
18+
{% endif %}
1619
oci_volumes_per_instance = {{ terraform_oci_volumes_per_instance }}
1720
oci_volumes_size = {{ terraform_oci_volumes_size }}
1821
oci_data_volume_device_file_name = "{{ terraform_oci_data_volume_device_file_name }}"

terraform/oci/kconfigs/Kconfig.network

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ config TERRAFORM_OCI_ASSIGN_PUBLIC_IP
77
assigned to each instance. Leave it unset to prevent your
88
instances from being accessible on the public internet.
99

10+
config TERRAFORM_OCI_USE_EXISTING_VCN
11+
bool "Attach instances to an existing VCN"
12+
output yaml
13+
default y
14+
help
15+
If your tenancy administrator prefers to create and secure
16+
the network resources used within a compartment, or your
17+
tenancy has special networking requirements, enable this
18+
option. Then enter the OCID of the existing subnet in the
19+
TERRAFORM_OCI_SUBNET_OCID option below. kdevops will join
20+
its compute instances to that subnet.
21+
22+
Disable this option if you'd like kdevops to create a
23+
secure VPN and subnet automatically.
24+
25+
if TERRAFORM_OCI_USE_EXISTING_VCN
26+
1027
config TERRAFORM_OCI_SUBNET_OCID
1128
string "OCI Subnet OCID"
1229
output yaml
@@ -18,3 +35,5 @@ config TERRAFORM_OCI_SUBNET_OCID
1835
kdevops does not manage this resource. Before running
1936
"make bringup", the subnet must already exist and your OCI
2037
user must have permission to attach to it.
38+
39+
endif # TERRAFORM_OCI_USE_EXISTING_VCN

terraform/oci/main.tf

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ resource "oci_core_instance" "kdevops_instance" {
3030

3131
create_vnic_details {
3232
assign_public_ip = var.oci_assign_public_ip
33-
subnet_id = var.oci_subnet_ocid
33+
subnet_id = var.oci_use_existing_vcn ? var.oci_subnet_ocid : one(oci_core_subnet.kdevops_subnet[*].id)
3434
}
3535

3636
metadata = {
@@ -53,6 +53,8 @@ module "volumes" {
5353
}
5454

5555
resource "oci_core_vcn" "kdevops_vcn" {
56+
count = var.oci_use_existing_vcn ? 0 : 1
57+
5658
cidr_blocks = [
5759
"10.0.0.0/16",
5860
]
@@ -63,15 +65,19 @@ resource "oci_core_vcn" "kdevops_vcn" {
6365
}
6466

6567
resource "oci_core_internet_gateway" "kdevops_internet_gateway" {
68+
count = var.oci_use_existing_vcn ? 0 : 1
69+
6670
compartment_id = data.oci_identity_compartments.kdevops_compartment.compartments[0].id
6771
display_name = "kdevops internet gateway"
68-
vcn_id = oci_core_vcn.kdevops_vcn.id
72+
vcn_id = one(oci_core_vcn.kdevops_vcn[*].id)
6973
}
7074

7175
resource "oci_core_dhcp_options" "kdevops_dhcp_options" {
76+
count = var.oci_use_existing_vcn ? 0 : 1
77+
7278
compartment_id = data.oci_identity_compartments.kdevops_compartment.compartments[0].id
7379
display_name = "kdevops dhcp options"
74-
vcn_id = oci_core_vcn.kdevops_vcn.id
80+
vcn_id = one(oci_core_vcn.kdevops_vcn[*].id)
7581

7682
options {
7783
type = "DomainNameServer"
@@ -84,20 +90,24 @@ resource "oci_core_dhcp_options" "kdevops_dhcp_options" {
8490
}
8591

8692
resource "oci_core_route_table" "kdevops_route_table" {
93+
count = var.oci_use_existing_vcn ? 0 : 1
94+
8795
compartment_id = data.oci_identity_compartments.kdevops_compartment.compartments[0].id
8896
display_name = "kdevops route table"
89-
vcn_id = oci_core_vcn.kdevops_vcn.id
97+
vcn_id = one(oci_core_vcn.kdevops_vcn[*].id)
9098
route_rules {
9199
destination = "0.0.0.0/0"
92100
destination_type = "CIDR_BLOCK"
93-
network_entity_id = oci_core_internet_gateway.kdevops_internet_gateway.id
101+
network_entity_id = one(oci_core_internet_gateway.kdevops_internet_gateway[*].id)
94102
}
95103
}
96104

97105
resource "oci_core_security_list" "kdevops_security_list" {
106+
count = var.oci_use_existing_vcn ? 0 : 1
107+
98108
compartment_id = data.oci_identity_compartments.kdevops_compartment.compartments[0].id
99109
display_name = "kdevops security list"
100-
vcn_id = oci_core_vcn.kdevops_vcn.id
110+
vcn_id = one(oci_core_vcn.kdevops_vcn[*].id)
101111

102112
egress_security_rules {
103113
description = "Allow all outbound traffic"
@@ -153,13 +163,15 @@ resource "oci_core_security_list" "kdevops_security_list" {
153163
}
154164

155165
resource "oci_core_subnet" "kdevops_subnet" {
166+
count = var.oci_use_existing_vcn ? 0 : 1
167+
156168
availability_domain = data.oci_identity_availability_domain.kdevops_av_domain.name
157169
cidr_block = "10.0.0.0/24"
158170
compartment_id = data.oci_identity_compartments.kdevops_compartment.compartments[0].id
159-
dhcp_options_id = oci_core_dhcp_options.kdevops_dhcp_options.id
171+
dhcp_options_id = one(oci_core_dhcp_options.kdevops_dhcp_options[*].id)
160172
dns_label = "runners"
161173
display_name = "kdevops subnet"
162-
route_table_id = oci_core_route_table.kdevops_route_table.id
163-
security_list_ids = ["${oci_core_security_list.kdevops_security_list.id}"]
164-
vcn_id = oci_core_vcn.kdevops_vcn.id
174+
route_table_id = one(oci_core_route_table.kdevops_route_table[*].id)
175+
security_list_ids = ["${one(oci_core_security_list.kdevops_security_list[*].id)}"]
176+
vcn_id = one(oci_core_vcn.kdevops_vcn[*].id)
165177
}

terraform/oci/vars.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,16 @@ variable "oci_assign_public_ip" {
5151
}
5252

5353
variable "oci_subnet_ocid" {
54+
default = null
5455
description = "Subnet OCID"
5556
type = string
5657
}
5758

59+
variable "oci_use_existing_vcn" {
60+
description = "Use a pre-existing VCN"
61+
type = bool
62+
}
63+
5864
variable "oci_volumes_per_instance" {
5965
description = "The count of additional block volumes per instance"
6066
type = number

0 commit comments

Comments
 (0)