From 0d6feda4cad31cde44ee6cc7d7938c9bbca19e6c Mon Sep 17 00:00:00 2001 From: Eric Bischoff Date: Wed, 11 Jul 2018 12:51:19 +0200 Subject: [PATCH] Make DHCP optional when addresses are defined --- libvirt/domain.go | 6 +++-- libvirt/resource_libvirt_network.go | 35 +++++++++++++++++++---------- website/docs/r/network.markdown | 20 ++++++++++++++--- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/libvirt/domain.go b/libvirt/domain.go index c07840de6..6e5d93395 100644 --- a/libvirt/domain.go +++ b/libvirt/domain.go @@ -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 diff --git a/libvirt/resource_libvirt_network.go b/libvirt/resource_libvirt_network.go index f0955adb3..79433daa5 100644 --- a/libvirt/resource_libvirt_network.go +++ b/libvirt/resource_libvirt_network.go @@ -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, @@ -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) @@ -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), @@ -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) } diff --git a/website/docs/r/network.markdown b/website/docs/r/network.markdown index 1f4698ade..0ee76ee98 100644 --- a/website/docs/r/network.markdown +++ b/website/docs/r/network.markdown @@ -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) @@ -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.