Skip to content

Commit

Permalink
bugfix: use PodRequests helper function from k8s.io/kubectl for corre…
Browse files Browse the repository at this point in the history
…ct allocatable resource calculation

(cherry picked from commit 01cdc85)
  • Loading branch information
neogopher committed Feb 6, 2025
1 parent 2822d85 commit 54a9798
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions pkg/controllers/resources/nodes/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/loft-sh/vcluster/pkg/util/translate"
corev1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
resourceutil "k8s.io/kubectl/pkg/util/resource"
)

var (
Expand Down Expand Up @@ -165,10 +166,12 @@ func (s *nodeSyncer) translateUpdateStatus(ctx *synccontext.SyncContext, pNode *
if s.enableScheduler {
// calculate what's really allocatable
if translatedStatus.Allocatable != nil {
cpu := translatedStatus.Allocatable.Cpu().MilliValue()
memory := translatedStatus.Allocatable.Memory().Value()
storageEphemeral := translatedStatus.Allocatable.StorageEphemeral().Value()
pods := translatedStatus.Allocatable.Pods().Value()
allocatable := map[corev1.ResourceName]int64{
corev1.ResourceCPU: translatedStatus.Allocatable.Cpu().MilliValue(),
corev1.ResourceMemory: translatedStatus.Allocatable.Memory().Value(),
corev1.ResourceEphemeralStorage: translatedStatus.Allocatable.StorageEphemeral().Value(),
corev1.ResourcePods: translatedStatus.Allocatable.Pods().Value(),
}

var nonVClusterPods int64
podList := &corev1.PodList{}
Expand All @@ -187,31 +190,33 @@ func (s *nodeSyncer) translateUpdateStatus(ctx *synccontext.SyncContext, pNode *
// count pods that are not synced by this vcluster
nonVClusterPods++
}
for _, container := range pod.Spec.InitContainers {
cpu -= container.Resources.Requests.Cpu().MilliValue()
memory -= container.Resources.Requests.Memory().Value()
storageEphemeral -= container.Resources.Requests.StorageEphemeral().Value()
}
for _, container := range pod.Spec.Containers {
cpu -= container.Resources.Requests.Cpu().MilliValue()
memory -= container.Resources.Requests.Memory().Value()
storageEphemeral -= container.Resources.Requests.StorageEphemeral().Value()

reqs, _ := resourceutil.PodRequestsAndLimits(&pod)

for _, resName := range []corev1.ResourceName{corev1.ResourceCPU, corev1.ResourceMemory, corev1.ResourceEphemeralStorage} {
if req, ok := reqs[resName]; ok {
if resName == corev1.ResourceCPU {
allocatable[resName] -= req.MilliValue()
} else {
allocatable[resName] -= req.Value()
}
}
}
}
}

pods -= nonVClusterPods
if pods > 0 {
translatedStatus.Allocatable[corev1.ResourcePods] = *resource.NewQuantity(pods, resource.DecimalSI)
allocatable[corev1.ResourcePods] -= nonVClusterPods
if allocatable[corev1.ResourcePods] > 0 {
translatedStatus.Allocatable[corev1.ResourcePods] = *resource.NewQuantity(allocatable[corev1.ResourcePods], resource.DecimalSI)
}
if cpu > 0 {
translatedStatus.Allocatable[corev1.ResourceCPU] = *resource.NewMilliQuantity(cpu, resource.DecimalSI)
if allocatable[corev1.ResourceCPU] > 0 {
translatedStatus.Allocatable[corev1.ResourceCPU] = *resource.NewMilliQuantity(allocatable[corev1.ResourceCPU], resource.DecimalSI)
}
if memory > 0 {
translatedStatus.Allocatable[corev1.ResourceMemory] = *resource.NewQuantity(memory, resource.BinarySI)
if allocatable[corev1.ResourceMemory] > 0 {
translatedStatus.Allocatable[corev1.ResourceMemory] = *resource.NewQuantity(allocatable[corev1.ResourceMemory], resource.BinarySI)
}
if storageEphemeral > 0 {
translatedStatus.Allocatable[corev1.ResourceEphemeralStorage] = *resource.NewQuantity(storageEphemeral, resource.BinarySI)
if allocatable[corev1.ResourceEphemeralStorage] > 0 {
translatedStatus.Allocatable[corev1.ResourceEphemeralStorage] = *resource.NewQuantity(allocatable[corev1.ResourceEphemeralStorage], resource.BinarySI)
}
}

Expand Down

0 comments on commit 54a9798

Please sign in to comment.