Skip to content

Commit 527120a

Browse files
luomingmengwaynepeking348
authored andcommitted
fix vpa status current update
1 parent 34f852e commit 527120a

File tree

4 files changed

+77
-25
lines changed

4 files changed

+77
-25
lines changed

pkg/controller/vpa/util/resource.go

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -337,46 +337,84 @@ func GetVPAResourceStatusWithCurrent(vpa *apis.KatalystVerticalPodAutoscaler, po
337337
return nil, nil, err
338338
}
339339

340+
updateContainerResourcesCurrent := func(targetResourceNames map[consts.ContainerName][]core.ResourceName,
341+
containerName consts.ContainerName,
342+
target core.ResourceList, current *core.ResourceList,
343+
specResource core.ResourceList,
344+
) {
345+
// if pod apply strategy is 'Pod', we not need update container current,
346+
// because each pod has different recommendation.
347+
if vpa.Spec.UpdatePolicy.PodApplyStrategy == apis.PodApplyStrategyStrategyPod {
348+
*current = nil
349+
return
350+
}
351+
352+
_, ok := targetResourceNames[containerName]
353+
if !ok {
354+
*current = target.DeepCopy()
355+
resourceNames := make([]core.ResourceName, 0, len(target))
356+
for resourceName := range target {
357+
resourceNames = append(resourceNames, resourceName)
358+
}
359+
sort.SliceStable(resourceNames, func(i, j int) bool {
360+
return string(resourceNames[i]) < string(resourceNames[j])
361+
})
362+
targetResourceNames[containerName] = resourceNames
363+
}
364+
365+
specCurrent := cropResourcesWithBounds(specResource, nil, nil, targetResourceNames[containerName])
366+
if !native.ResourcesEqual(target, specCurrent) &&
367+
(native.ResourcesEqual(target, *current) ||
368+
!native.ResourcesLess(*current, specCurrent, targetResourceNames[containerName])) {
369+
*current = specCurrent
370+
}
371+
}
372+
340373
for containerName := range containerResources {
341374
// get container resource current requests
342375
if containerResources[containerName].Requests != nil {
376+
targetResourceNames := make(map[consts.ContainerName][]core.ResourceName)
343377
func(pods []*core.Pod) {
344378
for _, pod := range pods {
345379
for _, container := range pod.Spec.Containers {
346380
if container.Name != string(containerName) {
347381
continue
348382
}
349383

350-
containerResources[containerName].Requests.Current = container.Resources.Requests
351-
// if some container current requests doesn't equal to target, return it immediately
352-
if !native.ResourcesEqual(containerResources[containerName].Requests.Target, container.Resources.Requests) {
353-
return
354-
}
384+
updateContainerResourcesCurrent(targetResourceNames, containerName, containerResources[containerName].Requests.Target,
385+
&containerResources[containerName].Requests.Current, container.Resources.Requests)
355386
}
356387
}
357388
}(pods)
358389
}
359390

360391
// get container resource current limits
361392
if containerResources[containerName].Limits != nil {
393+
targetResourceNames := make(map[consts.ContainerName][]core.ResourceName)
362394
func(pods []*core.Pod) {
363395
for _, pod := range pods {
364396
for _, container := range pod.Spec.Containers {
365397
if container.Name != string(containerName) {
366398
continue
367399
}
368400

369-
containerResources[containerName].Limits.Current = container.Resources.Limits
370-
// if some container current limits doesn't equal to target, return it immediately
371-
if !native.ResourcesEqual(containerResources[containerName].Limits.Target, container.Resources.Limits) {
372-
return
373-
}
401+
updateContainerResourcesCurrent(targetResourceNames, containerName, containerResources[containerName].Limits.Target,
402+
&containerResources[containerName].Limits.Current, container.Resources.Limits)
374403
}
375404
}
376405
}(pods)
377406
}
378407
}
379408

409+
getPodContainerResourcesCurrent := func(target core.ResourceList, specResource core.ResourceList) core.ResourceList {
410+
resourceNames := make([]core.ResourceName, 0, len(target))
411+
for resourceName := range target {
412+
resourceNames = append(resourceNames, resourceName)
413+
}
414+
415+
return cropResourcesWithBounds(specResource, nil, nil, resourceNames)
416+
}
417+
380418
for podContainerName := range podResources {
381419
podName, containerName, err := native.ParsePodContainerName(podContainerName)
382420
if err != nil {
@@ -396,7 +434,7 @@ func GetVPAResourceStatusWithCurrent(vpa *apis.KatalystVerticalPodAutoscaler, po
396434
continue
397435
}
398436

399-
podResources[podContainerName].Requests.Current = container.Resources.Requests
437+
podResources[podContainerName].Requests.Current = getPodContainerResourcesCurrent(podResources[podContainerName].Requests.Target, container.Resources.Requests)
400438
return
401439
}
402440
}
@@ -415,7 +453,8 @@ func GetVPAResourceStatusWithCurrent(vpa *apis.KatalystVerticalPodAutoscaler, po
415453
if container.Name != containerName {
416454
continue
417455
}
418-
podResources[podContainerName].Limits.Current = container.Resources.Limits
456+
457+
podResources[podContainerName].Limits.Current = getPodContainerResourcesCurrent(podResources[podContainerName].Limits.Target, container.Resources.Limits)
419458
return
420459
}
421460
}

pkg/controller/vpa/util/resource_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,7 @@ func testGetVPAResourceStatusWithCurrentAndIndexer(t *testing.T, podIndexers cac
422422
{
423423
ContainerName: pointer.String("c1"),
424424
Limits: &apis.ContainerResourceList{
425-
Current: v1.ResourceList{
426-
v1.ResourceMemory: resource.MustParse("4Gi"),
427-
v1.ResourceCPU: resource.MustParse("4"),
428-
},
425+
Current: v1.ResourceList{},
429426
Target: v1.ResourceList{},
430427
UncappedTarget: v1.ResourceList{},
431428
LowerBound: v1.ResourceList{

pkg/controller/vpa/vpa_status.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -324,22 +324,21 @@ func (vs *vpaStatusController) syncVPA(key string) error {
324324
// setRecommendationAppliedCondition set vpa recommendation applied condition by checking all pods whether
325325
// are updated to the expected resources in their annotations
326326
func (vs *vpaStatusController) setRecommendationAppliedCondition(vpa *apis.KatalystVerticalPodAutoscaler, pods []*v1.Pod) error {
327-
allPodSpecUpdated := func() bool {
328-
for _, pod := range pods {
329-
if !katalystutil.CheckPodSpecUpdated(pod) {
330-
return false
331-
}
327+
failedCount := 0
328+
for _, pod := range pods {
329+
if !katalystutil.CheckPodSpecUpdated(pod) {
330+
failedCount += 1
332331
}
333-
return true
334-
}()
332+
}
335333

336-
if allPodSpecUpdated {
334+
if failedCount == 0 {
337335
err := util.SetVPAConditions(vpa, apis.RecommendationApplied, v1.ConditionTrue, util.VPAConditionReasonPodSpecUpdated, "")
338336
if err != nil {
339337
return err
340338
}
341339
} else {
342-
err := util.SetVPAConditions(vpa, apis.RecommendationApplied, v1.ConditionFalse, util.VPAConditionReasonPodSpecNoUpdate, "")
340+
msg := fmt.Sprintf("failed to update %d pods, total %d pods", failedCount, len(pods))
341+
err := util.SetVPAConditions(vpa, apis.RecommendationApplied, v1.ConditionFalse, util.VPAConditionReasonPodSpecNoUpdate, msg)
343342
if err != nil {
344343
return err
345344
}

pkg/util/native/resources.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,23 @@ func ResourcesEqual(a, b v1.ResourceList) bool {
145145
return true
146146
}
147147

148+
// ResourcesLess checks whether the given resources a are less than b
149+
func ResourcesLess(a, b v1.ResourceList, resourceNameList []v1.ResourceName) bool {
150+
for _, name := range resourceNameList {
151+
quantityA, okA := a[name]
152+
quantityB, okB := b[name]
153+
if okA != okB {
154+
return okA
155+
} else if okA {
156+
if quantityA.Cmp(quantityB) < 0 {
157+
return true
158+
}
159+
}
160+
}
161+
162+
return false
163+
}
164+
148165
// AddResources sums up two ResourceList, and returns the summed as results.
149166
func AddResources(a, b v1.ResourceList) v1.ResourceList {
150167
res := make(v1.ResourceList)

0 commit comments

Comments
 (0)