|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package resourcequotas |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/google/go-cmp/cmp" |
| 23 | + "github.com/stretchr/testify/mock" |
| 24 | + apiv1 "k8s.io/api/core/v1" |
| 25 | + "k8s.io/autoscaler/cluster-autoscaler/cloudprovider" |
| 26 | + cptest "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/test" |
| 27 | + "k8s.io/autoscaler/cluster-autoscaler/context" |
| 28 | + "k8s.io/autoscaler/cluster-autoscaler/processors/customresources" |
| 29 | + drasnapshot "k8s.io/autoscaler/cluster-autoscaler/simulator/dynamicresources/snapshot" |
| 30 | + "k8s.io/autoscaler/cluster-autoscaler/utils/errors" |
| 31 | + "k8s.io/autoscaler/cluster-autoscaler/utils/test" |
| 32 | +) |
| 33 | + |
| 34 | +type mockCustomResourcesProcessor struct { |
| 35 | + mock.Mock |
| 36 | +} |
| 37 | + |
| 38 | +func (m *mockCustomResourcesProcessor) FilterOutNodesWithUnreadyResources(_ *context.AutoscalingContext, allNodes, readyNodes []*apiv1.Node, _ *drasnapshot.Snapshot) ([]*apiv1.Node, []*apiv1.Node) { |
| 39 | + return allNodes, readyNodes |
| 40 | +} |
| 41 | + |
| 42 | +func (m *mockCustomResourcesProcessor) GetNodeResourceTargets(autoscalingCtx *context.AutoscalingContext, node *apiv1.Node, nodeGroup cloudprovider.NodeGroup) ([]customresources.CustomResourceTarget, errors.AutoscalerError) { |
| 43 | + args := m.Called(autoscalingCtx, node, nodeGroup) |
| 44 | + return args.Get(0).([]customresources.CustomResourceTarget), nil |
| 45 | +} |
| 46 | + |
| 47 | +func (m *mockCustomResourcesProcessor) CleanUp() { |
| 48 | + return |
| 49 | +} |
| 50 | + |
| 51 | +func TestNodeResourcesCache(t *testing.T) { |
| 52 | + node := test.BuildTestNode("n1", 1000, 2000) |
| 53 | + autoscalingCtx := &context.AutoscalingContext{} |
| 54 | + ng1 := cptest.NewTestNodeGroup("ng1", 1, 10, 1, true, false, "n1-template", nil, nil) |
| 55 | + ng2 := cptest.NewTestNodeGroup("ng2", 1, 10, 1, true, false, "n2-template", nil, nil) |
| 56 | + resourceTargets := []customresources.CustomResourceTarget{ |
| 57 | + {ResourceType: "gpu", ResourceCount: 1}, |
| 58 | + } |
| 59 | + wantResources := resourceList{"cpu": 1, "memory": 2000, "nodes": 1, "gpu": 1} |
| 60 | + |
| 61 | + type nodeResourcesCall struct { |
| 62 | + node *apiv1.Node |
| 63 | + nodeGroup cloudprovider.NodeGroup |
| 64 | + } |
| 65 | + |
| 66 | + testCases := []struct { |
| 67 | + name string |
| 68 | + calls []nodeResourcesCall |
| 69 | + setupCRPExpectations func(*mock.Mock) |
| 70 | + }{ |
| 71 | + { |
| 72 | + name: "cache hit", |
| 73 | + calls: []nodeResourcesCall{ |
| 74 | + {node: node, nodeGroup: ng1}, |
| 75 | + {node: node, nodeGroup: ng1}, |
| 76 | + }, |
| 77 | + setupCRPExpectations: func(m *mock.Mock) { |
| 78 | + m.On("GetNodeResourceTargets", autoscalingCtx, node, ng1).Return(resourceTargets, nil).Once() |
| 79 | + }, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "cache miss on different node group", |
| 83 | + calls: []nodeResourcesCall{ |
| 84 | + {node: node, nodeGroup: ng1}, |
| 85 | + {node: node, nodeGroup: ng2}, |
| 86 | + }, |
| 87 | + setupCRPExpectations: func(m *mock.Mock) { |
| 88 | + m.On("GetNodeResourceTargets", autoscalingCtx, node, ng1).Return(resourceTargets, nil).Once(). |
| 89 | + On("GetNodeResourceTargets", autoscalingCtx, node, ng2).Return(resourceTargets, nil).Once() |
| 90 | + }, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "no node group bypasses cache", |
| 94 | + calls: []nodeResourcesCall{ |
| 95 | + {node: node, nodeGroup: nil}, |
| 96 | + {node: node, nodeGroup: nil}, |
| 97 | + }, |
| 98 | + setupCRPExpectations: func(m *mock.Mock) { |
| 99 | + m.On("GetNodeResourceTargets", autoscalingCtx, node, nil).Return(resourceTargets, nil).Twice() |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | + for _, tc := range testCases { |
| 104 | + t.Run(tc.name, func(t *testing.T) { |
| 105 | + mockCRP := &mockCustomResourcesProcessor{} |
| 106 | + tc.setupCRPExpectations(&mockCRP.Mock) |
| 107 | + nc := newNodeResourcesCache(mockCRP) |
| 108 | + for _, call := range tc.calls { |
| 109 | + resources, err := nc.nodeResources(autoscalingCtx, call.node, call.nodeGroup) |
| 110 | + if err != nil { |
| 111 | + t.Fatalf("nodeResources unexpected error: %v", err) |
| 112 | + } |
| 113 | + if diff := cmp.Diff(wantResources, resources); diff != "" { |
| 114 | + t.Errorf("nodeResources mismatch (-want, +got):\n%s", diff) |
| 115 | + } |
| 116 | + } |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
| 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 | +} |
0 commit comments