util

package
v1.27.8 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: Apache-2.0 Imports: 17 Imported by: 192

Documentation

Index

Constants

View Source
const (
	// IPv4ZeroCIDR is the CIDR block for the whole IPv4 address space
	IPv4ZeroCIDR = "0.0.0.0/0"

	// IPv6ZeroCIDR is the CIDR block for the whole IPv6 address space
	IPv6ZeroCIDR = "::/0"
)

Variables

View Source
var (
	// ErrAddressNotAllowed indicates the address is not allowed
	ErrAddressNotAllowed = errors.New("address not allowed")

	// ErrNoAddresses indicates there are no addresses for the hostname
	ErrNoAddresses = errors.New("no addresses for hostname")
)

Functions

func AddressSet added in v1.23.0

func AddressSet(isValid func(ip net.IP) bool, addrs []net.Addr) sets.Set[string]

AddressSet validates the addresses in the slice using the "isValid" function. Addresses that pass the validation are returned as a string Set.

func AppendPortIfNeeded added in v1.15.0

func AppendPortIfNeeded(addr string, port int32) string

AppendPortIfNeeded appends the given port to IP address unless it is already in "ipv4:port" or "[ipv6]:port" format.

func BuildPortsToEndpointsMap added in v1.16.0

func BuildPortsToEndpointsMap(endpoints *v1.Endpoints) map[string][]string

BuildPortsToEndpointsMap builds a map of portname -> all ip:ports for that portname. Explode Endpoints.Subsets[*] into this structure.

func EnsureSysctl added in v1.19.0

func EnsureSysctl(sysctl utilsysctl.Interface, name string, newVal int) error

EnsureSysctl sets a kernel sysctl to a given numeric value.

func GetClusterIPByFamily added in v1.20.0

func GetClusterIPByFamily(ipFamily v1.IPFamily, service *v1.Service) string

GetClusterIPByFamily returns a service clusterip by family

func GetLocalAddrSet added in v1.21.0

func GetLocalAddrSet() netutils.IPSet

GetLocalAddrSet return a local IPSet. If failed to get local addr, will assume no local ips.

func GetLocalAddrs added in v1.18.0

func GetLocalAddrs() ([]net.IP, error)

GetLocalAddrs returns a list of all network addresses on the local system

func IPPart added in v1.9.0

func IPPart(s string) string

IPPart returns just the IP part of an IP or IP:port or endpoint string. If the IP part is an IPv6 address enclosed in brackets (e.g. "[fd00:1::5]:9999"), then the brackets are stripped as well.

func IsAllowedHost added in v1.20.0

func IsAllowedHost(host net.IP, denied []*net.IPNet) error

IsAllowedHost checks if the given IP host address is in a network in the denied list.

func IsLoopBack added in v1.26.0

func IsLoopBack(ip string) bool

IsLoopBack checks if a given IP address is a loopback address.

func IsProxyableHostname added in v1.10.12

func IsProxyableHostname(ctx context.Context, resolv Resolver, hostname string) error

IsProxyableHostname checks if the IP addresses for a given hostname are permitted to be proxied

func IsProxyableIP added in v1.10.12

func IsProxyableIP(ip string) error

IsProxyableIP checks if a given IP address is permitted to be proxied

func IsZeroCIDR added in v1.10.0

func IsZeroCIDR(cidr string) bool

IsZeroCIDR checks whether the input CIDR string is either the IPv4 or IPv6 zero CIDR

func LogAndEmitIncorrectIPVersionEvent added in v1.10.0

func LogAndEmitIncorrectIPVersionEvent(recorder events.EventRecorder, fieldName, fieldValue, svcNamespace, svcName string, svcUID types.UID)

LogAndEmitIncorrectIPVersionEvent logs and emits incorrect IP version event.

func MapCIDRsByIPFamily added in v1.21.0

func MapCIDRsByIPFamily(cidrStrings []string) map[v1.IPFamily][]string

MapCIDRsByIPFamily maps a slice of IPs to their respective IP families (v4 or v6)

func MapIPsByIPFamily added in v1.21.0

func MapIPsByIPFamily(ipStrings []string) map[v1.IPFamily][]string

MapIPsByIPFamily maps a slice of IPs to their respective IP families (v4 or v6)

func OtherIPFamily added in v1.21.0

func OtherIPFamily(ipFamily v1.IPFamily) v1.IPFamily

OtherIPFamily returns the other ip family

func PortPart added in v1.9.0

func PortPart(s string) (int, error)

PortPart returns just the port part of an endpoint string.

func RevertPorts added in v1.8.0

func RevertPorts(replacementPortsMap, originalPortsMap map[netutils.LocalPort]netutils.Closeable)

RevertPorts is closing ports in replacementPortsMap but not in originalPortsMap. In other words, it only closes the ports opened in this sync.

func ShouldSkipService added in v1.8.0

func ShouldSkipService(service *v1.Service) bool

ShouldSkipService checks if a given service should skip proxying

func ShuffleStrings added in v1.16.0

func ShuffleStrings(s []string) []string

ShuffleStrings copies strings from the specified slice into a copy in random order. It returns a new slice.

Types

type DialContext added in v1.20.0

type DialContext = func(context.Context, string, string) (net.Conn, error)

DialContext is a dial function matching the signature of net.Dialer.DialContext.

func NewFilteredDialContext added in v1.20.0

func NewFilteredDialContext(wrapped DialContext, resolv Resolver, opts *FilteredDialOptions) DialContext

NewFilteredDialContext returns a DialContext function that filters connections based on a FilteredDialOptions.

type FilteredDialOptions added in v1.20.0

type FilteredDialOptions struct {
	// DialHostIPDenylist restricts hosts from being dialed.
	DialHostCIDRDenylist []*net.IPNet
	// AllowLocalLoopback controls connections to local loopback hosts (as defined by
	// IsProxyableIP).
	AllowLocalLoopback bool
}

FilteredDialOptions configures how a DialContext is wrapped by NewFilteredDialContext.

type LineBuffer added in v1.23.0

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

func (*LineBuffer) Bytes added in v1.23.0

func (buf *LineBuffer) Bytes() []byte

Bytes returns the contents of buf as a []byte

func (*LineBuffer) Lines added in v1.24.0

func (buf *LineBuffer) Lines() int

Lines returns the number of lines in buf. Note that more precisely, this returns the number of times Write() or WriteBytes() was called; it assumes that you never wrote any newlines to the buffer yourself.

func (*LineBuffer) Reset added in v1.23.0

func (buf *LineBuffer) Reset()

Reset clears buf

func (*LineBuffer) Write added in v1.23.0

func (buf *LineBuffer) Write(args ...interface{})

Write takes a list of arguments, each a string or []string, joins all the individual strings with spaces, terminates with newline, and writes to buf. Any other argument type will panic.

func (*LineBuffer) WriteBytes added in v1.23.0

func (buf *LineBuffer) WriteBytes(bytes []byte)

WriteBytes writes bytes to buffer, and terminates with newline.

type NetworkInterfacer added in v1.10.0

type NetworkInterfacer interface {
	InterfaceAddrs() ([]net.Addr, error)
}

NetworkInterfacer defines an interface for several net library functions. Production code will forward to net library functions, and unit tests will override the methods for testing purposes.

type NodePortAddresses added in v1.27.0

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

NodePortAddresses is used to handle the --nodeport-addresses flag

func NewNodePortAddresses added in v1.27.0

func NewNodePortAddresses(cidrStrings []string) *NodePortAddresses

NewNodePortAddresses takes the `--nodeport-addresses` value (which is assumed to contain only valid CIDRs) and returns a NodePortAddresses object. If cidrStrings is empty, this is treated as `["0.0.0.0/0", "::/0"]`.

func (*NodePortAddresses) ContainsIPv4Loopback added in v1.27.0

func (npa *NodePortAddresses) ContainsIPv4Loopback() bool

ContainsIPv4Loopback returns true if npa's CIDRs contain an IPv4 loopback address.

func (*NodePortAddresses) GetNodeAddresses added in v1.27.0

func (npa *NodePortAddresses) GetNodeAddresses(nw NetworkInterfacer) (sets.String, error)

GetNodeAddresses return all matched node IP addresses for npa's CIDRs. If npa's CIDRs include "0.0.0.0/0" and/or "::/0", then those values will be returned verbatim in the response and no actual IPs of that family will be returned. If no matching IPs are found, GetNodeAddresses will return an error. NetworkInterfacer is injected for test purpose.

func (*NodePortAddresses) String added in v1.27.0

func (npa *NodePortAddresses) String() string

type RealNetwork added in v1.10.0

type RealNetwork struct{}

RealNetwork implements the NetworkInterfacer interface for production code, just wrapping the underlying net library function calls.

func (RealNetwork) InterfaceAddrs added in v1.22.0

func (RealNetwork) InterfaceAddrs() ([]net.Addr, error)

InterfaceAddrs wraps net.InterfaceAddrs(), it's a part of NetworkInterfacer interface.

type Resolver added in v1.10.12

type Resolver interface {
	LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error)
}

Resolver is an interface for net.Resolver

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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