/
pod_based_enricher.go
367 lines (326 loc) · 13.9 KB
/
pod_based_enricher.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
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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.
// Copyright 2018-2019 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package processors
import (
"fmt"
"time"
log "github.com/sirupsen/logrus"
kube_api "k8s.io/api/core/v1"
v1listers "k8s.io/client-go/listers/core/v1"
"github.com/wavefronthq/observability-for-kubernetes/collector/internal/metrics"
"github.com/wavefronthq/observability-for-kubernetes/collector/internal/util"
)
type PodBasedEnricher struct {
podLister v1listers.PodLister
labelCopier *util.LabelCopier
collectionInterval time.Duration
workloadCache util.WorkloadCache
}
func (pbe *PodBasedEnricher) Name() string {
return "pod_based_enricher"
}
func (pbe *PodBasedEnricher) Process(batch *metrics.Batch) (*metrics.Batch, error) {
newMs := make(map[metrics.ResourceKey]*metrics.Set, len(batch.Sets))
for resourceKey, metricSet := range batch.Sets {
switch metricSet.Labels[metrics.LabelMetricSetType.Key] {
case metrics.MetricSetTypePod:
namespace := metricSet.Labels[metrics.LabelNamespaceName.Key]
podName := metricSet.Labels[metrics.LabelPodName.Key]
pod, err := pbe.getPod(namespace, podName)
if err != nil {
delete(batch.Sets, resourceKey)
log.Debugf("Failed to get pod %s from cache: %v", metrics.PodKey(namespace, podName), err)
continue
}
pbe.addPodInfo(metricSet, pod, batch, newMs)
case metrics.MetricSetTypePodContainer:
namespace := metricSet.Labels[metrics.LabelNamespaceName.Key]
podName := metricSet.Labels[metrics.LabelPodName.Key]
pod, err := pbe.getPod(namespace, podName)
if err != nil {
delete(batch.Sets, resourceKey)
log.Debugf("Failed to get pod %s from cache: %v", metrics.PodKey(namespace, podName), err)
continue
}
pbe.addContainerInfo(resourceKey, metricSet, pod, batch, newMs)
}
}
for k, v := range newMs {
batch.Sets[k] = v
}
return batch, nil
}
func (pbe *PodBasedEnricher) getPod(namespace, name string) (*kube_api.Pod, error) {
pod, err := pbe.podLister.Pods(namespace).Get(name)
if err != nil {
return nil, err
}
if pod == nil {
return nil, fmt.Errorf("cannot find pod definition")
}
return pod, nil
}
func (pbe *PodBasedEnricher) addContainerInfo(key metrics.ResourceKey, containerMs *metrics.Set, pod *kube_api.Pod, batch *metrics.Batch, newMs map[metrics.ResourceKey]*metrics.Set) {
for _, container := range pod.Spec.Containers {
if key == metrics.PodContainerKey(pod.Namespace, pod.Name, container.Name) {
updateContainerResourcesAndLimits(containerMs, container)
if _, ok := containerMs.Labels[metrics.LabelContainerBaseImage.Key]; !ok {
containerMs.Labels[metrics.LabelContainerBaseImage.Key] = container.Image
}
break
}
}
for _, containerStatus := range pod.Status.ContainerStatuses {
if key == metrics.PodContainerKey(pod.Namespace, pod.Name, containerStatus.Name) {
containerMs.Values[metrics.MetricRestartCount.Name] = intValue(int64(containerStatus.RestartCount))
if !pod.Status.StartTime.IsZero() {
containerMs.EntityCreateTime = pod.Status.StartTime.Time
}
pbe.addContainerStatus(batch.Timestamp, containerMs, &metrics.MetricContainerStatus, containerStatus)
break
}
}
workloadName, workloadKind := pbe.workloadCache.GetWorkloadForPod(pod)
containerMs.Labels[metrics.LabelWorkloadName.Key] = workloadName
containerMs.Labels[metrics.LabelWorkloadKind.Key] = workloadKind
containerMs.Labels[metrics.LabelPodId.Key] = string(pod.UID)
pbe.labelCopier.Copy(pod.Labels, containerMs.Labels)
namespace := containerMs.Labels[metrics.LabelNamespaceName.Key]
podName := containerMs.Labels[metrics.LabelPodName.Key]
podKey := metrics.PodKey(namespace, podName)
_, oldfound := batch.Sets[podKey]
if !oldfound {
_, newfound := newMs[podKey]
if !newfound {
log.Debugf("Pod %s not found, creating a stub", podKey)
podMs := &metrics.Set{
Values: make(map[string]metrics.Value),
Labels: map[string]string{
metrics.LabelMetricSetType.Key: metrics.MetricSetTypePod,
metrics.LabelNamespaceName.Key: namespace,
metrics.LabelPodName.Key: podName,
metrics.LabelNodename.Key: containerMs.Labels[metrics.LabelNodename.Key],
metrics.LabelHostname.Key: containerMs.Labels[metrics.LabelHostname.Key],
metrics.LabelHostID.Key: containerMs.Labels[metrics.LabelHostID.Key],
},
}
if !pod.Status.StartTime.IsZero() {
podMs.EntityCreateTime = pod.Status.StartTime.Time
}
newMs[podKey] = podMs
pbe.addPodInfo(podMs, pod, batch, newMs)
}
}
}
func (pbe *PodBasedEnricher) addPodInfo(podMs *metrics.Set, pod *kube_api.Pod, batch *metrics.Batch, newMs map[metrics.ResourceKey]*metrics.Set) {
// Pod based enricher only adds metrics for pods that are in running state
// Pods that are in other states will be processed by kstate/non_running_pods
if pod.Status.Phase != kube_api.PodRunning {
return
}
// Add UID and create time to pod
podMs.Labels[metrics.LabelPodId.Key] = string(pod.UID)
if !pod.Status.StartTime.IsZero() {
podMs.EntityCreateTime = pod.Status.StartTime.Time
}
pbe.labelCopier.Copy(pod.Labels, podMs.Labels)
// Add pod phase
addLabeledIntMetric(podMs, &metrics.MetricPodPhase, map[string]string{"phase": string(pod.Status.Phase)}, util.ConvertPodPhase(pod.Status.Phase))
// Add workload name and workload kind
workloadName, workloadKind := pbe.workloadCache.GetWorkloadForPod(pod)
podMs.Labels[metrics.LabelWorkloadName.Key] = workloadName
podMs.Labels[metrics.LabelWorkloadKind.Key] = workloadKind
// Add workload status metric for pods with no owner references
if !util.HasOwnerReference(pod.OwnerReferences) {
pbe.addWorkloadStatusMetric(podMs, pod, newMs)
}
// Add cpu/mem requests and limits to containers
for _, container := range pod.Spec.Containers {
containerKey := metrics.PodContainerKey(pod.Namespace, pod.Name, container.Name)
if _, found := batch.Sets[containerKey]; found {
continue
}
if _, found := newMs[containerKey]; found {
continue
}
log.Debugf("Container %s not found, creating a stub", containerKey)
containerMs := &metrics.Set{
Values: make(map[string]metrics.Value),
Labels: map[string]string{
metrics.LabelMetricSetType.Key: metrics.MetricSetTypePodContainer,
metrics.LabelNamespaceName.Key: pod.Namespace,
metrics.LabelPodName.Key: pod.Name,
metrics.LabelContainerName.Key: container.Name,
metrics.LabelContainerBaseImage.Key: container.Image,
metrics.LabelPodId.Key: string(pod.UID),
metrics.LabelNodename.Key: podMs.Labels[metrics.LabelNodename.Key],
metrics.LabelHostname.Key: podMs.Labels[metrics.LabelHostname.Key],
metrics.LabelHostID.Key: podMs.Labels[metrics.LabelHostID.Key],
metrics.LabelWorkloadName.Key: podMs.Labels[metrics.LabelWorkloadName.Key],
metrics.LabelWorkloadKind.Key: podMs.Labels[metrics.LabelWorkloadKind.Key],
},
EntityCreateTime: podMs.CollectionStartTime,
}
pbe.labelCopier.Copy(pod.Labels, containerMs.Labels)
updateContainerResourcesAndLimits(containerMs, container)
newMs[containerKey] = containerMs
}
pbe.updateContainerStatus(newMs, pod, pod.Status.ContainerStatuses, batch.Timestamp)
}
func (pbe *PodBasedEnricher) addWorkloadStatusMetric(podMs *metrics.Set, pod *kube_api.Pod, newMs map[metrics.ResourceKey]*metrics.Set) {
workloadMs := &metrics.Set{
Values: make(map[string]metrics.Value),
Labels: map[string]string{
metrics.LabelNamespaceName.Key: pod.Namespace,
metrics.LabelWorkloadName.Key: podMs.Labels[metrics.LabelWorkloadName.Key],
metrics.LabelWorkloadKind.Key: podMs.Labels[metrics.LabelWorkloadKind.Key],
metrics.LabelAvailable.Key: podMs.Labels[metrics.LabelAvailable.Key],
metrics.LabelDesired.Key: podMs.Labels[metrics.LabelDesired.Key],
},
}
workloadStatus := 1
for _, containerStatus := range pod.Status.ContainerStatuses {
if !containerStatus.Ready {
if containerStatus.State.Waiting != nil {
workloadStatus = 0
workloadMs.Labels[metrics.LabelReason.Key] = containerStatus.State.Waiting.Reason
workloadMs.Labels[metrics.LabelMessage.Key] = containerStatus.State.Waiting.Message
break
} else if containerStatus.State.Terminated != nil {
workloadStatus = 0
workloadMs.Labels[metrics.LabelReason.Key] = containerStatus.State.Terminated.Reason
// TODO: add message for pod stuck in terminating
workloadMs.Labels[metrics.LabelMessage.Key] = containerStatus.State.Terminated.Message
break
}
}
}
workloadMs.Labels[metrics.LabelAvailable.Key] = fmt.Sprint(workloadStatus)
workloadMs.Labels[metrics.LabelDesired.Key] = "1"
addLabeledIntMetric(workloadMs, &metrics.MetricWorkloadStatus, nil, int64(workloadStatus))
newMs[metrics.WorkloadStatusPodKey(pod.Namespace, pod.Name)] = workloadMs
}
func updateContainerResourcesAndLimits(metricSet *metrics.Set, container kube_api.Container) {
requests := container.Resources.Requests
for key, val := range container.Resources.Requests {
metric, found := metrics.ResourceRequestMetrics[key]
// Inserts a metric to metrics.ResourceRequestMetrics if there is no
// existing one for the given resource. The name of this metric is
// ResourceName/request where ResourceName is the name of the resource
// requested in container resource requests.
if !found {
metric = metrics.Metric{
MetricDescriptor: metrics.MetricDescriptor{
Name: string(key) + "/request",
Description: string(key) + " resource request. This metric is Kubernetes specific.",
Type: metrics.Gauge,
ValueType: metrics.ValueInt64,
Units: metrics.Count,
},
}
metrics.ResourceRequestMetrics[key] = metric
}
if key == kube_api.ResourceCPU {
metricSet.Values[metric.Name] = intValue(val.MilliValue())
} else {
metricSet.Values[metric.Name] = intValue(val.Value())
}
}
// For primary resources like cpu and memory, explicitly sets their request resource
// metric to zero if they are not requested.
if _, found := requests[kube_api.ResourceCPU]; !found {
metricSet.Values[metrics.MetricCpuRequest.Name] = intValue(0)
}
if _, found := requests[kube_api.ResourceMemory]; !found {
metricSet.Values[metrics.MetricMemoryRequest.Name] = intValue(0)
}
if _, found := requests[kube_api.ResourceEphemeralStorage]; !found {
metricSet.Values[metrics.MetricEphemeralStorageRequest.Name] = intValue(0)
}
limits := container.Resources.Limits
if val, found := limits[kube_api.ResourceCPU]; found {
metricSet.Values[metrics.MetricCpuLimit.Name] = intValue(val.MilliValue())
} else {
metricSet.Values[metrics.MetricCpuLimit.Name] = intValue(0)
}
if val, found := limits[kube_api.ResourceMemory]; found {
metricSet.Values[metrics.MetricMemoryLimit.Name] = intValue(val.Value())
} else {
metricSet.Values[metrics.MetricMemoryLimit.Name] = intValue(0)
}
if val, found := limits[kube_api.ResourceEphemeralStorage]; found {
metricSet.Values[metrics.MetricEphemeralStorageLimit.Name] = intValue(val.Value())
} else {
metricSet.Values[metrics.MetricEphemeralStorageLimit.Name] = intValue(0)
}
}
func (pbe *PodBasedEnricher) addContainerStatus(collectionTime time.Time, containerMs *metrics.Set, metric *metrics.Metric, status kube_api.ContainerStatus) {
labels := make(map[string]string, 2)
containerStateInfo := pbe.findContainerState(collectionTime, status)
containerStateInfo.AddMetricTags(labels)
addLabeledIntMetric(containerMs, metric, labels, int64(containerStateInfo.Value))
}
func (pbe *PodBasedEnricher) findContainerState(collectionTime time.Time, status kube_api.ContainerStatus) util.ContainerStateInfo {
if status.LastTerminationState.Terminated == nil {
return util.NewContainerStateInfo(status.State)
}
lastTerminationTime := status.LastTerminationState.Terminated.FinishedAt.Time
lastCollectionTime := collectionTime.Add(-1 * pbe.collectionInterval)
if lastCollectionTime.After(lastTerminationTime) {
return util.NewContainerStateInfo(status.State)
}
return util.NewContainerStateInfo(status.LastTerminationState)
}
func (pbe *PodBasedEnricher) updateContainerStatus(metricSets map[metrics.ResourceKey]*metrics.Set, pod *kube_api.Pod, statuses []kube_api.ContainerStatus, collectionTime time.Time) {
if len(statuses) == 0 {
return
}
for _, status := range statuses {
containerKey := metrics.PodContainerKey(pod.Namespace, pod.Name, status.Name)
containerMs, found := metricSets[containerKey]
if !found {
log.Debugf("Container key %s not found", containerKey)
continue
}
pbe.addContainerStatus(collectionTime, containerMs, &metrics.MetricContainerStatus, status)
}
}
// addLabeledIntMetric is a convenience method for adding the labeled metric and value to the metric set.
func addLabeledIntMetric(ms *metrics.Set, metric *metrics.Metric, labels map[string]string, value int64) {
val := metrics.LabeledValue{
Name: metric.Name,
Labels: labels,
Value: metrics.Value{
ValueType: metrics.ValueInt64,
IntValue: value,
},
}
ms.LabeledValues = append(ms.LabeledValues, val)
}
func intValue(value int64) metrics.Value {
return metrics.Value{
IntValue: value,
ValueType: metrics.ValueInt64,
}
}
func NewPodBasedEnricher(podLister v1listers.PodLister, workloadCache util.WorkloadCache, labelCopier *util.LabelCopier, collectionInterval time.Duration) *PodBasedEnricher {
return &PodBasedEnricher{
podLister: podLister,
labelCopier: labelCopier,
collectionInterval: collectionInterval,
workloadCache: workloadCache,
}
}