-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathcluster.go
448 lines (405 loc) · 16.5 KB
/
cluster.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
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 model
import (
"fmt"
"time"
apiv1 "k8s.io/api/core/v1"
labels "k8s.io/apimachinery/pkg/labels"
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
vpa_utils "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa"
"k8s.io/klog"
)
const (
// RecommendationMissingMaxDuration is maximum time that we accept the recommendation can be missing.
RecommendationMissingMaxDuration = 30 * time.Minute
)
// ClusterState holds all runtime information about the cluster required for the
// VPA operations, i.e. configuration of resources (pods, containers,
// VPA objects), aggregated utilization of compute resources (CPU, memory) and
// events (container OOMs).
// All input to the VPA Recommender algorithm lives in this structure.
type ClusterState struct {
// Pods in the cluster.
Pods map[PodID]*PodState
// VPA objects in the cluster.
Vpas map[VpaID]*Vpa
// VPA objects in the cluster that have no recommendation mapped to the first
// time we've noticed the recommendation missing or last time we logged
// a warning about it.
EmptyVPAs map[VpaID]time.Time
// Observed VPAs. Used to check if there are updates needed.
ObservedVpas []*vpa_types.VerticalPodAutoscaler
// All container aggregations where the usage samples are stored.
aggregateStateMap aggregateContainerStatesMap
// Map with all label sets used by the aggregations. It serves as a cache
// that allows to quickly access labels.Set corresponding to a labelSetKey.
labelSetMap labelSetMap
}
// StateMapSize is the number of pods being tracked by the VPA
func (cluster *ClusterState) StateMapSize() int {
return len(cluster.aggregateStateMap)
}
// AggregateStateKey determines the set of containers for which the usage samples
// are kept aggregated in the model.
type AggregateStateKey interface {
Namespace() string
ContainerName() string
Labels() labels.Labels
}
// String representation of the labels.LabelSet. This is the value returned by
// labelSet.String(). As opposed to the LabelSet object, it can be used as a map key.
type labelSetKey string
// Map of label sets keyed by their string representation.
type labelSetMap map[labelSetKey]labels.Set
// AggregateContainerStatesMap is a map from AggregateStateKey to AggregateContainerState.
type aggregateContainerStatesMap map[AggregateStateKey]*AggregateContainerState
// PodState holds runtime information about a single Pod.
type PodState struct {
// Unique id of the Pod.
ID PodID
// Set of labels attached to the Pod.
labelSetKey labelSetKey
// Containers that belong to the Pod, keyed by the container name.
Containers map[string]*ContainerState
// PodPhase describing current life cycle phase of the Pod.
Phase apiv1.PodPhase
}
// NewClusterState returns a new ClusterState with no pods.
func NewClusterState() *ClusterState {
return &ClusterState{
Pods: make(map[PodID]*PodState),
Vpas: make(map[VpaID]*Vpa),
EmptyVPAs: make(map[VpaID]time.Time),
aggregateStateMap: make(aggregateContainerStatesMap),
labelSetMap: make(labelSetMap),
}
}
// ContainerUsageSampleWithKey holds a ContainerUsageSample together with the
// ID of the container it belongs to.
type ContainerUsageSampleWithKey struct {
ContainerUsageSample
Container ContainerID
}
// AddOrUpdatePod updates the state of the pod with a given PodID, if it is
// present in the cluster object. Otherwise a new pod is created and added to
// the Cluster object.
// If the labels of the pod have changed, it updates the links between the containers
// and the aggregations.
func (cluster *ClusterState) AddOrUpdatePod(podID PodID, newLabels labels.Set, phase apiv1.PodPhase) {
pod, podExists := cluster.Pods[podID]
if !podExists {
pod = newPod(podID)
cluster.Pods[podID] = pod
}
newlabelSetKey := cluster.getLabelSetKey(newLabels)
if podExists && pod.labelSetKey != newlabelSetKey {
// This Pod is already counted in the old VPA, remove the link.
cluster.removePodFromItsVpa(pod)
}
if !podExists || pod.labelSetKey != newlabelSetKey {
pod.labelSetKey = newlabelSetKey
// Set the links between the containers and aggregations based on the current pod labels.
for containerName, container := range pod.Containers {
containerID := ContainerID{PodID: podID, ContainerName: containerName}
container.aggregator = cluster.findOrCreateAggregateContainerState(containerID)
}
cluster.addPodToItsVpa(pod)
}
pod.Phase = phase
}
// addPodToItsVpa increases the count of Pods associated with a VPA object.
// Does a scan similar to findOrCreateAggregateContainerState so could be optimized if needed.
func (cluster *ClusterState) addPodToItsVpa(pod *PodState) {
for _, vpa := range cluster.Vpas {
if vpa_utils.PodLabelsMatchVPA(pod.ID.Namespace, cluster.labelSetMap[pod.labelSetKey], vpa.ID.Namespace, vpa.PodSelector) {
vpa.PodCount++
}
}
}
// removePodFromItsVpa decreases the count of Pods associated with a VPA object.
func (cluster *ClusterState) removePodFromItsVpa(pod *PodState) {
for _, vpa := range cluster.Vpas {
if vpa_utils.PodLabelsMatchVPA(pod.ID.Namespace, cluster.labelSetMap[pod.labelSetKey], vpa.ID.Namespace, vpa.PodSelector) {
vpa.PodCount--
}
}
}
// GetContainer returns the ContainerState object for a given ContainerID or
// null if it's not present in the model.
func (cluster *ClusterState) GetContainer(containerID ContainerID) *ContainerState {
pod, podExists := cluster.Pods[containerID.PodID]
if podExists {
container, containerExists := pod.Containers[containerID.ContainerName]
if containerExists {
return container
}
}
return nil
}
// DeletePod removes an existing pod from the cluster.
func (cluster *ClusterState) DeletePod(podID PodID) {
pod, found := cluster.Pods[podID]
if found {
cluster.removePodFromItsVpa(pod)
}
delete(cluster.Pods, podID)
}
// AddOrUpdateContainer creates a new container with the given ContainerID and
// adds it to the parent pod in the ClusterState object, if not yet present.
// Requires the pod to be added to the ClusterState first. Otherwise an error is
// returned.
func (cluster *ClusterState) AddOrUpdateContainer(containerID ContainerID, request Resources) error {
pod, podExists := cluster.Pods[containerID.PodID]
if !podExists {
return NewKeyError(containerID.PodID)
}
if container, containerExists := pod.Containers[containerID.ContainerName]; !containerExists {
cluster.findOrCreateAggregateContainerState(containerID)
pod.Containers[containerID.ContainerName] = NewContainerState(request, NewContainerStateAggregatorProxy(cluster, containerID))
} else {
// Container aleady exists. Possibly update the request.
container.Request = request
}
return nil
}
// AddSample adds a new usage sample to the proper container in the ClusterState
// object. Requires the container as well as the parent pod to be added to the
// ClusterState first. Otherwise an error is returned.
func (cluster *ClusterState) AddSample(sample *ContainerUsageSampleWithKey) error {
pod, podExists := cluster.Pods[sample.Container.PodID]
if !podExists {
return NewKeyError(sample.Container.PodID)
}
containerState, containerExists := pod.Containers[sample.Container.ContainerName]
if !containerExists {
return NewKeyError(sample.Container)
}
if !containerState.AddSample(&sample.ContainerUsageSample) {
return fmt.Errorf("sample discarded (invalid or out of order)")
}
return nil
}
// RecordOOM adds info regarding OOM event in the model as an artificial memory sample.
func (cluster *ClusterState) RecordOOM(containerID ContainerID, timestamp time.Time, requestedMemory ResourceAmount) error {
pod, podExists := cluster.Pods[containerID.PodID]
if !podExists {
return NewKeyError(containerID.PodID)
}
containerState, containerExists := pod.Containers[containerID.ContainerName]
if !containerExists {
return NewKeyError(containerID.ContainerName)
}
err := containerState.RecordOOM(timestamp, requestedMemory)
if err != nil {
return fmt.Errorf("error while recording OOM for %v, Reason: %v", containerID, err)
}
return nil
}
// AddOrUpdateVpa adds a new VPA with a given ID to the ClusterState if it
// didn't yet exist. If the VPA already existed but had a different pod
// selector, the pod selector is updated. Updates the links between the VPA and
// all aggregations it matches.
func (cluster *ClusterState) AddOrUpdateVpa(apiObject *vpa_types.VerticalPodAutoscaler, selector labels.Selector) error {
vpaID := VpaID{Namespace: apiObject.Namespace, VpaName: apiObject.Name}
annotationsMap := apiObject.Annotations
conditionsMap := make(vpaConditionsMap)
for _, condition := range apiObject.Status.Conditions {
conditionsMap[condition.Type] = condition
}
var currentRecommendation *vpa_types.RecommendedPodResources
if conditionsMap[vpa_types.RecommendationProvided].Status == apiv1.ConditionTrue {
currentRecommendation = apiObject.Status.Recommendation
}
vpa, vpaExists := cluster.Vpas[vpaID]
if vpaExists && (vpa.PodSelector.String() != selector.String()) {
// Pod selector was changed. Delete the VPA object and recreate
// it with the new selector.
if err := cluster.DeleteVpa(vpaID); err != nil {
return err
}
vpaExists = false
}
if !vpaExists {
vpa = NewVpa(vpaID, selector, apiObject.CreationTimestamp.Time)
cluster.Vpas[vpaID] = vpa
for aggregationKey, aggregation := range cluster.aggregateStateMap {
vpa.UseAggregationIfMatching(aggregationKey, aggregation)
}
vpa.PodCount = len(cluster.GetMatchingPods(vpa))
}
vpa.TargetRef = apiObject.Spec.TargetRef
vpa.Annotations = annotationsMap
vpa.Conditions = conditionsMap
vpa.Recommendation = currentRecommendation
vpa.SetUpdateMode(apiObject.Spec.UpdatePolicy)
vpa.SetResourcePolicy(apiObject.Spec.ResourcePolicy)
return nil
}
// DeleteVpa removes a VPA with the given ID from the ClusterState.
func (cluster *ClusterState) DeleteVpa(vpaID VpaID) error {
vpa, vpaExists := cluster.Vpas[vpaID]
if !vpaExists {
return NewKeyError(vpaID)
}
for _, state := range vpa.aggregateContainerStates {
state.MarkNotAutoscaled()
}
delete(cluster.Vpas, vpaID)
delete(cluster.EmptyVPAs, vpaID)
return nil
}
func newPod(id PodID) *PodState {
return &PodState{
ID: id,
Containers: make(map[string]*ContainerState),
}
}
// getLabelSetKey puts the given labelSet in the global labelSet map and returns a
// corresponding labelSetKey.
func (cluster *ClusterState) getLabelSetKey(labelSet labels.Set) labelSetKey {
labelSetKey := labelSetKey(labelSet.String())
cluster.labelSetMap[labelSetKey] = labelSet
return labelSetKey
}
// MakeAggregateStateKey returns the AggregateStateKey that should be used
// to aggregate usage samples from a container with the given name in a given pod.
func (cluster *ClusterState) MakeAggregateStateKey(pod *PodState, containerName string) AggregateStateKey {
return aggregateStateKey{
namespace: pod.ID.Namespace,
containerName: containerName,
labelSetKey: pod.labelSetKey,
labelSetMap: &cluster.labelSetMap,
}
}
// aggregateStateKeyForContainerID returns the AggregateStateKey for the ContainerID.
// The pod with the corresponding PodID must already be present in the ClusterState.
func (cluster *ClusterState) aggregateStateKeyForContainerID(containerID ContainerID) AggregateStateKey {
pod, podExists := cluster.Pods[containerID.PodID]
if !podExists {
panic(fmt.Sprintf("Pod not present in the ClusterState: %v", containerID.PodID))
}
return cluster.MakeAggregateStateKey(pod, containerID.ContainerName)
}
// findOrCreateAggregateContainerState returns (possibly newly created) AggregateContainerState
// that should be used to aggregate usage samples from container with a given ID.
// The pod with the corresponding PodID must already be present in the ClusterState.
func (cluster *ClusterState) findOrCreateAggregateContainerState(containerID ContainerID) *AggregateContainerState {
aggregateStateKey := cluster.aggregateStateKeyForContainerID(containerID)
aggregateContainerState, aggregateStateExists := cluster.aggregateStateMap[aggregateStateKey]
if !aggregateStateExists {
aggregateContainerState = NewAggregateContainerState()
cluster.aggregateStateMap[aggregateStateKey] = aggregateContainerState
// Link the new aggregation to the existing VPAs.
for _, vpa := range cluster.Vpas {
vpa.UseAggregationIfMatching(aggregateStateKey, aggregateContainerState)
}
}
return aggregateContainerState
}
// GarbageCollectAggregateCollectionStates removes obsolete AggregateCollectionStates from the ClusterState.
// AggregateCollectionState is obsolete in following situations:
// 1) It has no samples and there are no more active pods that can contribute,
// 2) The last sample is too old to give meaningful recommendation (>8 days),
// 3) There are no samples and the aggregate state was created >8 days ago.
func (cluster *ClusterState) GarbageCollectAggregateCollectionStates(now time.Time) {
klog.V(1).Info("Garbage collection of AggregateCollectionStates triggered")
keysToDelete := make([]AggregateStateKey, 0)
activeKeys := cluster.getActiveAggregateStateKeys()
for key, aggregateContainerState := range cluster.aggregateStateMap {
isKeyActive := activeKeys[key]
if !isKeyActive && aggregateContainerState.isEmpty() {
keysToDelete = append(keysToDelete, key)
klog.V(1).Infof("Removing empty and inactive AggregateCollectionState for %+v", key)
continue
}
if aggregateContainerState.isExpired(now) {
keysToDelete = append(keysToDelete, key)
klog.V(1).Infof("Removing expired AggregateCollectionState for %+v", key)
}
}
for _, key := range keysToDelete {
delete(cluster.aggregateStateMap, key)
for _, vpa := range cluster.Vpas {
vpa.DeleteAggregation(key)
}
}
}
func (cluster *ClusterState) getActiveAggregateStateKeys() map[AggregateStateKey]bool {
activeKeys := map[AggregateStateKey]bool{}
for _, pod := range cluster.Pods {
// Pods that will not run anymore are considered inactive.
if pod.Phase == apiv1.PodSucceeded || pod.Phase == apiv1.PodFailed {
continue
}
for container := range pod.Containers {
activeKeys[cluster.MakeAggregateStateKey(pod, container)] = true
}
}
return activeKeys
}
// RecordRecommendation marks the state of recommendation in the cluster. We
// keep track of empty recommendations and log information about them
// periodically.
func (cluster *ClusterState) RecordRecommendation(vpa *Vpa, now time.Time) error {
if vpa.Recommendation != nil && len(vpa.Recommendation.ContainerRecommendations) > 0 {
delete(cluster.EmptyVPAs, vpa.ID)
return nil
}
lastLogged, ok := cluster.EmptyVPAs[vpa.ID]
if !ok {
cluster.EmptyVPAs[vpa.ID] = now
} else {
if lastLogged.Add(RecommendationMissingMaxDuration).Before(now) {
cluster.EmptyVPAs[vpa.ID] = now
return fmt.Errorf("VPA %v/%v is missing recommendation for more than %v", vpa.ID.Namespace, vpa.ID.VpaName, RecommendationMissingMaxDuration)
}
}
return nil
}
// GetMatchingPods returns a list of currently active pods that match the
// given VPA. Traverses through all pods in the cluster - use sparingly.
func (cluster *ClusterState) GetMatchingPods(vpa *Vpa) []PodID {
matchingPods := []PodID{}
for podID, pod := range cluster.Pods {
if vpa_utils.PodLabelsMatchVPA(podID.Namespace, cluster.labelSetMap[pod.labelSetKey],
vpa.ID.Namespace, vpa.PodSelector) {
matchingPods = append(matchingPods, podID)
}
}
return matchingPods
}
// Implementation of the AggregateStateKey interface. It can be used as a map key.
type aggregateStateKey struct {
namespace string
containerName string
labelSetKey labelSetKey
// Pointer to the global map from labelSetKey to labels.Set.
// Note: a pointer is used so that two copies of the same key are equal.
labelSetMap *labelSetMap
}
// Labels returns the namespace for the aggregateStateKey.
func (k aggregateStateKey) Namespace() string {
return k.namespace
}
// ContainerName returns the name of the container for the aggregateStateKey.
func (k aggregateStateKey) ContainerName() string {
return k.containerName
}
// Labels returns the set of labels for the aggregateStateKey.
func (k aggregateStateKey) Labels() labels.Labels {
if k.labelSetMap == nil {
return labels.Set{}
}
return (*k.labelSetMap)[k.labelSetKey]
}