forked from kubernetes/autoscaler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cloud_provider.go
375 lines (320 loc) · 11.8 KB
/
test_cloud_provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package test
import (
"fmt"
"sync"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
"k8s.io/autoscaler/cluster-autoscaler/utils/errors"
schedulercache "k8s.io/kubernetes/pkg/scheduler/cache"
)
// OnScaleUpFunc is a function called on node group increase in TestCloudProvider.
// First parameter is the NodeGroup id, second is the increase delta.
type OnScaleUpFunc func(string, int) error
// OnScaleDownFunc is a function called on cluster scale down
type OnScaleDownFunc func(string, string) error
// OnNodeGroupCreateFunc is a function called when a new node group is created.
type OnNodeGroupCreateFunc func(string) error
// OnNodeGroupDeleteFunc is a function called when a node group is deleted.
type OnNodeGroupDeleteFunc func(string) error
// TestCloudProvider is a dummy cloud provider to be used in tests.
type TestCloudProvider struct {
sync.Mutex
nodes map[string]string
groups map[string]cloudprovider.NodeGroup
onScaleUp func(string, int) error
onScaleDown func(string, string) error
onNodeGroupCreate func(string) error
onNodeGroupDelete func(string) error
machineTypes []string
machineTemplates map[string]*schedulercache.NodeInfo
resourceLimiter *cloudprovider.ResourceLimiter
}
// NewTestCloudProvider builds new TestCloudProvider
func NewTestCloudProvider(onScaleUp OnScaleUpFunc, onScaleDown OnScaleDownFunc) *TestCloudProvider {
return &TestCloudProvider{
nodes: make(map[string]string),
groups: make(map[string]cloudprovider.NodeGroup),
onScaleUp: onScaleUp,
onScaleDown: onScaleDown,
resourceLimiter: cloudprovider.NewResourceLimiter(make(map[string]int64), make(map[string]int64)),
}
}
// NewTestAutoprovisioningCloudProvider builds new TestCloudProvider with autoprovisioning support
func NewTestAutoprovisioningCloudProvider(onScaleUp OnScaleUpFunc, onScaleDown OnScaleDownFunc,
onNodeGroupCreate OnNodeGroupCreateFunc, onNodeGroupDelete OnNodeGroupDeleteFunc,
machineTypes []string, machineTemplates map[string]*schedulercache.NodeInfo) *TestCloudProvider {
return &TestCloudProvider{
nodes: make(map[string]string),
groups: make(map[string]cloudprovider.NodeGroup),
onScaleUp: onScaleUp,
onScaleDown: onScaleDown,
onNodeGroupCreate: onNodeGroupCreate,
onNodeGroupDelete: onNodeGroupDelete,
machineTypes: machineTypes,
machineTemplates: machineTemplates,
resourceLimiter: cloudprovider.NewResourceLimiter(make(map[string]int64), make(map[string]int64)),
}
}
// Name returns name of the cloud provider.
func (tcp *TestCloudProvider) Name() string {
return "TestCloudProvider"
}
// NodeGroups returns all node groups configured for this cloud provider.
func (tcp *TestCloudProvider) NodeGroups() []cloudprovider.NodeGroup {
tcp.Lock()
defer tcp.Unlock()
result := make([]cloudprovider.NodeGroup, 0)
for _, group := range tcp.groups {
result = append(result, group)
}
return result
}
// GetNodeGroup returns node group with the given name.
func (tcp *TestCloudProvider) GetNodeGroup(name string) cloudprovider.NodeGroup {
tcp.Lock()
defer tcp.Unlock()
return tcp.groups[name]
}
// NodeGroupForNode returns the node group for the given node, nil if the node
// should not be processed by cluster autoscaler, or non-nil error if such
// occurred.
func (tcp *TestCloudProvider) NodeGroupForNode(node *apiv1.Node) (cloudprovider.NodeGroup, error) {
tcp.Lock()
defer tcp.Unlock()
groupName, found := tcp.nodes[node.Name]
if !found {
return nil, nil
}
group, found := tcp.groups[groupName]
if !found {
return nil, nil
}
return group, nil
}
// Pricing returns pricing model for this cloud provider or error if not available.
func (tcp *TestCloudProvider) Pricing() (cloudprovider.PricingModel, errors.AutoscalerError) {
return nil, cloudprovider.ErrNotImplemented
}
// GetAvailableMachineTypes get all machine types that can be requested from the cloud provider.
func (tcp *TestCloudProvider) GetAvailableMachineTypes() ([]string, error) {
return tcp.machineTypes, nil
}
// NewNodeGroup builds a theoretical node group based on the node definition provided. The node group is not automatically
// created on the cloud provider side. The node group is not returned by NodeGroups() until it is created.
func (tcp *TestCloudProvider) NewNodeGroup(machineType string, labels map[string]string, systemLabels map[string]string,
taints []apiv1.Taint, extraResources map[string]resource.Quantity) (cloudprovider.NodeGroup, error) {
return &TestNodeGroup{
cloudProvider: tcp,
id: "autoprovisioned-" + machineType,
minSize: 0,
maxSize: 1000,
targetSize: 0,
exist: false,
autoprovisioned: true,
machineType: machineType,
}, nil
}
// AddNodeGroup adds node group to test cloud provider.
func (tcp *TestCloudProvider) AddNodeGroup(id string, min int, max int, size int) {
tcp.Lock()
defer tcp.Unlock()
tcp.groups[id] = &TestNodeGroup{
cloudProvider: tcp,
id: id,
minSize: min,
maxSize: max,
targetSize: size,
exist: true,
autoprovisioned: false,
}
}
// AddAutoprovisionedNodeGroup adds node group to test cloud provider.
func (tcp *TestCloudProvider) AddAutoprovisionedNodeGroup(id string, min int, max int, size int, machineType string) {
tcp.Lock()
defer tcp.Unlock()
tcp.groups[id] = &TestNodeGroup{
cloudProvider: tcp,
id: id,
minSize: min,
maxSize: max,
targetSize: size,
exist: true,
autoprovisioned: true,
machineType: machineType,
}
}
// AddNode adds the given node to the group.
func (tcp *TestCloudProvider) AddNode(nodeGroupId string, node *apiv1.Node) {
tcp.Lock()
defer tcp.Unlock()
tcp.nodes[node.Name] = nodeGroupId
}
// GetResourceLimiter returns struct containing limits (max, min) for resources (cores, memory etc.).
func (tcp *TestCloudProvider) GetResourceLimiter() (*cloudprovider.ResourceLimiter, error) {
return tcp.resourceLimiter, nil
}
// SetResourceLimiter sets resource limiter.
func (tcp *TestCloudProvider) SetResourceLimiter(resourceLimiter *cloudprovider.ResourceLimiter) {
tcp.resourceLimiter = resourceLimiter
}
// Cleanup this is a function to close resources associated with the cloud provider
func (tcp *TestCloudProvider) Cleanup() error {
return nil
}
// Refresh is called before every main loop and can be used to dynamically update cloud provider state.
// In particular the list of node groups returned by NodeGroups can change as a result of CloudProvider.Refresh().
func (tcp *TestCloudProvider) Refresh() error {
return nil
}
// TestNodeGroup is a node group used by TestCloudProvider.
type TestNodeGroup struct {
sync.Mutex
cloudProvider *TestCloudProvider
id string
maxSize int
minSize int
targetSize int
exist bool
autoprovisioned bool
machineType string
}
// MaxSize returns maximum size of the node group.
func (tng *TestNodeGroup) MaxSize() int {
tng.Lock()
defer tng.Unlock()
return tng.maxSize
}
// MinSize returns minimum size of the node group.
func (tng *TestNodeGroup) MinSize() int {
tng.Lock()
defer tng.Unlock()
return tng.minSize
}
// TargetSize returns the current target size of the node group. It is possible that the
// number of nodes in Kubernetes is different at the moment but should be equal
// to Size() once everything stabilizes (new nodes finish startup and registration or
// removed nodes are deleted completely)
func (tng *TestNodeGroup) TargetSize() (int, error) {
tng.Lock()
defer tng.Unlock()
return tng.targetSize, nil
}
// SetTargetSize sets target size for group. Function is used only in tests.
func (tng *TestNodeGroup) SetTargetSize(size int) {
tng.Lock()
defer tng.Unlock()
tng.targetSize = size
}
// IncreaseSize increases the size of the node group. To delete a node you need
// to explicitly name it and use DeleteNode. This function should wait until
// node group size is updated.
func (tng *TestNodeGroup) IncreaseSize(delta int) error {
tng.Lock()
tng.targetSize += delta
tng.Unlock()
return tng.cloudProvider.onScaleUp(tng.id, delta)
}
// Exist checks if the node group really exists on the cloud provider side. Allows to tell the
// theoretical node group from the real one.
func (tng *TestNodeGroup) Exist() bool {
tng.Lock()
defer tng.Unlock()
return tng.exist
}
// Create creates the node group on the cloud provider side.
func (tng *TestNodeGroup) Create() error {
if tng.Exist() {
return fmt.Errorf("Group already exist")
}
tng.cloudProvider.AddAutoprovisionedNodeGroup(tng.id, tng.minSize, tng.maxSize, 0, tng.machineType)
return tng.cloudProvider.onNodeGroupCreate(tng.id)
}
// Delete deletes the node group on the cloud provider side.
// This will be executed only for autoprovisioned node groups, once their size drops to 0.
func (tng *TestNodeGroup) Delete() error {
return tng.cloudProvider.onNodeGroupDelete(tng.id)
}
// DecreaseTargetSize decreases the target size of the node group. This function
// doesn't permit to delete any existing node and can be used only to reduce the
// request for new nodes that have not been yet fulfilled. Delta should be negative.
func (tng *TestNodeGroup) DecreaseTargetSize(delta int) error {
tng.Lock()
tng.targetSize += delta
tng.Unlock()
return tng.cloudProvider.onScaleUp(tng.id, delta)
}
// DeleteNodes deletes nodes from this node group. Error is returned either on
// failure or if the given node doesn't belong to this node group. This function
// should wait until node group size is updated.
func (tng *TestNodeGroup) DeleteNodes(nodes []*apiv1.Node) error {
tng.Lock()
id := tng.id
tng.targetSize -= len(nodes)
tng.Unlock()
for _, node := range nodes {
err := tng.cloudProvider.onScaleDown(id, node.Name)
if err != nil {
return err
}
}
return nil
}
// Id returns an unique identifier of the node group.
func (tng *TestNodeGroup) Id() string {
tng.Lock()
defer tng.Unlock()
return tng.id
}
// Debug returns a string containing all information regarding this node group.
func (tng *TestNodeGroup) Debug() string {
tng.Lock()
defer tng.Unlock()
return fmt.Sprintf("%s target:%d min:%d max:%d", tng.id, tng.targetSize, tng.minSize, tng.maxSize)
}
// Nodes returns a list of all nodes that belong to this node group.
func (tng *TestNodeGroup) Nodes() ([]string, error) {
tng.Lock()
defer tng.Unlock()
result := make([]string, 0)
for node, nodegroup := range tng.cloudProvider.nodes {
if nodegroup == tng.id {
result = append(result, node)
}
}
return result, nil
}
// Autoprovisioned returns true if the node group is autoprovisioned.
func (tng *TestNodeGroup) Autoprovisioned() bool {
return tng.autoprovisioned
}
// TemplateNodeInfo returns a node template for this node group.
func (tng *TestNodeGroup) TemplateNodeInfo() (*schedulercache.NodeInfo, error) {
if tng.cloudProvider.machineTemplates == nil {
return nil, cloudprovider.ErrNotImplemented
}
if tng.autoprovisioned {
template, found := tng.cloudProvider.machineTemplates[tng.machineType]
if !found {
return nil, fmt.Errorf("No template declared for %s", tng.machineType)
}
return template, nil
}
template, found := tng.cloudProvider.machineTemplates[tng.id]
if !found {
return nil, fmt.Errorf("No template declared for %s", tng.id)
}
return template, nil
}