-
Notifications
You must be signed in to change notification settings - Fork 7
/
podbyipcache.go
146 lines (124 loc) · 3.14 KB
/
podbyipcache.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
package kube
import (
"context"
"fmt"
"sync"
"time"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/cache"
)
var (
// there are cases when connections might be kept in conntrack for 120 seconds
// https://www.kernel.org/doc/Documentation/networking/nf_conntrack-sysctl.txt
podTimeout = 2 * time.Minute
)
type PodByIPCache struct {
indexer cache.Indexer
// sorted by DeletionTimestamp in ascending order
deletedPods []*corev1.Pod
mu sync.Mutex
log logrus.FieldLogger
}
func (c *PodByIPCache) Get(key string) (*corev1.Pod, error) {
items, err := c.indexer.ByIndex(podIPIdx, key)
if err != nil {
return nil, err
}
if len(items) == 0 {
return nil, ErrNotFound
}
var earliestPod *corev1.Pod
for _, item := range items {
pod, ok := item.(*corev1.Pod)
if !ok {
continue
}
if earliestPod == nil || pod.CreationTimestamp.Before(&earliestPod.CreationTimestamp) {
earliestPod = pod
}
}
if earliestPod != nil {
return earliestPod, nil
}
return nil, ErrNotFound
}
func (c *PodByIPCache) onDelete(obj interface{}) {
pod, ok := obj.(*corev1.Pod)
if !ok {
return
}
c.mu.Lock()
defer c.mu.Unlock()
c.deletedPods = append(c.deletedPods, pod)
}
func (c *PodByIPCache) onAddOrUpdate(obj interface{}) {
pod, ok := obj.(*corev1.Pod)
if !ok {
return
}
err := c.indexer.Add(pod)
if err != nil {
c.log.Errorf("cannot add pod to cache %s", err)
}
}
func (c *PodByIPCache) cleanupExpired(ctx context.Context) {
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for {
select {
case <-ticker.C:
c.mu.Lock()
now := time.Now()
var i int
for i = 0; i < len(c.deletedPods); i++ {
pod := c.deletedPods[i]
// deletedPods should be sorted by DeletionTimeout in ascending order
// as soon as we observe a pod that is not yet timed out it is safe to break
// we shouldn't have pods with no deletion timestamp in this slice
if pod.DeletionTimestamp != nil && now.Sub(pod.DeletionTimestamp.Time) <= podTimeout {
break
}
err := c.indexer.Delete(pod)
if err != nil {
c.log.Errorf("cannot remove pod from cache %s", err)
}
}
c.deletedPods = c.deletedPods[i:]
c.mu.Unlock()
case <-ctx.Done():
return
}
}
}
func NewPodByIPCache(ctx context.Context, informer cache.SharedIndexInformer, log logrus.FieldLogger) *PodByIPCache {
if err := informer.SetTransform(defaultTransformFunc); err != nil {
panic(err)
}
indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{
podIPIdx: func(obj interface{}) ([]string, error) {
switch t := obj.(type) {
case *corev1.Pod:
return []string{t.Status.PodIP}, nil
}
return nil, fmt.Errorf("expected pod, got unknown type %T", obj)
},
})
c := &PodByIPCache{indexer: indexer, mu: sync.Mutex{}, log: log}
_, err := informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
DeleteFunc: func(obj interface{}) {
c.onDelete(obj)
},
AddFunc: func(obj interface{}) {
c.onAddOrUpdate(obj)
},
UpdateFunc: func(oldObj, newObj interface{}) {
c.onAddOrUpdate(newObj)
},
})
if err != nil {
panic(err)
}
go c.cleanupExpired(ctx)
return c
}