util

package
v0.0.0-...-7983b3b Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotImplemented = errors.New("not implemented yet")

ErrNotImplemented is the "not implemented" error given by `gopsutil` when an OS doesn't support and API. Unfortunately it's in an internal package so we can't import it so we'll copy it here.

View Source
var IPBufferPool = sync.Pool{
	New: func() interface{} {
		return make([]byte, net.IPv6len)
	},
}

IPBufferPool is meant to be used in conjunction with `NetIPFromAddress`

View Source
var NullContainerRates = ContainerRateMetrics{
	CPU: &metrics.ContainerCPUStats{
		User:   -1,
		System: -1,
		Shares: -1,
	},
	IO:         &metrics.ContainerIOStats{},
	NetworkSum: &metrics.InterfaceNetStats{},
	Network:    metrics.ContainerNetStats{},
}

NullContainerRates can be safely used for containers that have no previous rate values stored (new containers)

Functions

func ExtractContainerRateMetric

func ExtractContainerRateMetric(containers []*containers.Container) map[string]ContainerRateMetrics

ExtractContainerRateMetric extracts relevant rate values from a container list for later reuse, while reducing memory usage to only the needed fields

func GetContainers

func GetContainers() ([]*containers.Container, error)

GetContainers returns containers found on the machine GetContainers autodetects the best backend from available sources if the users don't specify the preferred container sources

func GetCurrentIno

func GetCurrentIno() (uint32, error)

GetCurrentIno returns the ino number for the current network namespace

func GetDockerSocketPath

func GetDockerSocketPath() (string, error)

GetDockerSocketPath is only for exposing the sockpath out of the module

func GetEnv

func GetEnv(key string, dfault string, combineWith ...string) string

GetEnv retrieves the environment variable key. If it does not exist it returns the default.

func GetInoForNs

func GetInoForNs(ns netns.NsHandle) (uint32, error)

GetInoForNs gets the inode number for the given network namespace

func GetNetNamespaceFromPid

func GetNetNamespaceFromPid(procRoot string, pid int) (netns.NsHandle, error)

GetNetNamespaceFromPid gets the network namespace for a given `pid`

func GetNetNamespaces

func GetNetNamespaces(procRoot string) ([]netns.NsHandle, error)

GetNetNamespaces returns a list of network namespaces on the machine. The caller is responsible for calling Close() on each of the returned NsHandle's.

func GetNetNsInoFromPid

func GetNetNsInoFromPid(procRoot string, pid int) (uint32, error)

GetNetNsInoFromPid gets the network namespace inode number for the given `pid`

func GetProcRoot

func GetProcRoot() string

GetProcRoot retrieves the current procfs dir we should use

func GetRootNSPID

func GetRootNSPID() (int, error)

GetRootNSPID returns the current PID from the root namespace

func GetRootNetNamespace

func GetRootNetNamespace(procRoot string) (netns.NsHandle, error)

GetRootNetNamespace gets the root network namespace

func GetSysRoot

func GetSysRoot() string

GetSysRoot retrieves the current sysfs dir we should use

func HandleSignals

func HandleSignals(exit chan struct{})

HandleSignals tells us whether we should exit.

func HostProc

func HostProc(combineWith ...string) string

HostProc returns the location of a host's procfs. This can and will be overridden when running inside a container.

func HostSys

func HostSys(combineWith ...string) string

HostSys returns the location of a host's /sys. This can and will be overridden when running inside a container.

func NetIPFromAddress

func NetIPFromAddress(addr Address, buf []byte) net.IP

NetIPFromAddress returns a net.IP from an Address

func PathExists

func PathExists(filename string) bool

PathExists returns a boolean indicating if the given path exists on the file system.

func ReadLines

func ReadLines(filename string) ([]string, error)

ReadLines reads contents from a file and splits them by new lines.

func SetContainerSources

func SetContainerSources(names []string)

SetContainerSources allows config to force one or multiple container sources

func StringInSlice

func StringInSlice(slice []string, searchString string) bool

StringInSlice returns true if the given searchString is in the given slice, false otherwise.

func ToLowHigh

func ToLowHigh(addr Address) (l, h uint64)

ToLowHigh converts an address into a pair of uint64 numbers

func WithAllProcs

func WithAllProcs(procRoot string, fn func(int) error) error

WithAllProcs will execute `fn` for every pid under procRoot. `fn` is passed the `pid`. If `fn` returns an error the iteration aborts, returning the last error returned from `fn`.

func WithNS

func WithNS(procRoot string, ns netns.NsHandle, fn func() error) error

WithNS executes the given function in the given network namespace, and then switches back to the previous namespace.

func WithRootNS

func WithRootNS(procRoot string, fn func() error) error

WithRootNS executes a function within root network namespace and then switch back to the previous namespace. If the thread is already in the root network namespace, the function is executed without calling SYS_SETNS.

Types

type Address

type Address interface {
	Bytes() []byte
	WriteTo([]byte) int
	String() string
	IsLoopback() bool
}

Address is an IP abstraction that is family (v4/v6) agnostic

func AddressFromNetIP

func AddressFromNetIP(ip net.IP) Address

AddressFromNetIP returns an Address from a provided net.IP

func AddressFromString

func AddressFromString(ip string) Address

AddressFromString creates an Address using the string representation of an v4 IP

func V4Address

func V4Address(ip uint32) Address

V4Address creates an Address using the uint32 representation of an v4 IP

func V4AddressFromBytes

func V4AddressFromBytes(buf []byte) Address

V4AddressFromBytes creates an Address using the byte representation of an v4 IP

func V6Address

func V6Address(low, high uint64) Address

V6Address creates an Address using the uint128 representation of an v6 IP

func V6AddressFromBytes

func V6AddressFromBytes(buf []byte) Address

V6AddressFromBytes creates an Address using the byte representation of an v6 IP

type ContainerRateMetrics

type ContainerRateMetrics struct {
	CPU        *metrics.ContainerCPUStats
	IO         *metrics.ContainerIOStats
	NetworkSum *metrics.InterfaceNetStats
	Network    metrics.ContainerNetStats
}

ContainerRateMetrics holds previous values for a container, in order to compute rates

type LogLimit

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

LogLimit is a utility that can be used to avoid logging noisily

func NewLogLimit

func NewLogLimit(n int, interval time.Duration) *LogLimit

NewLogLimit creates a LogLimit where shouldLog will return true the first N times it is called, and will return true once every interval thereafter.

func (*LogLimit) Close

func (l *LogLimit) Close()

Close will stop the underlying ticker

func (*LogLimit) ShouldLog

func (l *LogLimit) ShouldLog() bool

ShouldLog returns true if the caller should log

type SSBytes

type SSBytes [][]byte

SSBytes implements the sort.Interface for the [][]byte type

func (SSBytes) Len

func (ss SSBytes) Len() int

func (SSBytes) Less

func (ss SSBytes) Less(i, j int) bool

func (SSBytes) Search

func (ss SSBytes) Search(x []byte) int

Search returns the index of element x if found or the length of the SSBytes otherwise. SSBytes is expected to be sorted.

func (SSBytes) Swap

func (ss SSBytes) Swap(i, j int)

Directories

Path Synopsis
api

Jump to

Keyboard shortcuts

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