Skip to content

Commit c86f85a

Browse files
committed
Move nodeResources to cache file
1 parent d632ae0 commit c86f85a

File tree

4 files changed

+94
-95
lines changed

4 files changed

+94
-95
lines changed

cluster-autoscaler/resourcequotas/cache.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,30 @@ limitations under the License.
1717
package resourcequotas
1818

1919
import (
20+
"fmt"
21+
2022
corev1 "k8s.io/api/core/v1"
2123
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
2224
cacontext "k8s.io/autoscaler/cluster-autoscaler/context"
25+
"k8s.io/autoscaler/cluster-autoscaler/core/utils"
2326
"k8s.io/autoscaler/cluster-autoscaler/processors/customresources"
2427
)
2528

2629
type nodeCache struct {
27-
crp customresources.CustomResourcesProcessor
28-
deltas map[string]resourceList
30+
crp customresources.CustomResourcesProcessor
31+
resources map[string]resourceList
2932
}
3033

3134
func newNodeCache(crp customresources.CustomResourcesProcessor) *nodeCache {
3235
return &nodeCache{
33-
crp: crp,
34-
deltas: make(map[string]resourceList),
36+
crp: crp,
37+
resources: make(map[string]resourceList),
3538
}
3639
}
3740

3841
func (nc *nodeCache) nodeResources(autoscalingCtx *cacontext.AutoscalingContext, node *corev1.Node, nodeGroup cloudprovider.NodeGroup) (resourceList, error) {
3942
if nodeGroup != nil {
40-
if delta, ok := nc.deltas[nodeGroup.Id()]; ok {
43+
if delta, ok := nc.resources[nodeGroup.Id()]; ok {
4144
return delta, nil
4245
}
4346
}
@@ -46,7 +49,29 @@ func (nc *nodeCache) nodeResources(autoscalingCtx *cacontext.AutoscalingContext,
4649
return nil, err
4750
}
4851
if nodeGroup != nil {
49-
nc.deltas[nodeGroup.Id()] = delta
52+
nc.resources[nodeGroup.Id()] = delta
5053
}
5154
return delta, nil
5255
}
56+
57+
// nodeResources calculates the amount of resources that a node contains.
58+
func nodeResources(autoscalingCtx *cacontext.AutoscalingContext, crp customresources.CustomResourcesProcessor, node *corev1.Node, nodeGroup cloudprovider.NodeGroup) (resourceList, error) {
59+
// TODO: storage?
60+
nodeCPU, nodeMemory := utils.GetNodeCoresAndMemory(node)
61+
nodeResources := resourceList{
62+
string(corev1.ResourceCPU): nodeCPU,
63+
string(corev1.ResourceMemory): nodeMemory,
64+
ResourceNodes: 1,
65+
}
66+
67+
resourceTargets, err := crp.GetNodeResourceTargets(autoscalingCtx, node, nodeGroup)
68+
if err != nil {
69+
return nil, fmt.Errorf("failed to get custom resources: %w", err)
70+
}
71+
72+
for _, resourceTarget := range resourceTargets {
73+
nodeResources[resourceTarget.ResourceType] = resourceTarget.ResourceCount
74+
}
75+
76+
return nodeResources, nil
77+
}

cluster-autoscaler/resourcequotas/cache_test.go

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (m *mockCustomResourcesProcessor) CleanUp() {
4848
return
4949
}
5050

51-
func TestNodeCacheNodeResources(t *testing.T) {
51+
func TestNodeCache(t *testing.T) {
5252
node := test.BuildTestNode("n1", 1000, 2000)
5353
autoscalingCtx := &context.AutoscalingContext{}
5454
ng1 := cptest.NewTestNodeGroup("ng1", 1, 10, 1, true, false, "n1-template", nil, nil)
@@ -117,3 +117,65 @@ func TestNodeCacheNodeResources(t *testing.T) {
117117
})
118118
}
119119
}
120+
121+
func TestNodeResources(t *testing.T) {
122+
testCases := []struct {
123+
name string
124+
node *apiv1.Node
125+
crp customresources.CustomResourcesProcessor
126+
wantDelta resourceList
127+
}{
128+
{
129+
name: "node just with CPU and memory",
130+
node: test.BuildTestNode("test", 1000, 2048),
131+
crp: &fakeCustomResourcesProcessor{},
132+
wantDelta: resourceList{
133+
"cpu": 1,
134+
"memory": 2048,
135+
"nodes": 1,
136+
},
137+
},
138+
{
139+
// nodes should not have milliCPUs in the capacity, so we round it up
140+
// to the nearest integer.
141+
name: "node just with CPU and memory, milli cores rounded up",
142+
node: test.BuildTestNode("test", 2500, 4096),
143+
crp: &fakeCustomResourcesProcessor{},
144+
wantDelta: resourceList{
145+
"cpu": 3,
146+
"memory": 4096,
147+
"nodes": 1,
148+
},
149+
},
150+
{
151+
name: "node with custom resources",
152+
node: test.BuildTestNode("test", 1000, 2048),
153+
crp: &fakeCustomResourcesProcessor{NodeResourceTargets: func(node *apiv1.Node) []customresources.CustomResourceTarget {
154+
return []customresources.CustomResourceTarget{
155+
{
156+
ResourceType: "gpu",
157+
ResourceCount: 1,
158+
},
159+
}
160+
}},
161+
wantDelta: resourceList{
162+
"cpu": 1,
163+
"memory": 2048,
164+
"gpu": 1,
165+
"nodes": 1,
166+
},
167+
},
168+
}
169+
for _, tc := range testCases {
170+
t.Run(tc.name, func(t *testing.T) {
171+
ctx := &context.AutoscalingContext{}
172+
delta, err := nodeResources(ctx, tc.crp, tc.node, nil)
173+
if err != nil {
174+
t.Errorf("nodeResources: unexpected error: %v", err)
175+
}
176+
if diff := cmp.Diff(tc.wantDelta, delta); diff != "" {
177+
t.Errorf("delta mismatch (-want +got):\n%s", diff)
178+
}
179+
})
180+
}
181+
}

cluster-autoscaler/resourcequotas/tracker.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@ limitations under the License.
1717
package resourcequotas
1818

1919
import (
20-
"fmt"
21-
2220
corev1 "k8s.io/api/core/v1"
2321
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
2422
"k8s.io/autoscaler/cluster-autoscaler/context"
25-
"k8s.io/autoscaler/cluster-autoscaler/core/utils"
26-
"k8s.io/autoscaler/cluster-autoscaler/processors/customresources"
2723
)
2824

2925
const (
@@ -173,25 +169,3 @@ type ExceededQuota struct {
173169
ID string
174170
ExceededResources []string
175171
}
176-
177-
// nodeResources calculates the amount of resources that will be used from the cluster when creating a node.
178-
func nodeResources(autoscalingCtx *context.AutoscalingContext, crp customresources.CustomResourcesProcessor, node *corev1.Node, nodeGroup cloudprovider.NodeGroup) (resourceList, error) {
179-
// TODO: storage?
180-
nodeCPU, nodeMemory := utils.GetNodeCoresAndMemory(node)
181-
nodeResources := resourceList{
182-
string(corev1.ResourceCPU): nodeCPU,
183-
string(corev1.ResourceMemory): nodeMemory,
184-
ResourceNodes: 1,
185-
}
186-
187-
resourceTargets, err := crp.GetNodeResourceTargets(autoscalingCtx, node, nodeGroup)
188-
if err != nil {
189-
return nil, fmt.Errorf("failed to get custom resources: %w", err)
190-
}
191-
192-
for _, resourceTarget := range resourceTargets {
193-
nodeResources[resourceTarget.ResourceType] = resourceTarget.ResourceCount
194-
}
195-
196-
return nodeResources, nil
197-
}

cluster-autoscaler/resourcequotas/tracker_test.go

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -312,65 +312,3 @@ func TestApplyDelta(t *testing.T) {
312312
})
313313
}
314314
}
315-
316-
func TestNodeResources(t *testing.T) {
317-
testCases := []struct {
318-
name string
319-
node *apiv1.Node
320-
crp customresources.CustomResourcesProcessor
321-
wantDelta resourceList
322-
}{
323-
{
324-
name: "node just with CPU and memory",
325-
node: test.BuildTestNode("test", 1000, 2048),
326-
crp: &fakeCustomResourcesProcessor{},
327-
wantDelta: resourceList{
328-
"cpu": 1,
329-
"memory": 2048,
330-
"nodes": 1,
331-
},
332-
},
333-
{
334-
// nodes should not have milliCPUs in the capacity, so we round it up
335-
// to the nearest integer.
336-
name: "node just with CPU and memory, milli cores rounded up",
337-
node: test.BuildTestNode("test", 2500, 4096),
338-
crp: &fakeCustomResourcesProcessor{},
339-
wantDelta: resourceList{
340-
"cpu": 3,
341-
"memory": 4096,
342-
"nodes": 1,
343-
},
344-
},
345-
{
346-
name: "node with custom resources",
347-
node: test.BuildTestNode("test", 1000, 2048),
348-
crp: &fakeCustomResourcesProcessor{NodeResourceTargets: func(node *apiv1.Node) []customresources.CustomResourceTarget {
349-
return []customresources.CustomResourceTarget{
350-
{
351-
ResourceType: "gpu",
352-
ResourceCount: 1,
353-
},
354-
}
355-
}},
356-
wantDelta: resourceList{
357-
"cpu": 1,
358-
"memory": 2048,
359-
"gpu": 1,
360-
"nodes": 1,
361-
},
362-
},
363-
}
364-
for _, tc := range testCases {
365-
t.Run(tc.name, func(t *testing.T) {
366-
ctx := &context.AutoscalingContext{}
367-
delta, err := nodeResources(ctx, tc.crp, tc.node, nil)
368-
if err != nil {
369-
t.Errorf("nodeResources: unexpected error: %v", err)
370-
}
371-
if diff := cmp.Diff(tc.wantDelta, delta); diff != "" {
372-
t.Errorf("delta mismatch (-want +got):\n%s", diff)
373-
}
374-
})
375-
}
376-
}

0 commit comments

Comments
 (0)