-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
magnum_manager_impl.go
510 lines (426 loc) · 18.1 KB
/
magnum_manager_impl.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
Copyright 2020 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 magnum
import (
"fmt"
"sort"
"strings"
"github.com/gofrs/uuid"
apiv1 "k8s.io/api/core/v1"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/magnum/gophercloud"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/magnum/gophercloud/openstack/containerinfra/v1/clusters"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/magnum/gophercloud/openstack/containerinfra/v1/nodegroups"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/magnum/gophercloud/openstack/orchestration/v1/stackresources"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/magnum/gophercloud/openstack/orchestration/v1/stacks"
"k8s.io/autoscaler/cluster-autoscaler/config"
klog "k8s.io/klog/v2"
)
type nodeGroupStacks struct {
stackID string
stackName string
kubeMinionsStackID string
kubeMinionsStackName string
}
// magnumManagerImpl implements the magnumManager interface.
type magnumManagerImpl struct {
clusterClient *gophercloud.ServiceClient
heatClient *gophercloud.ServiceClient
clusterName string
stackInfo map[string]nodeGroupStacks
providerIDToNodeGroupCache map[string]string
}
// createMagnumManagerImpl creates an instance of magnumManagerImpl.
func createMagnumManagerImpl(clusterClient, heatClient *gophercloud.ServiceClient, opts config.AutoscalingOptions) (*magnumManagerImpl, error) {
manager := magnumManagerImpl{
clusterClient: clusterClient,
heatClient: heatClient,
clusterName: opts.ClusterName,
stackInfo: make(map[string]nodeGroupStacks),
providerIDToNodeGroupCache: make(map[string]string),
}
return &manager, nil
}
func uniqueName(ng *nodegroups.NodeGroup) string {
name := ng.Name
id := ng.UUID
idSegment := strings.Split(id, "-")[0]
uniqueName := fmt.Sprintf("%s-%s", name, idSegment)
return uniqueName
}
func (mgr *magnumManagerImpl) uniqueNameAndIDForNodeGroup(nodegroup string) (string, string, error) {
ng, err := nodegroups.Get(mgr.clusterClient, mgr.clusterName, nodegroup).Extract()
if err != nil {
return "", "", fmt.Errorf("could not get node group: %v", err)
}
uniqueName := uniqueName(ng)
return uniqueName, ng.UUID, nil
}
// fetchNodeGroupStackIDs fetches and caches the IDs and names of the
// nodegroup Heat stack and the related kube_minions stack.
//
// Calling this a second time for the same node group will return the
// cached result.
func (mgr *magnumManagerImpl) fetchNodeGroupStackIDs(nodegroup string) (nodeGroupStacks, error) {
if stacks, ok := mgr.stackInfo[nodegroup]; ok {
// Stack info has already been fetched
return stacks, nil
}
// Get the node group.
ng, err := nodegroups.Get(mgr.clusterClient, mgr.clusterName, nodegroup).Extract()
if err != nil {
return nodeGroupStacks{}, fmt.Errorf("could not get node group: %v", err)
}
stackID := ng.StackID
// Get the Heat stack for the node group.
stack, err := stacks.Find(mgr.heatClient, stackID).Extract()
if err != nil {
return nodeGroupStacks{}, fmt.Errorf("could not find node group stack: %v", err)
}
stackName := stack.Name
// Get the kube_minions stack resource for the node group stack.
minionsResource, err := stackresources.Get(mgr.heatClient, stackName, stackID, "kube_minions").Extract()
if err != nil {
return nodeGroupStacks{}, fmt.Errorf("could not get node group stack kube_minions resource: %v", err)
}
minionsStackID := minionsResource.PhysicalID
// Get the stack for that resource.
minionsStack, err := stacks.Find(mgr.heatClient, minionsStackID).Extract()
if err != nil {
return nodeGroupStacks{}, fmt.Errorf("could not find node group kube_minions stack: %v", err)
}
minionsStackName := minionsStack.Name
mgr.stackInfo[nodegroup] = nodeGroupStacks{
stackID: stackID,
stackName: stackName,
kubeMinionsStackID: minionsStackID,
kubeMinionsStackName: minionsStackName,
}
return mgr.stackInfo[nodegroup], nil
}
// autoDiscoverNodeGroups lists all node groups that belong to this cluster
// and finds the ones which are valid for autoscaling and that match the
// auto discovery configuration.
func (mgr *magnumManagerImpl) autoDiscoverNodeGroups(cfgs []magnumAutoDiscoveryConfig) ([]*nodegroups.NodeGroup, error) {
ngs := []*nodegroups.NodeGroup{}
pages, err := nodegroups.List(mgr.clusterClient, mgr.clusterName, nodegroups.ListOpts{}).AllPages()
if err != nil {
return nil, fmt.Errorf("could not fetch node group pages: %v", err)
}
groups, err := nodegroups.ExtractNodeGroups(pages)
if err != nil {
return nil, fmt.Errorf("could not extract node groups: %v", err)
}
for _, group := range groups {
if group.Role == "master" {
// Don't yet support autoscaling for master node groups.
continue
}
// Listing node groups does not return the min/max node count,
// have to use a Get for those properties.
detail, err := nodegroups.Get(mgr.clusterClient, mgr.clusterName, group.UUID).Extract()
if err != nil {
return nil, fmt.Errorf("could not get detail for node group %s: %v", group.Name, err)
}
// Max node count must be set to be eligible for autoscaling.
if detail.MaxNodeCount == nil {
klog.V(4).Infof("Node group %s does not have max node count set", detail.Name)
continue
}
// The group must match at least one auto discovery config.
var matchesAny bool
for _, cfg := range cfgs {
for _, role := range cfg.Roles {
if detail.Role == role {
matchesAny = true
}
}
}
if !matchesAny {
klog.V(2).Infof("Node group %s has max node count set but does not match any auto discovery configs", detail.Name)
continue
}
ngs = append(ngs, detail)
}
return ngs, nil
}
// nodeGroupSize gets the current node count of the given node group.
func (mgr *magnumManagerImpl) nodeGroupSize(nodegroup string) (int, error) {
ng, err := nodegroups.Get(mgr.clusterClient, mgr.clusterName, nodegroup).Extract()
if err != nil {
return 0, fmt.Errorf("could not get node group: %v", err)
}
return ng.NodeCount, nil
}
// updateNodeCount performs a cluster resize targeting the given node group.
func (mgr *magnumManagerImpl) updateNodeCount(nodegroup string, nodes int) error {
resizeOpts := clusters.ResizeOpts{
NodeCount: &nodes,
NodeGroup: nodegroup,
}
resizeResult := clusters.Resize(mgr.clusterClient, mgr.clusterName, resizeOpts)
_, err := resizeResult.Extract()
if err != nil {
return fmt.Errorf("could not resize cluster: %v", err)
}
return nil
}
// getNodes returns Instances with ProviderIDs and running states
// of all nodes that exist in OpenStack for a node group.
func (mgr *magnumManagerImpl) getNodes(nodegroup string) ([]cloudprovider.Instance, error) {
var nodes []cloudprovider.Instance
stackInfo, err := mgr.fetchNodeGroupStackIDs(nodegroup)
if err != nil {
return nil, fmt.Errorf("could not fetch stack IDs for node group %s: %v", nodegroup, err)
}
minionResourcesPages, err := stackresources.List(mgr.heatClient, stackInfo.kubeMinionsStackName, stackInfo.kubeMinionsStackID, nil).AllPages()
if err != nil {
return nil, fmt.Errorf("could not list minion resources: %v", err)
}
minionResources, err := stackresources.ExtractResources(minionResourcesPages)
if err != nil {
return nil, fmt.Errorf("could not extract minion resources: %v", err)
}
stack, err := stacks.Get(mgr.heatClient, stackInfo.kubeMinionsStackName, stackInfo.kubeMinionsStackID).Extract()
if err != nil {
return nil, fmt.Errorf("could not get kube_minions stack from heat: %v", err)
}
// mapping from minion index to server ID e.g
// "0": "4c30961a-6e2f-42be-be01-5270e1546a89"
//
// The value in refs_map goes through several stages:
// 1. The initial value is the node index (same as the key).
// 2. It then changes to be "kube-minion".
// 3. When a server has been created it changes to the server ID.
refsMap := make(map[string]string)
for _, output := range stack.Outputs {
if output["output_key"] == "refs_map" {
refsMapOutput := output["output_value"].(map[string]interface{})
for index, ID := range refsMapOutput {
refsMap[index] = ID.(string)
}
}
}
for _, minion := range minionResources {
// Prepare fake provider ID in the format "fake:///nodegroup/index" in case the minion does not yet have a server ID in refs_map.
// This fake provider ID is necessary to have in case a server can not be created (e.g quota exceeded).
// The minion.Name is its index e.g "2".
fakeName := fmt.Sprintf("fake:///%s/%s", nodegroup, minion.Name)
instance := cloudprovider.Instance{Id: fakeName, Status: &cloudprovider.InstanceStatus{}}
switch minion.Status {
case "DELETE_COMPLETE":
// Don't return this instance
continue
case "DELETE_IN_PROGRESS":
serverID, found := refsMap[minion.Name]
if !found || serverID == "kube-minion" {
// If a server ID can't be found for this minion, assume it is already deleted.
klog.V(4).Infof("Minion %q is DELETE_IN_PROGRESS but has no refs_map entry", minion.Name)
continue
}
instance.Id = fmt.Sprintf("openstack:///%s", serverID)
instance.Status.State = cloudprovider.InstanceDeleting
case "INIT_COMPLETE", "CREATE_IN_PROGRESS":
instance.Status.State = cloudprovider.InstanceCreating
case "UPDATE_IN_PROGRESS":
// UPDATE_IN_PROGRESS can either be a creating node that was moved to updating by a separate
// stack update before going to a complete status, or an old node that has already completed and is
// only temporarily in an updating status.
// We need to differentiate between these two states for the instance status.
// If the minion is not yet in the refs_map it must still be creating.
serverID, found := refsMap[minion.Name]
if !found || serverID == "kube-minion" {
instance.Status.State = cloudprovider.InstanceCreating
klog.V(4).Infof("Minion %q is UPDATE_IN_PROGRESS but has no refs_map entry", minion.Name)
break
}
instance.Id = fmt.Sprintf("openstack:///%s", serverID)
// Otherwise, have to check the stack resources for this minion, as they do not change even when the stack is updated.
// There are several resources but the two important ones are kube-minion (provisioning the server)
// and node_config_deployment (the heat-container-agent running on the node).
// If these two are CREATE_COMPLETE then this node must be a running node, not one being created.
minionStackID := minion.PhysicalID
// Only the stack ID is known, not the stack name, so this operation has to be a Find.
minionResources, err := stackresources.Find(mgr.heatClient, minionStackID).Extract()
if err != nil {
return nil, fmt.Errorf("could not get stack resources for minion %q", minion.Name)
}
// The Find returns a list of all resources for this node, we have to loop through and find
// the right statuses.
var minionServerStatus string
var minionNodeDeploymentStatus string
for _, resource := range minionResources {
switch resource.Name {
case "kube-minion":
minionServerStatus = resource.Status
case "node_config_deployment":
minionNodeDeploymentStatus = resource.Status
}
}
if minionServerStatus == "CREATE_COMPLETE" && minionNodeDeploymentStatus == "CREATE_COMPLETE" {
// The minion is one that is already running.
klog.V(4).Infof("Minion %q in UPDATE_IN_PROGRESS is an already running node", minion.Name)
instance.Status.State = cloudprovider.InstanceRunning
} else {
// The minion is one that is still being created.
klog.V(4).Infof("Minion %q in UPDATE_IN_PROGRESS is a new node", minion.Name)
instance.Status.State = cloudprovider.InstanceCreating
}
case "CREATE_FAILED", "UPDATE_FAILED":
instance.Status.State = cloudprovider.InstanceCreating
errorClass := cloudprovider.OtherErrorClass
// Check if the error message is for exceeding the project quota.
if strings.Contains(strings.ToLower(minion.StatusReason), "quota") {
errorClass = cloudprovider.OutOfResourcesErrorClass
}
instance.Status.ErrorInfo = &cloudprovider.InstanceErrorInfo{
ErrorClass: errorClass,
ErrorMessage: minion.StatusReason,
}
klog.V(3).Infof("Instance %s failed with reason: %s", minion.Name, minion.StatusReason)
case "CREATE_COMPLETE", "UPDATE_COMPLETE":
if serverID, found := refsMap[minion.Name]; found && serverID != "kube-minion" {
instance.Id = fmt.Sprintf("openstack:///%s", serverID)
}
instance.Status.State = cloudprovider.InstanceRunning
default:
// If the minion is in an unknown state.
klog.V(3).Infof("Ignoring minion %s in state %s", minion.Name, minion.Status)
continue
}
nodes = append(nodes, instance)
}
return nodes, nil
}
// deleteNodes deletes nodes by resizing the cluster to a smaller size
// and specifying which nodes should be removed.
//
// The nodes are referenced by server ID for nodes which have them,
// or by the minion index for nodes which are creating or in an error state.
func (mgr *magnumManagerImpl) deleteNodes(nodegroup string, nodes []NodeRef, updatedNodeCount int) error {
var nodesToRemove []string
for _, nodeRef := range nodes {
if nodeRef.IsFake {
_, index, err := parseFakeProviderID(nodeRef.Name)
if err != nil {
return fmt.Errorf("error handling fake node: %v", err)
}
nodesToRemove = append(nodesToRemove, index)
continue
}
klog.V(2).Infof("manager deleting node: %s", nodeRef.Name)
nodesToRemove = append(nodesToRemove, nodeRef.SystemUUID)
}
resizeOpts := clusters.ResizeOpts{
NodeCount: &updatedNodeCount,
NodesToRemove: nodesToRemove,
NodeGroup: nodegroup,
}
klog.V(2).Infof("resizeOpts: node_count=%d, remove=%v", *resizeOpts.NodeCount, resizeOpts.NodesToRemove)
resizeResult := clusters.Resize(mgr.clusterClient, mgr.clusterName, resizeOpts)
_, err := resizeResult.Extract()
if err != nil {
return fmt.Errorf("could not resize cluster: %v", err)
}
return nil
}
// nodeGroupForNode returns the UUID of the node group that the given node is a member of.
func (mgr *magnumManagerImpl) nodeGroupForNode(node *apiv1.Node) (string, error) {
if groupUUID, ok := mgr.providerIDToNodeGroupCache[node.Spec.ProviderID]; ok {
klog.V(5).Infof("nodeGroupForNode: already cached %s in node group %s", node.Spec.ProviderID, groupUUID)
return groupUUID, nil
}
// There are two possibilities for "fake" nodes:
// * The node is in creation and has a provider ID like fake:///
// * The node has just been created but is not yet registered in kubernetes,
// in which case it is "fake" but has an openstack:/// provider ID as it
// does exist in OpenStack.
// Only the first case needs to be handled specially.
if isFakeNode(node) && strings.HasPrefix(node.Spec.ProviderID, "fake:///") {
groupUUID, _, err := parseFakeProviderID(node.Spec.ProviderID)
if err != nil {
return "", err
}
klog.V(5).Infof("nodeGroupForNode: parsed fake node, %s in node group %s", node.Spec.ProviderID, groupUUID)
return groupUUID, nil
}
// Otherwise, have to loop through all node groups and check the stack of each one.
pages, err := nodegroups.List(mgr.clusterClient, mgr.clusterName, nodegroups.ListOpts{}).AllPages()
if err != nil {
return "", fmt.Errorf("could not fetch node group pages: %v", err)
}
allNodeGroups, err := nodegroups.ExtractNodeGroups(pages)
if err != nil {
return "", fmt.Errorf("could not extract node groups: %v", err)
}
allNodeGroupUUIDs := []string{}
nodeGroupSizes := make(map[string]int)
for _, group := range allNodeGroups {
if group.Role == "master" {
continue
}
allNodeGroupUUIDs = append(allNodeGroupUUIDs, group.UUID)
nodeGroupSizes[group.UUID] = group.NodeCount
}
// Sort the node groups IDs to put the groups with the highest number of nodes first.
// This should maximise the chance of finding the node early.
sort.Slice(allNodeGroupUUIDs, func(i, j int) bool {
return nodeGroupSizes[allNodeGroupUUIDs[i]] > nodeGroupSizes[allNodeGroupUUIDs[j]]
})
for _, ngUUID := range allNodeGroupUUIDs {
klog.V(5).Infof("Checking node group %s, size %d", ngUUID, nodeGroupSizes[ngUUID])
stackInfo, err := mgr.fetchNodeGroupStackIDs(ngUUID)
if err != nil {
return "", fmt.Errorf("could not fetch stack IDs for node group %s: %v", ngUUID, err)
}
minionsStack, err := stacks.Get(mgr.heatClient, stackInfo.kubeMinionsStackName, stackInfo.kubeMinionsStackID).Extract()
if err != nil {
return "", fmt.Errorf("could not get minions stack: %v", err)
}
for _, output := range minionsStack.Outputs {
if output["output_key"] == "refs_map" {
refsMapOutput, ok := output["output_value"].(map[string]interface{})
if !ok {
// Output value was nil, possibly because the node group is being deleted.
return "", fmt.Errorf("could not check the minions stack refs_map")
}
// Temporarily hold the result and loop over the entire refs_map
// instead of returning instantly, to cache as much as possible.
var found bool
for _, ID := range refsMapOutput {
ID := ID.(string)
// Check that the ID is a proper server UUID.
// (It could be an index or "kube-minion" in the refs_map instead).
_, err := uuid.FromString(ID)
if err != nil {
continue
}
// Convert to a providerID and cache it as belonging to this node group.
providerID := fmt.Sprintf("openstack:///%s", ID)
mgr.providerIDToNodeGroupCache[providerID] = ngUUID
// If the node matches this provider ID, then remember that this is
// the correct node group but keep looping through the refs_map
// to cache other providerIDs.
if providerID == node.Spec.ProviderID {
found = true
}
}
if found {
klog.V(5).Infof("nodeGroupForNode: found node %s in node group %s", node.Spec.ProviderID, ngUUID)
return ngUUID, nil
}
}
}
}
return "", fmt.Errorf("could not find node group for node %s", node.Spec.ProviderID)
}