Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple definition of subnet addresses from definition of DHCP range #321

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions libvirt/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,10 @@ func getDomainInterfacesFromNetworks(domain libvirtxml.Domain,
networkDef, err := newDefNetworkfromLibvirt(network)
macAddresses := make(map[string][]string)
for _, ips := range networkDef.IPs {
for _, dhcpHost := range ips.DHCP.Hosts {
macAddresses[dhcpHost.MAC] = append(macAddresses[dhcpHost.MAC], dhcpHost.IP)
if ips.DHCP != nil {
for _, dhcpHost := range ips.DHCP.Hosts {
macAddresses[dhcpHost.MAC] = append(macAddresses[dhcpHost.MAC], dhcpHost.IP)
}
}
}
networkMacAddresses[networkName] = macAddresses
Expand Down
35 changes: 23 additions & 12 deletions libvirt/resource_libvirt_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ func resourceLibvirtNetwork() *schema.Resource {
Type: schema.TypeString,
},
},
"enable_dhcp": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: true,
},
"autostart": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -197,9 +203,11 @@ func resourceLibvirtNetworkCreate(d *schema.ResourceData, meta interface{}) erro
networkDef.Forward.NAT = nil
}

// some network modes require a DHCP/DNS server
// set the addresses for DHCP
// set the addresses for the network, for the real host, and optionally also for DHCP
if addresses, ok := d.GetOk("addresses"); ok {
// check whether we want DHCP or not
enableDHCP := d.Get("enable_dhcp").(bool)

ipsPtrsLst := []libvirtxml.NetworkIP{}
for _, addressI := range addresses.([]interface{}) {
address := addressI.(string)
Expand All @@ -217,8 +225,9 @@ func resourceLibvirtNetworkCreate(d *schema.ResourceData, meta interface{}) erro
return fmt.Errorf("Netmask seems to be too strict: only %d IPs available (%s)", ipsRange-3, family)
}

// we should calculate the range served by DHCP. For example, for
// 192.168.121.0/24 we will serve 192.168.121.2 - 192.168.121.254
// we should calculate the range of the network. For example, for
// 192.168.121.0/24 the host will have 192.168.121.1.
// If DHCP is required, we will also serve 192.168.121.2 - 192.168.121.254
start, end := networkRange(ipNet)

// skip the .0, (for the network),
Expand All @@ -231,16 +240,18 @@ func resourceLibvirtNetworkCreate(d *schema.ResourceData, meta interface{}) erro
Family: family,
}

start[len(start)-1]++ // then skip the .1
end[len(end)-1]-- // and skip the .255 (for broadcast)
if enableDHCP {
start[len(start)-1]++ // then skip the .1
end[len(end)-1]-- // and skip the .255 (for broadcast)

dni.DHCP = &libvirtxml.NetworkDHCP{
Ranges: []libvirtxml.NetworkDHCPRange{
{
Start: start.String(),
End: end.String(),
dni.DHCP = &libvirtxml.NetworkDHCP{
Ranges: []libvirtxml.NetworkDHCPRange{
{
Start: start.String(),
End: end.String(),
},
},
},
}
}
ipsPtrsLst = append(ipsPtrsLst, dni)
}
Expand Down
20 changes: 17 additions & 3 deletions website/docs/r/network.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ resource "libvirt_network" "kube_network" {
# the domain used by the DNS server in this network
domain = "k8s.local"

# the addresses allowed for domains connected and served by the DHCP server
# the addresses allowed for domains connected
# also derived to define the host addresses
# also derived to define the addresses served by the DHCP server
addresses = ["10.17.3.0/24", "2001:db8:ca2:2::1/64"]

# (optional) start a DHCP server or not
# defaults to true
# enable_dhcp = false

# (optional) the bridge device defines the name of a bridge device
# which will be used to construct the virtual network.
# (only necessary in "bridge" mode)
Expand All @@ -48,8 +54,16 @@ The following arguments are supported:
* `name` - (Required) A unique name for the resource, required by libvirt.
Changing this forces a new resource to be created.
* `domain` - The domain used by the DNS server.
* `addresses` - A list of (0 or 1) ipv4 and (0 or 1) ipv6 subnets in CIDR notation
format for being served by the DHCP server. Address of subnet should be used.
* `addresses` - A list of (0 or 1) IPv4 and (0 or 1) IPv6 subnets in
CIDR notation. This defines the subnets associated to that network.
This argument is also used to define the address on the real host,
by appending `.1` at the end. Finally, if `enable_dhcp` is true,
this argument is also used to define the address range served by
the DHCP server, between `.2` and the last but one address
in the subnet (`.254` for a `/24`).
* `enable_dhcp` - A boolean (`true` or `false`). If true, a DHCP server will
be put in place to serve addresses on that network. If not specified,
`true` is assumed. For it to have an effect, `addresses` must be used.
* `mode` - One of:
- `none`: the guests can talk to each other and the host OS, but cannot reach
any other machines on the LAN.
Expand Down