-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NodeInfo processor to Refine synthetic NodeInfos
Comparing synthetic NodeInfos obtained from nodegroup's TemplateInfo() to NodeInfos obtained from real-world nodes is bound to fail, even with kube reservations provided through nodegroups labels/annotations (for instance: kernel mem reservation is hard to predict). This makes `balance-similar-node-groups` likely to misbehave when `scale-up-from-zero` is enabled (and a first nodegroup gets a real node), for instance. Following [Maciek Pytel suggestion](#3608 (comment)) (from discussions on a previous attempt at solving this), we can implement a NodeInfo Processor that would improve template-generated NodeInfos whenever a node was created off a similar nodegroup. We're storing node's virtual origin through machineid, which works fine but is a bit ugly (suggestions welcome). Tested this solves balance-similar-node-groups + scale-up-from-zero, with various instance types on AWS and GCP. Previous attempts to solve that issue/discussions: * #2892 (comment) * #3608 (comment)
- Loading branch information
Showing
6 changed files
with
351 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
cluster-autoscaler/processors/nodeinfos/refine_node_infos_processor.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/* | ||
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 nodeinfos | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider" | ||
"k8s.io/autoscaler/cluster-autoscaler/context" | ||
"k8s.io/autoscaler/cluster-autoscaler/core/utils" | ||
"k8s.io/autoscaler/cluster-autoscaler/processors/nodegroupset" | ||
"k8s.io/autoscaler/cluster-autoscaler/utils/errors" | ||
"k8s.io/autoscaler/cluster-autoscaler/utils/taints" | ||
klog "k8s.io/klog/v2" | ||
|
||
schedulerframework "k8s.io/kubernetes/pkg/scheduler/framework" | ||
) | ||
|
||
const templateNodeInfoCacheTTL = 10 * time.Minute | ||
|
||
// RefineNodeInfosProcessor improves NodeInfos accuracy where possible. | ||
// For now it adjusts synthetic NodeInfos generated by TemplateInfo() with more accurate | ||
// NodeInfos obtained from real-world nodes for similar nodegroups (when available). | ||
type RefineNodeInfosProcessor struct { | ||
NodeGroupSetProcessor nodegroupset.NodeGroupSetProcessor | ||
RefineUsingSimilarNodeGroups bool | ||
nodeInfoCache nodeInfoCache | ||
} | ||
|
||
type nodeInfoCache struct { | ||
sync.RWMutex | ||
nodeInfos map[string]*schedulerframework.NodeInfo | ||
lastUpdated time.Time | ||
} | ||
|
||
// Process refines nodeInfos obtained by TemplateInfos when possible | ||
func (p *RefineNodeInfosProcessor) Process(ctx *context.AutoscalingContext, nodeInfosForNodeGroups map[string]*schedulerframework.NodeInfo) (map[string]*schedulerframework.NodeInfo, error) { | ||
inaccurateNodeInfos := make(map[string]*schedulerframework.NodeInfo) | ||
templatesNodeInfo := make(map[string]*schedulerframework.NodeInfo) | ||
|
||
// for now this processor only supports refining by leveraging similar nodegroups | ||
if !p.RefineUsingSimilarNodeGroups { | ||
return nodeInfosForNodeGroups, nil | ||
} | ||
|
||
nodeGroups := make(map[string]cloudprovider.NodeGroup) | ||
for _, nodeGroup := range ctx.CloudProvider.NodeGroups() { | ||
nodeGroups[nodeGroup.Id()] = nodeGroup | ||
} | ||
|
||
if err := p.nodeInfoCache.update(nodeGroups); err != nil { | ||
return nodeInfosForNodeGroups, err | ||
} | ||
|
||
ignoredTaints := make(taints.TaintKeySet) | ||
for _, taintKey := range ctx.IgnoredTaints { | ||
ignoredTaints[taintKey] = true | ||
} | ||
|
||
// build comparable (all from templates) NodeInfos | ||
for groupID, nodeInfo := range nodeInfosForNodeGroups { | ||
templatedNodeInfo, ok := p.nodeInfoCache.nodeInfos[groupID] | ||
if !ok || templatedNodeInfo == nil { | ||
continue | ||
} | ||
|
||
templatesNodeInfo[groupID] = templatedNodeInfo | ||
|
||
if utils.IsNodeInfoBuiltFromTemplate(nodeInfo) { | ||
// use the provided nodeInfo rather than the one we just generated: | ||
// we want to keep the original daemonsets pods in refined template. | ||
inaccurateNodeInfos[groupID] = nodeInfo | ||
} | ||
} | ||
|
||
// refine inaccurate nodeinfos when we can find similar nodeInfos built from real nodes | ||
for groupID, nodeInfo := range inaccurateNodeInfos { | ||
nodeGroup, ok := nodeGroups[groupID] | ||
if !ok { | ||
continue | ||
} | ||
|
||
similars, err := p.NodeGroupSetProcessor.FindSimilarNodeGroups(ctx, nodeGroup, templatesNodeInfo) | ||
if err != nil { | ||
klog.Warningf("Failed to lookup for matching node groups for %s: %v", groupID, err) | ||
return nodeInfosForNodeGroups, nil | ||
} | ||
|
||
for _, nodeGroup := range similars { | ||
similarInfo, found := nodeInfosForNodeGroups[nodeGroup.Id()] | ||
if !found || utils.IsNodeInfoBuiltFromTemplate(similarInfo) { | ||
continue | ||
} | ||
refinedNodeInfo, err := refineNodeInfoUsingSimilarGroup(nodeInfo, similarInfo, groupID, ignoredTaints) | ||
if err != nil { | ||
return nodeInfosForNodeGroups, err | ||
} | ||
nodeInfosForNodeGroups[groupID] = refinedNodeInfo | ||
break | ||
} | ||
} | ||
|
||
return nodeInfosForNodeGroups, nil | ||
} | ||
|
||
// CleanUp cleans up processor's internal structures. | ||
func (p *RefineNodeInfosProcessor) CleanUp() { | ||
} | ||
|
||
func refineNodeInfoUsingSimilarGroup(nodeInfo, similarInfo *schedulerframework.NodeInfo, nodeGroupName string, ignoredTaints taints.TaintKeySet) (*schedulerframework.NodeInfo, errors.AutoscalerError) { | ||
nodeInfoCopy := nodeInfo.Clone() | ||
|
||
// original node identity and locality (region, zone, ...) must be retained | ||
newNode := similarInfo.Node().DeepCopy() | ||
if newNode.Labels == nil { | ||
newNode.Labels = make(map[string]string) | ||
} | ||
for label, val := range nodeInfo.Node().GetLabels() { | ||
if _, found := nodegroupset.BasicIgnoredLabels[label]; found { | ||
newNode.Labels[label] = val | ||
} | ||
} | ||
newNode.Name = nodeInfo.Node().Name | ||
newNode.UID = nodeInfo.Node().UID | ||
nodeInfoCopy.SetNode(newNode) | ||
|
||
utils.SetNodeInfoBuiltFromTemplate(nodeInfoCopy) | ||
return utils.SanitizeNodeInfo(nodeInfoCopy, nodeGroupName, ignoredTaints) | ||
} | ||
|
||
func (n *nodeInfoCache) update(nodeGroups map[string]cloudprovider.NodeGroup) error { | ||
n.Lock() | ||
defer n.Unlock() | ||
|
||
if len(n.nodeInfos) == 0 { | ||
n.nodeInfos = make(map[string]*schedulerframework.NodeInfo) | ||
n.lastUpdated = time.Now() | ||
} | ||
needsRefresh := n.lastUpdated.Add(templateNodeInfoCacheTTL).Before(time.Now()) | ||
|
||
for groupID := range n.nodeInfos { | ||
if _, ok := nodeGroups[groupID]; !ok { | ||
delete(n.nodeInfos, groupID) | ||
} | ||
} | ||
|
||
for groupID, nodeGroup := range nodeGroups { | ||
cachedNodeInfo, ok := n.nodeInfos[groupID] | ||
if ok && !needsRefresh && !utils.IsNodeInfoBuiltFromTemplate(cachedNodeInfo) { | ||
continue | ||
} | ||
|
||
nodeInfo, err := nodeGroup.TemplateNodeInfo() | ||
if err != nil { | ||
if err == cloudprovider.ErrNotImplemented { | ||
continue | ||
} else { | ||
return errors.ToAutoscalerError(errors.CloudProviderError, err) | ||
} | ||
} | ||
n.nodeInfos[groupID] = nodeInfo | ||
} | ||
|
||
if needsRefresh { | ||
n.lastUpdated = time.Now() | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.