Skip to content

Commit

Permalink
autoscaler: only log non-zero values for processed recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
nikimanoledaki committed May 27, 2024
1 parent 093618e commit 1800a6e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (calc *UpdatePriorityCalculator) AddPod(pod *apiv1.Pod, now time.Time) {
klog.V(4).Infof("not updating pod %v/%v because resource would not change", pod.Namespace, pod.Name)
return
}
klog.V(2).Infof("pod accepted for update %v/%v with priority %v - processed recommendations:\n%v", pod.Namespace, pod.Name, updatePriority.ResourceDiff, getProcessedRecommendationTargets(processedRecommendation))
klog.V(2).Infof("pod accepted for update %v/%v with priority %v - processed recommendations:\n%v", pod.Namespace, pod.Name, updatePriority.ResourceDiff, calc.GetProcessedRecommendationTargets(processedRecommendation))
calc.pods = append(calc.pods, prioritizedPod{
pod: pod,
priority: updatePriority,
Expand All @@ -165,6 +165,33 @@ func (calc *UpdatePriorityCalculator) GetSortedPods(admission PodEvictionAdmissi
return result
}

func (calc *UpdatePriorityCalculator) GetProcessedRecommendationTargets(r *vpa_types.RecommendedPodResources) string {
sb := &strings.Builder{}
for _, cr := range r.ContainerRecommendations {
sb.WriteString(fmt.Sprintf("%s: ", cr.ContainerName))
if cr.Target != nil {
sb.WriteString("target: ")
if !cr.Target.Memory().IsZero() {
sb.WriteString(fmt.Sprintf("%sK ", cr.Target.Memory().AsDec()))
}
if !cr.Target.Cpu().IsZero() {
sb.WriteString(fmt.Sprintf("%vm; ", cr.Target.Cpu().MilliValue()))
}
}
if cr.UncappedTarget != nil {
sb.WriteString("uncappedTarget: ")
if !cr.UncappedTarget.Memory().IsZero() {
sb.WriteString(fmt.Sprintf("%sK ", cr.UncappedTarget.Memory().AsDec()))
}
if !cr.UncappedTarget.Cpu().IsZero() {
sb.WriteString(fmt.Sprintf("%vm;", cr.UncappedTarget.Cpu().MilliValue()))
}
}
sb.WriteString("\n")
}
return sb.String()
}

func parseVpaObservedContainers(pod *apiv1.Pod) (bool, sets.String) {
observedContainers, hasObservedContainers := pod.GetAnnotations()[annotations.VpaObservedContainersLabel]
vpaContainerSet := sets.NewString()
Expand All @@ -179,21 +206,6 @@ func parseVpaObservedContainers(pod *apiv1.Pod) (bool, sets.String) {
return hasObservedContainers, vpaContainerSet
}

func getProcessedRecommendationTargets(r *vpa_types.RecommendedPodResources) string {
sb := &strings.Builder{}
for _, cr := range r.ContainerRecommendations {
sb.WriteString(fmt.Sprintf("%s:", cr.ContainerName))
if cr.Target != nil {
sb.WriteString(fmt.Sprintf("target: %sK, %vm;", cr.Target.Memory().AsDec(), cr.Target.Cpu().MilliValue()))
}
if cr.UncappedTarget != nil {
sb.WriteString(fmt.Sprintf("uncappedTarget: %sK, %vm;", cr.UncappedTarget.Memory().AsDec(), cr.UncappedTarget.Cpu().MilliValue()))
}
sb.WriteString("\n")
}
return sb.String()
}

type prioritizedPod struct {
pod *apiv1.Pod
priority PodPriority
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,39 @@ func TestLessPodPriority(t *testing.T) {
}

}

func TestAddPodLogs(t *testing.T) {
testCases := []struct {
name string
givenRec *vpa_types.RecommendedPodResources
expectedLog string
}{
{
name: "container with target and uncappedTarget",
givenRec: test.Recommendation().WithContainer(containerName).WithTarget("4", "10M").Get(),
expectedLog: "container1: target: 10000000K 4000m; uncappedTarget: 10000000K 4000m;\n",
},
{
name: "container with cpu only",
givenRec: test.Recommendation().WithContainer(containerName).WithTarget("8", "").Get(),
expectedLog: "container1: target: 8000m; uncappedTarget: 8000m;\n",
},
{
name: "container with memory only",
givenRec: test.Recommendation().WithContainer(containerName).WithTarget("", "10M").Get(),
expectedLog: "container1: target: 10000000K uncappedTarget: 10000000K \n",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
vpa := test.VerticalPodAutoscaler().WithContainer(containerName).WithTarget("10", "").Get()
priorityProcessor := NewFakeProcessor(map[string]PodPriority{
"POD1": {ScaleUp: true, ResourceDiff: 4.0}})
calculator := NewUpdatePriorityCalculator(vpa, nil,
&test.FakeRecommendationProcessor{}, priorityProcessor)

actualLog := calculator.GetProcessedRecommendationTargets(tc.givenRec)
assert.Equal(t, tc.expectedLog, actualLog)
})
}
}

0 comments on commit 1800a6e

Please sign in to comment.