-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathclient.go
357 lines (313 loc) · 9.54 KB
/
client.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
// 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://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 kube
import (
"fmt"
"regexp"
"strings"
"sync"
"time"
"go.opentelemetry.io/collector/translator/conventions"
"go.uber.org/zap"
api_v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sprocessor/observability"
)
// WatchClient is the main interface provided by this package to a kubernetes cluster.
type WatchClient struct {
m sync.RWMutex
deleteMut sync.Mutex
logger *zap.Logger
kc kubernetes.Interface
informer cache.SharedInformer
deploymentRegex *regexp.Regexp
deleteQueue []deleteRequest
stopCh chan struct{}
Pods map[string]*Pod
Rules ExtractionRules
Filters Filters
}
// Extract deployment name from the pod name. Pod name is created using
// format: [deployment-name]-[Random-String-For-ReplicaSet]-[Random-String-For-Pod]
var dRegex = regexp.MustCompile(`^(.*)-[0-9a-zA-Z]*-[0-9a-zA-Z]*$`)
// New initializes a new k8s Client.
func New(logger *zap.Logger, apiCfg k8sconfig.APIConfig, rules ExtractionRules, filters Filters, newClientSet APIClientsetProvider, newInformer InformerProvider) (Client, error) {
c := &WatchClient{logger: logger, Rules: rules, Filters: filters, deploymentRegex: dRegex, stopCh: make(chan struct{})}
go c.deleteLoop(time.Second*30, defaultPodDeleteGracePeriod)
c.Pods = map[string]*Pod{}
if newClientSet == nil {
newClientSet = k8sconfig.MakeClient
}
kc, err := newClientSet(apiCfg)
if err != nil {
return nil, err
}
c.kc = kc
labelSelector, fieldSelector, err := selectorsFromFilters(c.Filters)
if err != nil {
return nil, err
}
logger.Info(
"k8s filtering",
zap.String("labelSelector", labelSelector.String()),
zap.String("fieldSelector", fieldSelector.String()),
)
if newInformer == nil {
newInformer = newSharedInformer
}
c.informer = newInformer(c.kc, c.Filters.Namespace, labelSelector, fieldSelector)
return c, err
}
// Start registers pod event handlers and starts watching the kubernetes cluster for pod changes.
func (c *WatchClient) Start() {
c.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: c.handlePodAdd,
UpdateFunc: c.handlePodUpdate,
DeleteFunc: c.handlePodDelete,
})
c.informer.Run(c.stopCh)
}
// Stop signals the the k8s watcher/informer to stop watching for new events.
func (c *WatchClient) Stop() {
close(c.stopCh)
}
func (c *WatchClient) handlePodAdd(obj interface{}) {
observability.RecordPodAdded()
if pod, ok := obj.(*api_v1.Pod); ok {
c.addOrUpdatePod(pod)
} else {
c.logger.Error("object received was not of type api_v1.Pod", zap.Any("received", obj))
}
}
func (c *WatchClient) handlePodUpdate(old, new interface{}) {
observability.RecordPodUpdated()
if pod, ok := new.(*api_v1.Pod); ok {
// TODO: update or remove based on whether container is ready/unready?.
c.addOrUpdatePod(pod)
} else {
c.logger.Error("object received was not of type api_v1.Pod", zap.Any("received", new))
}
}
func (c *WatchClient) handlePodDelete(obj interface{}) {
observability.RecordPodDeleted()
if pod, ok := obj.(*api_v1.Pod); ok {
c.forgetPod(pod)
} else {
c.logger.Error("object received was not of type api_v1.Pod", zap.Any("received", obj))
}
}
func (c *WatchClient) deleteLoop(interval time.Duration, gracePeriod time.Duration) {
// This loop runs after N seconds and deletes pods from cache.
// It iterates over the delete queue and deletes all that aren't
// in the grace period anymore.
for {
select {
case <-time.After(interval):
var cutoff int
now := time.Now()
c.deleteMut.Lock()
for i, d := range c.deleteQueue {
if d.ts.Add(gracePeriod).After(now) {
break
}
cutoff = i + 1
}
toDelete := c.deleteQueue[:cutoff]
c.deleteQueue = c.deleteQueue[cutoff:]
c.deleteMut.Unlock()
c.m.Lock()
for _, d := range toDelete {
if p, ok := c.Pods[d.ip]; ok {
// Sanity check: make sure we are deleting the same pod
// and the underlying state (ip<>pod mapping) has not changed.
if p.Name == d.name {
delete(c.Pods, d.ip)
}
}
}
c.m.Unlock()
case <-c.stopCh:
return
}
}
}
// GetPodByIP takes an IP address and returns the pod the IP address is associated with.
func (c *WatchClient) GetPodByIP(ip string) (*Pod, bool) {
c.m.RLock()
pod, ok := c.Pods[ip]
c.m.RUnlock()
if ok {
if pod.Ignore {
return nil, false
}
return pod, ok
}
observability.RecordIPLookupMiss()
return nil, false
}
func (c *WatchClient) extractPodAttributes(pod *api_v1.Pod) map[string]string {
tags := map[string]string{}
if c.Rules.PodName {
tags[conventions.AttributeK8sPod] = pod.Name
}
if c.Rules.Namespace {
tags[conventions.AttributeK8sNamespace] = pod.GetNamespace()
}
if c.Rules.StartTime {
ts := pod.GetCreationTimestamp()
if !ts.IsZero() {
tags[tagStartTime] = ts.String()
}
}
if c.Rules.PodUID {
uid := pod.GetUID()
tags[conventions.AttributeK8sPodUID] = string(uid)
}
if c.Rules.Deployment {
// format: [deployment-name]-[Random-String-For-ReplicaSet]-[Random-String-For-Pod]
parts := c.deploymentRegex.FindStringSubmatch(pod.Name)
if len(parts) == 2 {
tags[conventions.AttributeK8sDeployment] = parts[1]
}
}
if c.Rules.Node {
tags[tagNodeName] = pod.Spec.NodeName
}
if c.Rules.Cluster {
clusterName := pod.GetClusterName()
if clusterName != "" {
tags[conventions.AttributeK8sCluster] = clusterName
}
}
for _, r := range c.Rules.Labels {
if v, ok := pod.Labels[r.Key]; ok {
tags[r.Name] = c.extractField(v, r)
}
}
for _, r := range c.Rules.Annotations {
if v, ok := pod.Annotations[r.Key]; ok {
tags[r.Name] = c.extractField(v, r)
}
}
return tags
}
func (c *WatchClient) extractField(v string, r FieldExtractionRule) string {
// Check if a subset of the field should be extracted with a regular expression
// instead of the whole field.
if r.Regex == nil {
return v
}
matches := r.Regex.FindStringSubmatch(v)
if len(matches) == 2 {
return matches[1]
}
return ""
}
func (c *WatchClient) addOrUpdatePod(pod *api_v1.Pod) {
if pod.Status.PodIP == "" {
return
}
c.m.Lock()
defer c.m.Unlock()
// compare initial scheduled timestamp for existing pod and new pod with same IP
// and only replace old pod if scheduled time of new pod is newer? This should fix
// the case where scheduler has assigned the same IP to a new pod but update event for
// the old pod came in later
if p, ok := c.Pods[pod.Status.PodIP]; ok {
if p.StartTime != nil && pod.Status.StartTime.Before(p.StartTime) {
return
}
}
newPod := &Pod{
Name: pod.Name,
Address: pod.Status.PodIP,
StartTime: pod.Status.StartTime,
}
if c.shouldIgnorePod(pod) {
newPod.Ignore = true
} else {
newPod.Attributes = c.extractPodAttributes(pod)
}
c.Pods[pod.Status.PodIP] = newPod
}
func (c *WatchClient) forgetPod(pod *api_v1.Pod) {
if pod.Status.PodIP == "" {
return
}
c.m.RLock()
p, ok := c.GetPodByIP(pod.Status.PodIP)
c.m.RUnlock()
if ok && p.Name == pod.Name {
c.deleteMut.Lock()
c.deleteQueue = append(c.deleteQueue, deleteRequest{
ip: pod.Status.PodIP,
name: pod.Name,
ts: time.Now(),
})
c.deleteMut.Unlock()
}
}
func (c *WatchClient) shouldIgnorePod(pod *api_v1.Pod) bool {
// Host network mode is not supported right now with IP based
// tagging as all pods in host network get same IP addresses.
// Such pods are very rare and usually are used to monitor or control
// host traffic (e.g, linkerd, flannel) instead of service business needs.
// We plan to support host network pods in future.
if pod.Spec.HostNetwork {
return true
}
// Check if user requested the pod to be ignored through annotations
if v, ok := pod.Annotations[ignoreAnnotation]; ok {
if strings.ToLower(strings.TrimSpace(v)) == "true" {
return true
}
}
// Check well known names that should be ignored
for _, rexp := range podNameIgnorePatterns {
if rexp.MatchString(pod.Name) {
return true
}
}
return false
}
func selectorsFromFilters(filters Filters) (labels.Selector, fields.Selector, error) {
labelSelector := labels.Everything()
for _, f := range filters.Labels {
r, err := labels.NewRequirement(f.Key, f.Op, []string{f.Value})
if err != nil {
return nil, nil, err
}
labelSelector = labelSelector.Add(*r)
}
var selectors []fields.Selector
for _, f := range filters.Fields {
switch f.Op {
case selection.Equals:
selectors = append(selectors, fields.OneTermEqualSelector(f.Key, f.Value))
case selection.NotEquals:
selectors = append(selectors, fields.OneTermNotEqualSelector(f.Key, f.Value))
default:
return nil, nil, fmt.Errorf("field filters don't support operator: '%s'", f.Op)
}
}
if filters.Node != "" {
selectors = append(selectors, fields.OneTermEqualSelector(podNodeField, filters.Node))
}
return labelSelector, fields.AndSelectors(selectors...), nil
}