-
Notifications
You must be signed in to change notification settings - Fork 51
/
runtime_cache.go
169 lines (153 loc) · 3.98 KB
/
runtime_cache.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
package k8smonitor
import (
"context"
"sync"
"time"
"go.aporeto.io/enforcerd/trireme-lib/policy"
"go.uber.org/zap"
)
var (
// defaultLoopWait defines how often we loop over the entries to discover dead containers
defaultLoopWait = time.Second * 5
)
type runtimeCacheInterface interface {
Delete(sandboxID string)
Get(sandboxID string) policy.RuntimeReader
Set(sandboxID string, runtime policy.RuntimeReader) error
}
var _ runtimeCacheInterface = &runtimeCache{}
type runtimeCache struct {
sync.RWMutex
runtimes map[string]runtimeCacheEntry
loopWait time.Duration
stopEvent stopEventFunc
}
type runtimeCacheEntry struct {
runtime policy.RuntimeReader
running bool
}
func newRuntimeCache(ctx context.Context, stopEvent stopEventFunc) *runtimeCache {
c := &runtimeCache{
runtimes: make(map[string]runtimeCacheEntry),
loopWait: defaultLoopWait,
stopEvent: stopEvent,
}
if c.loopWait > 0 {
go c.loop(ctx)
}
return c
}
func makeSnapshot(m map[string]runtimeCacheEntry) map[string]policy.RuntimeReader {
snap := make(map[string]policy.RuntimeReader, len(m))
for k, v := range m {
if v.running {
snap[k] = v.runtime
}
}
return snap
}
// loop is very awkward: it implements a runtime poller that checks if all runtimes
// are actually still running. If not, it sends a stop event.
// NOTE: this must be deprecated once we have hooked into the OCI runtime hooks in the bundle!
func (c *runtimeCache) loop(ctx context.Context) {
loop:
for {
select {
case <-ctx.Done():
break loop
case <-time.After(c.loopWait):
c.RLock()
if len(c.runtimes) > 0 {
// take a snapshot
snap := makeSnapshot(c.runtimes)
c.RUnlock()
// and process them
c.processRuntimes(ctx, snap)
} else {
c.RUnlock()
}
}
}
}
// processRuntimes takes a snapshot of the runtimeCache, checks if the process is running, and sends a stop event if not
func (c *runtimeCache) processRuntimes(ctx context.Context, snap map[string]policy.RuntimeReader) {
for id, runtime := range snap {
pid := runtime.Pid()
if pid > 0 {
if running, err := sandboxIsRunning(pid); !running {
// if there has been error checking, just continue
if err != nil {
zap.L().Error("K8sMonitor: runtime poller: failed to check if sandbox is still running",
zap.String("sandboxID", id),
zap.Int("sandboxPid", pid),
zap.Error(err),
)
continue
}
zap.L().Debug("K8sMonitor: runtime poller: sandbox container must have stopped", zap.String("sandboxID", id), zap.Int("sandboxPid", pid), zap.Error(err))
// update the entry
c.Lock()
if _, ok := c.runtimes[id]; ok {
c.runtimes[id] = runtimeCacheEntry{
runtime: runtime,
running: false,
}
}
c.Unlock()
// fire away a stop event to the policy engine
// log an error as every caller should do, but continue normally
// there is nothing we can do about the error
go func(ctx context.Context, sandboxID string) {
if err := c.stopEvent(ctx, sandboxID); err != nil {
zap.L().Error("K8sMonitor: runtime poller: failed to send stop event to policy engine", zap.String("sandboxID", sandboxID), zap.Error(err))
}
}(ctx, id)
}
}
}
}
func (c *runtimeCache) Get(sandboxID string) policy.RuntimeReader {
if c == nil {
return nil
}
c.RLock()
defer c.RUnlock()
if c.runtimes == nil {
return nil
}
r, ok := c.runtimes[sandboxID]
if !ok {
return nil
}
// TODO: should return a clone, not a pointer
return r.runtime
}
func (c *runtimeCache) Set(sandboxID string, runtime policy.RuntimeReader) error {
if c == nil {
return errCacheUninitialized
}
if sandboxID == "" {
return errSandboxEmpty
}
if runtime == nil {
return errRuntimeNil
}
c.Lock()
defer c.Unlock()
if c.runtimes == nil {
return errCacheUninitialized
}
c.runtimes[sandboxID] = runtimeCacheEntry{
runtime: runtime,
running: true,
}
return nil
}
func (c *runtimeCache) Delete(sandboxID string) {
if c == nil {
return
}
c.Lock()
defer c.Unlock()
delete(c.runtimes, sandboxID)
}