Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more precise logging for VPA resource recommendations #6723

Merged
merged 7 commits into from
May 29, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package priority

import (
"flag"
"fmt"
"sort"
"strings"
"time"

apiv1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -140,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", pod.Namespace, pod.Name, updatePriority.ResourceDiff)
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 @@ -163,6 +165,35 @@ func (calc *UpdatePriorityCalculator) GetSortedPods(admission PodEvictionAdmissi
return result
}

// GetProcessedRecommendationTargets takes a RecommendedPodResources object and returns a formatted string
// with the recommended pod resources. Specifically, it formats the target and uncapped target CPU and memory.
func (calc *UpdatePriorityCalculator) GetProcessedRecommendationTargets(r *vpa_types.RecommendedPodResources) string {
sb := &strings.Builder{}
for _, cr := range r.ContainerRecommendations {
sb.WriteString(fmt.Sprintf("%s: ", cr.ContainerName))
Copy link
Member

Choose a reason for hiding this comment

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

As you're already using the string builder here you could write this without fmt.Sprintf to be more efficient.

if cr.Target != nil {
sb.WriteString("target: ")
if !cr.Target.Memory().IsZero() {
sb.WriteString(fmt.Sprintf("%sK ", cr.Target.Memory().AsDec()))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need to scale this in order to be Kilobytes, otherwise AsDec() returns the memory value in Bytes

Suggested change
sb.WriteString(fmt.Sprintf("%sK ", cr.Target.Memory().AsDec()))
sb.WriteString(fmt.Sprintf("%sk ", cr.Target.Memory().ScaledValue(resource.Kilo)))

whereas resource is imported from
"k8s.io/apimachinery/pkg/api/resource"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point - added this in the latest commit + updated the tests!

}
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 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)
})
}
}
Loading