util

package
v2.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package util contains utility functions which are used in the agent implementation.

Index

Constants

View Source
const (
	FamilyIPv4 uint8 = 4
	FamilyIPv6 uint8 = 6
)

Variables

This section is empty.

Functions

func ConfigureLinkAddresses

func ConfigureLinkAddresses(idx int, ipNets []*net.IPNet) error

ConfigureLinkAddresses adds the provided addresses to the interface identified by index idx, if they are missing from the interface. Any other existing address already configured for the interface will be removed, unless it is a link-local address.

func ConfigureLinkRoutes

func ConfigureLinkRoutes(link netlink.Link, routes []interface{}) error

func EnsureARPAnnounceOnInterface

func EnsureARPAnnounceOnInterface(ifaceName string, value int) error

func EnsureIPv6EnabledOnInterface

func EnsureIPv6EnabledOnInterface(ifaceName string) error

func EnsurePromoteSecondariesOnInterface

func EnsurePromoteSecondariesOnInterface(ifaceName string) error

func EnsureRPFilterOnInterface

func EnsureRPFilterOnInterface(ifaceName string, value int) error

func ExtendCIDRWithIP

func ExtendCIDRWithIP(ipNet *net.IPNet, ip net.IP) (*net.IPNet, error)

ExtendCIDRWithIP is used for extending an IPNet with an IP.

func GenerateContainerHostVethName

func GenerateContainerHostVethName(podName, podNamespace, containerID, containerVeth string) string

GenerateContainerHostVethName generates a unique interface name using the Pod's Name, container ID, and the container veth interface name. The output should be deterministic.

func GenerateContainerInterfaceKey

func GenerateContainerInterfaceKey(containerID, ifDev string) string

GenerateContainerInterfaceKey generates a unique string for a Pod's interface as: "c/<Container-ID>/<IFDev-Name>". We must use ContainerID instead of PodNamespace + PodName because there could be more than one container associated with the same Pod at some point. For example, when deleting a StatefulSet Pod with 0 second grace period, the Pod will be removed from the Kubernetes API very quickly and a new Pod will be created immediately, and kubelet may process the deletion of the previous Pod and the addition of the new Pod simultaneously.

func GenerateContainerInterfaceName

func GenerateContainerInterfaceName(podName, podNamespace, containerID string) string

GenerateContainerInterfaceName generates a unique interface name using the Pod's Namespace, name and container ID. The output should be deterministic (so that multiple calls to GenerateContainerInterfaceName with the same parameters return the same value). The output has the length of interfaceNameLength(15). The probability of collision should be neglectable.

func GenerateNodeTunnelInterfaceKey

func GenerateNodeTunnelInterfaceKey(nodeName string) string

GenerateNodeTunnelInterfaceKey generates a unique string for a Node's tunnel interface as: node/<Node-name>.

func GenerateNodeTunnelInterfaceName

func GenerateNodeTunnelInterfaceName(nodeName string) string

GenerateNodeTunnelInterfaceName generates a unique interface name for the tunnel to the Node, using the Node's name.

func GenerateOVSDatapathID

func GenerateOVSDatapathID(macString string) string

GenerateOVSDatapathID generates an OVS datapath ID string.

func GenerateRandomMAC

func GenerateRandomMAC() net.HardwareAddr

func GenerateUplinkInterfaceName

func GenerateUplinkInterfaceName(name string) string

GenerateUplinkInterfaceName generates the uplink interface name after bridged to OVS

func GetAllNodeAddresses

func GetAllNodeAddresses(excludeDeviceMatchers []func(string) bool) ([]net.IP, []net.IP, error)

GetAllNodeAddresses gets all Node IP addresses (not including IPv6 link local address).

func GetGatewayIPForPodCIDR

func GetGatewayIPForPodCIDR(cidr *net.IPNet) net.IP

GetGatewayIPForPodCIDR returns the gateway IP for a given Pod CIDR.

func GetGatewayIPForPodPrefix

func GetGatewayIPForPodPrefix(prefix netip.Prefix) netip.Addr

GetGatewayIPForPodPrefix acts like GetGatewayIPForPodCIDR but takes a netip.Prefix as a parameter and returns a netip.Addr value.

func GetIPNetDeviceByCIDRs

func GetIPNetDeviceByCIDRs(cidrsList []string) (v4IPNet, v6IPNet *net.IPNet, link *net.Interface, err error)

func GetIPNetDeviceByName

func GetIPNetDeviceByName(ifaceName string) (v4IPNet *net.IPNet, v6IPNet *net.IPNet, link *net.Interface, err error)

func GetIPNetDeviceFromIP

func GetIPNetDeviceFromIP(localIPs *utilip.DualStackIPs, ignoredInterfaces sets.Set[string]) (v4IPNet *net.IPNet, v6IPNet *net.IPNet, iface *net.Interface, err error)

GetIPNetDeviceFromIP returns local IPs/masks and associated device from IP, and ignores the interfaces which have names in the ignoredInterfaces.

func GetIPWithFamily

func GetIPWithFamily(ips []net.IP, addrFamily uint8) (net.IP, error)

func GetIPv4Addr

func GetIPv4Addr(ips []net.IP) net.IP

func GetInterfaceConfig

func GetInterfaceConfig(ifName string) (*net.Interface, []*net.IPNet, []interface{}, error)

func GetNSDevInterface

func GetNSDevInterface(nsPath, dev string) (*net.Interface, error)

GetNSDevInterface returns interface of dev in namespace nsPath.

func GetNSPath

func GetNSPath(netnsName string) (string, error)

GetNSPath returns the path of the specified netns.

func GetNSPeerDevBridge

func GetNSPeerDevBridge(nsPath, dev string) (*net.Interface, string, error)

GetNSPeerDevBridge returns peer device and its attached bridge (if applicable) for device dev in network space indicated by nsPath

func HostInterfaceExists

func HostInterfaceExists(ifName string) bool

func ListenLocalSocket

func ListenLocalSocket(address string) (net.Listener, error)

ListenLocalSocket creates a listener on a Unix domain socket.

func NewIPNet

func NewIPNet(ip net.IP) *net.IPNet

Copied from github.com/vishvananda/netlink/netlink.go NewIPNet generates an IPNet from an ip address using a netmask of 32 or 128.

func PortToUint16

func PortToUint16(port int) uint16

func PrepareHostInterfaceConnection

func PrepareHostInterfaceConnection(
	bridge ovsconfig.OVSBridgeClient,
	ifaceName string,
	ifaceOFPort int32,
	externalIDs map[string]string,
	mtu int,
) (string, bool, error)

PrepareHostInterfaceConnection prepares host interface connection to the OVS bridge client by: 1. Renaming the host interface (a bridged suffix will be added to it). 2. Creating an internal port (original name of the host interface will be used here). 3. Set the MTU of this new link/internal-port to the provided mtu parameter value, unless mtu is zero. 4. Moving IPs of host interface to this new link/internal-port. 5. Moving routes of host interface to the new link/internal-port. and returns the bridged name, true if it already exists, and error.

func RemoveLinkIPs

func RemoveLinkIPs(link netlink.Link) error

func RemoveLinkRoutes

func RemoveLinkRoutes(link netlink.Link) error

func RenameInterface

func RenameInterface(from, to string) error

func RestoreHostInterfaceConfiguration

func RestoreHostInterfaceConfiguration(brName string, interfaceName string) error

RestoreHostInterfaceConfiguration restores the configuration from the bridge back to the host interface, reverting the actions taken in PrepareHostInterfaceConnection. It returns an error when the interface config cannot be read before the destructive steps, or when the host and uplink OVS ports cannot be deleted atomically; the caller can then retry while the OVS ports and the kernel interface are still intact. Failures of the subsequent sub-steps (rename and IP/route restore) are logged but do not cause a return error: they represent best-effort cleanup after the point of no return, where a retry could not restore the host connection because the OVS ports are already gone.

func SetAdapterMACAddress

func SetAdapterMACAddress(adapterName string, macConfig *net.HardwareAddr) error

SetAdapterMACAddress set specified MAC address on interface.

func SetLinkUp

func SetLinkUp(name string) (net.HardwareAddr, int, error)

Types

type LinkNotFound

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

Directories

Path Synopsis
Package ethtool provides Go wrappers for ioctl ethtool system calls on Linux
Package ethtool provides Go wrappers for ioctl ethtool system calls on Linux
testing
Package testing is a generated GoMock package.
Package testing is a generated GoMock package.
testing
Package testing is a generated GoMock package.
Package testing is a generated GoMock package.
Package ndp contains functions to send NDP neighbor advertisement on Linux.
Package ndp contains functions to send NDP neighbor advertisement on Linux.
testing
Package testing is a generated GoMock package.
Package testing is a generated GoMock package.
Package systcl contains utility functions to read and write sysctl configuration on Linux.
Package systcl contains utility functions to read and write sysctl configuration on Linux.
Package winfirewall contains utility functions to configure the Windows firewall.
Package winfirewall contains utility functions to configure the Windows firewall.
testing
Package testing is a generated GoMock package.
Package testing is a generated GoMock package.

Jump to

Keyboard shortcuts

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