-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
kubelet.go
205 lines (175 loc) · 5.93 KB
/
kubelet.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build !serverless
package listeners
import (
"errors"
"sort"
"time"
"github.com/DataDog/datadog-agent/comp/core/autodiscovery/common/utils"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/util/containers"
"github.com/DataDog/datadog-agent/pkg/util/kubernetes/kubelet"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/optional"
)
// KubeletListener listens to pod creation through a subscription
// to the workloadmeta store.
type KubeletListener struct {
workloadmetaListener
}
// NewKubeletListener returns a new KubeletListener.
func NewKubeletListener(_ Config, wmeta optional.Option[workloadmeta.Component]) (ServiceListener, error) {
const name = "ad-kubeletlistener"
l := &KubeletListener{}
filterParams := workloadmeta.FilterParams{
Kinds: []workloadmeta.Kind{workloadmeta.KindKubernetesPod},
Source: workloadmeta.SourceNodeOrchestrator,
EventType: workloadmeta.EventTypeAll,
}
f := workloadmeta.NewFilter(&filterParams)
wmetaInstance, ok := wmeta.Get()
if !ok {
return nil, errors.New("workloadmeta store is not initialized")
}
var err error
l.workloadmetaListener, err = newWorkloadmetaListener(name, f, l.processPod, wmetaInstance)
if err != nil {
return nil, err
}
return l, nil
}
func (l *KubeletListener) processPod(entity workloadmeta.Entity) {
pod := entity.(*workloadmeta.KubernetesPod)
wlmContainers := pod.GetAllContainers()
containers := make([]*workloadmeta.Container, 0, len(wlmContainers))
for _, podContainer := range wlmContainers {
container, err := l.Store().GetContainer(podContainer.ID)
if err != nil {
log.Debugf("pod %q has reference to non-existing container %q", pod.Name, podContainer.ID)
continue
}
l.createContainerService(pod, &podContainer, container)
containers = append(containers, container)
}
l.createPodService(pod, containers)
}
func (l *KubeletListener) createPodService(
pod *workloadmeta.KubernetesPod,
containers []*workloadmeta.Container,
) {
var ports []ContainerPort
for _, container := range containers {
for _, port := range container.Ports {
ports = append(ports, ContainerPort{
Port: port.Port,
Name: port.Name,
})
}
}
sort.Slice(ports, func(i, j int) bool {
return ports[i].Port < ports[j].Port
})
entity := kubelet.PodUIDToEntityName(pod.ID)
svc := &service{
entity: pod,
adIdentifiers: []string{entity},
hosts: map[string]string{"pod": pod.IP},
ports: ports,
ready: true,
}
svcID := buildSvcID(pod.GetID())
l.AddService(svcID, svc, "")
}
func (l *KubeletListener) createContainerService(
pod *workloadmeta.KubernetesPod,
podContainer *workloadmeta.OrchestratorContainer,
container *workloadmeta.Container,
) {
// we need to take the container name and image from the pod spec, as
// the information from the container in the workloadmeta store might
// have extra information resolved by the container runtime that won't
// match what the user specified.
containerName := podContainer.Name
containerImg := podContainer.Image
if l.IsExcluded(
containers.GlobalFilter,
pod.Annotations,
containerName,
containerImg.RawName,
pod.Namespace,
) {
log.Debugf("container %s filtered out: name %q image %q namespace %q", container.ID, containerName, containerImg.RawName, pod.Namespace)
return
}
// Note: Docker containers can have a "FinishedAt" time set even when
// they're running. That happens when they've been stopped and then
// restarted. "FinishedAt" corresponds to the last time the container was
// stopped.
if !container.State.Running && !container.State.FinishedAt.IsZero() {
finishedAt := container.State.FinishedAt
excludeAge := time.Duration(config.Datadog.GetInt("container_exclude_stopped_age")) * time.Hour
if time.Since(finishedAt) > excludeAge {
log.Debugf("container %q not running for too long, skipping", container.ID)
return
}
}
ports := make([]ContainerPort, 0, len(container.Ports))
for _, port := range container.Ports {
ports = append(ports, ContainerPort{
Port: port.Port,
Name: port.Name,
})
}
sort.Slice(ports, func(i, j int) bool {
return ports[i].Port < ports[j].Port
})
entity := containers.BuildEntityName(string(container.Runtime), container.ID)
svc := &service{
entity: container,
ready: pod.Ready,
ports: ports,
extraConfig: map[string]string{
"pod_name": pod.Name,
"namespace": pod.Namespace,
"pod_uid": pod.ID,
},
hosts: map[string]string{"pod": pod.IP},
// Exclude non-running containers (including init containers)
// from metrics collection but keep them for collecting logs.
metricsExcluded: l.IsExcluded(
containers.MetricsFilter,
pod.Annotations,
containerName,
containerImg.RawName,
pod.Namespace,
) || !container.State.Running,
logsExcluded: l.IsExcluded(
containers.LogsFilter,
pod.Annotations,
containerName,
containerImg.RawName,
pod.Namespace,
),
}
adIdentifier := containerName
if customADID, found := utils.ExtractCheckIDFromPodAnnotations(pod.Annotations, containerName); found {
adIdentifier = customADID
svc.adIdentifiers = append(svc.adIdentifiers, customADID)
}
svc.adIdentifiers = append(svc.adIdentifiers, entity, containerImg.RawName)
if len(containerImg.ShortName) > 0 && containerImg.ShortName != containerImg.RawName {
svc.adIdentifiers = append(svc.adIdentifiers, containerImg.ShortName)
}
var err error
svc.checkNames, err = utils.ExtractCheckNamesFromPodAnnotations(pod.Annotations, adIdentifier)
if err != nil {
log.Error(err.Error())
}
svcID := buildSvcID(container.GetID())
podSvcID := buildSvcID(pod.GetID())
l.AddService(svcID, svc, podSvcID)
}