/
kubernetes_processor.go
390 lines (363 loc) · 16.3 KB
/
kubernetes_processor.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
package k8sprocessor
import (
"strconv"
"go.uber.org/zap"
"github.com/Kindling-project/kindling/collector/pkg/component"
"github.com/Kindling-project/kindling/collector/pkg/component/consumer"
"github.com/Kindling-project/kindling/collector/pkg/component/consumer/processor"
"github.com/Kindling-project/kindling/collector/pkg/metadata/kubernetes"
"github.com/Kindling-project/kindling/collector/pkg/model"
"github.com/Kindling-project/kindling/collector/pkg/model/constlabels"
"github.com/Kindling-project/kindling/collector/pkg/model/constnames"
mpclient "github.com/Kindling-project/kindling/collector/pkg/metadata/metaprovider/client"
)
const (
K8sMetadata = "k8smetadataprocessor"
loopbackIp = "127.0.0.1"
)
type K8sMetadataProcessor struct {
config *Config
metadata *kubernetes.K8sMetaDataCache
nextConsumer consumer.Consumer
localNodeIp string
localNodeName string
telemetry *component.TelemetryTools
}
func NewKubernetesProcessor(cfg interface{}, telemetry *component.TelemetryTools, nextConsumer consumer.Consumer) processor.Processor {
config, ok := cfg.(*Config)
if !ok {
telemetry.Logger.Panic("Cannot convert Component config", zap.String("componentType", K8sMetadata))
}
if !config.Enable {
telemetry.Logger.Info("The kubernetes processor is disabled by the configuration. Won't connect to the API-server and no Kubernetes metadata will be fetched.")
return &K8sMetadataProcessor{
config: config,
metadata: kubernetes.MetaDataCache,
nextConsumer: nextConsumer,
telemetry: telemetry,
}
}
var options []kubernetes.Option
options = append(options,
kubernetes.WithAuthType(config.KubeAuthType),
kubernetes.WithKubeConfigDir(config.KubeConfigDir),
kubernetes.WithGraceDeletePeriod(config.GraceDeletePeriod),
kubernetes.WithFetchReplicaSet(config.EnableFetchReplicaSet),
)
if config.MetaDataProviderConfig != nil && config.MetaDataProviderConfig.Enable {
cli := mpclient.NewMetaDataWrapperClient(config.MetaDataProviderConfig.Endpoint, config.MetaDataProviderConfig.EnableTrace)
options = append(options, kubernetes.WithMetaDataProviderConfig(config.MetaDataProviderConfig, cli.ListAndWatch))
}
err := kubernetes.InitK8sHandler(options...)
if err != nil {
telemetry.Logger.Panicf("Failed to initialize [%s]: %v. Set the option 'enable' false if you want to run the agent in the non-Kubernetes environment.", K8sMetadata, err)
return nil
}
var localNodeIp, localNodeName string
if localNodeIp, err = getHostIpFromEnv(); err != nil {
telemetry.Logger.Warn("Local NodeIp can not found", zap.Error(err))
}
if localNodeName, err = getHostNameFromEnv(); err != nil {
telemetry.Logger.Warn("Local NodeName can not found", zap.Error(err))
}
return &K8sMetadataProcessor{
config: config,
metadata: kubernetes.MetaDataCache,
nextConsumer: nextConsumer,
localNodeIp: localNodeIp,
localNodeName: localNodeName,
telemetry: telemetry,
}
}
func (p *K8sMetadataProcessor) Consume(dataGroup *model.DataGroup) error {
if !p.config.Enable {
return p.nextConsumer.Consume(dataGroup)
}
name := dataGroup.Name
switch name {
case constnames.NetRequestMetricGroupName:
p.processNetRequestMetric(dataGroup)
case constnames.TcpRttMetricGroupName:
fallthrough
case constnames.TcpRetransmitMetricGroupName:
fallthrough
case constnames.TcpDropMetricGroupName:
p.processTcpMetric(dataGroup)
default:
p.processNetRequestMetric(dataGroup)
}
return p.nextConsumer.Consume(dataGroup)
}
func (p *K8sMetadataProcessor) processNetRequestMetric(dataGroup *model.DataGroup) {
isServer := dataGroup.Labels.GetBoolValue(constlabels.IsServer)
if isServer {
p.addK8sMetaDataForServerLabel(dataGroup.Labels)
} else {
p.addK8sMetaDataForClientLabel(dataGroup.Labels)
}
}
func (p *K8sMetadataProcessor) processTcpMetric(dataGroup *model.DataGroup) {
p.addK8sMetaDataViaIp(dataGroup.Labels)
}
func (p *K8sMetadataProcessor) addK8sMetaDataForClientLabel(labelMap *model.AttributeMap) {
// add metadata for src
containerId := labelMap.GetStringValue(constlabels.ContainerId)
if containerId != "" {
labelMap.UpdateAddStringValue(constlabels.SrcContainerId, containerId)
resInfo, ok := p.metadata.GetByContainerId(containerId)
if ok {
addContainerMetaInfoLabelSRC(labelMap, resInfo)
} else {
labelMap.UpdateAddStringValue(constlabels.SrcNodeIp, p.localNodeIp)
labelMap.UpdateAddStringValue(constlabels.SrcNode, p.localNodeName)
labelMap.UpdateAddStringValue(constlabels.SrcNamespace, constlabels.InternalClusterNamespace)
}
} else {
srcIp := labelMap.GetStringValue(constlabels.SrcIp)
if srcIp == loopbackIp {
labelMap.UpdateAddStringValue(constlabels.SrcNodeIp, p.localNodeIp)
labelMap.UpdateAddStringValue(constlabels.SrcNode, p.localNodeName)
}
podInfo, ok := p.metadata.GetPodByIp(srcIp)
if ok {
addPodMetaInfoLabelSRC(labelMap, podInfo)
} else {
if nodeName, ok := p.metadata.GetNodeNameByIp(srcIp); ok {
labelMap.UpdateAddStringValue(constlabels.SrcNodeIp, srcIp)
labelMap.UpdateAddStringValue(constlabels.SrcNode, nodeName)
labelMap.UpdateAddStringValue(constlabels.SrcNamespace, constlabels.InternalClusterNamespace)
} else {
labelMap.UpdateAddStringValue(constlabels.SrcNamespace, constlabels.ExternalClusterNamespace)
}
}
}
// add metadata for dst
dstIp := labelMap.GetStringValue(constlabels.DstIp)
if dstIp == loopbackIp {
labelMap.UpdateAddStringValue(constlabels.DstNodeIp, p.localNodeIp)
labelMap.UpdateAddStringValue(constlabels.DstNode, p.localNodeName)
// If the dst IP is a loopback address, we use its src IP for further searching.
dstIp = labelMap.GetStringValue(constlabels.SrcIp)
}
dstPort := labelMap.GetIntValue(constlabels.DstPort)
// DstIp is IP of a service
if svcInfo, ok := p.metadata.GetServiceByIpPort(dstIp, uint32(dstPort)); ok {
labelMap.UpdateAddStringValue(constlabels.DstNamespace, svcInfo.Namespace)
labelMap.UpdateAddStringValue(constlabels.DstService, svcInfo.ServiceName)
labelMap.UpdateAddStringValue(constlabels.DstWorkloadKind, svcInfo.WorkloadKind)
labelMap.UpdateAddStringValue(constlabels.DstWorkloadName, svcInfo.WorkloadName)
// find podInfo using dnat_ip
dNatIp := labelMap.GetStringValue(constlabels.DnatIp)
dNatPort := labelMap.GetIntValue(constlabels.DnatPort)
if dNatIp != "" && dNatPort != -1 {
resInfo, ok := p.metadata.GetContainerByIpPort(dNatIp, uint32(dNatPort))
if ok {
addContainerMetaInfoLabelDST(labelMap, resInfo)
} else {
// maybe dnat_ip is NodeIP
if nodeName, ok := p.metadata.GetNodeNameByIp(dNatIp); ok {
labelMap.UpdateAddStringValue(constlabels.DstNodeIp, dNatIp)
labelMap.UpdateAddStringValue(constlabels.DstNode, nodeName)
}
}
}
} else if resInfo, ok := p.metadata.GetContainerByIpPort(dstIp, uint32(dstPort)); ok {
// DstIp is IP of a container
addContainerMetaInfoLabelDST(labelMap, resInfo)
} else if resInfo, ok := p.metadata.GetContainerByHostIpPort(dstIp, uint32(dstPort)); ok {
addContainerMetaInfoLabelDST(labelMap, resInfo)
labelMap.UpdateAddStringValue(constlabels.DstIp, resInfo.RefPodInfo.Ip)
labelMap.UpdateAddIntValue(constlabels.DstPort, int64(resInfo.HostPortMap[int32(dstPort)]))
labelMap.UpdateAddStringValue(constlabels.DstService, dstIp+":"+strconv.Itoa(int(dstPort)))
} else {
// DstIp is a IP from external
if nodeName, ok := p.metadata.GetNodeNameByIp(dstIp); ok {
labelMap.UpdateAddStringValue(constlabels.DstNodeIp, dstIp)
labelMap.UpdateAddStringValue(constlabels.DstNode, nodeName)
labelMap.UpdateAddStringValue(constlabels.DstNamespace, constlabels.InternalClusterNamespace)
} else {
labelMap.UpdateAddStringValue(constlabels.DstNamespace, constlabels.ExternalClusterNamespace)
}
}
}
func (p *K8sMetadataProcessor) addK8sMetaDataForServerLabel(labelMap *model.AttributeMap) {
srcIp := labelMap.GetStringValue(constlabels.SrcIp)
if srcIp == loopbackIp {
labelMap.UpdateAddStringValue(constlabels.SrcNodeIp, p.localNodeIp)
labelMap.UpdateAddStringValue(constlabels.SrcNode, p.localNodeName)
}
podInfo, ok := p.metadata.GetPodByIp(srcIp)
if ok {
addPodMetaInfoLabelSRC(labelMap, podInfo)
} else {
if nodeName, ok := p.metadata.GetNodeNameByIp(srcIp); ok {
labelMap.UpdateAddStringValue(constlabels.SrcNodeIp, srcIp)
labelMap.UpdateAddStringValue(constlabels.SrcNode, nodeName)
labelMap.UpdateAddStringValue(constlabels.SrcNamespace, constlabels.InternalClusterNamespace)
} else {
labelMap.UpdateAddStringValue(constlabels.SrcNamespace, constlabels.ExternalClusterNamespace)
}
}
containerId := labelMap.GetStringValue(constlabels.ContainerId)
labelMap.UpdateAddStringValue(constlabels.DstContainerId, containerId)
containerInfo, ok := p.metadata.GetByContainerId(containerId)
if ok {
addContainerMetaInfoLabelDST(labelMap, containerInfo)
if containerInfo.RefPodInfo.ServiceInfo != nil {
labelMap.UpdateAddStringValue(constlabels.DstService, containerInfo.RefPodInfo.ServiceInfo.ServiceName)
}
} else {
labelMap.UpdateAddStringValue(constlabels.DstNodeIp, p.localNodeIp)
labelMap.UpdateAddStringValue(constlabels.DstNode, p.localNodeName)
labelMap.UpdateAddStringValue(constlabels.DstNamespace, constlabels.InternalClusterNamespace)
}
}
// addK8sMetaDataViaIp is used to add k8s metadata to tcp metrics.
// There is also a piece of code for removing "port" in this method, which
// should be moved into a processor that is used for relabeling tcp metrics later.
func (p *K8sMetadataProcessor) addK8sMetaDataViaIp(labelMap *model.AttributeMap) {
// Both Src and Dst should try:
// 1. (Only Dst)Use Ip Port to find Service (when found a Service,also use DNatIp to find the Pod)
// 2. Use Ip Port to find Container And Pod
// 3. Use Ip to find Pod
// add metadata for src
p.addK8sMetaDataViaIpSRC(labelMap)
// add metadata for dst
p.addK8sMetaDataViaIpDST(labelMap)
// We only care about the real connection, so here replace DstIp/DstPort with DNatIp/DNatPort
if labelMap.GetStringValue(constlabels.DnatIp) != "" {
labelMap.AddStringValue(constlabels.DstIp, labelMap.GetStringValue(constlabels.DnatIp))
}
if labelMap.GetIntValue(constlabels.DnatPort) > 0 {
labelMap.AddIntValue(constlabels.DstPort, labelMap.GetIntValue(constlabels.DnatPort))
}
labelMap.RemoveAttribute(constlabels.DnatIp)
labelMap.RemoveAttribute(constlabels.DnatPort)
// Metric shouldn't contain high-cardinality labels, so here we want to remove
// the dynamic port label and retain the listening one. But we can't know which
// port is dynamic for sure, so we work around that by comparing their number size.
//
// The default dynamic port range in /proc/sys/net/ipv4/ip_local_port_range is 32768~60999.
// At most cases, the larger port is the dynamic port and the other one is the listening port.
// But sometimes the listening port is also greater than 32768 in which case there is no way to
// tell which one is listening.
var defaultMinLocalPort int64 = 32768
srcPort := labelMap.GetIntValue(constlabels.SrcPort)
dstPort := labelMap.GetIntValue(constlabels.DstPort)
// If they are both smaller than 32768 then we remove the much smaller one.
if srcPort < defaultMinLocalPort && dstPort < defaultMinLocalPort {
if srcPort > dstPort {
labelMap.RemoveAttribute(constlabels.SrcPort)
} else {
labelMap.RemoveAttribute(constlabels.DstPort)
}
} else {
// Otherwise, we remove the port that is larger than 32768.
if srcPort >= defaultMinLocalPort {
labelMap.RemoveAttribute(constlabels.SrcPort)
}
if dstPort >= defaultMinLocalPort {
labelMap.RemoveAttribute(constlabels.DstPort)
}
}
}
func (p *K8sMetadataProcessor) addK8sMetaDataViaIpSRC(labelMap *model.AttributeMap) {
// 1. Use Ip Port to find Container And Pod
// 2. Use Ip to find Pod
srcIp := labelMap.GetStringValue(constlabels.SrcIp)
srcPort := labelMap.GetIntValue(constlabels.SrcPort)
srcContainerInfo, ok := p.metadata.GetContainerByIpPort(srcIp, uint32(srcPort))
if ok {
addContainerMetaInfoLabelSRC(labelMap, srcContainerInfo)
return
}
srcPodInfo, ok := p.metadata.GetPodByIp(srcIp)
if ok {
addPodMetaInfoLabelSRC(labelMap, srcPodInfo)
return
}
if _, ok := p.metadata.GetNodeNameByIp(srcIp); ok {
labelMap.UpdateAddStringValue(constlabels.SrcNamespace, constlabels.InternalClusterNamespace)
} else {
labelMap.UpdateAddStringValue(constlabels.SrcNamespace, constlabels.ExternalClusterNamespace)
}
}
func (p *K8sMetadataProcessor) addK8sMetaDataViaIpDST(labelMap *model.AttributeMap) {
// 1. (Only Dst)Use Ip Port to find Service (when found a Service,also use DNatIp to find the Pod)
// 2. Use Ip Port to find Container And Pod
// 3. Use Ip to find Pod
dstIp := labelMap.GetStringValue(constlabels.DstIp)
dstPort := labelMap.GetIntValue(constlabels.DstPort)
dstSvcInfo, ok := p.metadata.GetServiceByIpPort(dstIp, uint32(dstPort))
if ok {
labelMap.UpdateAddStringValue(constlabels.DstNamespace, dstSvcInfo.Namespace)
labelMap.UpdateAddStringValue(constlabels.DstService, dstSvcInfo.ServiceName)
labelMap.UpdateAddStringValue(constlabels.DstWorkloadKind, dstSvcInfo.WorkloadKind)
labelMap.UpdateAddStringValue(constlabels.DstWorkloadName, dstSvcInfo.WorkloadName)
// find podInfo using dnat_ip
dNatIp := labelMap.GetStringValue(constlabels.DnatIp)
dNatPort := labelMap.GetIntValue(constlabels.DnatPort)
if dNatIp != "" && dNatPort != -1 {
resInfo, ok := p.metadata.GetContainerByIpPort(dNatIp, uint32(dNatPort))
if ok {
addContainerMetaInfoLabelDST(labelMap, resInfo)
}
}
return
}
dstContainerInfo, ok := p.metadata.GetContainerByIpPort(dstIp, uint32(dstPort))
if ok {
addContainerMetaInfoLabelDST(labelMap, dstContainerInfo)
return
}
dstContainerInfo, ok = p.metadata.GetContainerByHostIpPort(dstIp, uint32(dstPort))
if ok {
addContainerMetaInfoLabelDST(labelMap, dstContainerInfo)
labelMap.UpdateAddStringValue(constlabels.DstIp, dstContainerInfo.RefPodInfo.Ip)
labelMap.UpdateAddIntValue(constlabels.DstPort, int64(dstContainerInfo.HostPortMap[int32(dstPort)]))
labelMap.UpdateAddStringValue(constlabels.DstService, dstIp+":"+strconv.Itoa(int(dstPort)))
}
dstPodInfo, ok := p.metadata.GetPodByIp(dstIp)
if ok {
addPodMetaInfoLabelDST(labelMap, dstPodInfo)
return
}
if _, ok := p.metadata.GetNodeNameByIp(dstIp); ok {
labelMap.UpdateAddStringValue(constlabels.DstNamespace, constlabels.InternalClusterNamespace)
} else {
labelMap.UpdateAddStringValue(constlabels.DstNamespace, constlabels.ExternalClusterNamespace)
}
}
func addContainerMetaInfoLabelSRC(labelMap *model.AttributeMap, containerInfo *kubernetes.K8sContainerInfo) {
labelMap.UpdateAddStringValue(constlabels.SrcContainer, containerInfo.Name)
labelMap.UpdateAddStringValue(constlabels.SrcContainerId, containerInfo.ContainerId)
addPodMetaInfoLabelSRC(labelMap, containerInfo.RefPodInfo)
}
func addPodMetaInfoLabelSRC(labelMap *model.AttributeMap, podInfo *kubernetes.K8sPodInfo) {
labelMap.UpdateAddStringValue(constlabels.SrcNode, podInfo.NodeName)
labelMap.UpdateAddStringValue(constlabels.SrcNodeIp, podInfo.NodeAddress)
labelMap.UpdateAddStringValue(constlabels.SrcNamespace, podInfo.Namespace)
labelMap.UpdateAddStringValue(constlabels.SrcWorkloadKind, podInfo.WorkloadKind)
labelMap.UpdateAddStringValue(constlabels.SrcWorkloadName, podInfo.WorkloadName)
labelMap.UpdateAddStringValue(constlabels.SrcPod, podInfo.PodName)
labelMap.UpdateAddStringValue(constlabels.SrcIp, podInfo.Ip)
if podInfo.ServiceInfo != nil {
labelMap.UpdateAddStringValue(constlabels.SrcService, podInfo.ServiceInfo.ServiceName)
}
}
func addContainerMetaInfoLabelDST(labelMap *model.AttributeMap, containerInfo *kubernetes.K8sContainerInfo) {
labelMap.UpdateAddStringValue(constlabels.DstContainer, containerInfo.Name)
labelMap.UpdateAddStringValue(constlabels.DstContainerId, containerInfo.ContainerId)
addPodMetaInfoLabelDST(labelMap, containerInfo.RefPodInfo)
}
func addPodMetaInfoLabelDST(labelMap *model.AttributeMap, podInfo *kubernetes.K8sPodInfo) {
labelMap.UpdateAddStringValue(constlabels.DstNode, podInfo.NodeName)
labelMap.UpdateAddStringValue(constlabels.DstNodeIp, podInfo.NodeAddress)
labelMap.UpdateAddStringValue(constlabels.DstNamespace, podInfo.Namespace)
labelMap.UpdateAddStringValue(constlabels.DstWorkloadKind, podInfo.WorkloadKind)
labelMap.UpdateAddStringValue(constlabels.DstWorkloadName, podInfo.WorkloadName)
labelMap.UpdateAddStringValue(constlabels.DstPod, podInfo.PodName)
if labelMap.GetStringValue(constlabels.DstIp) == "" {
labelMap.UpdateAddStringValue(constlabels.DstIp, podInfo.Ip)
}
}