model

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2022 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RecommendationMissingMaxDuration is maximum time that we accept the recommendation can be missing.
	RecommendationMissingMaxDuration = 30 * time.Minute
)

Variables

This section is empty.

Functions

func AggregateStateByContainerName

func AggregateStateByContainerName(aggregateContainerStateMap aggregateContainerStatesMap) vpa_model.ContainerNameToAggregateStateMap

AggregateStateByContainerName takes a set of AggregateContainerStates and merge them grouping by the container name. The result is a map from the container name to the aggregation from all input containers with the given name.

func NewContainerStateAggregatorProxy

func NewContainerStateAggregatorProxy(cluster *ClusterState, containerID vpa_model.ContainerID) vpa_model.ContainerStateAggregator

NewContainerStateAggregatorProxy creates a ContainerStateAggregatorProxy pointing to the cluster state.

Types

type ClusterState

type ClusterState struct {
	// Pods in the cluster.
	Pods map[vpa_model.PodID]*PodState

	// MPA objects in the cluster.
	Mpas map[MpaID]*Mpa
	// MPA objects in the cluster that have no recommendation mapped to the first
	// time we've noticed the recommendation missing or last time we logged
	// a warning about it.
	EmptyMPAs map[MpaID]time.Time
	// Observed MPAs. Used to check if there are updates needed.
	ObservedMpas []*mpa_types.MultidimPodAutoscaler
	// contains filtered or unexported fields
}

ClusterState holds all runtime information about the cluster required for the VPA operations, i.e. configuration of resources (pods, containers, VPA objects), aggregated utilization of compute resources (CPU, memory) and events (container OOMs). All input to the VPA Recommender algorithm lives in this structure.

func NewClusterState

func NewClusterState(gcInterval time.Duration) *ClusterState

NewClusterState returns a new ClusterState with no pods.

func (*ClusterState) AddOrUpdateContainer

func (cluster *ClusterState) AddOrUpdateContainer(containerID vpa_model.ContainerID, request vpa_model.Resources) error

AddOrUpdateContainer creates a new container with the given ContainerID and adds it to the parent pod in the ClusterState object, if not yet present. Requires the pod to be added to the ClusterState first. Otherwise an error is returned.

func (*ClusterState) AddOrUpdateMpa

func (cluster *ClusterState) AddOrUpdateMpa(apiObject *mpa_types.MultidimPodAutoscaler, selector labels.Selector) error

AddOrUpdateMpa adds a new MPA with a given ID to the ClusterState if it didn't yet exist. If the MPA already existed but had a different pod selector, the pod selector is updated. Updates the links between the MPA and all aggregations it matches.

func (*ClusterState) AddOrUpdatePod

func (cluster *ClusterState) AddOrUpdatePod(podID vpa_model.PodID, newLabels labels.Set, phase apiv1.PodPhase)

AddOrUpdatePod updates the state of the pod with a given PodID, if it is present in the cluster object. Otherwise a new pod is created and added to the Cluster object. If the labels of the pod have changed, it updates the links between the containers and the aggregations.

func (*ClusterState) AddSample

func (cluster *ClusterState) AddSample(sample *ContainerUsageSampleWithKey) error

AddSample adds a new usage sample to the proper container in the ClusterState object. Requires the container as well as the parent pod to be added to the ClusterState first. Otherwise an error is returned.

func (*ClusterState) DeleteMpa

func (cluster *ClusterState) DeleteMpa(mpaID MpaID) error

DeleteMpa removes a MPA with the given ID from the ClusterState.

func (*ClusterState) DeletePod

func (cluster *ClusterState) DeletePod(podID vpa_model.PodID)

DeletePod removes an existing pod from the cluster.

func (*ClusterState) GetContainer

func (cluster *ClusterState) GetContainer(containerID vpa_model.ContainerID) *ContainerState

GetContainer returns the ContainerState object for a given ContainerID or null if it's not present in the model.

func (*ClusterState) GetControllerForPodUnderVPA

func (cluster *ClusterState) GetControllerForPodUnderVPA(pod *PodState, controllerFetcher controllerfetcher.ControllerFetcher) *controllerfetcher.ControllerKeyWithAPIVersion

GetControllerForPodUnderVPA returns controller associated with given Pod. Returns nil if Pod is not controlled by a VPA object.

func (*ClusterState) GetControllingMPA

func (cluster *ClusterState) GetControllingMPA(pod *PodState) *Mpa

GetControllingVPA returns a VPA object controlling given Pod.

func (*ClusterState) GetMatchingPods

func (cluster *ClusterState) GetMatchingPods(mpa *Mpa) []vpa_model.PodID

GetMatchingPods returns a list of currently active pods that match the given MPA. Traverses through all pods in the cluster - use sparingly.

func (*ClusterState) MakeAggregateStateKey

func (cluster *ClusterState) MakeAggregateStateKey(pod *PodState, containerName string) vpa_model.AggregateStateKey

MakeAggregateStateKey returns the AggregateStateKey that should be used to aggregate usage samples from a container with the given name in a given pod.

func (*ClusterState) RateLimitedGarbageCollectAggregateCollectionStates

func (cluster *ClusterState) RateLimitedGarbageCollectAggregateCollectionStates(now time.Time, controllerFetcher controllerfetcher.ControllerFetcher)

RateLimitedGarbageCollectAggregateCollectionStates removes obsolete AggregateCollectionStates from the ClusterState. It performs clean up only if more than `gcInterval` passed since the last time it performed a clean up. AggregateCollectionState is obsolete in following situations: 1) It has no samples and there are no more contributive pods - a pod is contributive in any of following situations:

a) It is in an active state - i.e. not PodSucceeded nor PodFailed.
b) Its associated controller (e.g. Deployment) still exists.

2) The last sample is too old to give meaningful recommendation (>8 days), 3) There are no samples and the aggregate state was created >8 days ago.

func (*ClusterState) RecordOOM

func (cluster *ClusterState) RecordOOM(containerID vpa_model.ContainerID, timestamp time.Time, requestedMemory vpa_model.ResourceAmount) error

RecordOOM adds info regarding OOM event in the model as an artificial memory sample.

func (*ClusterState) RecordRecommendation

func (cluster *ClusterState) RecordRecommendation(mpa *Mpa, now time.Time) error

RecordRecommendation marks the state of recommendation in the cluster. We keep track of empty recommendations and log information about them periodically.

func (*ClusterState) StateMapSize

func (cluster *ClusterState) StateMapSize() int

StateMapSize is the number of pods being tracked by the VPA

type ContainerState

type ContainerState struct {
	// Current request.
	Request vpa_model.Resources
	// Start of the latest CPU usage sample that was aggregated.
	LastCPUSampleStart time.Time

	// End time of the current memory aggregation interval (not inclusive).
	WindowEnd time.Time
	// contains filtered or unexported fields
}

ContainerState stores information about a single container instance. Each ContainerState has a pointer to the aggregation that is used for aggregating its usage samples. It holds the recent history of CPU and memory utilization.

Note: samples are added to intervals based on their start timestamps.

func NewContainerState

func NewContainerState(request vpa_model.Resources, aggregator vpa_model.ContainerStateAggregator) *ContainerState

NewContainerState returns a new ContainerState.

func (*ContainerState) AddSample

func (container *ContainerState) AddSample(sample *vpa_model.ContainerUsageSample) bool

AddSample adds a usage sample to the given ContainerState. Requires samples for a single resource to be passed in chronological order (i.e. in order of growing MeasureStart). Invalid samples (out of order or measure out of legal range) are discarded. Returns true if the sample was aggregated, false if it was discarded. Note: usage samples don't hold their end timestamp / duration. They are implicitly assumed to be disjoint when aggregating.

func (*ContainerState) GetMaxMemoryPeak

func (container *ContainerState) GetMaxMemoryPeak() vpa_model.ResourceAmount

GetMaxMemoryPeak returns maximum memory usage in the sample, possibly estimated from OOM

func (*ContainerState) RecordOOM

func (container *ContainerState) RecordOOM(timestamp time.Time, requestedMemory vpa_model.ResourceAmount) error

RecordOOM adds info regarding OOM event in the model as an artificial memory sample.

type ContainerStateAggregatorProxy

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

ContainerStateAggregatorProxy is a wrapper for ContainerStateAggregator that creates ContainerStateAgregator for container if it is no longer present in the cluster state.

func (*ContainerStateAggregatorProxy) AddSample

AddSample adds a container sample to the aggregator.

func (*ContainerStateAggregatorProxy) GetLastRecommendation

func (p *ContainerStateAggregatorProxy) GetLastRecommendation() corev1.ResourceList

GetLastRecommendation returns last recorded recommendation.

func (*ContainerStateAggregatorProxy) GetScalingMode

GetScalingMode returns scaling mode of container represented by the aggregator.

func (*ContainerStateAggregatorProxy) GetUpdateMode

GetUpdateMode returns update mode of VPA controlling the aggregator.

func (*ContainerStateAggregatorProxy) NeedsRecommendation

func (p *ContainerStateAggregatorProxy) NeedsRecommendation() bool

NeedsRecommendation returns true if the aggregator should have recommendation calculated.

func (*ContainerStateAggregatorProxy) SubtractSample

SubtractSample subtracts a container sample from the aggregator.

type ContainerUsageSampleWithKey

type ContainerUsageSampleWithKey struct {
	vpa_model.ContainerUsageSample
	Container vpa_model.ContainerID
}

ContainerUsageSampleWithKey holds a ContainerUsageSample together with the ID of the container it belongs to.

type Mpa

type Mpa struct {
	ID MpaID
	// Labels selector that determines which Pods are controlled by this MPA
	// object. Can be nil, in which case no Pod is matched.
	PodSelector labels.Selector
	// Map of the object annotations (key-value pairs).
	Annotations mpaAnnotationsMap
	// Map of the status conditions (keys are condition types).
	Conditions mpaConditionsMap
	// Most recently computed recommendation. Can be nil.
	Recommendation *vpa_types.RecommendedPodResources

	// Pod Resource Policy provided in the MPA API object. Can be nil.
	ResourcePolicy *vpa_types.PodResourcePolicy
	// Initial checkpoints of AggregateContainerStates for containers.
	// The key is container name.
	ContainersInitialAggregateState vpa_model.ContainerNameToAggregateStateMap
	// UpdateMode describes how recommendations will be applied to pods
	UpdateMode *vpa_types.UpdateMode
	// Created denotes timestamp of the original MPA object creation
	Created time.Time
	// CheckpointWritten indicates when last checkpoint for the MPA object was stored.
	CheckpointWritten time.Time
	// IsV1Beta1API is set to true if MPA object has labelSelector defined as in v1beta1 api.
	IsV1Beta1API bool
	// ScaleTargetRef points to the controller managing the set of pods.
	ScaleTargetRef *autoscaling.CrossVersionObjectReference
	// PodCount contains number of live Pods matching a given MPA object.
	PodCount int

	// Added for HPA-related fields.
	// TODO: Currently HPA-related logic is directly manipulating the MPA object but not the MPA
	// model here.
	Metrics                   []autoscalingv2.MetricSpec
	MinReplicas               int32
	MaxReplicas               int32
	HorizontalScalingBehavior *autoscalingv2.HorizontalPodAutoscalerBehavior
	DesiredReplicas           int32
	CurrentMetrics            []autoscalingv2.MetricStatus
	// contains filtered or unexported fields
}

Mpa (Multidimensional Pod Autoscaler) object is responsible for horizontal and vertical scaling of Pods matching a given label selector.

func NewMpa

func NewMpa(id MpaID, selector labels.Selector, created time.Time) *Mpa

NewMpa returns a new Mpa with a given ID and pod selector. Doesn't set the links to the matched aggregations.

func (*Mpa) AggregateStateByContainerName

func (mpa *Mpa) AggregateStateByContainerName() vpa_model.ContainerNameToAggregateStateMap

AggregateStateByContainerName returns a map from container name to the aggregated state of all containers with that name, belonging to pods matched by the MPA.

func (*Mpa) AsStatus

func (mpa *Mpa) AsStatus() *mpa_types.MultidimPodAutoscalerStatus

AsStatus returns this objects equivalent of MPA Status. UpdateConditions should be called first.

func (*Mpa) DeleteAggregation

func (mpa *Mpa) DeleteAggregation(aggregationKey vpa_model.AggregateStateKey)

DeleteAggregation deletes aggregation used by this container

func (*Mpa) HasMatchedPods

func (mpa *Mpa) HasMatchedPods() bool

HasMatchedPods returns true if there are are currently active pods in the cluster matching this MPA, based on conditions. UpdateConditions should be called first.

func (*Mpa) HasRecommendation

func (mpa *Mpa) HasRecommendation() bool

HasRecommendation returns if the MPA object contains any recommendation

func (*Mpa) MergeCheckpointedState

func (mpa *Mpa) MergeCheckpointedState(aggregateContainerStateMap vpa_model.ContainerNameToAggregateStateMap)

MergeCheckpointedState adds checkpointed MPA aggregations to the given aggregateStateMap.

func (*Mpa) SetCurrentMetrics added in v0.2.1

func (mpa *Mpa) SetCurrentMetrics(metrics []autoscalingv2.MetricStatus)

Set the current metrics.

func (*Mpa) SetDesiredNumberOfReplicas added in v0.2.1

func (mpa *Mpa) SetDesiredNumberOfReplicas(replicas int32)

Set the desired number of replicas.

func (*Mpa) SetHPAConstraints added in v0.2.1

func (mpa *Mpa) SetHPAConstraints(metrics []autoscalingv2.MetricSpec, minReplicas int32, maxReplicas int32, hpaBehavior *autoscalingv2.HorizontalPodAutoscalerBehavior)

Set HPA-related constraints.

func (*Mpa) SetResourcePolicy

func (mpa *Mpa) SetResourcePolicy(resourcePolicy *vpa_types.PodResourcePolicy)

SetResourcePolicy updates the resource policy of the MPA and the scaling policies of aggregators under this MPA.

func (*Mpa) SetUpdateMode

func (mpa *Mpa) SetUpdateMode(updatePolicy *mpa_types.PodUpdatePolicy)

SetUpdateMode updates the update mode of the MPA and aggregators under this MPA.

func (*Mpa) UpdateConditions

func (mpa *Mpa) UpdateConditions(podsMatched bool)

UpdateConditions updates the conditions of MPA objects based on it's state. PodsMatched is passed to indicate if there are currently active pods in the cluster matching this MPA.

func (*Mpa) UpdateRecommendation

func (mpa *Mpa) UpdateRecommendation(recommendation *vpa_types.RecommendedPodResources)

UpdateRecommendation updates the recommended resources in the MPA and its aggregations with the given recommendation.

func (*Mpa) UseAggregationIfMatching

func (mpa *Mpa) UseAggregationIfMatching(aggregationKey vpa_model.AggregateStateKey, aggregation *vpa_model.AggregateContainerState)

UseAggregationIfMatching checks if the given aggregation matches (contributes to) this MPA and adds it to the set of MPA's aggregations if that is the case.

func (*Mpa) UsesAggregation

func (mpa *Mpa) UsesAggregation(aggregationKey vpa_model.AggregateStateKey) bool

UsesAggregation returns true iff an aggregation with the given key contributes to the MPA.

type MpaID

type MpaID struct {
	Namespace string
	MpaName   string
}

MpaID contains information needed to identify a MPA API object within a cluster.

type PodState

type PodState struct {
	// Unique id of the Pod.
	ID vpa_model.PodID

	// Containers that belong to the Pod, keyed by the container name.
	Containers map[string]*ContainerState
	// PodPhase describing current life cycle phase of the Pod.
	Phase apiv1.PodPhase
	// contains filtered or unexported fields
}

PodState holds runtime information about a single Pod.

Jump to

Keyboard shortcuts

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