Documentation
¶
Overview ¶
Package dns manages a host's DNS resolver configuration through an injected exec.Runner.
Build a Manager for an explicit backend and call its methods:
r, _ := exec.NewRunner(exec.Direct) // the agent runs as root
m, err := dns.New(dns.Resolved, r)
if err != nil { ... }
err = m.Apply(ctx, dns.Config{Nameservers: []string{"1.1.1.1"}, SearchDomains: []string{"corp.example"}})
Two backends are implemented:
- Resolved (systemd-resolved via resolvectl) — supports a host-global config (an empty Config.Interface writes the managed /etc/systemd/resolved.conf.d drop-in and restarts the service) and per-link config (a set Interface uses runtime `resolvectl dns/domain <iface>`).
- NetworkManager (nmcli) — connection-scoped: it configures DNS on the active connection of Config.Interface, so Interface is REQUIRED. There is no clean host-global DNS via nmcli; use the Resolved backend for that.
Mutations escalate through the Runner; reads are unprivileged. Detect lists the backends whose tool is present on PATH; the consumer picks one and passes it to New (no auto-detection, no global state).
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidConfig = errors.New("dns: invalid config")
ErrInvalidConfig is returned when a Config field is unsafe or malformed for a resolver backend. Apply validates before touching any backend, so a bad config is rejected without side effects.
var ErrUnknownBackend = errors.New("dns: unknown backend")
ErrUnknownBackend is returned by New for the zero value or any Backend the SDK does not implement.
Functions ¶
This section is empty.
Types ¶
type Backend ¶
type Backend int
Backend selects the resolver framework a Manager drives. The zero value is invalid (New → ErrUnknownBackend); valid values start at 1. Only implemented backends exist — a new value is appended if and when a real implementation is written (resolvconf/dnsmasq are intentionally absent, not stubbed).
func Detect ¶
Detect reports the DNS backends usable on THIS host: Resolved when resolvectl is on PATH, NetworkManager when nmcli is. It lists; it never picks and never constructs a Manager — the consumer reads the list, picks a backend explicitly, and passes it to New. An empty slice means no usable DNS manager.
The ctx is accepted for signature uniformity with the other capability Detect functions; the probe itself is a pure PATH lookup.
type Config ¶
type Config struct {
// Nameservers are resolver IP addresses (v4 or v6 literals).
Nameservers []string
// SearchDomains are DNS search domains.
SearchDomains []string
// Interface scopes the configuration to one link. Empty means host-global
// (supported only by the Resolved backend; NetworkManager requires it).
Interface string
}
Config is the desired resolver configuration to Apply.
type Manager ¶
type Manager interface {
// Get reads the active resolver configuration.
Get(ctx context.Context) (State, error)
// Apply installs cfg. It validates cfg (rejecting non-IP nameservers,
// malformed/flag-shaped search domains, and bad interface names) before
// touching any backend, so an invalid config has no side effects.
Apply(ctx context.Context, cfg Config) error
}
Manager is the DNS configuration surface. Every method takes a context.
func New ¶
New returns a Manager for the named backend, driven by runner. Pure: it validates the backend is known and does NOT probe the host (use Detect). The zero value and any unimplemented backend are rejected with ErrUnknownBackend; a nil runner is rejected.
Example ¶
ExampleNew shows the construct-a-handle flow: pick a backend (here systemd-resolved), build a Manager over a Runner, and apply a host-global resolver config.
package main
import (
"context"
"log"
"github.com/manchtools/power-manage-sdk/sys/dns"
"github.com/manchtools/power-manage-sdk/sys/exec"
)
func main() {
r, err := exec.NewRunner(exec.Direct) // the agent runs as root
if err != nil {
log.Fatal(err)
}
m, err := dns.New(dns.Resolved, r)
if err != nil {
log.Fatal(err)
}
if err := m.Apply(context.Background(), dns.Config{
Nameservers: []string{"1.1.1.1", "9.9.9.9"},
SearchDomains: []string{"corp.example"},
}); err != nil {
log.Fatal(err)
}
}
Output: