iface

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package iface is the interface behavior: the network interfaces of the host.

Index

Constants

This section is empty.

Variables

View Source
var Behavior = behavior.Register(behavior.Define(
	"interface", "The network interfaces of the host",
	behavior.Node,
	behavior.API,
	behavior.Area(area.Networking),
	behavior.CLI,
	behavior.Platform(behavior.PlatformDebian, newDebian),
	behavior.Platform(behavior.PlatformLinux, newReader),
	behavior.Platform(behavior.PlatformDarwin, newReader),
	behavior.Requires(netplan.Behavior),
	behavior.RunsOnPlatform(behavior.PlatformLinux, "ip"),
	behavior.RunsOnPlatform(behavior.PlatformDarwin, "ifconfig", "netstat"),
	behavior.RunsIfPresent("ethtool"),
	behavior.SudoOnPlatform(behavior.PlatformDebian, "ip"),
	behavior.Writes(netplan.Dir+"/osapi-*.yaml"),
	behavior.Validator("mac_address", macRE.MatchString,
		func(t rule.Target) { t.Pattern(macPattern) },
		"is not a MAC address of six octets split by colons"),
	behavior.Read("list", List).
		Describe("List the network interfaces of the host").
		Explain("Reads each interface but the loopback, in the order of its index: "+
			"/sys/class/net and ip on Linux, ifconfig and netstat on macOS. dhcp4 is true "+
			"when a route of the interface has proto dhcp, and primary when it has the "+
			"default route.").
		Example("A Linux host", ListInput{}, []Entry{eth0Entry}, linuxHost...).
		Example("A Mac", ListInput{}, []Entry{en0Entry}, darwinHost...),
	behavior.Read("get", Get).Key("name").
		Describe("Get a network interface of the host").
		Explain("Reads the interface as list does.").
		Example("An interface with a DHCP address", GetInput{Name: "eth0"}, eth0Entry,
			append(slices.Clone(linuxHost), behavior.Live("name", behavior.FactInterface))...).
		Example("A Mac", GetInput{Name: "en0"}, en0Entry,
			append(slices.Clone(darwinHost), behavior.Live("name", behavior.FactInterface))...).
		Fails("An interface the host does not have", GetInput{Name: "eth9"}, "not found",
			linuxHost...).
		Fails("A name that is not an interface name", GetInput{Name: "eth0;reboot"}, "Name").
		Fails("A fact the agent does not know", GetInput{Name: "@fact.nonexistent"}, "Name"),
	refuse(behavior.Write("create", Create).Key("name").
		On(behavior.PlatformDebian).
		Describe("Configure a network interface").
		Explain("Writes a netplan file of the interface's own, such as "+
			"/etc/netplan/osapi-osapi0.yaml for osapi0, under a netplan definition that "+
			"is the interface's own, and applies it. type is ethernet, the default, or "+
			"dummy. An ethernet is an interface the host has, in the netplan section that "+
			"netplan status gives for it: one the host does not have is not found. A dummy "+
			"goes under dummy-devices, and netplan apply makes it, so the host need not "+
			"have it. dummy-devices needs netplan 0.107 or later. dhcp4 and dhcp6 are "+
			"false unless given, so the interface has only the addresses given. An "+
			"interface that a match defines, such as cloud-init's all for en*, is refused, "+
			"because the config would change each interface the match takes, now and later. "+
			"An interface osapi already configures does not change: update replaces its "+
			"config. It runs on the Debian family, and elsewhere, and in a container, "+
			"reports skipped.").
		Example("A dummy interface with a static address", osapi0Static,
			Result{Name: "osapi0", Changed: true},
			append(
				append([]behavior.HostOption{behavior.Wrote(osapi0File, osapi0YAML)}, writes...),
				behavior.Reads(GetInput{Name: "osapi0"}, Entry{Name: "osapi0", Addresses: []string{"10.0.1.5/24"}}),
			)...,
		).
		Example("Each setting",
			CreateInput{
				Name: "osapi0", Type: "dummy", DHCP6: true, Addresses: []string{"2001:db8::5/64"},
				MTU: 9000, MACAddress: "52:54:00:aa:bb:cc",
			},
			Result{Name: "osapi0", Changed: true},
			append(
				append([]behavior.HostOption{
					behavior.Wrote(osapi0File, "network:\n  version: 2\n  dummy-devices:\n    osapi0:\n"+
						"      dhcp4: false\n      dhcp6: true\n"+
						"      addresses:\n        - 2001:db8::5/64\n"+
						"      mtu: 9000\n      macaddress: 52:54:00:aa:bb:cc\n"),
				}, writes...),
				behavior.Reads(GetInput{Name: "osapi0"}, Entry{Name: "osapi0", MTU: 9000, MAC: "52:54:00:aa:bb:cc"}),
			)...,
		).
		Example("An interface the host has",
			CreateInput{Name: "eth1", DHCP4: true, WakeOnLAN: true},
			Result{Name: "eth1", Changed: true},
			append([]behavior.HostOption{
				behavior.Wrote(netplan.Dir+"/osapi-eth1.yaml",
					"network:\n  version: 2\n  ethernets:\n    eth1:\n"+
						"      dhcp4: true\n      dhcp6: false\n      wakeonlan: true\n"),
			}, eth1Host...)...).
		Example("An interface osapi already configures", osapi0Static, Result{Name: "osapi0"},
			append(
				slices.Clone(osapi0Host),
				behavior.After(osapi0Static),
				behavior.Reads(GetInput{Name: "osapi0"}, Entry{Name: "osapi0", Addresses: []string{"10.0.1.5/24"}}),
			)...,
		).
		Fails("An interface in a match that takes other interfaces",
			CreateInput{Name: "enp0s1", DHCP4: true}, "also takes enp0s2",
			behavior.Exists("/sys/class/net/enp0s1/ifindex", "2"),
			behavior.Prints("netplan status --format json",
				`{"enp0s1":{"type":"ethernet","id":"all"},"enp0s2":{"type":"ethernet","id":"all"}}`),
		).
		Fails("An interface in a match that takes it alone, for now",
			CreateInput{Name: "enp0s1", DHCP4: true}, "any interface it matches later",
			behavior.Exists("/sys/class/net/enp0s1/ifindex", "2"),
			behavior.Prints("netplan status --format json", `{"enp0s1":{"type":"ethernet","id":"all"}}`),
		).
		Fails("An interface the host does not have", CreateInput{Name: "eth9", DHCP4: true}, "not found",
			osapi0Host...),
		func(c Config) any { return CreateInput(c) }),
	refuse(behavior.Write("update", Update).Key("name").
		On(behavior.PlatformDebian).
		Describe("Change the config osapi has of a network interface").
		Explain("Writes the interface's netplan file over the one osapi has, as create "+
			"writes it, and applies it. Without type dummy, the config goes in the section "+
			"that netplan status gives, which is dummy-devices for a dummy, so a dummy "+
			"stays a dummy. An interface osapi does not configure is not found: create "+
			"configures it.").
		Example("Move an interface to DHCP", UpdateInput{Name: "osapi0", Type: "dummy", DHCP4: true},
			Result{Name: "osapi0", Changed: true},
			append(
				slices.Clone(writes),
				behavior.After(osapi0Static),
				behavior.Wrote(osapi0File, "network:\n  version: 2\n  dummy-devices:\n    osapi0:\n"+
					"      dhcp4: true\n      dhcp6: false\n"),
				behavior.ReadsNone(GetInput{Name: "osapi0"}, Entry{Name: "osapi0", Addresses: []string{"10.0.1.5/24"}}),
			)...,
		).
		Fails("An interface osapi does not configure", UpdateInput{Name: "osapi0", DHCP4: true},
			"not found"),
		func(c Config) any { return UpdateInput(c) }),
	behavior.Write("delete", Delete).Key("name").
		On(behavior.PlatformDebian).
		Describe("Drop the config osapi has of a network interface").
		Explain("Removes the interface's netplan file and applies what is left. netplan "+
			"apply leaves the link of a dummy, so delete then deletes the link with ip "+
			"link delete. An interface osapi does not configure does not change.").
		Example("A dummy interface osapi configures", DeleteInput{Name: "osapi0"},
			Result{Name: "osapi0", Changed: true},
			append(slices.Clone(osapi0Host),
				behavior.After(osapi0Static),
				behavior.Removed(osapi0File),
				behavior.Ran("netplan", "apply"),
				behavior.Ran("ip", "link", "delete", "osapi0"),
				behavior.ReadsNone(GetInput{Name: "osapi0"}, Entry{Name: "osapi0"}, "name"),
			)...,
		).
		Example("An interface osapi does not configure", DeleteInput{Name: "osapi0"},
			Result{Name: "osapi0"},
			behavior.ReadsNone(GetInput{Name: "osapi0"}, Entry{Name: "osapi0"}, "name"),
		).
		Fails("A name that is not an interface name", DeleteInput{Name: "eth0;reboot"}, "Name"),
))

Behavior is the network interfaces of the host. gohai reads them on every platform; a netplan file of osapi's own configures one on the Debian family.

Functions

This section is empty.

Types

type Config

type Config struct {
	Name       string              `` /* 203-byte string literal not displayed */
	Type       types.InterfaceType `` /* 186-byte string literal not displayed */
	DHCP4      bool                `json:"dhcp4,omitempty"       validate:"omitempty"                  doc:"Take an IPv4 address from DHCP"`
	DHCP6      bool                `json:"dhcp6,omitempty"       validate:"omitempty"                  doc:"Take an IPv6 address from DHCP"`
	Addresses  []string            `` /* 132-byte string literal not displayed */
	Gateway4   string              `json:"gateway4,omitempty"    validate:"omitempty,ipv4"             doc:"The IPv4 default gateway"`
	Gateway6   string              `json:"gateway6,omitempty"    validate:"omitempty,ipv6"             doc:"The IPv6 default gateway"`
	MTU        int                 `json:"mtu,omitempty"         validate:"omitempty,min=68,max=9000"  doc:"The MTU, from 68 to 9000"`
	MACAddress string              `json:"mac_address,omitempty" validate:"omitempty,mac_address"      doc:"The MAC address to set"`
	WakeOnLAN  bool                `json:"wakeonlan,omitempty"   validate:"omitempty"                  doc:"Turn on wake-on-LAN"`
}

Config is the config osapi writes for an interface. dhcp4 and dhcp6 are always written; another empty field is left out of the file. Type dummy writes the interface under netplan's dummy-devices, and netplan makes it.

type CreateInput

type CreateInput Config

CreateInput is the config of an interface osapi has no config of.

type DeleteInput

type DeleteInput struct {
	Name string `json:"name" validate:"required,interface_name" doc:"The interface, such as eth0, or a fact such as @fact.interface.primary"`
}

DeleteInput names the interface whose config osapi wrote.

type Entry

type Entry struct {
	Name      string   `json:"name"      doc:"The interface"`
	State     string   `json:"state"     doc:"up or down"`
	MAC       string   `json:"mac"       doc:"Its MAC address; empty for one without, such as lo"`
	MTU       int      `json:"mtu"       doc:"Its MTU"`
	IPv4      string   `json:"ipv4"      doc:"Its first IPv4 address that is not link-local"`
	IPv6      string   `json:"ipv6"      doc:"Its first IPv6 address that is not link-local"`
	Addresses []string `json:"addresses" doc:"Each of its addresses, in CIDR notation"`
	DHCP4     bool     `json:"dhcp4"     doc:"Whether DHCP gave it an IPv4 address: a route of it has proto dhcp"`
	Primary   bool     `json:"primary"   doc:"Whether it has the default route"`
}

Entry is an interface as the host reports it.

func Get

func Get(
	ctx context.Context,
	p Provider,
	in GetInput,
) (Entry, error)

Get is one interface of the host.

func List

func List(
	ctx context.Context,
	p Provider,
	_ ListInput,
) ([]Entry, error)

List is every interface of the host but lo.

type GetInput

type GetInput struct {
	Name string `json:"name" validate:"required,interface_name" doc:"The interface, such as eth0, or a fact such as @fact.interface.primary"`
}

GetInput names the interface to read.

type ListInput

type ListInput struct{}

ListInput takes nothing: list reads every interface.

type Provider

type Provider interface {
	// Interfaces is every interface of the host but lo.
	Interfaces(ctx context.Context) ([]Entry, error)
	// Interface is one interface of the host.
	Interface(ctx context.Context, name string) (Entry, error)
	// Create writes the interface's config. It changes nothing when osapi
	// already has a config of the interface.
	Create(ctx context.Context, in Config) (bool, error)
	// Update writes the interface's config over the one osapi has. It
	// fails when osapi has none.
	Update(ctx context.Context, in Config) (bool, error)
	// Remove drops osapi's config of the interface.
	Remove(ctx context.Context, name string) (bool, error)
}

Provider reads the interfaces of the host and writes osapi's config of one.

type Result

type Result struct {
	Name    string `json:"name"    doc:"The interface"`
	Changed bool   `json:"changed" doc:"Whether the host changed"`
}

Result is an interface after a write or a removal.

func Create

func Create(
	ctx context.Context,
	p Provider,
	in CreateInput,
) (Result, error)

Create writes osapi's config of an interface.

func Delete

func Delete(
	ctx context.Context,
	p Provider,
	in DeleteInput,
) (Result, error)

Delete drops osapi's config of an interface.

func Update

func Update(
	ctx context.Context,
	p Provider,
	in UpdateInput,
) (Result, error)

Update writes osapi's config of an interface over the one it has.

type UpdateInput

type UpdateInput Config

UpdateInput is the config to write over the one osapi has.

Directories

Path Synopsis
Package mocks holds the generated double of the interface provider, for the methods' tests.
Package mocks holds the generated double of the interface provider, for the methods' tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL