Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions vertical-pod-autoscaler/pkg/recommender/input/cluster_feeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,15 +499,17 @@ func (feeder *clusterStateFeeder) LoadRealTimeMetrics(ctx context.Context) {
sampleCount := 0
droppedSampleCount := 0
for _, containerMetrics := range containersMetrics {
podInitContainers := feeder.clusterState.Pods()[containerMetrics.ID.PodID].InitContainers
if slices.Contains(podInitContainers, containerMetrics.ID.ContainerName) {
klog.V(3).InfoS("Skipping metric samples for init container", "pod", klog.KRef(containerMetrics.ID.PodID.Namespace, containerMetrics.ID.PodID.PodName), "container", containerMetrics.ID.ContainerName)
droppedSampleCount += len(containerMetrics.Usage)
continue
// Container metrics are fetched for all pods, however, not all pod states are tracked in memory saver mode.
if pod, exists := feeder.clusterState.Pods()[containerMetrics.ID.PodID]; exists && pod != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a similar comment as below? Something like:

// Not all pod states are tracked in memory saver mode

Just helps give context behind the defensive check here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've applied the suggestion. PTAL when you can :)

if slices.Contains(pod.InitContainers, containerMetrics.ID.ContainerName) {
klog.V(3).InfoS("Skipping metric samples for init container", "pod", klog.KRef(containerMetrics.ID.PodID.Namespace, containerMetrics.ID.PodID.PodName), "container", containerMetrics.ID.ContainerName)
droppedSampleCount += len(containerMetrics.Usage)
continue
}
}
for _, sample := range newContainerUsageSamplesWithKey(containerMetrics) {
if err := feeder.clusterState.AddSample(sample); err != nil {
// Not all pod states are tracked in memory saver mode
// Not all pod states are tracked in memory saver mode.
if _, isKeyError := err.(model.KeyError); isKeyError && feeder.memorySaveMode {
continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ type fakeClusterState struct {
}

func (cs *fakeClusterState) AddSample(sample *model.ContainerUsageSampleWithKey) error {
_, podExists := cs.stubbedPods[sample.Container.PodID]
if !podExists {
return model.NewKeyError(sample.Container.PodID)
}
samplesForContainer := cs.addedSamples[sample.Container]
cs.addedSamples[sample.Container] = append(samplesForContainer, sample)
return nil
Expand Down Expand Up @@ -651,6 +655,27 @@ func TestClusterStateFeeder_LoadRealTimeMetrics(t *testing.T) {
samplesForContainer2 := clusterState.addedSamples[regularContainer2]
assert.Contains(t, samplesForContainer2, regularContainer2UsageSamples[0])
assert.Contains(t, samplesForContainer2, regularContainer2UsageSamples[1])

// Add extra container metrics for which there are no added pods to the state to simulate memory-saver=true
extraPodID := model.PodID{Namespace: namespaceName, PodName: "ExtraPod"}
extraContainer := model.ContainerID{PodID: extraPodID, ContainerName: "ExtraContainer"}
extraContainerMetricsSnapshot, _ := newContainerMetricsSnapshot(extraContainer, 200, 2048)
containerMetricsSnapshots = append(containerMetricsSnapshots, extraContainerMetricsSnapshot)

clusterState = NewFakeClusterState(nil, pods)

feeder = clusterStateFeeder{
memorySaveMode: true,
clusterState: clusterState,
metricsClient: fakeMetricsClient{snapshots: containerMetricsSnapshots},
}

feeder.LoadRealTimeMetrics(tctx)

assert.Equal(t, 2, len(clusterState.addedSamples))

_, samplesForExtraContainerExist := clusterState.addedSamples[extraContainer]
assert.False(t, samplesForExtraContainerExist)
}

type fakeHistoryProvider struct {
Expand Down
Loading