Skip to content

Commit

Permalink
Модуль network; добавление двух локальных сетей в модуль vm (#2)
Browse files Browse the repository at this point in the history
* init

* removed temp examples folder; added a new network module representing a local network with no internet connection; change vm module to include two local networks and two methods of attaching ports to a vm

* changed mks nat to have a gateway by default; updated vm ports to fix vm creation error; updated vm outputs to not include temp outputs

* fmt fixes

---------

Co-authored-by: orlov-a <orlov.aa@selectel.com>
  • Loading branch information
v1km4n and orlov-a authored Jan 26, 2024
1 parent 21dc173 commit 1827e2c
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 2 deletions.
1 change: 1 addition & 0 deletions modules/mks/k8s-cluster-standalone/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module "nat" {
source = "../../nat"
subnet_cidr = var.nat_subnet_cidr
router_name = "${var.cluster_name}-router"
no_gateway = var.no_gateway
}

module "kubernetes_cluster" {
Expand Down
5 changes: 5 additions & 0 deletions modules/mks/k8s-cluster-standalone/vars.tf
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,8 @@ variable "gpu_ng_taints" {
}))
default = []
}

variable "no_gateway" {
type = string
default = false
}
1 change: 1 addition & 0 deletions modules/nat/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ resource "openstack_networking_subnet_v2" "subnet_1" {
cidr = var.subnet_cidr
name = var.subnet_cidr
enable_dhcp = var.enable_dhcp
no_gateway = var.no_gateway
}

resource "openstack_networking_router_interface_v2" "router_interface_1" {
Expand Down
5 changes: 5 additions & 0 deletions modules/nat/vars.tf
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ variable "enable_dhcp" {
type = bool
default = false
}

variable "no_gateway" {
default = true
description = "(Optional) Do not set a gateway IP on this subnet. Changing this removes or adds a default gateway IP of the existing subnet."
}
11 changes: 11 additions & 0 deletions modules/network/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "openstack_networking_network_v2" "network_1" {
name = var.network_name
}

resource "openstack_networking_subnet_v2" "subnet_1" {
network_id = openstack_networking_network_v2.network_1.id
name = var.subnet_cidr
cidr = var.subnet_cidr
enable_dhcp = var.enable_dhcp
no_gateway = var.no_gateway
}
7 changes: 7 additions & 0 deletions modules/network/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "network_id" {
value = openstack_networking_network_v2.network_1.id
}

output "subnet_id" {
value = openstack_networking_subnet_v2.subnet_1.id
}
15 changes: 15 additions & 0 deletions modules/network/vars.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
variable "network_name" {
default = "network_1"
}

variable "subnet_cidr" {
default = "192.168.0.0/24"
}

variable "enable_dhcp" {
default = false
}

variable "no_gateway" {
default = true
}
9 changes: 9 additions & 0 deletions modules/network/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
openstack = {
source = "terraform-provider-openstack/openstack"
version = "1.53.0"
}
}
required_version = ">= 1.5.0"
}
42 changes: 42 additions & 0 deletions modules/vm/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ module "nat" {
router_name = var.router_name
network_name = var.network_name
enable_dhcp = var.enable_dhcp
no_gateway = var.no_gateway
}

module "network_1" {
source = "../network"

subnet_cidr = var.local_network_1_subnet_cidr
network_name = var.local_network_1_name
}

module "network_2" {
source = "../network"

subnet_cidr = var.local_network_2_subnet_cidr
network_name = var.local_network_2_name
}

resource "openstack_networking_port_v2" "port_1" {
Expand All @@ -38,6 +53,24 @@ resource "openstack_networking_port_v2" "port_1" {
}
}

resource "openstack_networking_port_v2" "port_2" {
name = "${var.vm_name}-eth1"
network_id = module.network_1.network_id

fixed_ip {
subnet_id = module.network_1.subnet_id
}
}

resource "openstack_networking_port_v2" "port_3" {
name = "${var.vm_name}-eth2"
network_id = module.network_2.network_id

fixed_ip {
subnet_id = module.network_2.subnet_id
}
}

resource "openstack_compute_instance_v2" "instance_1" {
name = var.vm_name

Expand All @@ -48,6 +81,10 @@ resource "openstack_compute_instance_v2" "instance_1" {
port = openstack_networking_port_v2.port_1.id
}

network {
port = openstack_networking_port_v2.port_2.id
}

dynamic "block_device" {
for_each = module.volume
content {
Expand All @@ -68,3 +105,8 @@ resource "openstack_compute_instance_v2" "instance_1" {
]
}
}

resource "openstack_compute_interface_attach_v2" "port_3_attach" {
instance_id = openstack_compute_instance_v2.instance_1.id
port_id = openstack_networking_port_v2.port_3.id
}
2 changes: 1 addition & 1 deletion modules/vm/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ output "nat_sub_id" {

output "vm_port_id" {
value = openstack_networking_port_v2.port_1.id
}
}
31 changes: 30 additions & 1 deletion modules/vm/vars.tf
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ variable "dns_nameservers" {
}

variable "subnet_cidr" {
description = "Subnet CIRD to be created"
description = "Subnet CIDR to be created"
type = string
default = "192.168.0.0/24"
}
Expand All @@ -82,8 +82,37 @@ variable "network_name" {
default = "network_1"
}

variable "local_network_1_name" {
description = "Local network name to be created"
type = string
default = "local_network_1"
}

variable "local_network_2_name" {
description = "Local network name to be created"
type = string
default = "local_network_2"
}

variable "local_network_1_subnet_cidr" {
description = "Subnet CIDR to be created"
type = string
default = "192.168.1.0/24"
}

variable "local_network_2_subnet_cidr" {
description = "Subnet CIDR to be created"
type = string
default = "192.168.2.0/24"
}

variable "enable_dhcp" {
description = "DHCP enable flag"
type = bool
default = false
}

variable "no_gateway" {
type = string
default = false
}

0 comments on commit 1827e2c

Please sign in to comment.