Skip to content

Commit

Permalink
make network dns enabled by default (#1100)
Browse files Browse the repository at this point in the history
https://libvirt.org/formatnetwork.html#addressing specifies dns is disabled only if
enable="no", but if omitted, it is enabled by default.

However, it claims if disabled, it would ignore the dns entries, but in reality
it fails with "error: XML error: Extra data in disabled network".

This change makes:

- if enabled is omitted, we use the libvirt default
- we only add dns information to the xml if enabled
  • Loading branch information
dmacvicar authored Sep 19, 2024
1 parent bbf9056 commit ec7a38d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
12 changes: 0 additions & 12 deletions libvirt/network_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,6 @@ func getDNSForwardersFromResource(d *schema.ResourceData) ([]libvirtxml.NetworkD
return dnsForwarders, nil
}

// getDNSEnableFromResource returns string to enable ("yes") or disable ("no") dns
// in the network definition.
func getDNSEnableFromResource(d *schema.ResourceData) string {
if dnsEnabled, ok := d.GetOk(dnsPrefix + ".enabled"); ok {
if dnsEnabled.(bool) {
return "yes" // this "boolean" must be "yes"|"no"
}
return "no"
}
return "no"
}

// getDNSSRVFromResource returns a list of libvirt's DNS SRVs
// in the network definition.
func getDNSSRVFromResource(d *schema.ResourceData) ([]libvirtxml.NetworkDNSSRV, error) {
Expand Down
42 changes: 23 additions & 19 deletions libvirt/resource_libvirt_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"libvirt.org/go/libvirtxml"

"github.com/dmacvicar/terraform-provider-libvirt/libvirt/util"
)

const (
Expand Down Expand Up @@ -102,6 +104,7 @@ func resourceLibvirtNetwork() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
Required: false,
Default: true,
},
"local_only": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -378,30 +381,31 @@ func resourceLibvirtNetworkCreate(ctx context.Context, d *schema.ResourceData, m
}
networkDef.IPs = ips

dnsEnabled := getDNSEnableFromResource(d)
if dnsEnabled, ok := d.GetOk(dnsPrefix + ".enabled"); ok && dnsEnabled.(bool) {
dnsForwarders, err := getDNSForwardersFromResource(d)
if err != nil {
return diag.FromErr(err)
}

dnsForwarders, err := getDNSForwardersFromResource(d)
if err != nil {
return diag.FromErr(err)
}
dnsSRVs, err := getDNSSRVFromResource(d)
if err != nil {
return diag.FromErr(err)
}

dnsSRVs, err := getDNSSRVFromResource(d)
if err != nil {
return diag.FromErr(err)
}
dnsHosts, err := getDNSHostsFromResource(d)
if err != nil {
return diag.FromErr(err)
}

dnsHosts, err := getDNSHostsFromResource(d)
if err != nil {
return diag.FromErr(err)
}
dns := libvirtxml.NetworkDNS{
Enable: util.FormatBoolYesNo(dnsEnabled.(bool)),
Forwarders: dnsForwarders,
Host: dnsHosts,
SRVs: dnsSRVs,
}

dns := libvirtxml.NetworkDNS{
Enable: dnsEnabled,
Forwarders: dnsForwarders,
Host: dnsHosts,
SRVs: dnsSRVs,
networkDef.DNS = &dns
}
networkDef.DNS = &dns
} else if networkDef.Forward.Mode == netModeBridge {
if networkDef.Bridge.Name == "" {
return diag.Errorf("'bridge' must be provided when using the bridged network mode")
Expand Down
6 changes: 3 additions & 3 deletions website/docs/r/network.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ resource "libvirt_network" "kube_network" {
# (Optional) DNS configuration
dns {
# (Optional, default false)
# Set to true, if no other option is specified and you still want to
# enable dns.
# (Optional, default true)
# If disabled, no dns will be setup for this network and dns configuration
# will be ignored.
enabled = true
# (Optional, default false)
# true: DNS requests under this domain will only be resolved by the
Expand Down

0 comments on commit ec7a38d

Please sign in to comment.