-
Notifications
You must be signed in to change notification settings - Fork 4k
/
updater.go
303 lines (269 loc) · 10.7 KB
/
updater.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
/*
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 logic
import (
"context"
"fmt"
"time"
"golang.org/x/time/rate"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
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_lister "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/client/listers/autoscaling.k8s.io/v1"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/target"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/updater/eviction"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/updater/priority"
metrics_updater "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/metrics/updater"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/status"
vpa_api_util "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa"
kube_client "k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/kubernetes/scheme"
clientv1 "k8s.io/client-go/kubernetes/typed/core/v1"
v1lister "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
)
// Updater performs updates on pods if recommended by Vertical Pod Autoscaler
type Updater interface {
// RunOnce represents single iteration in the main-loop of Updater
RunOnce(context.Context)
}
type updater struct {
vpaLister vpa_lister.VerticalPodAutoscalerLister
podLister v1lister.PodLister
eventRecorder record.EventRecorder
evictionFactory eviction.PodsEvictionRestrictionFactory
recommendationProcessor vpa_api_util.RecommendationProcessor
evictionAdmission priority.PodEvictionAdmission
priorityProcessor priority.PriorityProcessor
evictionRateLimiter *rate.Limiter
selectorFetcher target.VpaTargetSelectorFetcher
useAdmissionControllerStatus bool
statusValidator status.Validator
}
// NewUpdater creates Updater with given configuration
func NewUpdater(
kubeClient kube_client.Interface,
vpaClient *vpa_clientset.Clientset,
minReplicasForEvicition int,
evictionRateLimit float64,
evictionRateBurst int,
evictionToleranceFraction float64,
useAdmissionControllerStatus bool,
statusNamespace string,
recommendationProcessor vpa_api_util.RecommendationProcessor,
evictionAdmission priority.PodEvictionAdmission,
selectorFetcher target.VpaTargetSelectorFetcher,
priorityProcessor priority.PriorityProcessor,
namespace string,
) (Updater, error) {
evictionRateLimiter := getRateLimiter(evictionRateLimit, evictionRateBurst)
factory, err := eviction.NewPodsEvictionRestrictionFactory(kubeClient, minReplicasForEvicition, evictionToleranceFraction)
if err != nil {
return nil, fmt.Errorf("Failed to create eviction restriction factory: %v", err)
}
return &updater{
vpaLister: vpa_api_util.NewVpasLister(vpaClient, make(chan struct{}), namespace),
podLister: newPodLister(kubeClient, namespace),
eventRecorder: newEventRecorder(kubeClient),
evictionFactory: factory,
recommendationProcessor: recommendationProcessor,
evictionRateLimiter: evictionRateLimiter,
evictionAdmission: evictionAdmission,
priorityProcessor: priorityProcessor,
selectorFetcher: selectorFetcher,
useAdmissionControllerStatus: useAdmissionControllerStatus,
statusValidator: status.NewValidator(
kubeClient,
status.AdmissionControllerStatusName,
statusNamespace,
),
}, nil
}
// RunOnce represents single iteration in the main-loop of Updater
func (u *updater) RunOnce(ctx context.Context) {
timer := metrics_updater.NewExecutionTimer()
defer timer.ObserveTotal()
if u.useAdmissionControllerStatus {
isValid, err := u.statusValidator.IsStatusValid(status.AdmissionControllerStatusTimeout)
if err != nil {
klog.Errorf("Error getting Admission Controller status: %v. Skipping eviction loop", err)
return
}
if !isValid {
klog.Warningf("Admission Controller status has been refreshed more than %v ago. Skipping eviction loop",
status.AdmissionControllerStatusTimeout)
return
}
}
vpaList, err := u.vpaLister.List(labels.Everything())
if err != nil {
klog.Fatalf("failed get VPA list: %v", err)
}
timer.ObserveStep("ListVPAs")
vpas := make([]*vpa_api_util.VpaWithSelector, 0)
for _, vpa := range vpaList {
if vpa_api_util.GetUpdateMode(vpa) != vpa_types.UpdateModeRecreate &&
vpa_api_util.GetUpdateMode(vpa) != vpa_types.UpdateModeAuto {
klog.V(3).Infof("skipping VPA object %v because its mode is not \"Recreate\" or \"Auto\"", vpa.Name)
continue
}
selector, err := u.selectorFetcher.Fetch(vpa)
if err != nil {
klog.V(3).Infof("skipping VPA object %v because we cannot fetch selector", vpa.Name)
continue
}
vpas = append(vpas, &vpa_api_util.VpaWithSelector{
Vpa: vpa,
Selector: selector,
})
}
if len(vpas) == 0 {
klog.Warningf("no VPA objects to process")
if u.evictionAdmission != nil {
u.evictionAdmission.CleanUp()
}
return
}
podsList, err := u.podLister.List(labels.Everything())
if err != nil {
klog.Errorf("failed to get pods list: %v", err)
return
}
timer.ObserveStep("ListPods")
allLivePods := filterDeletedPods(podsList)
controlledPods := make(map[*vpa_types.VerticalPodAutoscaler][]*apiv1.Pod)
for _, pod := range allLivePods {
controllingVPA := vpa_api_util.GetControllingVPAForPod(pod, vpas)
if controllingVPA != nil {
controlledPods[controllingVPA.Vpa] = append(controlledPods[controllingVPA.Vpa], pod)
}
}
timer.ObserveStep("FilterPods")
if u.evictionAdmission != nil {
u.evictionAdmission.LoopInit(allLivePods, controlledPods)
}
timer.ObserveStep("AdmissionInit")
// wrappers for metrics which are computed every loop run
controlledPodsCounter := metrics_updater.NewControlledPodsCounter()
evictablePodsCounter := metrics_updater.NewEvictablePodsCounter()
vpasWithEvictablePodsCounter := metrics_updater.NewVpasWithEvictablePodsCounter()
vpasWithEvictedPodsCounter := metrics_updater.NewVpasWithEvictedPodsCounter()
// using defer to protect against 'return' after evictionRateLimiter.Wait
defer controlledPodsCounter.Observe()
defer evictablePodsCounter.Observe()
defer vpasWithEvictablePodsCounter.Observe()
defer vpasWithEvictedPodsCounter.Observe()
// NOTE: this loop assumes that controlledPods are filtered
// to contain only Pods controlled by a VPA in auto or recreate mode
for vpa, livePods := range controlledPods {
vpaSize := len(livePods)
controlledPodsCounter.Add(vpaSize, vpaSize)
evictionLimiter := u.evictionFactory.NewPodsEvictionRestriction(livePods, vpa)
podsForUpdate := u.getPodsUpdateOrder(filterNonEvictablePods(livePods, evictionLimiter), vpa)
evictablePodsCounter.Add(vpaSize, len(podsForUpdate))
withEvictable := false
withEvicted := false
for _, pod := range podsForUpdate {
withEvictable = true
if !evictionLimiter.CanEvict(pod) {
continue
}
err := u.evictionRateLimiter.Wait(ctx)
if err != nil {
klog.Warningf("evicting pod %v failed: %v", pod.Name, err)
return
}
klog.V(2).Infof("evicting pod %v", pod.Name)
evictErr := evictionLimiter.Evict(pod, u.eventRecorder)
if evictErr != nil {
klog.Warningf("evicting pod %v failed: %v", pod.Name, evictErr)
} else {
withEvicted = true
metrics_updater.AddEvictedPod(vpaSize)
}
}
if withEvictable {
vpasWithEvictablePodsCounter.Add(vpaSize, 1)
}
if withEvicted {
vpasWithEvictedPodsCounter.Add(vpaSize, 1)
}
}
timer.ObserveStep("EvictPods")
}
func getRateLimiter(evictionRateLimit float64, evictionRateLimitBurst int) *rate.Limiter {
var evictionRateLimiter *rate.Limiter
if evictionRateLimit <= 0 {
// As a special case if the rate is set to rate.Inf, the burst rate is ignored
// see https://github.com/golang/time/blob/master/rate/rate.go#L37
evictionRateLimiter = rate.NewLimiter(rate.Inf, 0)
klog.V(1).Info("Rate limit disabled")
} else {
evictionRateLimiter = rate.NewLimiter(rate.Limit(evictionRateLimit), evictionRateLimitBurst)
}
return evictionRateLimiter
}
// getPodsUpdateOrder returns list of pods that should be updated ordered by update priority
func (u *updater) getPodsUpdateOrder(pods []*apiv1.Pod, vpa *vpa_types.VerticalPodAutoscaler) []*apiv1.Pod {
priorityCalculator := priority.NewUpdatePriorityCalculator(
vpa,
nil,
u.recommendationProcessor,
u.priorityProcessor)
for _, pod := range pods {
priorityCalculator.AddPod(pod, time.Now())
}
return priorityCalculator.GetSortedPods(u.evictionAdmission)
}
func filterNonEvictablePods(pods []*apiv1.Pod, evictionRestriciton eviction.PodsEvictionRestriction) []*apiv1.Pod {
result := make([]*apiv1.Pod, 0)
for _, pod := range pods {
if evictionRestriciton.CanEvict(pod) {
result = append(result, pod)
}
}
return result
}
func filterDeletedPods(pods []*apiv1.Pod) []*apiv1.Pod {
result := make([]*apiv1.Pod, 0)
for _, pod := range pods {
if pod.DeletionTimestamp == nil {
result = append(result, pod)
}
}
return result
}
func newPodLister(kubeClient kube_client.Interface, namespace string) v1lister.PodLister {
selector := fields.ParseSelectorOrDie("spec.nodeName!=" + "" + ",status.phase!=" +
string(apiv1.PodSucceeded) + ",status.phase!=" + string(apiv1.PodFailed))
podListWatch := cache.NewListWatchFromClient(kubeClient.CoreV1().RESTClient(), "pods", apiv1.NamespaceAll, selector)
store := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
podLister := v1lister.NewPodLister(store)
podReflector := cache.NewReflector(podListWatch, &apiv1.Pod{}, store, time.Hour)
stopCh := make(chan struct{})
go podReflector.Run(stopCh)
return podLister
}
func newEventRecorder(kubeClient kube_client.Interface) record.EventRecorder {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(klog.V(4).Infof)
if _, isFake := kubeClient.(*fake.Clientset); !isFake {
eventBroadcaster.StartRecordingToSink(&clientv1.EventSinkImpl{Interface: clientv1.New(kubeClient.CoreV1().RESTClient()).Events("")})
}
return eventBroadcaster.NewRecorder(scheme.Scheme, apiv1.EventSource{Component: "vpa-updater"})
}