-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Expand file tree
/
Copy pathautoscaler.go
More file actions
180 lines (169 loc) · 7.38 KB
/
autoscaler.go
File metadata and controls
180 lines (169 loc) · 7.38 KB
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
/*
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 core
import (
"context"
"strings"
"time"
cqv1alpha1 "k8s.io/autoscaler/cluster-autoscaler/apis/capacityquota/autoscaling.x-k8s.io/v1alpha1"
cloudBuilder "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/builder"
ca_context "k8s.io/autoscaler/cluster-autoscaler/context"
coreoptions "k8s.io/autoscaler/cluster-autoscaler/core/options"
"k8s.io/autoscaler/cluster-autoscaler/core/scaledown/pdb"
"k8s.io/autoscaler/cluster-autoscaler/core/utils"
"k8s.io/autoscaler/cluster-autoscaler/estimator"
"k8s.io/autoscaler/cluster-autoscaler/expander/factory"
"k8s.io/autoscaler/cluster-autoscaler/observers/loopstart"
ca_processors "k8s.io/autoscaler/cluster-autoscaler/processors"
"k8s.io/autoscaler/cluster-autoscaler/resourcequotas"
"k8s.io/autoscaler/cluster-autoscaler/resourcequotas/capacityquota"
"k8s.io/autoscaler/cluster-autoscaler/simulator/clustersnapshot/predicate"
"k8s.io/autoscaler/cluster-autoscaler/simulator/clustersnapshot/store"
csinodeprovider "k8s.io/autoscaler/cluster-autoscaler/simulator/csi/provider"
"k8s.io/autoscaler/cluster-autoscaler/simulator/drainability/rules"
draprovider "k8s.io/autoscaler/cluster-autoscaler/simulator/dynamicresources/provider"
"k8s.io/autoscaler/cluster-autoscaler/simulator/framework"
"k8s.io/autoscaler/cluster-autoscaler/utils/backoff"
"k8s.io/autoscaler/cluster-autoscaler/utils/errors"
"k8s.io/client-go/informers"
)
// Autoscaler is the main component of CA which scales up/down node groups according to its configuration
// The configuration can be injected at the creation of an autoscaler
type Autoscaler interface {
// Start starts components running in background.
Start() error
// RunOnce represents an iteration in the control-loop of CA
RunOnce(currentTime time.Time) errors.AutoscalerError
// ExitCleanUp is a clean-up performed just before process termination.
ExitCleanUp()
// LastScaleUpTime is a time of the last scale up
LastScaleUpTime() time.Time
// LastScaleDownDeleteTime is a time of the last scale down
LastScaleDownDeleteTime() time.Time
}
// NewAutoscaler creates an autoscaler of an appropriate type according to the parameters
func NewAutoscaler(ctx context.Context, opts coreoptions.AutoscalerOptions, informerFactory informers.SharedInformerFactory) (Autoscaler, errors.AutoscalerError) {
err := initializeDefaultOptions(ctx, &opts, informerFactory)
if err != nil {
return nil, errors.ToAutoscalerError(errors.InternalError, err)
}
return NewStaticAutoscaler(
opts.AutoscalingOptions,
opts.FrameworkHandle,
opts.ClusterSnapshot,
opts.AutoscalingKubeClients,
opts.Processors,
opts.LoopStartNotifier,
opts.CloudProvider,
opts.ExpanderStrategy,
opts.EstimatorBuilder,
opts.Backoff,
opts.DebuggingSnapshotter,
opts.RemainingPdbTracker,
opts.ScaleUpOrchestrator,
opts.DeleteOptions,
opts.DrainabilityRules,
opts.DraProvider,
opts.QuotasTrackerOptions,
opts.CSIProvider,
opts.CapacityBufferPodsRegistry,
), nil
}
// Initialize default options if not provided.
func initializeDefaultOptions(ctx context.Context, opts *coreoptions.AutoscalerOptions, informerFactory informers.SharedInformerFactory) error {
if opts.Processors == nil {
opts.Processors = ca_processors.DefaultProcessors(opts.AutoscalingOptions)
}
if opts.LoopStartNotifier == nil {
opts.LoopStartNotifier = loopstart.NewObserversList(nil)
}
if opts.AutoscalingKubeClients == nil {
opts.AutoscalingKubeClients = ca_context.NewAutoscalingKubeClients(ctx, opts.AutoscalingOptions, opts.KubeClient, opts.InformerFactory)
}
if opts.FrameworkHandle == nil {
fwHandle, err := framework.NewHandle(ctx, opts.InformerFactory, opts.SchedulerConfig, opts.DynamicResourceAllocationEnabled, opts.CSINodeAwareSchedulingEnabled, opts.FastPredicatesEnabled)
if err != nil {
return err
}
opts.FrameworkHandle = fwHandle
}
if opts.ClusterSnapshot == nil {
opts.ClusterSnapshot = predicate.NewPredicateSnapshot(store.NewBasicSnapshotStore(opts.FastPredicatesEnabled), opts.FrameworkHandle, opts.DynamicResourceAllocationEnabled, opts.PredicateParallelism, opts.CSINodeAwareSchedulingEnabled, opts.FastPredicatesEnabled)
}
if opts.RemainingPdbTracker == nil {
opts.RemainingPdbTracker = pdb.NewBasicRemainingPdbTracker()
}
if opts.EstimatorBuilder == nil {
thresholds := []estimator.Threshold{
estimator.NewStaticThreshold(opts.MaxNodesPerScaleUp, opts.MaxNodeGroupBinpackingDuration),
estimator.NewSngCapacityThreshold(),
estimator.NewClusterCapacityThreshold(),
}
estimatorBuilder, err := estimator.NewEstimatorBuilder(
opts.EstimatorName,
estimator.NewThresholdBasedEstimationLimiter(thresholds),
estimator.NewDecreasingPodOrderer(),
/* EstimationAnalyserFunc */ nil,
opts.FastpathBinpackingEnabled,
)
if err != nil {
return err
}
opts.EstimatorBuilder = estimatorBuilder
}
if opts.Backoff == nil {
opts.Backoff =
backoff.NewIdBasedExponentialBackoff(opts.InitialNodeGroupBackoffDuration, opts.MaxNodeGroupBackoffDuration, opts.NodeGroupBackoffResetTimeout)
}
if opts.DrainabilityRules == nil {
opts.DrainabilityRules = rules.Default(opts.DeleteOptions)
}
if opts.DraProvider == nil && opts.DynamicResourceAllocationEnabled {
opts.DraProvider = draprovider.NewProviderFromInformers(informerFactory)
}
if opts.CloudProvider == nil {
opts.CloudProvider = cloudBuilder.NewCloudProvider(opts, informerFactory)
}
if opts.ExpanderStrategy == nil {
expanderFactory := factory.NewFactory()
expanderFactory.RegisterDefaultExpanders(opts.CloudProvider, opts.AutoscalingKubeClients, opts.KubeClient, opts.ConfigNamespace, opts.GRPCExpanderCert, opts.GRPCExpanderURL)
expanderStrategy, err := expanderFactory.Build(strings.Split(opts.ExpanderNames, ","))
if err != nil {
return err
}
opts.ExpanderStrategy = expanderStrategy
}
if opts.QuotasTrackerOptions.QuotaProvider == nil {
providers := []resourcequotas.Provider{resourcequotas.NewCloudQuotasProvider(opts.CloudProvider)}
if opts.CapacityQuotasEnabled {
// register informer here to disable lazy initialization
if _, err := opts.KubeCache.GetInformer(context.TODO(), &cqv1alpha1.CapacityQuota{}); err != nil {
return err
}
providers = append(providers, capacityquota.NewCapacityQuotasProvider(opts.KubeClientNew))
}
opts.QuotasTrackerOptions.QuotaProvider = resourcequotas.NewCombinedQuotasProvider(providers)
}
if opts.QuotasTrackerOptions.CustomResourcesProcessor == nil {
opts.QuotasTrackerOptions.CustomResourcesProcessor = opts.Processors.CustomResourcesProcessor
}
if opts.QuotasTrackerOptions.NodeFilter == nil {
virtualKubeletNodeFilter := utils.VirtualKubeletNodeFilter{}
opts.QuotasTrackerOptions.NodeFilter = resourcequotas.NewCombinedNodeFilter([]resourcequotas.NodeFilter{virtualKubeletNodeFilter})
}
if opts.CSINodeAwareSchedulingEnabled {
csiProvider := csinodeprovider.NewCSINodeProviderFromInformers(informerFactory)
opts.CSIProvider = csiProvider
}
return nil
}