namespaces

package
v0.110.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WrapIfFail

func WrapIfFail(msg string, originalerr error, fn func() error) error

WrapIfFail executes the provided function. If the function succeeds it simply returns the original error (that can be nil). If the function fails then it assesses if an original error was provided and wraps it if true. This function is a sugar to be used at when deferring function that can also return errors, we don't want to loose any context.

Types

type Configuration

type Configuration struct {
	// Logf is a function that will be used to log messages. If not
	// provided the default logger will be used.
	Logf func(string, ...interface{})
	// Port is the port to use for the UDP and TCP pings.
	Port int
	// Timeout is the timeout for the UDP and TCP connection to finish.
	Timeout time.Duration
}

Configuration holds the runtime configuration for this package.

func NewConfiguration

func NewConfiguration(options ...Option) Configuration

NewConfiguration creates a new configuration with the provided options.

type InterfacePair

type InterfacePair struct {
	// contains filtered or unexported fields
}

InterfacePair represents a pair of virtual ethernets that are connected to each other. these are used to connect a network namespace to the outside world.

func NewInterfacePair

func NewInterfacePair(prefix string, options ...Option) *InterfacePair

NewInterfacePair creates a pair of connected virtual ethernets. interfaces are named `prefix-in` and `prefix-out`.

func (*InterfacePair) Close

func (p *InterfacePair) Close() error

Close deletes the interface pair. by deleting one of the interfaces, the other is deleted as well.

func (*InterfacePair) SetExternalIP

func (p *InterfacePair) SetExternalIP(outaddr string) error

SetExternalIP assigns an ip address to the interface living in the default namespace (outside interface).

func (*InterfacePair) Setup

func (p *InterfacePair) Setup() (err error)

Setup sets up the interface pair. this function will create the veth pair and bring the interfaces up.

type ManagedNetworkNamespace

type ManagedNetworkNamespace struct {
	*NetworkNamespace
	*InterfacePair

	InternalIP net.IP
	ExternalIP net.IP
	// contains filtered or unexported fields
}

ManagedNetworkNamespace is a struct that helps up setting up a namespace with a pre-defined configuration. See NewManagedNetworkNamespace for more information on how the namespace is configured.

func NewManagedNetworkNamespace

func NewManagedNetworkNamespace(name, cidraddr string, options ...Option) (*ManagedNetworkNamespace, error)

NewManagedNetworkNamespace creates a new configured network namespace. This network namespace will have an interface configurwed with the first ip address of the provided cidr. The external interface (living in the default namespace) will be configured with the last ip address of the provided cidr and will be set as the default gateway for the namespace.

func (*ManagedNetworkNamespace) Close

func (n *ManagedNetworkNamespace) Close() error

Close destroys both the interface pair and the namespace. Here we only need to worry about deleting the namespace as the veth pair will be deleted automatically.

type NamespaceHandle

type NamespaceHandle struct{}

NamespaceHandle is a struct that exists solely for the purpose of mocking netns functions on tests. It just wraps calls to the netns package.

func (NamespaceHandle) DeleteNamed

func (n NamespaceHandle) DeleteNamed(name string) error

DeleteNamed calls netns.DeleteNamed.

func (NamespaceHandle) Get

func (n NamespaceHandle) Get() (netns.NsHandle, error)

Get calls netns.Get.

func (NamespaceHandle) NewNamed

func (n NamespaceHandle) NewNamed(name string) (netns.NsHandle, error)

NewNamed calls netns.NewNamed.

func (NamespaceHandle) Set

func (n NamespaceHandle) Set(ns netns.NsHandle) error

Set calls netns.Set.

type NamespaceHandler

type NamespaceHandler interface {
	DeleteNamed(string) error
	Set(netns.NsHandle) error
	Get() (netns.NsHandle, error)
	NewNamed(string) (netns.NsHandle, error)
}

NamespaceHandler is an interface that represents the netns functions that we need to mock. This only exists for test purposes.

type NamespacePinger

type NamespacePinger struct {
	*ManagedNetworkNamespace
	// contains filtered or unexported fields
}

func NewNamespacePinger

func NewNamespacePinger(name, cidraddr string, options ...Option) (*NamespacePinger, error)

func (*NamespacePinger) PingTCP

func (n *NamespacePinger) PingTCP(dst net.IP) error

PingTCP communicates with the provided IP address from within the namespace. This functions sends an TCP packet and expects to receive an echo back.

func (*NamespacePinger) PingUDP

func (n *NamespacePinger) PingUDP(dst net.IP) error

PingUDP communicates with the provided IP address from within the namespace. This functions sends an UDP packet and expects to receive an echo back.

func (*NamespacePinger) StartTCPEchoServer

func (n *NamespacePinger) StartTCPEchoServer(errors chan error)

StartTCPEchoServer is a helper to run startTCPEchoServer inside a goroutine. This function blocks until the server is ready to receive packets or failed to start. Errors are sent to the provided channel.

func (*NamespacePinger) StartUDPEchoServer

func (n *NamespacePinger) StartUDPEchoServer(errors chan error)

StartUDPEchoServer is a helper to run startUDPTCPEchoServer inside a goroutine. This function blocks until the server is ready to receive packets or failed to start. Errors are sent to the provided channel.

type NetlinkHandle

type NetlinkHandle struct{}

NetlinkHandle is a struct that exists solely for the purpose of mocking netlink functions on tests.

func (NetlinkHandle) AddrAdd

func (n NetlinkHandle) AddrAdd(l netlink.Link, a *netlink.Addr) error

AddrAdd calls netlink.AddrAdd.

func (NetlinkHandle) LinkAdd

func (n NetlinkHandle) LinkAdd(link netlink.Link) error

LinkAdd calls netlink.LinkAdd.

func (NetlinkHandle) LinkByName

func (n NetlinkHandle) LinkByName(name string) (netlink.Link, error)

LinkByName calls netlink.LinkByName.

func (NetlinkHandle) LinkDel

func (n NetlinkHandle) LinkDel(link netlink.Link) error

LinkDel calls netlink.LinkDel.

func (NetlinkHandle) LinkSetNsFd

func (n NetlinkHandle) LinkSetNsFd(link netlink.Link, fd int) error

LinkSetNsFd calls netlink.LinkSetNsFd.

func (NetlinkHandle) LinkSetUp

func (n NetlinkHandle) LinkSetUp(link netlink.Link) error

LinkSetUp calls netlink.LinkSetUp.

func (NetlinkHandle) ParseAddr

func (n NetlinkHandle) ParseAddr(s string) (*netlink.Addr, error)

ParseAddr calls netlink.ParseAddr.

func (NetlinkHandle) RouteAdd

func (n NetlinkHandle) RouteAdd(route *netlink.Route) error

RouteAdd calls netlink.RouteAdd.

type NetlinkHandler

type NetlinkHandler interface {
	ParseAddr(string) (*netlink.Addr, error)
	AddrAdd(netlink.Link, *netlink.Addr) error
	LinkSetUp(netlink.Link) error
	LinkDel(netlink.Link) error
	LinkAdd(netlink.Link) error
	LinkByName(string) (netlink.Link, error)
	LinkSetNsFd(netlink.Link, int) error
	RouteAdd(*netlink.Route) error
}

NetlinkHandler is an interface that represents the netlink functions that we need to mock. This only exists for test purposes.

type NetworkNamespace

type NetworkNamespace struct {
	// contains filtered or unexported fields
}

NetworkNamespace represents a network namespace.

func NewNetworkNamespace

func NewNetworkNamespace(name string, options ...Option) *NetworkNamespace

NewNetworkNamespace creates a new network namespace. once the namespace is created this function restores the thread to the original namespace.

func (*NetworkNamespace) AttachInterface

func (n *NetworkNamespace) AttachInterface(ifname string) error

AttachInterface attaches the the provided interface into the namespace. This function does not bring the interface up.

func (*NetworkNamespace) BringInterfaceUp

func (n *NetworkNamespace) BringInterfaceUp(ifname string) error

BringInterfaceUp brings the provided interface up inside the namespace.

func (*NetworkNamespace) Close

func (n *NetworkNamespace) Close() error

Close closes and deletes the network namespace.

func (*NetworkNamespace) Join

func (n *NetworkNamespace) Join() (err error)

Join makes the thread join the namespace. The current thread is saved in the origin field. Callers are responsible for calling Leave() once they are done. This namespace can only be joined once and this is by design. You need to Leave() before Joining again. The current OS thread will be locked to the namespace.

func (*NetworkNamespace) Leave

func (n *NetworkNamespace) Leave() error

Leave makes the thread leave the namespace. This function returns the thread to the previous namespace. Leaves() can't be called without Joining first. This function unlocks the current OS thread so it can be used again by multiple goroutines.

func (*NetworkNamespace) Run

func (n *NetworkNamespace) Run(f func() error) (err error)

Run runs the provided function inside the namespace. Restores the original namespace once the function has finished.

func (*NetworkNamespace) SetDefaultGateway

func (n *NetworkNamespace) SetDefaultGateway(addr string) error

SetDefaultGateway sets the default gateway for the namespace.

func (*NetworkNamespace) SetInterfaceIP

func (n *NetworkNamespace) SetInterfaceIP(ifname, ipaddr string) error

SetInterfaceIP sets the ip address for the provided interface.

func (*NetworkNamespace) Setup

func (n *NetworkNamespace) Setup() (err error)

type Option

type Option func(*Configuration)

Option is a function that sets an optional configuration.

func WithLogf

func WithLogf(f func(string, ...interface{})) Option

WithLogf sets the log function for this package.

func WithPort

func WithPort(port int) Option

WithPort sets the port to use for the UDP and TCP pings.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the timeout for the UDP and TCP connections.

Jump to

Keyboard shortcuts

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