Skip to content

Commit

Permalink
Merge pull request #5 from kubealex/add-dhcp-ranges
Browse files Browse the repository at this point in the history
add dhcp range and fix dns variables naming
  • Loading branch information
kubealex authored Feb 4, 2024
2 parents c8de873 + 23cc800 commit ea512b4
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 25 deletions.
28 changes: 15 additions & 13 deletions examples/terraform-libvirt-network/main.tf
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
module "libvirt_network" {
source = "kubealex/libvirt-resources/libvirt//modules/terraform-libvirt-pool"
version = "0.0.1"
network_autostart = true
network_autostart = true
network_name = "example_network"
network_mode = "nat"
network_domain = "example.com"
network_cidr = ["192.168.122.0/24"]
network_bridge = "br0"
network_mtu = 1500
network_dhcp_enabled = true
network_dhcp_local = false
network_dnsmasq_options = {
network_mode = "nat"
network_domain = "example.com"
network_cidr = ["192.168.122.0/24"]
network_bridge = "br0"
network_mtu = 1500
network_dns_enabled = true
network_dns_local = false
network_dhcp_enabled = true
network_dhcp_range_start = "192.168.122.15"
network_dhcp_range_end = "192.168.122.50"
network_dnsmasq_options = {
"server" = "/example.com/192.168.122.1"
}
network_dns_entries = {
network_dns_entries = {
"example" = "192.168.122.2"
}
network_routes = {
network_routes = {
"10.0.0.0/24" = "10.0.0.1"
}
}
}
16 changes: 11 additions & 5 deletions modules/terraform-libvirt-network/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ This Terraform module sets up a libvirt network with customizable configurations
| Variable Name | Description | Required | Default Value |
| --------------------------------------| -------------------------------------------------- | -------- | ------------- |
| `network_autostart` | Whether to autostart the libvirt network || `true` |
| `network` | Name of the libvirt network | ✔️ | |
| `network_name` | Name of the libvirt network | ✔️ | |
| `network_mode` | Mode of the libvirt network || `nat` |
| `network_domain` | Domain of the libvirt network || |
| `network_cidr` | CIDR for the libvirt network || `["192.168.122.0/24"]` |
| `network_bridge` | Bridge for the libvirt network || |
| `network_mtu` | MTU for the libvirt network || |
| `network_dns_enabled` | Whether DNS is enabled for the libvirt network || `false` |
| `network_dns_local` | Whether DNS is local-only for the libvirt network || `false` |
| `network_dhcp_enabled` | Whether DHCP is enabled for the libvirt network || `false` |
| `network_dhcp_local` | Whether DHCP is local-only for the libvirt network || `false` |
| `network_dhcp_range_start` | DHCP Range start for given address || |
| `network_dhcp_range_end` | DHCP Range end for given address || |
| `network_dnsmasq_options` | Map of dnsmasq options for the libvirt network || `{}` |
| `network_dns_entries` | Map of DNS entries for the libvirt network || `{}` |
| `network_routes` | Map of routes for the libvirt network || `{}` |
Expand All @@ -25,16 +28,19 @@ This Terraform module sets up a libvirt network with customizable configurations

```hcl
module "libvirt_network" {
source = "github.com/kubealex/terraform-libvirt//modules/terraform-libvirt-network"
source = "kubealex/libvirt-resources/libvirt//modules/terraform-libvirt-pool"
network_autostart = true
network = "example_network"
network_name = "example_network"
network_mode = "nat"
network_domain = "example.com"
network_cidr = ["192.168.122.0/24"]
network_bridge = "br0"
network_mtu = 1500
network_dns_enabled = true
network_dns_local = false
network_dhcp_enabled = true
network_dhcp_local = false
network_dhcp_range_start = "192.168.122.15"
network_dhcp_range_end = "192.168.122.50"
network_dnsmasq_options = {
"server" = "/example.com/192.168.122.1"
}
Expand Down
14 changes: 11 additions & 3 deletions modules/terraform-libvirt-network/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ resource "libvirt_network" "vm_network" {
mtu = var.network_mtu

dns {
enabled = var.network_dhcp_enabled
local_only = var.network_dhcp_local
enabled = var.network_dns_enabled
local_only = var.network_dns_local

dynamic "hosts" {
for_each = concat(
Expand All @@ -20,7 +20,10 @@ resource "libvirt_network" "vm_network" {
ip = hosts.value.ip
}
}
}

dhcp {
enabled = var.network_dhcp_enabled
}

dnsmasq_options {
Expand All @@ -35,7 +38,6 @@ resource "libvirt_network" "vm_network" {
}
}


dynamic "routes" {
for_each = var.network_routes
content {
Expand All @@ -44,6 +46,12 @@ resource "libvirt_network" "vm_network" {
}
}

xml {
xslt = var.network_dhcp_range_start != null && var.network_dhcp_range_start != null ? templatefile("${path.module}/templates/dhcp-range-patch.xslt", {
network_dhcp_range_start = var.network_dhcp_range_start
network_dhcp_range_end = var.network_dhcp_range_end
}) : null
}
}

data "libvirt_network_dnsmasq_options_template" "options" {
Expand Down
16 changes: 16 additions & 0 deletions modules/terraform-libvirt-network/templates/dhcp-range-patch.xslt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/network/ip/dhcp/range">
<xsl:copy>
<xsl:attribute name="start">${network_dhcp_range_start}</xsl:attribute>
<xsl:attribute name="end">${network_dhcp_range_end}</xsl:attribute>
<xsl:apply-templates select="@*[not(local-name()='end') and not(local-name()='start')]|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
26 changes: 22 additions & 4 deletions modules/terraform-libvirt-network/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,36 @@ variable "network_mtu" {
default = null
}

variable "network_dhcp_enabled" {
description = "Whether DHCP is enabled for the libvirt network"
variable "network_dns_enabled" {
description = "Whether DNS is enabled for the libvirt network"
type = bool
default = false
}

variable "network_dns_local" {
description = "Whether DNS is local-only for the libvirt network"
type = bool
default = false
}

variable "network_dhcp_local" {
description = "Whether DHCP is local-only for the libvirt network"
variable "network_dhcp_enabled" {
description = "Whether DHCP is enabled for the libvirt network"
type = bool
default = false
}

variable "network_dhcp_range_start" {
description = "DHCP range start"
type = string
default = null
}

variable "network_dhcp_range_end" {
description = "DHCP range end"
type = string
default = null
}

variable "network_dnsmasq_options" {
description = "Map of dnsmasq options for the libvirt network"
type = map(string)
Expand Down

0 comments on commit ea512b4

Please sign in to comment.