Documentation
¶
Overview ¶
Package netconfig manages a host's per-interface IP / routing / MTU configuration through an injected exec.Runner.
It is distinct from sys/network (WiFi connection profiles) and sys/dns (resolver policy): a device can use different tools for each concern (NetworkManager for WiFi auth, systemd-networkd for interface IPs, etc.).
r, _ := exec.NewRunner(exec.Direct)
m, err := netconfig.New(netconfig.SystemdNetworkd, r)
if err != nil { ... }
err = m.Apply(ctx, netconfig.InterfaceConfig{
Name: "eth0", Mode: netconfig.Static,
Addresses: []string{"192.0.2.10/24"}, Gateway: "192.0.2.1",
})
Two backends are implemented: NetworkManager (nmcli, connection-scoped) and SystemdNetworkd (writes /etc/systemd/network/<name>.network via the fs.Manager and reloads networkd). Apply is declarative desired-state — routes are folded into InterfaceConfig and applied atomically with the interface, matching both backends' models. Get reads the EFFECTIVE kernel state via `ip -j` (backend-agnostic). Mutations escalate through the Runner; the read does not.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidConfig = errors.New("netconfig: invalid config")
ErrInvalidConfig is returned when an InterfaceConfig field is unsafe or malformed. Apply validates before touching any backend, so a bad config is rejected without side effects.
var ErrUnknownBackend = errors.New("netconfig: 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 AddressMode ¶
type AddressMode int
AddressMode selects DHCP vs static addressing. The zero value is invalid so a caller must choose explicitly (a forgotten Mode never silently defaults).
const ( // DHCP requests addresses dynamically. DHCP AddressMode = iota + 1 // Static uses the Addresses/Gateway from the config. Static )
type Backend ¶
type Backend int
Backend selects the tool that manages interface IP configuration. The zero value is invalid (New → ErrUnknownBackend); valid values start at 1. Only implemented backends exist (netplan/dhcpcd/ifupdown are intentionally absent, not stubbed).
func Detect ¶
Detect reports the interface-config backends usable on THIS host: NetworkManager when nmcli is on PATH, SystemdNetworkd when networkctl 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 interface-config manager.
The ctx is accepted for signature uniformity with the other capability Detect functions; the probe itself is a pure PATH lookup.
type InterfaceConfig ¶
type InterfaceConfig struct {
Name string // interface name, e.g. eth0
Mode AddressMode // DHCP or Static (required) — governs addressing only
Addresses []string // CIDR list, static mode only, e.g. ["192.0.2.10/24"]
Gateway string // default gateway, static mode only (family must match an address)
DNS []string // resolver IPs (applied in both modes; sys/dns is DNS policy's proper home)
MTU int // 0 = backend default (applied in both modes)
Routes []Route // additional static routes (applied in both modes)
}
InterfaceConfig is the desired state of a network interface.
Mode governs ADDRESSING only: Addresses and Gateway are honoured solely in Static mode (under DHCP the server supplies them, so they are not emitted — though still validated if present). DNS, MTU, and Routes are independent of the addressing mode and ARE applied in both modes — a common, valid setup is a DHCP address plus a static route to a management subnet, or a static DNS override on a DHCP link.
type Manager ¶
type Manager interface {
// Apply validates cfg and installs it as the interface's desired state
// (addresses/gateway/DNS/MTU/routes). Validation runs before any backend
// mutation, so an invalid config has no side effects.
Apply(ctx context.Context, cfg InterfaceConfig) error
// Get reads the EFFECTIVE kernel state of the named interface via `ip -j`
// (addresses, gateway, MTU, routes). Mode and DNS are not recoverable from
// the kernel and are left at their zero values.
Get(ctx context.Context, name string) (InterfaceConfig, error)
}
Manager is the interface-configuration surface.
func New ¶
New returns a Manager for the named backend, driven by runner. Pure: validates the backend is known; 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-networkd), build a Manager over a Runner, and apply a static IP config with a default route.
package main
import (
"context"
"log"
"github.com/manchtools/power-manage-sdk/sys/exec"
"github.com/manchtools/power-manage-sdk/sys/netconfig"
)
func main() {
r, err := exec.NewRunner(exec.Direct) // the agent runs as root
if err != nil {
log.Fatal(err)
}
m, err := netconfig.New(netconfig.SystemdNetworkd, r)
if err != nil {
log.Fatal(err)
}
if err := m.Apply(context.Background(), netconfig.InterfaceConfig{
Name: "eth0",
Mode: netconfig.Static,
Addresses: []string{"192.0.2.10/24"},
Gateway: "192.0.2.1",
DNS: []string{"1.1.1.1"},
MTU: 1500,
Routes: []netconfig.Route{{Destination: "10.0.0.0/8", Gateway: "192.0.2.254"}},
}); err != nil {
log.Fatal(err)
}
}
Output:
type Route ¶
type Route struct {
// Destination is a CIDR (e.g. "10.0.0.0/8") or the literal "default".
Destination string
// Gateway is the next-hop IP. Required.
Gateway string
// Metric is the route priority (0 = backend default).
Metric int
}
Route is a routing-table entry applied with an interface.