-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathobserver.go
179 lines (155 loc) · 5.12 KB
/
observer.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
/*
Copyright 2018 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 oom
import (
"strings"
"time"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/client-go/tools/cache"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/recommender/model"
"k8s.io/klog/v2"
)
// OomInfo contains data of the OOM event occurrence
type OomInfo struct {
Timestamp time.Time
Memory model.ResourceAmount
ContainerID model.ContainerID
}
// Observer can observe pod resource update and collect OOM events.
type Observer interface {
GetObservedOomsChannel() chan OomInfo
OnEvent(*apiv1.Event)
cache.ResourceEventHandler
}
// observer can observe pod resource update and collect OOM events.
type observer struct {
observedOomsChannel chan OomInfo
}
// NewObserver returns new instance of the observer.
func NewObserver() *observer {
return &observer{
observedOomsChannel: make(chan OomInfo, 5000),
}
}
func (o *observer) GetObservedOomsChannel() chan OomInfo {
return o.observedOomsChannel
}
func parseEvictionEvent(event *apiv1.Event) []OomInfo {
if event.Reason != "Evicted" ||
event.InvolvedObject.Kind != "Pod" {
return []OomInfo{}
}
extractArray := func(annotationsKey string) []string {
str, found := event.Annotations[annotationsKey]
if !found {
return []string{}
}
return strings.Split(str, ",")
}
offendingContainers := extractArray("offending_containers")
offendingContainersUsage := extractArray("offending_containers_usage")
starvedResource := extractArray("starved_resource")
if len(offendingContainers) != len(offendingContainersUsage) ||
len(offendingContainers) != len(starvedResource) {
return []OomInfo{}
}
result := make([]OomInfo, 0, len(offendingContainers))
for i, container := range offendingContainers {
if starvedResource[i] != "memory" {
continue
}
memory, err := resource.ParseQuantity(offendingContainersUsage[i])
if err != nil {
klog.ErrorS(err, "Cannot parse resource quantity in eviction", "event", offendingContainersUsage[i])
continue
}
oomInfo := OomInfo{
Timestamp: event.CreationTimestamp.Time.UTC(),
Memory: model.ResourceAmount(memory.Value()),
ContainerID: model.ContainerID{
PodID: model.PodID{
Namespace: event.InvolvedObject.Namespace,
PodName: event.InvolvedObject.Name,
},
ContainerName: container,
},
}
result = append(result, oomInfo)
}
return result
}
// OnEvent inspects k8s eviction events and translates them to OomInfo.
func (o *observer) OnEvent(event *apiv1.Event) {
klog.V(1).InfoS("OOM Observer processing event", "event", event)
for _, oomInfo := range parseEvictionEvent(event) {
o.observedOomsChannel <- oomInfo
}
}
func findStatus(name string, containerStatuses []apiv1.ContainerStatus) *apiv1.ContainerStatus {
for _, containerStatus := range containerStatuses {
if containerStatus.Name == name {
return &containerStatus
}
}
return nil
}
func findSpec(name string, containers []apiv1.Container) *apiv1.Container {
for _, containerSpec := range containers {
if containerSpec.Name == name {
return &containerSpec
}
}
return nil
}
// OnAdd is Noop
func (o *observer) OnAdd(obj interface{}, isInInitialList bool) {}
// OnUpdate inspects if the update contains oom information and
// passess it to the ObservedOomsChannel
func (o *observer) OnUpdate(oldObj, newObj interface{}) {
oldPod, ok := oldObj.(*apiv1.Pod)
if !ok {
klog.ErrorS(nil, "OOM observer received invalid oldObj", "oldObj", oldObj)
}
newPod, ok := newObj.(*apiv1.Pod)
if !ok {
klog.ErrorS(nil, "OOM observer received invalid newObj", "newObj", newObj)
}
for _, containerStatus := range newPod.Status.ContainerStatuses {
if containerStatus.RestartCount > 0 &&
containerStatus.LastTerminationState.Terminated != nil &&
containerStatus.LastTerminationState.Terminated.Reason == "OOMKilled" {
oldStatus := findStatus(containerStatus.Name, oldPod.Status.ContainerStatuses)
if oldStatus != nil && containerStatus.RestartCount > oldStatus.RestartCount {
oldSpec := findSpec(containerStatus.Name, oldPod.Spec.Containers)
if oldSpec != nil {
memory := oldSpec.Resources.Requests[apiv1.ResourceMemory]
oomInfo := OomInfo{
Timestamp: containerStatus.LastTerminationState.Terminated.FinishedAt.Time.UTC(),
Memory: model.ResourceAmount(memory.Value()),
ContainerID: model.ContainerID{
PodID: model.PodID{
Namespace: newPod.ObjectMeta.Namespace,
PodName: newPod.ObjectMeta.Name,
},
ContainerName: containerStatus.Name,
},
}
o.observedOomsChannel <- oomInfo
}
}
}
}
}
// OnDelete is Noop
func (*observer) OnDelete(obj interface{}) {}