forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pods.go
258 lines (218 loc) · 7.52 KB
/
pods.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
// Copyright 2020, OpenTelemetry 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:https://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 collection
import (
"strings"
"time"
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
"go.opentelemetry.io/collector/translator/conventions"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/tools/cache"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/utils"
)
const (
// Keys for pod metadata.
podCreationTime = "pod.creation_timestamp"
)
var podPhaseMetric = &metricspb.MetricDescriptor{
Name: "k8s/pod/phase",
Description: "Current phase of the pod (1 - Pending, 2 - Running, 3 - Succeeded, 4 - Failed, 5 - Unknown)",
Type: metricspb.MetricDescriptor_GAUGE_INT64,
}
func getMetricsForPod(pod *corev1.Pod) []*resourceMetrics {
metrics := []*metricspb.Metric{
{
MetricDescriptor: podPhaseMetric,
Timeseries: []*metricspb.TimeSeries{
utils.GetInt64TimeSeries(int64(phaseToInt(pod.Status.Phase))),
},
},
}
podRes := getResourceForPod(pod)
containerResByName := map[string]*resourceMetrics{}
for _, cs := range pod.Status.ContainerStatuses {
if cs.ContainerID == "" {
continue
}
contLabels := getAllContainerLabels(cs, podRes.Labels)
containerResByName[cs.Name] = &resourceMetrics{resource: getResourceForContainer(contLabels)}
containerResByName[cs.Name].metrics = getStatusMetricsForContainer(cs)
}
for _, c := range pod.Spec.Containers {
cr := containerResByName[c.Name]
// This likely will not happen since both pod spec and status return
// information about the same set of containers. However, if there's
// a mismatch, skip collecting spec metrics.
if cr == nil {
continue
}
cr.metrics = append(cr.metrics, getSpecMetricsForContainer(c)...)
}
out := []*resourceMetrics{
{
resource: podRes,
metrics: metrics,
},
}
out = append(out, listResourceMetrics(containerResByName)...)
return out
}
func listResourceMetrics(rms map[string]*resourceMetrics) []*resourceMetrics {
out := make([]*resourceMetrics, len(rms))
i := 0
for _, rm := range rms {
out[i] = rm
i++
}
return out
}
// getResourceForPod returns a proto representation of the pod.
func getResourceForPod(pod *corev1.Pod) *resourcepb.Resource {
return &resourcepb.Resource{
Type: k8sType,
Labels: map[string]string{
k8sKeyPodUID: string(pod.UID),
conventions.AttributeK8sPod: pod.Name,
k8sKeyNodeName: pod.Spec.NodeName,
conventions.AttributeK8sNamespace: pod.Namespace,
conventions.AttributeK8sCluster: pod.ClusterName,
},
}
}
func phaseToInt(phase corev1.PodPhase) int32 {
switch phase {
case corev1.PodPending:
return 1
case corev1.PodRunning:
return 2
case corev1.PodSucceeded:
return 3
case corev1.PodFailed:
return 4
case corev1.PodUnknown:
return 5
default:
return 5
}
}
// getMetadataForPod returns all metadata associated with the pod.
func getMetadataForPod(pod *corev1.Pod, mc *metadataStore) map[ResourceID]*KubernetesMetadata {
metadata := utils.MergeStringMaps(map[string]string{}, pod.Labels)
metadata[podCreationTime] = pod.CreationTimestamp.Format(time.RFC3339)
for _, or := range pod.OwnerReferences {
metadata[strings.ToLower(or.Kind)] = or.Name
metadata[strings.ToLower(or.Kind)+"_uid"] = string(or.UID)
// defer syncing replicaset and job workload metadata.
if or.Kind == k8sKindReplicaSet || or.Kind == k8sKindJob {
continue
}
metadata[k8sKeyWorkLoadKind] = or.Kind
metadata[k8sKeyWorkLoadName] = or.Name
}
if mc.services != nil {
metadata = utils.MergeStringMaps(metadata,
getPodServiceTags(pod, mc.services),
)
}
if mc.jobs != nil {
metadata = utils.MergeStringMaps(metadata,
collectPodJobProperties(pod, mc.jobs),
)
}
if mc.replicaSets != nil {
metadata = utils.MergeStringMaps(metadata,
collectPodReplicaSetProperties(pod, mc.replicaSets),
)
}
podID := ResourceID(pod.UID)
return mergeKubernetesMetadataMaps(map[ResourceID]*KubernetesMetadata{
podID: {
resourceIDKey: k8sKeyPodUID,
resourceID: podID,
metadata: metadata,
},
}, getPodContainerProperties(pod))
}
// collectPodJobProperties checks if pod owner of type Job is cached. Check owners reference
// on Job to see if it was created by a CronJob. Sync metadata accordingly.
func collectPodJobProperties(pod *corev1.Pod, JobStore cache.Store) map[string]string {
jobRef := utils.FindOwnerWithKind(pod.OwnerReferences, k8sKindJob)
if jobRef != nil {
job, ok, _ := JobStore.GetByKey(utils.GetIDForCache(pod.Namespace, jobRef.Name))
if ok {
jobObj := job.(*batchv1.Job)
if cronJobRef := utils.FindOwnerWithKind(jobObj.OwnerReferences, k8sKindCronJob); cronJobRef != nil {
return getWorkloadProperties(cronJobRef, k8sKeyCronJobName)
}
return getWorkloadProperties(jobRef, k8sKeyJobName)
}
}
return nil
}
// collectPodReplicaSetProperties checks if pod owner of type ReplicaSet is cached. Check owners reference
// on ReplicaSet to see if it was created by a Deployment. Sync metadata accordingly.
func collectPodReplicaSetProperties(pod *corev1.Pod, replicaSetstore cache.Store) map[string]string {
rsRef := utils.FindOwnerWithKind(pod.OwnerReferences, k8sKindReplicaSet)
if rsRef != nil {
replicaSet, ok, _ := replicaSetstore.GetByKey(utils.GetIDForCache(pod.Namespace, rsRef.Name))
if ok {
replicaSetObj := replicaSet.(*appsv1.ReplicaSet)
if deployRef := utils.FindOwnerWithKind(replicaSetObj.OwnerReferences, k8sKindDeployment); deployRef != nil {
return getWorkloadProperties(deployRef, k8sKeyDeploymentName)
}
return getWorkloadProperties(rsRef, k8sKeyReplicaSetName)
}
}
return nil
}
// getPodServiceTags returns a set of services associated with the pod.
func getPodServiceTags(pod *corev1.Pod, services cache.Store) map[string]string {
properties := map[string]string{}
for _, ser := range services.List() {
serObj := ser.(*corev1.Service)
if serObj.Namespace == pod.Namespace &&
labels.Set(serObj.Spec.Selector).AsSelectorPreValidated().Matches(labels.Set(pod.Labels)) {
properties["kubernetes_service_"+serObj.Name] = ""
}
}
return properties
}
// getWorkloadProperties returns workload metadata for provided owner reference.
func getWorkloadProperties(ref *v1.OwnerReference, labelKey string) map[string]string {
kind := ref.Kind
uidKey := strings.ToLower(kind) + "_uid"
return map[string]string{
k8sKeyWorkLoadKind: kind,
k8sKeyWorkLoadName: ref.Name,
labelKey: ref.Name,
uidKey: string(ref.UID),
}
}
func getPodContainerProperties(pod *corev1.Pod) map[ResourceID]*KubernetesMetadata {
km := map[ResourceID]*KubernetesMetadata{}
for _, cs := range pod.Status.ContainerStatuses {
// Skip if container id returned is empty.
if cs.ContainerID == "" {
continue
}
md := getMetadataForContainer(cs)
km[md.resourceID] = md
}
return km
}