libnetwork/osl: remove Interface Interface

There's only one implementation; let's use that.
Also fixing a linting issue;

    libnetwork/osl/interface_linux.go:91:2: S1001: should use copy(to, from) instead of a loop (gosimple)
        for i, iface := range n.iFaces {
        ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-08-02 23:24:33 +02:00
parent 8631e69cdf
commit b9bf407929
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
6 changed files with 47 additions and 91 deletions

View file

@ -14,12 +14,12 @@ import (
"github.com/vishvananda/netns"
)
// nwIface represents the settings and identity of a network device.
// Interface represents the settings and identity of a network device.
// It is used as a return type for Network.Link, and it is common practice
// for the caller to use this information when moving interface SrcName from
// host namespace to DstName in a different net namespace with the appropriate
// network settings.
type nwIface struct {
type Interface struct {
srcName string
dstName string
master string
@ -34,7 +34,7 @@ type nwIface struct {
}
// SrcName returns the name of the interface in the origin network namespace.
func (i *nwIface) SrcName() string {
func (i *Interface) SrcName() string {
return i.srcName
}
@ -42,46 +42,46 @@ func (i *nwIface) SrcName() string {
// moved inside a network namespace. When the caller passes in a DstName,
// it is only expected to pass a prefix. The name will be modified with an
// auto-generated suffix.
func (i *nwIface) DstName() string {
func (i *Interface) DstName() string {
return i.dstName
}
func (i *nwIface) DstMaster() string {
func (i *Interface) DstMaster() string {
return i.dstMaster
}
// Bridge returns true if the interface is a bridge.
func (i *nwIface) Bridge() bool {
func (i *Interface) Bridge() bool {
return i.bridge
}
// Master returns the srcname of the master interface for this interface.
func (i *nwIface) Master() string {
func (i *Interface) Master() string {
return i.master
}
func (i *nwIface) MacAddress() net.HardwareAddr {
func (i *Interface) MacAddress() net.HardwareAddr {
return types.GetMacCopy(i.mac)
}
// Address returns the IPv4 address for the interface.
func (i *nwIface) Address() *net.IPNet {
func (i *Interface) Address() *net.IPNet {
return types.GetIPNetCopy(i.address)
}
// AddressIPv6 returns the IPv6 address for the interface.
func (i *nwIface) AddressIPv6() *net.IPNet {
func (i *Interface) AddressIPv6() *net.IPNet {
return types.GetIPNetCopy(i.addressIPv6)
}
// LinkLocalAddresses returns the link-local IP addresses assigned to the
// interface.
func (i *nwIface) LinkLocalAddresses() []*net.IPNet {
func (i *Interface) LinkLocalAddresses() []*net.IPNet {
return i.llAddrs
}
// Routes returns IP routes for the interface.
func (i *nwIface) Routes() []*net.IPNet {
func (i *Interface) Routes() []*net.IPNet {
routes := make([]*net.IPNet, len(i.routes))
for index, route := range i.routes {
routes[index] = types.GetIPNetCopy(route)
@ -92,7 +92,7 @@ func (i *nwIface) Routes() []*net.IPNet {
// Remove an interface from the sandbox by renaming to original name
// and moving it out of the sandbox.
func (i *nwIface) Remove() error {
func (i *Interface) Remove() error {
i.ns.Lock()
isDefault := i.ns.isDefault
nlh := i.ns.nlHandle
@ -143,7 +143,7 @@ func (i *nwIface) Remove() error {
}
// Statistics returns the sandbox's side veth interface statistics.
func (i *nwIface) Statistics() (*types.InterfaceStatistics, error) {
func (i *Interface) Statistics() (*types.InterfaceStatistics, error) {
l, err := i.ns.nlHandle.LinkByName(i.DstName())
if err != nil {
return nil, fmt.Errorf("failed to retrieve the statistics for %s in netns %s: %v", i.DstName(), i.ns.path, err)
@ -185,7 +185,7 @@ func (n *networkNamespace) findDst(srcName string, isBridge bool) string {
// to only provide a prefix for DstName. The AddInterface api will auto-generate
// an appropriate suffix for the DstName to disambiguate.
func (n *networkNamespace) AddInterface(srcName, dstPrefix string, options ...IfaceOption) error {
i := &nwIface{
i := &Interface{
srcName: srcName,
dstName: dstPrefix,
ns: n,
@ -299,10 +299,10 @@ func (n *networkNamespace) AddInterface(srcName, dstPrefix string, options ...If
return nil
}
func configureInterface(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
func configureInterface(nlh *netlink.Handle, iface netlink.Link, i *Interface) error {
ifaceName := iface.Attrs().Name
ifaceConfigurators := []struct {
Fn func(*netlink.Handle, netlink.Link, *nwIface) error
Fn func(*netlink.Handle, netlink.Link, *Interface) error
ErrMessage string
}{
{setInterfaceName, fmt.Sprintf("error renaming interface %q to %q", ifaceName, i.DstName())},
@ -321,7 +321,7 @@ func configureInterface(nlh *netlink.Handle, iface netlink.Link, i *nwIface) err
return nil
}
func setInterfaceMaster(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
func setInterfaceMaster(nlh *netlink.Handle, iface netlink.Link, i *Interface) error {
if i.DstMaster() == "" {
return nil
}
@ -331,14 +331,14 @@ func setInterfaceMaster(nlh *netlink.Handle, iface netlink.Link, i *nwIface) err
})
}
func setInterfaceMAC(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
func setInterfaceMAC(nlh *netlink.Handle, iface netlink.Link, i *Interface) error {
if i.MacAddress() == nil {
return nil
}
return nlh.LinkSetHardwareAddr(iface, i.MacAddress())
}
func setInterfaceIP(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
func setInterfaceIP(nlh *netlink.Handle, iface netlink.Link, i *Interface) error {
if i.Address() == nil {
return nil
}
@ -349,7 +349,7 @@ func setInterfaceIP(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
return nlh.AddrAdd(iface, ipAddr)
}
func setInterfaceIPv6(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
func setInterfaceIPv6(nlh *netlink.Handle, iface netlink.Link, i *Interface) error {
if i.AddressIPv6() == nil {
return nil
}
@ -363,7 +363,7 @@ func setInterfaceIPv6(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error
return nlh.AddrAdd(iface, ipAddr)
}
func setInterfaceLinkLocalIPs(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
func setInterfaceLinkLocalIPs(nlh *netlink.Handle, iface netlink.Link, i *Interface) error {
for _, llIP := range i.LinkLocalAddresses() {
ipAddr := &netlink.Addr{IPNet: llIP}
if err := nlh.AddrAdd(iface, ipAddr); err != nil {
@ -373,11 +373,11 @@ func setInterfaceLinkLocalIPs(nlh *netlink.Handle, iface netlink.Link, i *nwIfac
return nil
}
func setInterfaceName(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
func setInterfaceName(nlh *netlink.Handle, iface netlink.Link, i *Interface) error {
return nlh.LinkSetName(iface, i.DstName())
}
func setInterfaceRoutes(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
func setInterfaceRoutes(nlh *netlink.Handle, iface netlink.Link, i *Interface) error {
for _, route := range i.Routes() {
err := nlh.RouteAdd(&netlink.Route{
Scope: netlink.SCOPE_LINK,

View file

@ -2,4 +2,4 @@
package osl
type nwIface struct{}
type Interface struct{}

View file

@ -319,7 +319,7 @@ func createNamespaceFile(path string) (err error) {
// can be added dynamically.
type networkNamespace struct {
path string
iFaces []*nwIface
iFaces []*Interface
gw net.IP
gwv6 net.IP
staticRoutes []*types.StaticRoute
@ -335,11 +335,9 @@ type networkNamespace struct {
// method. Note that this doesn't include network interfaces added in any
// other way (such as the default loopback interface which is automatically
// created on creation of a sandbox).
func (n *networkNamespace) Interfaces() []Interface {
ifaces := make([]Interface, len(n.iFaces))
for i, iface := range n.iFaces {
ifaces[i] = iface
}
func (n *networkNamespace) Interfaces() []*Interface {
ifaces := make([]*Interface, len(n.iFaces))
copy(ifaces, n.iFaces)
return ifaces
}
@ -483,7 +481,7 @@ func (n *networkNamespace) Destroy() error {
func (n *networkNamespace) Restore(ifsopt map[Iface][]IfaceOption, routes []*types.StaticRoute, gw net.IP, gw6 net.IP) error {
// restore interfaces
for name, opts := range ifsopt {
i := &nwIface{
i := &Interface{
srcName: name.SrcName,
dstName: name.DstPrefix,
ns: n,

View file

@ -24,7 +24,7 @@ func WithFamily(family int) NeighOption {
}
}
func (i *nwIface) processInterfaceOptions(options ...IfaceOption) error {
func (i *Interface) processInterfaceOptions(options ...IfaceOption) error {
for _, opt := range options {
if opt != nil {
// TODO(thaJeztah): use multi-error instead of returning early.
@ -38,7 +38,7 @@ func (i *nwIface) processInterfaceOptions(options ...IfaceOption) error {
// WithIsBridge sets whether the interface is a bridge.
func WithIsBridge(isBridge bool) IfaceOption {
return func(i *nwIface) error {
return func(i *Interface) error {
i.bridge = isBridge
return nil
}
@ -48,7 +48,7 @@ func WithIsBridge(isBridge bool) IfaceOption {
// master interface name should refer to the srcName of a previously added
// interface of type bridge.
func WithMaster(name string) IfaceOption {
return func(i *nwIface) error {
return func(i *Interface) error {
i.master = name
return nil
}
@ -56,7 +56,7 @@ func WithMaster(name string) IfaceOption {
// WithMACAddress sets the interface MAC-address.
func WithMACAddress(mac net.HardwareAddr) IfaceOption {
return func(i *nwIface) error {
return func(i *Interface) error {
i.mac = mac
return nil
}
@ -64,7 +64,7 @@ func WithMACAddress(mac net.HardwareAddr) IfaceOption {
// WithIPv4Address sets the IPv4 address of the interface.
func WithIPv4Address(addr *net.IPNet) IfaceOption {
return func(i *nwIface) error {
return func(i *Interface) error {
i.address = addr
return nil
}
@ -72,7 +72,7 @@ func WithIPv4Address(addr *net.IPNet) IfaceOption {
// WithIPv6Address sets the IPv6 address of the interface.
func WithIPv6Address(addr *net.IPNet) IfaceOption {
return func(i *nwIface) error {
return func(i *Interface) error {
i.addressIPv6 = addr
return nil
}
@ -80,7 +80,7 @@ func WithIPv6Address(addr *net.IPNet) IfaceOption {
// WithLinkLocalAddresses set the link-local IP addresses of the interface.
func WithLinkLocalAddresses(list []*net.IPNet) IfaceOption {
return func(i *nwIface) error {
return func(i *Interface) error {
i.llAddrs = list
return nil
}
@ -88,7 +88,7 @@ func WithLinkLocalAddresses(list []*net.IPNet) IfaceOption {
// WithRoutes sets the interface routes.
func WithRoutes(routes []*net.IPNet) IfaceOption {
return func(i *nwIface) error {
return func(i *Interface) error {
i.routes = routes
return nil
}

View file

@ -22,7 +22,7 @@ type Iface struct {
}
// IfaceOption is a function option type to set interface options.
type IfaceOption func(i *nwIface) error
type IfaceOption func(i *Interface) error
// NeighOption is a function option type to set neighbor options.
type NeighOption func(nh *neigh)
@ -100,7 +100,7 @@ type Info interface {
// method. Note that this doesn't include network interfaces added in any
// other way (such as the default loopback interface which is automatically
// created on creation of a sandbox).
Interfaces() []Interface
Interfaces() []*Interface
// Gateway returns the IPv4 gateway for the sandbox.
Gateway() net.IP
@ -113,45 +113,3 @@ type Info interface {
// refer to.
StaticRoutes() []*types.StaticRoute
}
// Interface represents the settings and identity of a network device. It is
// used as a return type for Network.Link, and it is common practice for the
// caller to use this information when moving interface SrcName from host
// namespace to DstName in a different net namespace with the appropriate
// network settings.
type Interface interface {
// SrcName returns the name of the interface in the origin network namespace.
SrcName() string
// DstName returns the name that will be assigned to the interface once
// moved inside a network namespace. When the caller passes in a DstName,
// it is only expected to pass a prefix. The name will be modified with an
// auto-generated suffix.
DstName() string
// Address returns the IPv4 address for the interface.
Address() *net.IPNet
// AddressIPv6 returns the IPv6 address for the interface.
AddressIPv6() *net.IPNet
// LinkLocalAddresses returns the link-local IP addresses assigned to the
// interface.
LinkLocalAddresses() []*net.IPNet
// Routes returns IP routes for the interface.
Routes() []*net.IPNet
// Bridge returns true if the interface is a bridge.
Bridge() bool
// Master returns the srcname of the master interface for this interface.
Master() string
// Remove an interface from the sandbox by renaming to original name
// and moving it out of the sandbox.
Remove() error
// Statistics returns the statistics for this interface
Statistics() (*types.InterfaceStatistics, error)
}

View file

@ -85,7 +85,7 @@ func newInfo(t *testing.T, hnd *netlink.Handle) (Sandbox, error) {
// Store the sandbox side pipe interface
// This is needed for cleanup on DeleteEndpoint()
intf1 := &nwIface{
intf1 := &Interface{
srcName: vethName2,
dstName: sboxIfaceName,
address: addr,
@ -93,7 +93,7 @@ func newInfo(t *testing.T, hnd *netlink.Handle) (Sandbox, error) {
routes: []*net.IPNet{route},
}
intf2 := &nwIface{
intf2 := &Interface{
srcName: "testbridge",
dstName: sboxIfaceName,
bridge: true,
@ -107,14 +107,14 @@ func newInfo(t *testing.T, hnd *netlink.Handle) (Sandbox, error) {
return nil, err
}
intf3 := &nwIface{
intf3 := &Interface{
srcName: vethName4,
dstName: sboxIfaceName,
master: "testbridge",
}
return &networkNamespace{
iFaces: []*nwIface{intf1, intf2, intf3},
iFaces: []*Interface{intf1, intf2, intf3},
gw: net.ParseIP("192.168.1.1"),
gwv6: net.ParseIP("fe80::1"),
}, nil
@ -182,7 +182,7 @@ func TestDisableIPv6DAD(t *testing.T) {
nlh := n.nlHandle
ipv6, _ := types.ParseCIDR("2001:db8::44/64")
iface := &nwIface{addressIPv6: ipv6, ns: n, dstName: "sideA"}
iface := &Interface{addressIPv6: ipv6, ns: n, dstName: "sideA"}
veth := &netlink.Veth{
LinkAttrs: netlink.LinkAttrs{Name: "sideA"},
@ -242,7 +242,7 @@ func TestSetInterfaceIP(t *testing.T) {
ipv4, _ := types.ParseCIDR("172.30.0.33/24")
ipv6, _ := types.ParseCIDR("2001:db8::44/64")
iface := &nwIface{address: ipv4, addressIPv6: ipv6, ns: n, dstName: "sideA"}
iface := &Interface{address: ipv4, addressIPv6: ipv6, ns: n, dstName: "sideA"}
if err := nlh.LinkAdd(&netlink.Veth{
LinkAttrs: netlink.LinkAttrs{Name: "sideA"},
@ -316,7 +316,7 @@ func TestLiveRestore(t *testing.T) {
ipv4, _ := types.ParseCIDR("172.30.0.33/24")
ipv6, _ := types.ParseCIDR("2001:db8::44/64")
iface := &nwIface{address: ipv4, addressIPv6: ipv6, ns: n, dstName: "sideA"}
iface := &Interface{address: ipv4, addressIPv6: ipv6, ns: n, dstName: "sideA"}
if err := nlh.LinkAdd(&netlink.Veth{
LinkAttrs: netlink.LinkAttrs{Name: "sideA"},