cache

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// UserWeightCutoff is the cutoff we clamp user-provided weights to.
	UserWeightCutoff = 1000
	// DefaultWeight is the default assigned weight if omitted in annotations.
	DefaultWeight int32 = 1
)
View Source
const (
	// CPU marks changes that can be applied by the CPU controller.
	CPU = "cpu"
	// NRI marks changes that can be applied by NRI.
	NRI = "nri"
	// RDT marks changes that can be applied by the RDT controller.
	RDT = "rdt"
	// BlockIO marks changes that can be applied by the BlockIO controller.
	BlockIO = "blockio"

	// RDTClassKey is the pod annotation key for specifying a container RDT class.
	RDTClassKey = "rdtclass" + "." + kubernetes.ResmgrKeyNamespace
	// BlockIOClassKey is the pod annotation key for specifying a container Block I/O class.
	BlockIOClassKey = "blockioclass" + "." + kubernetes.ResmgrKeyNamespace
	// ToptierLimitKey is the pod annotation key for specifying container top tier memory limits.
	ToptierLimitKey = "toptierlimit" + "." + kubernetes.ResmgrKeyNamespace

	// RDTClassPodQoS denotes that the RDTClass should be taken from PodQosClass
	RDTClassPodQoS = "/PodQos"

	// ToptierLimitUnset is the reserved value for indicating unset top tier limits.
	ToptierLimitUnset int64 = -1

	// TopologyHintsKey can be used to opt out from automatic topology hint generation.
	TopologyHintsKey = "topologyhints" + "." + kubernetes.ResmgrKeyNamespace

	// PreserveCpuKey means that CPU resources should not be touched.
	PreserveCpuKey = "cpu.preserve." + kubernetes.ResmgrKeyNamespace
	// PreserveMemoryKey means that memory resources should not be touched.
	PreserveMemoryKey = "memory.preserve." + kubernetes.ResmgrKeyNamespace
)
View Source
const (
	// ContainerStateCreating marks a container being created.
	ContainerStateCreating = ContainerState(nri.ContainerState_CONTAINER_UNKNOWN - 1)
	// ContainerStateUnknown marks a container to be in an unknown state.
	ContainerStateUnknown = nri.ContainerState_CONTAINER_UNKNOWN
	// ContainerStateCreated marks a container created, not running.
	ContainerStateCreated = nri.ContainerState_CONTAINER_CREATED
	// ContainerStateRunning marks a container created, running.
	ContainerStateRunning = nri.ContainerState_CONTAINER_RUNNING
	// ContainerStateExited marks a container exited.
	ContainerStateExited = nri.ContainerState_CONTAINER_STOPPED
	// ContainerStateStale marks a container removed.
	ContainerStateStale = ContainerState(nri.ContainerState_CONTAINER_STOPPED + 1)
)
View Source
const (
	// CacheVersion is the running version of the cache.
	CacheVersion = "1"
)

Variables

View Source
var (
	SharesToMilliCPU = kubernetes.SharesToMilliCPU
	QuotaToMilliCPU  = kubernetes.QuotaToMilliCPU
	MilliCPUToShares = kubernetes.MilliCPUToShares
	MilliCPUToQuota  = kubernetes.MilliCPUToQuota
)

CompareByQOSMemoryCPU is a slice for comparing container by QOS, memory, and CPU.

Functions

func CompareCPU

func CompareCPU(ci, cj Container) int

CompareCPU compares containers by CPU requests and limits.

func CompareContainerCtime added in v0.4.0

func CompareContainerCtime(ci, cj Container) int

func CompareMemory

func CompareMemory(ci, cj Container) int

CompareMemory compares containers by memory requests and limits.

func ComparePodCtime added in v0.4.0

func ComparePodCtime(ci, cj Container) int

func CompareQOS

func CompareQOS(ci, cj Container) int

CompareQOS compares containers by QOS class.

func IsPodQOSClassName

func IsPodQOSClassName(class string) bool

IsPodQOSClassName returns true if the given class is one of the Pod QOS classes.

func SortContainers

func SortContainers(containers []Container, compareFns ...CompareContainersFn)

SortContainers sorts a slice of containers using the given comparison functions. If the containers are otherwise equal they are sorted by pod and container name. If the comparison functions are omitted, containers are compared by QoS class, memory and cpuset size.

Types

type Affinity

type Affinity struct {
	Scope  *resmgr.Expression `json:"scope,omitempty"`  // scope for evaluating this affinity
	Match  *resmgr.Expression `json:"match"`            // affinity expression
	Weight int32              `json:"weight,omitempty"` // (optional) weight for this affinity
}

Affinity specifies a single container affinity.

func GlobalAffinity

func GlobalAffinity(key string, weight int32) *Affinity

GlobalAffinity creates an affinity with all containers in scope.

func GlobalAntiAffinity

func GlobalAntiAffinity(key string, weight int32) *Affinity

GlobalAntiAffinity creates an anti-affinity with all containers in scope.

func (*Affinity) String

func (a *Affinity) String() string

String returns the affinity as a string.

func (*Affinity) Validate

func (a *Affinity) Validate() error

Validate checks the affinity for (obvious) invalidity.

type Cachable

type Cachable interface {
	// Set value (via a pointer receiver) to the object.
	Set(value interface{})
	// Get the object that should be cached.
	Get() interface{}
}

type Cache

type Cache interface {
	// InsertPod inserts a pod into the cache, using a runtime request or reply.
	InsertPod(pod *nri.PodSandbox) (Pod, error)
	// DeletePod deletes a pod from the cache.
	DeletePod(id string) Pod
	// LookupPod looks up a pod in the cache.
	LookupPod(id string) (Pod, bool)
	// InsertContainer inserts a container into the cache, using a runtime request or reply.
	InsertContainer(*nri.Container) (Container, error)
	// DeleteContainer deletes a container from the cache.
	DeleteContainer(id string) Container
	// LookupContainer looks up a container in the cache.
	LookupContainer(id string) (Container, bool)
	// LookupContainerByCgroup looks up a container for the given cgroup path.
	LookupContainerByCgroup(path string) (Container, bool)

	// GetPendingContainers returs all containers with pending changes.
	GetPendingContainers() []Container

	// GetPods returns all the pods known to the cache.
	GetPods() []Pod
	// GetContainers returns all the containers known to the cache.
	GetContainers() []Container

	// GetContaineIds return the ids of all containers.
	GetContainerIds() []string

	// FilterScope returns the containers selected by the scope expression.
	FilterScope(*resmgr.Expression) []Container
	// EvaluateAffinity evaluates the given affinity against all known in-scope containers
	EvaluateAffinity(*Affinity) map[string]int32
	// AddImplicitAffinities adds a set of implicit affinities (added to all containers).
	AddImplicitAffinities(map[string]ImplicitAffinity) error

	// GetActivePolicy returns the name of the active policy stored in the cache.
	GetActivePolicy() string
	// SetActivePolicy updates the name of the active policy stored in the cache.
	SetActivePolicy(string) error

	// ResetActivePolicy clears the active policy any any policy-specific data from the cache.
	ResetActivePolicy() error

	// SetPolicyEntry sets the policy entry for a key.
	SetPolicyEntry(string, interface{})
	// GetPolicyEntry gets the policy entry for a key.
	GetPolicyEntry(string, interface{}) bool

	// Save requests a cache save.
	Save() error

	// RefreshPods purges/inserts stale/new pods/containers using a pod sandbox list response.
	RefreshPods([]*nri.PodSandbox) ([]Pod, []Pod, []Container)
	// RefreshContainers purges/inserts stale/new containers using a container list response.
	RefreshContainers([]*nri.Container) ([]Container, []Container)

	// Get the container (data) directory for a container.
	ContainerDirectory(string) string
	// OpenFile opens the names container data file, creating it if necessary.
	OpenFile(string, string, os.FileMode) (*os.File, error)
	// WriteFile writes a container data file, creating it if necessary.
	WriteFile(string, string, os.FileMode, []byte) error
}

Cache is the primary interface exposed for tracking pods and containers.

Cache tracks pods and containers in the runtime, mostly by processing CRI requests and responses which the cache is fed as these are being procesed. Cache also saves its state upon changes to secondary storage and restores itself upon startup.

func NewCache

func NewCache(options Options) (Cache, error)

NewCache instantiates a new cache. Load it from the given path if it exists.

type CompareContainersFn

type CompareContainersFn func(Container, Container) int

CompareContainersFn compares two containers by some arbitrary property. It returns a negative integer, 0, or a positive integer, depending on whether the first container is considered smaller, equal, or larger than the second.

type Container

type Container interface {
	// GetPod returns the pod of the container and a boolean indicating if there was one.
	GetPod() (Pod, bool)
	// GetID returns the ID of the container.
	GetID() string
	// GetPodID returns the pod ID of the container.
	GetPodID() string
	// GetName returns the name of the container.
	GetName() string
	// GetNamespace returns the namespace of the container.
	GetNamespace() string
	// GetCtime returns the creation time of the container cache object.
	GetCtime() time.Time
	// UpdateState updates the state of the container.
	UpdateState(ContainerState)
	// GetState returns the ContainerState of the container.
	GetState() ContainerState
	// GetQOSClass returns the QoS class the pod would have if this was its only container.
	GetQOSClass() v1.PodQOSClass
	// GetArgs returns the container command arguments.
	GetArgs() []string
	// GetLabel returns the value of a container label.
	GetLabel(string) (string, bool)
	// GetAnnotation returns the value of a container annotation.
	GetAnnotation(key string, objPtr interface{}) (string, bool)
	// GetEnv returns the value of a container environment variable.
	GetEnv(string) (string, bool)
	// GetMounts returns all the mounts of the container.
	GetMounts() []*Mount
	// GetDevices returns all the linux devices of the container.
	GetDevices() []*Device

	// PrettyName returns the user-friendly $namespace/$pod/$container for the container.
	PrettyName() string

	// GetResmgrLabel returns the value of a container label from the
	// nri-resource-policy namespace.
	GetResmgrLabel(string) (string, bool)
	// GetResmgrAnnotation returns the value of a container annotation from the
	// nri-resource-policy namespace.
	GetResmgrAnnotation(key string, objPtr interface{}) (string, bool)
	// GetEffectiveAnnotation returns the effective annotation for the container from the pod.
	GetEffectiveAnnotation(key string) (string, bool)

	// Containers can be subject for expression evaluation.
	resmgr.Evaluable

	// Expand a string with possible key references.
	Expand(string, bool) (string, error)

	// We have String() for containers.
	fmt.Stringer

	// GetResourceRequirements returns the resource requirements for this container.
	// The requirements are calculated from the containers cgroup parameters.
	GetResourceRequirements() v1.ResourceRequirements

	// SetResourceUpdates sets updated resources for a container. Returns true if the
	// resources were really updated.
	SetResourceUpdates(*nri.LinuxResources) bool
	// GetResourceUpdates() returns any updated resource requirements for this container.
	// The updates are calculated from the cgroups parameters in the resource update.
	GetResourceUpdates() (v1.ResourceRequirements, bool)

	// InsertMount inserts a mount into the container.
	InsertMount(*Mount)

	// Get any attached topology hints.
	GetTopologyHints() topology.Hints

	// SetCPUShares sets the CFS CPU shares of the container.
	SetCPUShares(int64)
	// SetCPUQuota sets the CFS CPU quota of the container.
	SetCPUQuota(int64)
	// SetCPUPeriod sets the CFS CPU period of the container.
	SetCPUPeriod(int64)
	// SetCpusetCpu sets the cgroup cpuset.cpus of the container.
	SetCpusetCpus(string)
	// SetCpusetMems sets the cgroup cpuset.mems of the container.
	SetCpusetMems(string)
	// SetMemoryLimit sets the memory limit in bytes for the container.
	SetMemoryLimit(int64)
	// SetMemorySwap sets the swap limit in bytes for the container.
	SetMemorySwap(int64)

	// GetCPUShares gets the CFS CPU shares of the container.
	GetCPUShares() int64
	// GetCPUQuota gets the CFS CPU quota of the container.
	GetCPUQuota() int64
	// GetCPUPeriod gets the CFS CPU period of the container.
	GetCPUPeriod() int64
	// GetCpusetCpu gets the cgroup cpuset.cpus of the container.
	GetCpusetCpus() string
	// GetCpusetMems gets the cgroup cpuset.mems of the container.
	GetCpusetMems() string
	// GetMemoryLimit gets the memory limit in bytes for the container.
	GetMemoryLimit() int64
	// GetMemorySwap gets the swap limit in bytes for the container.
	GetMemorySwap() int64

	// PreserveCpuResources() returns true if CPU resources of the
	// container must not be changed.
	PreserveCpuResources() bool
	// PreserveMemoryResources() returns true if memory resources
	// of the container must not be changed.
	PreserveMemoryResources() bool

	// GetPendingAdjusmentn clears and returns any pending adjustment for the container.
	GetPendingAdjustment() *nri.ContainerAdjustment
	// GetPendingUpdate clears and returns any pending update for the container.
	GetPendingUpdate() *nri.ContainerUpdate

	// GetAffinity returns the annotated affinity expressions for this container.
	GetAffinity() ([]*Affinity, error)

	// GetCgroupDir returns the relative path of the cgroup directory for the container.
	GetCgroupDir() string

	// SetRDTClass assigns this container to the given RDT class.
	SetRDTClass(string)
	// GetRDTClass returns the RDT class for this container.
	GetRDTClass() string

	// SetBlockIOClass assigns this container to the given BlockIO class.
	SetBlockIOClass(string)
	// GetBlockIOClass returns the BlockIO class for this container.
	GetBlockIOClass() string

	// GetProcesses returns the pids of processes in the container.
	GetProcesses() ([]string, error)
	// GetTasks returns the pids of threads in the container.
	GetTasks() ([]string, error)

	// GetPending gets the names of the controllers with pending changes.
	GetPending() []string
	// HasPending checks if the container has pending chanhes for the given controller.
	HasPending(string) bool
	// ClearPending clears the pending change marker for the given controller.
	ClearPending(string)

	// GetTag gets the value of the given tag.
	GetTag(string) (string, bool)
	// SetTag sets the value of the given tag and returns its previous value..
	SetTag(string, string) (string, bool)
	// DeleteTag deletes the given tag, returning its deleted value.
	DeleteTag(string) (string, bool)
}

Container is the exposed interface from a cached container.

type ContainerState

type ContainerState = nri.ContainerState

ContainerState is the container state in the runtime.

type Device

type Device = nri.LinuxDevice

type Expression

type Expression struct {
	resmgr.Expression
}

Expression is used to describe affinity container scope and matching criteria.

type ImplicitAffinity

type ImplicitAffinity func(Container, bool) *Affinity

ImplicitAffinity can implicitly inject affinities to containers.

type MatchType

type MatchType int
const (
	PrefixMatch MatchType = iota
	GlobMatch
)

func (*MatchType) UnmarshalJSON

func (t *MatchType) UnmarshalJSON(data []byte) error

type Mount

type Mount = nri.Mount

type Options

type Options struct {
	// CacheDir is the directory the cache should save its state in.
	CacheDir string
}

Options contains the configurable cache options.

type PathList

type PathList struct {
	Type  MatchType `yaml:"type"`
	Paths []string  `yaml:"paths"`
}

type Pod

type Pod interface {
	// GetContainers returns the containers of the pod.
	GetContainers() []Container
	// GetId returns the pod id of the pod.
	GetID() string
	// GetUID returns the (kubernetes) unique id of the pod.
	GetUID() string
	// GetName returns the name of the pod.
	GetName() string
	// GetNamespace returns the namespace of the pod.
	GetNamespace() string
	// GetCtime returns the creation time of the pod cache object.
	GetCtime() time.Time
	// GetQOSClass returns the PodQOSClass of the pod.
	GetQOSClass() v1.PodQOSClass
	// GetLabel returns the value of the given label and whether it was found.
	GetLabel(string) (string, bool)
	// GetAnnotation returns the value of the given annotation and whether it was found.
	GetAnnotation(key string) (string, bool)
	// GetCgroupParent returns the pods cgroup parent directory.
	GetCgroupParent() string

	// PrettyName returns $namespace/$name as the pretty name for the pod.
	PrettyName() string

	// GetResmgrLabel returns the value of a pod label from the
	// nri-resource-policy namespace.
	GetResmgrLabel(string) (string, bool)
	// GetAnnotation returns the value of a pod annotation from the
	// nri-resource-policy namespace and whether it was found.
	GetResmgrAnnotation(key string) (string, bool)
	// GetEffectiveAnnotation returns the effective annotation for a container.
	// For any given key $K and container $C it will look for annotations in
	// this order and return the first one found:
	//     $K/container.$C
	//     $K/pod
	//     $K
	// and return the value of the first key found.
	GetEffectiveAnnotation(key, container string) (string, bool)

	// GetContainerAffinity returns the affinity expressions for the named container.
	GetContainerAffinity(string) ([]*Affinity, error)
	// ScopeExpression returns an affinity expression for defining this pod as the scope.
	ScopeExpression() *resmgr.Expression

	// Pods can be subject for expression evaluation.
	resmgr.Evaluable

	// Expand a string with possible key references.
	Expand(string, bool) (string, error)

	// We have String() for pods.
	fmt.Stringer

	// GetProcesses returns the pids of all processes in the pod either excluding
	// container processes, if called with false, or including those if called with true.
	GetProcesses(bool) ([]string, error)
	// GetTasks returns the pids of all threads in the pod either excluding cotnainer
	// processes, if called with false, or including those if called with true.
	GetTasks(bool) ([]string, error)
}

Pod is the exposed interface from a cached pod.

type PodState

type PodState int32

PodState is the pod state in the runtime.

Jump to

Keyboard shortcuts

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