Skip to content

Commit

Permalink
perf: Drop unneeded recalculation for resources and available (#1823)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis authored Nov 17, 2024
1 parent 41801d2 commit 879964d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
12 changes: 7 additions & 5 deletions pkg/controllers/provisioning/scheduling/existingnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

type ExistingNode struct {
*state.StateNode
cachedAvailable v1.ResourceList // Cache so we don't have to re-subtract resources on the StateNode every time

Pods []*v1.Pod
topology *Topology
Expand All @@ -51,10 +52,11 @@ func NewExistingNode(n *state.StateNode, topology *Topology, daemonResources v1.
}
}
node := &ExistingNode{
StateNode: n,
topology: topology,
requests: remainingDaemonResources,
requirements: scheduling.NewLabelRequirements(n.Labels()),
StateNode: n,
cachedAvailable: n.Available(),
topology: topology,
requests: remainingDaemonResources,
requirements: scheduling.NewLabelRequirements(n.Labels()),
}
node.requirements.Add(scheduling.NewRequirement(v1.LabelHostname, v1.NodeSelectorOpIn, n.HostName()))
topology.Register(v1.LabelHostname, n.HostName())
Expand Down Expand Up @@ -84,7 +86,7 @@ func (n *ExistingNode) Add(ctx context.Context, kubeClient client.Client, pod *v
// node, which at this point can't be increased in size
requests := resources.Merge(n.requests, resources.RequestsForPods(pod))

if !resources.Fits(requests, n.Available()) {
if !resources.Fits(requests, n.cachedAvailable) {
return fmt.Errorf("exceeds node resources")
}

Expand Down
19 changes: 9 additions & 10 deletions pkg/utils/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,18 @@ func MaxResources(resources ...v1.ResourceList) v1.ResourceList {

// MergeResourceLimitsIntoRequests merges resource limits into requests if no request exists for the given resource
func MergeResourceLimitsIntoRequests(container v1.Container) v1.ResourceList {
resources := container.Resources.DeepCopy()
if resources.Requests == nil {
resources.Requests = v1.ResourceList{}
}

if resources.Limits != nil {
for resourceName, quantity := range resources.Limits {
if _, ok := resources.Requests[resourceName]; !ok {
resources.Requests[resourceName] = quantity
ret := v1.ResourceList{}
for resourceName, quantity := range container.Resources.Requests {
ret[resourceName] = quantity
}
if container.Resources.Limits != nil {
for resourceName, quantity := range container.Resources.Limits {
if _, ok := container.Resources.Requests[resourceName]; !ok {
ret[resourceName] = quantity
}
}
}
return resources.Requests
return ret
}

// Quantity parses the string value into a *Quantity
Expand Down

0 comments on commit 879964d

Please sign in to comment.