route

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: 14 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 route is the route behavior: the routes of an interface.

Index

Constants

This section is empty.

Variables

View Source
var Behavior = behavior.Register(behavior.Define(
	"route", "The routes of an interface",
	behavior.Title("Interface route"),
	behavior.Node,
	behavior.API,
	behavior.Area(area.Networking),
	behavior.CLI,
	behavior.Of(iface.Behavior),
	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.Writes(netplan.Dir+"/osapi-*-routes.yaml"),
	behavior.Read("list", List).
		Describe("List the routes of an interface").
		Explain("Reads the routes of the main table that go out of the interface: ip route "+
			"on Linux, netstat on macOS. proto says where a route came from, such as dhcp, "+
			"kernel or static. An interface with no routes has none; one the host does not "+
			"have is not found.").
		Example("An interface with routes", ListInput{Name: "eth0"}, eth0Routes,
			append(slices.Clone(linuxHost), behavior.Live("name", behavior.FactInterface))...).
		Example("An interface with no routes", ListInput{Name: "eth1"}, []Entry{},
			append(
				slices.Clone(linuxHost),
				behavior.NotLive(
					"a second interface with no routes, which a real host may not have",
				),
			)...).
		Example("A Mac", ListInput{Name: "en0"}, en0Routes,
			append(slices.Clone(darwinHost), behavior.Live("name", behavior.FactInterface))...).
		Fails("An interface the host does not have", ListInput{Name: "eth9"}, "not found",
			linuxHost...).
		Fails("A name that is not an interface name", ListInput{Name: "eth0;reboot"}, "Name"),
	refuse(behavior.Write("create", Create).
		On(behavior.PlatformDebian).
		Describe("Add a static route to an interface").
		Explain("Adds the route to the interface's netplan file, such as "+
			"/etc/netplan/osapi-osapi0-routes.yaml for osapi0, under a netplan definition "+
			"that is the interface's own, and applies it. A route osapi already has, the "+
			"same, does not change; one to the same destination by another gateway or "+
			"metric is refused: update changes it. A default route is refused: the "+
			"interface behavior sets the gateway. An interface the host does not have is "+
			"not found. An interface that a match defines, such as cloud-init's all for "+
			"en*, is refused, because the route would go on each interface the match "+
			"takes. It runs on the Debian family, and elsewhere, and in a container, "+
			"reports skipped.").
		Example("A route", v4, Result{Destination: v4.Destination, Changed: true},
			writes(
				behavior.Wrote(osapi0File, routesHead+v4YAML),
				behavior.Reads(ListInput{Name: "osapi0"}, Entry{Destination: "198.51.100.0/24", Gateway: "192.0.2.1"}),
			)...,
		).
		Example("A second route, with a metric", v6, Result{Destination: v6.Destination, Changed: true},
			writes(
				behavior.After(v4),
				behavior.Wrote(osapi0File, routesHead+v4YAML+v6YAML),
				behavior.Reads(ListInput{Name: "osapi0"}, Entry{Destination: "2001:db8:1::/48", Gateway: "2001:db8::1", Metric: 200}),
			)...,
		).
		Example("A route osapi already has", v4, Result{Destination: v4.Destination},
			append(
				slices.Clone(osapi0Host),
				behavior.After(v4),
				behavior.Reads(ListInput{Name: "osapi0"}, Entry{Destination: "198.51.100.0/24", Gateway: "192.0.2.1"}),
			)...,
		).
		Fails("A default route",
			CreateInput{Name: "osapi0", Destination: "0.0.0.0/0", Via: "192.0.2.1"},
			"default route", osapi0Host...).
		Fails("A gateway of the other family",
			CreateInput{Name: "osapi0", Destination: "198.51.100.0/24", Via: "2001:db8::1"},
			"not an address of the family", osapi0Host...).
		Fails("Another route to a destination osapi has",
			CreateInput{Name: "osapi0", Destination: "198.51.100.0/24", Via: "192.0.2.2"},
			"update changes it", append(slices.Clone(osapi0Host), behavior.After(v4))...).
		Fails("An interface in a match that takes other interfaces",
			CreateInput{
				Name:        "enp0s1",
				Destination: v4.Destination,
				Via:         v4.Via,
			}, "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", Destination: v4.Destination, Via: v4.Via},
			"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", Destination: v4.Destination, Via: v4.Via}, "not found",
			osapi0Host...),
		func(r Route) any { return CreateInput(r) }),
	refuse(behavior.Write("update", Update).
		On(behavior.PlatformDebian).
		Describe("Change the gateway or the metric of a route").
		Explain("Writes the route over the one osapi has to the same destination, and "+
			"applies it. A destination osapi has no route to is not found: create adds it.").
		Example("A new gateway and metric",
			UpdateInput{
				Name:        "osapi0",
				Destination: "198.51.100.0/24",
				Via:         "192.0.2.2",
				Metric:      10,
			},
			Result{Destination: v4.Destination, Changed: true},
			writes(
				behavior.After(v4),
				behavior.Wrote(osapi0File, routesHead+
					"        - to: 198.51.100.0/24\n          via: 192.0.2.2\n          metric: 10\n"),
				behavior.Reads(
					ListInput{Name: "osapi0"},
					Entry{Destination: "198.51.100.0/24", Gateway: "192.0.2.2", Metric: 10},
				),
			)...,
		).
		Fails("A destination osapi has no route to", UpdateInput(v4), "not found", osapi0Host...),
		func(r Route) any { return UpdateInput(r) }),
	behavior.Write("delete", Delete).
		On(behavior.PlatformDebian).
		Describe("Drop a route that osapi wrote").
		Explain("Drops the route from the interface's netplan file and applies what is "+
			"left; the last route removes the file. A route osapi does not have does not "+
			"change.").
		Example("One of two routes", DeleteInput{Name: "osapi0", Destination: v4.Destination},
			Result{Destination: v4.Destination, Changed: true},
			writes(
				behavior.After(v4),
				behavior.After(v6),
				behavior.Wrote(osapi0File, routesHead+v6YAML),
				behavior.ReadsNone(ListInput{Name: "osapi0"}, Entry{Destination: "198.51.100.0/24"}, "destination"),
			)...,
		).
		Example("The last route", DeleteInput{Name: "osapi0", Destination: v4.Destination},
			Result{Destination: v4.Destination, Changed: true},
			append(
				slices.Clone(osapi0Host),
				behavior.After(v4),
				behavior.Removed(osapi0File),
				behavior.Ran("netplan", "apply"),
				behavior.ReadsNone(ListInput{Name: "osapi0"}, Entry{Destination: "198.51.100.0/24"}, "destination"),
			)...,
		).
		Example("A route osapi does not have", DeleteInput{Name: "osapi0", Destination: v4.Destination},
			Result{Destination: v4.Destination},
			behavior.ReadsNone(ListInput{Name: "osapi0"}, Entry{Destination: "198.51.100.0/24"}, "destination"),
		).
		Fails("A destination that is not CIDR", DeleteInput{Name: "osapi0", Destination: "10.0.0.0"},
			"Destination").
		Fails("A name that is not an interface name", DeleteInput{Name: "eth0;reboot", Destination: v4.Destination},
			"Name"),
))

Behavior is the routes of an interface. gohai reads them on every platform; a netplan file of osapi's own writes them on the Debian family, one route a call, as ip route add and ip route del do.

Functions

This section is empty.

Types

type CreateInput

type CreateInput Route

CreateInput is a route that osapi does not have yet.

type DeleteInput

type DeleteInput struct {
	Name        string `` /* 147-byte string literal not displayed */
	Destination string `` /* 147-byte string literal not displayed */
}

DeleteInput names a route that osapi wrote.

type Entry

type Entry struct {
	Destination string `json:"destination" doc:"The destination in CIDR notation, or default"`
	Gateway     string `json:"gateway"     doc:"The gateway; empty for a route on the link"`
	Interface   string `json:"interface"   doc:"The interface the route goes out of"`
	Family      string `json:"family"      doc:"inet or inet6"`
	Metric      int    `json:"metric"      doc:"The metric; 0 when the kernel gives none"`
	Proto       string `json:"proto"       doc:"Where the route came from, such as kernel, dhcp or static; empty on macOS"`
	Scope       string `json:"scope"       doc:"The scope, such as link; empty for a global route"`
}

Entry is a route of the host's main routing table.

func List

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

List is the routes of an interface.

type ListInput

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

ListInput names the interface whose routes to read.

type Provider

type Provider interface {
	// Routes is the routes of one interface of the host's main table.
	Routes(ctx context.Context, name string) ([]Entry, error)
	// Set writes the route r of its interface. With create, a route of that
	// destination that osapi has is unchanged when it is the same, and
	// refused when it is not; without it, a destination osapi does not have
	// is not found.
	Set(ctx context.Context, r Route, create bool) (bool, error)
	// Remove drops osapi's route to destination of the interface.
	Remove(ctx context.Context, name string, destination string) (bool, error)
}

Provider reads the routes of an interface and writes osapi's routes of it, one at a time.

type Result

type Result struct {
	Destination string `json:"destination" doc:"The destination of the route"`
	Changed     bool   `json:"changed"     doc:"Whether the host changed"`
}

Result is a route after a write or a removal.

func Create

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

Create adds a route to an interface.

func Delete

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

Delete drops a route that osapi wrote.

func Update

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

Update changes the gateway or the metric of a route that osapi has.

type Route

type Route struct {
	Name        string `` /* 168-byte string literal not displayed */
	Destination string `` /* 168-byte string literal not displayed */
	Via         string `` /* 144-byte string literal not displayed */
	Metric      int    `json:"metric,omitempty" validate:"omitempty,min=0,max=4294967295" doc:"The metric, from 0 to 4294967295; none when it is 0"`
}

Route is one route of an interface that osapi writes.

type UpdateInput

type UpdateInput Route

UpdateInput is a new gateway or metric of a route that osapi has.

Directories

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

Jump to

Keyboard shortcuts

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