Documentation
¶
Index ¶
- Constants
- func SetLogger(l Logger)
- type Configurator
- type Interface
- type Logger
- type Option
- func WithAllowNoBackends(allowed bool) Option
- func WithAllowPrimaryRemoval(allowed bool) Option
- func WithBackupRetention(n int) Option
- func WithConnectivityCheck(enabled bool) Option
- func WithConnectivityTimeout(d time.Duration) Option
- func WithPingCount(n int) Option
- func WithPingTimeout(d time.Duration) Option
- func WithServiceReadyTimeout(timeout time.Duration) Option
- func WithSkipConnectivityCheck() Option
- func WithSkipPanels(skip bool) Option
- func WithTestAddress(addr string) Option
- type Route
Constants ¶
const ( Public = "public-internet" Public6 = "public-internet-6" )
Variables ¶
This section is empty.
Functions ¶
func SetLogger ¶
func SetLogger(l Logger)
SetLogger replaces the package-wide logger used for non-fatal diagnostics. It is package scoped (not a per-Configurator option) because the configuration backends and control panels log independently of any single Configurator instance; the setting therefore applies to every Configurator in the process. A nil logger is ignored.
Types ¶
type Configurator ¶
type Configurator interface {
GetInterfaces(ctx context.Context) ([]*Interface, error)
AddAddress(ctx context.Context, iface string, addr *net.IPNet, gateway net.IP) error
SetPrimaryAddress(ctx context.Context, iface string, addr *net.IPNet) error
RemoveAddress(ctx context.Context, iface string, addr *net.IPNet) error
AddRoute(ctx context.Context, iface string, dst *net.IPNet, gateway net.IP, metric int) error
RemoveRoute(ctx context.Context, iface string, dst *net.IPNet, gateway net.IP) error
SetDNS(ctx context.Context, iface string, servers []net.IP, searchDomains []string) error
// SetDHCP turns each address family's DHCP client on or off. Both families
// are stated explicitly, so a caller moving an interface to DHCPv4 while
// keeping a static IPv6 address passes (true, false).
//
// Adding a static address does not imply disabling DHCP: every backend
// except ifupdown can carry static addresses alongside a lease, and which
// of the two the operator wants is not something AddAddress can infer.
// SetDHCP is how that choice is made.
//
// Enabling a family also asks the running system to acquire a lease now,
// rather than at the next reboot. Disabling one only rewrites the
// configuration: an interface's existing lease is left in place until it
// expires or the network is reconfigured, so the call cannot strand a
// caller that is connected over the leased address.
SetDHCP(ctx context.Context, iface string, dhcp4, dhcp6 bool) error
}
func NewConfigurator ¶
func NewConfigurator(ctx context.Context, opts ...Option) (Configurator, error)
Returns the linux network configurator. Backends are auto-detected; ctx bounds that detection, which queries systemd over D-Bus and may shell out to chkconfig and netplan. Behaviour is tuned with Option values such as WithTestAddress and WithConnectivityCheck; use SetLogger to replace the package-wide logger.
type Interface ¶
type Interface struct {
Name string
MAC net.HardwareAddr
Addresses []*net.IPNet
Gateway4 net.IP
Gateway6 net.IP
Routes []*Route
DNS []net.IP
// Up reports whether the interface can carry traffic right now, and
// Physical whether it is backed by a network device rather than created by
// the kernel or the hypervisor host — a bridge, bond, VLAN, tunnel, or the
// veth pair of a container is not physical, a virtio or vmxnet NIC is.
// Both describe the running system and are only set by GetInterfaces; the
// Interface values the configuration backends read back leave them false.
Up bool
Physical bool
// DHCP4 and DHCP6 report whether the interface runs a DHCP client for that
// address family. They describe the persisted configuration, not the
// running system: the kernel cannot be asked whether an address arrived
// from a lease, so these are read back from the configuration backends the
// same way DNS is. An interface can run a DHCP client and still carry
// static addresses of the same family.
DHCP4 bool
DHCP6 bool
SearchDomains []string
Link any
}
func FindInterfaceByName ¶
Take a name and a list of interfaces and finds an interface by its name.
func FindPhysicalInterfaces ¶
FindPhysicalInterfaces returns the host's physical interfaces — leaving out the bridges, bonds, VLANs, tunnels, and container veth pairs that should never be handed a public address — ordered so the interface most likely to be the one a caller wants to configure for internet access comes first.
Interfaces that are up sort ahead of those that are down, then those already carrying a default gateway (IPv4 ahead of IPv6-only), then wired ahead of wireless, and finally by name read the way a human reads it, so eth0 comes before eth1 and eth2 before eth10.
This is a ranking rather than a decision: a caller looking for an interface that meets some further requirement — one not already holding a public address, say — filters the returned slice and takes the first survivor.
type Logger ¶
type Logger interface {
Printf(format string, args ...interface{})
Println(args ...interface{})
}
Logger is the minimal logging surface used across the package. It is satisfied by the standard library *log.Logger, logrus, and most other logging libraries, so callers can plug in their own logger via SetLogger.
type Option ¶
type Option func(*configOptions)
Option configures a Configurator constructed with NewConfigurator.
func WithAllowNoBackends ¶
WithAllowNoBackends lets NewConfigurator return a configurator on a Linux host where backend detection found no network configuration backend. By default that is an error: netlink would apply an address change to the running system, but with nothing to write it to the change would not survive a reboot, and the caller would never be told. Enable this when the configurator is only used to read state (GetInterfaces), or when the caller accepts runtime-only changes.
func WithAllowPrimaryRemoval ¶
WithAllowPrimaryRemoval permits RemoveAddress to remove an address that is the primary of its family on its interface. By default removing the primary is refused so a caller cannot accidentally change the system's source address or leave a control panel without a main IP; the intended flow is to SetPrimaryAddress on another IP first. Enable this only when you are deliberately tearing down the current primary.
func WithBackupRetention ¶
WithBackupRetention sets how many .bak.* copies are kept per original configuration file. The oldest backups beyond this number are pruned each time a backend saves. Pass 0 (or a negative value) to disable pruning and keep every backup.
func WithConnectivityCheck ¶
WithConnectivityCheck enables or disables the post-change internet reachability test (and the automatic rollback that depends on it). It is enabled by default; pass false to skip it, for example on hosts with no outbound internet access.
func WithConnectivityTimeout ¶
WithConnectivityTimeout sets the timeout for the HTTP reachability test run after an address or gateway change. A smaller value fails faster on a black-holed route; a larger value tolerates slower links.
func WithPingCount ¶
WithPingCount sets the number of ICMP probes sent when confirming that a candidate address or gateway is reachable. More probes are more reliable on lossy links but take longer.
func WithPingTimeout ¶
WithPingTimeout sets the overall timeout for a single ping run.
func WithServiceReadyTimeout ¶
WithServiceReadyTimeout bounds how long NewConfigurator waits for a backend whose configuration is made through a daemon rather than a file. Today that is NetworkManager: it is configured over D-Bus, so a configurator built before the daemon owns its bus name would find no connections to change.
This matters at boot. A host that starts this program from a unit ordered alongside NetworkManager, rather than after it, reaches backend detection while the daemon is still starting; without the wait it would either fail to register the backend at all, or register one that reports an empty interface list. The wait ends as soon as the daemon answers and reports it has finished starting up, so a host where it is already running pays nothing.
A timeout of zero or less does not wait: detection probes once and proceeds, which is the right choice for a caller that has already ordered itself after the daemon and would rather fail fast than block.
func WithSkipConnectivityCheck ¶
func WithSkipConnectivityCheck() Option
WithSkipConnectivityCheck is a convenience option that disables the post-change internet reachability test and its automatic rollback. It is equivalent to WithConnectivityCheck(false).
func WithSkipPanels ¶
WithSkipPanels makes the configurator skip all control-panel backends (cPanel, Plesk, InterWorx) during mutating operations. When enabled, AddAddress, SetPrimaryAddress, and RemoveAddress do not tell the panels to reload, set the main IP, or release an IP; only the running system and the network-manager configuration files are changed. This is intended for maintenance paths where the panel must be left untouched, or where the panel is expected to be reconciled separately.
func WithTestAddress ¶
WithTestAddress overrides the URL used for the post-change connectivity test. The two sentinel values "test_success" and "test_fail" force the test to pass or fail without performing a request, which is useful in tests.