-
Notifications
You must be signed in to change notification settings - Fork 4k
/
recommender.go
262 lines (230 loc) · 10.8 KB
/
recommender.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
/*
Copyright 2017 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 routines
import (
"context"
"flag"
"time"
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
vpa_clientset "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/client/clientset/versioned"
vpa_api "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/client/clientset/versioned/typed/autoscaling.k8s.io/v1"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/recommender/checkpoint"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/recommender/input"
controllerfetcher "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/recommender/input/controller_fetcher"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/recommender/logic"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/recommender/model"
metrics_recommender "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/metrics/recommender"
vpa_utils "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa"
"k8s.io/client-go/informers"
kube_client "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
)
const (
// AggregateContainerStateGCInterval defines how often expired AggregateContainerStates are garbage collected.
AggregateContainerStateGCInterval = 1 * time.Hour
scaleCacheEntryLifetime time.Duration = time.Hour
scaleCacheEntryFreshnessTime time.Duration = 10 * time.Minute
scaleCacheEntryJitterFactor float64 = 1.
defaultResyncPeriod time.Duration = 10 * time.Minute
)
var (
checkpointsWriteTimeout = flag.Duration("checkpoints-timeout", time.Minute, `Timeout for writing checkpoints since the start of the recommender's main loop`)
minCheckpointsPerRun = flag.Int("min-checkpoints", 10, "Minimum number of checkpoints to write per recommender's main loop")
memorySaver = flag.Bool("memory-saver", false, `If true, only track pods which have an associated VPA`)
)
// Recommender recommend resources for certain containers, based on utilization periodically got from metrics api.
type Recommender interface {
// RunOnce performs one iteration of recommender duties followed by update of recommendations in VPA objects.
RunOnce()
// GetClusterState returns ClusterState used by Recommender
GetClusterState() *model.ClusterState
// GetClusterStateFeeder returns ClusterStateFeeder used by Recommender
GetClusterStateFeeder() input.ClusterStateFeeder
// UpdateVPAs computes recommendations and sends VPAs status updates to API Server
UpdateVPAs()
// MaintainCheckpoints stores current checkpoints in API Server and garbage collect old ones
// MaintainCheckpoints writes at least minCheckpoints if there are more checkpoints to write.
// Checkpoints are written until ctx permits or all checkpoints are written.
MaintainCheckpoints(ctx context.Context, minCheckpoints int)
}
type recommender struct {
clusterState *model.ClusterState
clusterStateFeeder input.ClusterStateFeeder
checkpointWriter checkpoint.CheckpointWriter
checkpointsGCInterval time.Duration
controllerFetcher controllerfetcher.ControllerFetcher
lastCheckpointGC time.Time
vpaClient vpa_api.VerticalPodAutoscalersGetter
podResourceRecommender logic.PodResourceRecommender
useCheckpoints bool
lastAggregateContainerStateGC time.Time
}
func (r *recommender) GetClusterState() *model.ClusterState {
return r.clusterState
}
func (r *recommender) GetClusterStateFeeder() input.ClusterStateFeeder {
return r.clusterStateFeeder
}
// Updates VPA CRD objects' statuses.
func (r *recommender) UpdateVPAs() {
cnt := metrics_recommender.NewObjectCounter()
defer cnt.Observe()
for _, observedVpa := range r.clusterState.ObservedVpas {
key := model.VpaID{
Namespace: observedVpa.Namespace,
VpaName: observedVpa.Name,
}
vpa, found := r.clusterState.Vpas[key]
if !found {
continue
}
resources := r.podResourceRecommender.GetRecommendedPodResources(GetContainerNameToAggregateStateMap(vpa))
had := vpa.HasRecommendation()
vpa.UpdateRecommendation(getCappedRecommendation(vpa.ID, resources, observedVpa.Spec.ResourcePolicy))
if vpa.HasRecommendation() && !had {
metrics_recommender.ObserveRecommendationLatency(vpa.Created)
}
hasMatchingPods := vpa.PodCount > 0
vpa.UpdateConditions(hasMatchingPods)
if err := r.clusterState.RecordRecommendation(vpa, time.Now()); err != nil {
klog.Warningf("%v", err)
if klog.V(4).Enabled() {
klog.Infof("VPA dump")
klog.Infof("%+v", vpa)
klog.Infof("HasMatchingPods: %v", hasMatchingPods)
klog.Infof("PodCount: %v", vpa.PodCount)
pods := r.clusterState.GetMatchingPods(vpa)
klog.Infof("MatchingPods: %+v", pods)
if len(pods) != vpa.PodCount {
klog.Errorf("ClusterState pod count and matching pods disagree for vpa %v/%v", vpa.ID.Namespace, vpa.ID.VpaName)
}
}
}
cnt.Add(vpa)
_, err := vpa_utils.UpdateVpaStatusIfNeeded(
r.vpaClient.VerticalPodAutoscalers(vpa.ID.Namespace), vpa.ID.VpaName, vpa.AsStatus(), &observedVpa.Status)
if err != nil {
klog.Errorf(
"Cannot update VPA %v object. Reason: %+v", vpa.ID.VpaName, err)
}
}
}
// getCappedRecommendation creates a recommendation based on recommended pod
// resources, setting the UncappedTarget to the calculated recommended target
// and if necessary, capping the Target, LowerBound and UpperBound according
// to the ResourcePolicy.
func getCappedRecommendation(vpaID model.VpaID, resources logic.RecommendedPodResources,
policy *vpa_types.PodResourcePolicy) *vpa_types.RecommendedPodResources {
containerResources := make([]vpa_types.RecommendedContainerResources, 0, len(resources))
for containerName, res := range resources {
containerResources = append(containerResources, vpa_types.RecommendedContainerResources{
ContainerName: containerName,
Target: model.ResourcesAsResourceList(res.Target),
LowerBound: model.ResourcesAsResourceList(res.LowerBound),
UpperBound: model.ResourcesAsResourceList(res.UpperBound),
UncappedTarget: model.ResourcesAsResourceList(res.Target),
})
}
recommendation := &vpa_types.RecommendedPodResources{
ContainerRecommendations: containerResources,
}
cappedRecommendation, err := vpa_utils.ApplyVPAPolicy(recommendation, policy)
if err != nil {
klog.Errorf("Failed to apply policy for VPA %v/%v: %v", vpaID.Namespace, vpaID.VpaName, err)
return recommendation
}
return cappedRecommendation
}
func (r *recommender) MaintainCheckpoints(ctx context.Context, minCheckpointsPerRun int) {
now := time.Now()
if r.useCheckpoints {
if err := r.checkpointWriter.StoreCheckpoints(ctx, now, minCheckpointsPerRun); err != nil {
klog.Warningf("Failed to store checkpoints. Reason: %+v", err)
}
if time.Since(r.lastCheckpointGC) > r.checkpointsGCInterval {
r.lastCheckpointGC = now
r.clusterStateFeeder.GarbageCollectCheckpoints()
}
}
}
func (r *recommender) RunOnce() {
timer := metrics_recommender.NewExecutionTimer()
defer timer.ObserveTotal()
ctx := context.Background()
ctx, cancelFunc := context.WithDeadline(ctx, time.Now().Add(*checkpointsWriteTimeout))
defer cancelFunc()
klog.V(3).Infof("Recommender Run")
r.clusterStateFeeder.LoadVPAs()
timer.ObserveStep("LoadVPAs")
r.clusterStateFeeder.LoadPods()
timer.ObserveStep("LoadPods")
r.clusterStateFeeder.LoadRealTimeMetrics()
timer.ObserveStep("LoadMetrics")
klog.V(3).Infof("ClusterState is tracking %v PodStates and %v VPAs", len(r.clusterState.Pods), len(r.clusterState.Vpas))
r.UpdateVPAs()
timer.ObserveStep("UpdateVPAs")
r.MaintainCheckpoints(ctx, *minCheckpointsPerRun)
timer.ObserveStep("MaintainCheckpoints")
r.clusterState.RateLimitedGarbageCollectAggregateCollectionStates(time.Now(), r.controllerFetcher)
timer.ObserveStep("GarbageCollect")
klog.V(3).Infof("ClusterState is tracking %d aggregated container states", r.clusterState.StateMapSize())
}
// RecommenderFactory makes instances of Recommender.
type RecommenderFactory struct {
ClusterState *model.ClusterState
ClusterStateFeeder input.ClusterStateFeeder
ControllerFetcher controllerfetcher.ControllerFetcher
CheckpointWriter checkpoint.CheckpointWriter
PodResourceRecommender logic.PodResourceRecommender
VpaClient vpa_api.VerticalPodAutoscalersGetter
CheckpointsGCInterval time.Duration
UseCheckpoints bool
}
// Make creates a new recommender instance,
// which can be run in order to provide continuous resource recommendations for containers.
func (c RecommenderFactory) Make() Recommender {
recommender := &recommender{
clusterState: c.ClusterState,
clusterStateFeeder: c.ClusterStateFeeder,
checkpointWriter: c.CheckpointWriter,
checkpointsGCInterval: c.CheckpointsGCInterval,
controllerFetcher: c.ControllerFetcher,
useCheckpoints: c.UseCheckpoints,
vpaClient: c.VpaClient,
podResourceRecommender: c.PodResourceRecommender,
lastAggregateContainerStateGC: time.Now(),
lastCheckpointGC: time.Now(),
}
klog.V(3).Infof("New Recommender created %+v", recommender)
return recommender
}
// NewRecommender creates a new recommender instance.
// Dependencies are created automatically.
// Deprecated; use RecommenderFactory instead.
func NewRecommender(config *rest.Config, checkpointsGCInterval time.Duration, useCheckpoints bool, namespace string) Recommender {
clusterState := model.NewClusterState(AggregateContainerStateGCInterval)
kubeClient := kube_client.NewForConfigOrDie(config)
factory := informers.NewSharedInformerFactoryWithOptions(kubeClient, defaultResyncPeriod, informers.WithNamespace(namespace))
controllerFetcher := controllerfetcher.NewControllerFetcher(config, kubeClient, factory, scaleCacheEntryFreshnessTime, scaleCacheEntryLifetime, scaleCacheEntryJitterFactor)
return RecommenderFactory{
ClusterState: clusterState,
ClusterStateFeeder: input.NewClusterStateFeeder(config, clusterState, *memorySaver, namespace),
ControllerFetcher: controllerFetcher,
CheckpointWriter: checkpoint.NewCheckpointWriter(clusterState, vpa_clientset.NewForConfigOrDie(config).AutoscalingV1()),
VpaClient: vpa_clientset.NewForConfigOrDie(config).AutoscalingV1(),
PodResourceRecommender: logic.CreatePodResourceRecommender(),
CheckpointsGCInterval: checkpointsGCInterval,
UseCheckpoints: useCheckpoints,
}.Make()
}