forked from kubernetes/autoscaler
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NodeInfo Processor for exclusive usage of template infos (with warnings)
This is a third attempt at kubernetes#1021 (might also provides an alternate solution for kubernetes#2892 and kubernetes#3608 amd kubernetes#3609). Some uses cases includes: balance Similar when uspscaling from zero, edited/updated ASGs/MIGs taints and labels, updated instance type. Per kubernetes#1021 discussion, a flag might be acceptable, if defaulting to false and describing the limitations (not all cloud providers) and the risk of using it (loss of accuracy, risk of upscaling unusable nodes or leaving pending pods). Per kubernetes#3609 discussion, using a NodeInfo processor is prefered.
- Loading branch information
Showing
6 changed files
with
180 additions
and
3 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
101 changes: 101 additions & 0 deletions
101
cluster-autoscaler/processors/nodeinfos/template_only_node_info_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,101 @@ | ||
/* | ||
Copyright 2021 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 ( | ||
"math/rand" | ||
"time" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider" | ||
"k8s.io/autoscaler/cluster-autoscaler/context" | ||
"k8s.io/autoscaler/cluster-autoscaler/core/utils" | ||
"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/v1alpha1" | ||
) | ||
|
||
const nodeInfoRefreshInterval = 15 * time.Minute | ||
|
||
type nodeInfoCacheEntry struct { | ||
nodeInfo *schedulerframework.NodeInfo | ||
lastRefresh time.Time | ||
} | ||
|
||
// TemplateOnlyNodeInfoProcessor return NodeInfos built from node group templates. | ||
type TemplateOnlyNodeInfoProcessor struct { | ||
nodeInfoCache map[string]*nodeInfoCacheEntry | ||
} | ||
|
||
// Process returns nodeInfos built from node groups templates. | ||
func (p *TemplateOnlyNodeInfoProcessor) Process(ctx *context.AutoscalingContext, nodeInfosForNodeGroups map[string]*schedulerframework.NodeInfo, daemonsets []*appsv1.DaemonSet, ignoredTaints taints.TaintKeySet) (map[string]*schedulerframework.NodeInfo, error) { | ||
result := make(map[string]*schedulerframework.NodeInfo) | ||
seenGroups := make(map[string]bool) | ||
|
||
for _, nodeGroup := range ctx.CloudProvider.NodeGroups() { | ||
id := nodeGroup.Id() | ||
seenGroups[id] = true | ||
|
||
splay := rand.New(rand.NewSource(time.Now().UnixNano())).Intn(int(nodeInfoRefreshInterval.Seconds() + 1)) | ||
lastRefresh := time.Now().Add(-time.Second * time.Duration(splay)) | ||
if ng, ok := p.nodeInfoCache[id]; ok { | ||
if ng.lastRefresh.Add(nodeInfoRefreshInterval).After(time.Now()) { | ||
result[id] = ng.nodeInfo | ||
continue | ||
} | ||
lastRefresh = time.Now() | ||
} | ||
|
||
nodeInfo, err := utils.GetNodeInfoFromTemplate(nodeGroup, daemonsets, ctx.PredicateChecker, ignoredTaints) | ||
if err != nil { | ||
if err == cloudprovider.ErrNotImplemented { | ||
klog.Warningf("Running in template only mode, but template isn't implemented for group %s", id) | ||
continue | ||
} else { | ||
klog.Errorf("Unable to build proper template node for %s: %v", id, err) | ||
return map[string]*schedulerframework.NodeInfo{}, | ||
errors.ToAutoscalerError(errors.CloudProviderError, err) | ||
} | ||
} | ||
|
||
p.nodeInfoCache[id] = &nodeInfoCacheEntry{ | ||
nodeInfo: nodeInfo, | ||
lastRefresh: lastRefresh, | ||
} | ||
result[id] = nodeInfo | ||
} | ||
|
||
for id := range p.nodeInfoCache { | ||
if _, ok := seenGroups[id]; !ok { | ||
delete(p.nodeInfoCache, id) | ||
} | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
// CleanUp cleans up processor's internal structures. | ||
func (p *TemplateOnlyNodeInfoProcessor) CleanUp() { | ||
} | ||
|
||
// NewTemplateOnlyNodeInfoProcessor returns a NodeInfoProcessor generating NodeInfos from node group templates. | ||
func NewTemplateOnlyNodeInfoProcessor() *TemplateOnlyNodeInfoProcessor { | ||
return &TemplateOnlyNodeInfoProcessor{ | ||
nodeInfoCache: make(map[string]*nodeInfoCacheEntry), | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
cluster-autoscaler/processors/nodeinfos/template_only_node_info_processor_test.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,63 @@ | ||
/* | ||
Copyright 2021 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 ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
testprovider "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/test" | ||
"k8s.io/autoscaler/cluster-autoscaler/context" | ||
"k8s.io/autoscaler/cluster-autoscaler/simulator" | ||
|
||
. "k8s.io/autoscaler/cluster-autoscaler/utils/test" | ||
|
||
schedulerframework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1" | ||
) | ||
|
||
func TestTemplateOnlyNodeInfoProcessorProcess(t *testing.T) { | ||
predicateChecker, err := simulator.NewTestPredicateChecker() | ||
assert.NoError(t, err) | ||
|
||
tni := schedulerframework.NewNodeInfo() | ||
tni.SetNode(BuildTestNode("tn", 100, 100)) | ||
|
||
provider1 := testprovider.NewTestAutoprovisioningCloudProvider( | ||
nil, nil, nil, nil, nil, | ||
map[string]*schedulerframework.NodeInfo{"ng1": tni, "ng2": tni}) | ||
provider1.AddNodeGroup("ng1", 1, 10, 1) | ||
provider1.AddNodeGroup("ng2", 2, 20, 2) | ||
|
||
ctx := &context.AutoscalingContext{ | ||
CloudProvider: provider1, | ||
PredicateChecker: predicateChecker, | ||
} | ||
|
||
processor := NewTemplateOnlyNodeInfoProcessor() | ||
res, err := processor.Process(ctx, nil, nil, nil) | ||
|
||
// nodegroups providing templates | ||
assert.NoError(t, err) | ||
assert.Equal(t, 2, len(res)) | ||
assert.Contains(t, res, "ng1") | ||
assert.Contains(t, res, "ng2") | ||
|
||
// nodegroup not providing templates | ||
provider1.AddNodeGroup("ng3", 0, 1000, 0) | ||
_, err = processor.Process(ctx, nil, nil, nil) | ||
assert.Error(t, err) | ||
} |