-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlifecycle.go
More file actions
429 lines (373 loc) · 15.8 KB
/
lifecycle.go
File metadata and controls
429 lines (373 loc) · 15.8 KB
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package containerprofilemanager
import (
"context"
"fmt"
"time"
"github.com/cenkalti/backoff/v5"
containercollection "github.com/inspektor-gadget/inspektor-gadget/pkg/container-collection"
"github.com/kubescape/go-logger"
"github.com/kubescape/go-logger/helpers"
"github.com/kubescape/node-agent/pkg/objectcache"
"github.com/kubescape/node-agent/pkg/utils"
)
// ContainerCallback handles container lifecycle events
func (cpm *ContainerProfileManager) ContainerCallback(notif containercollection.PubSubEvent) {
switch notif.Type {
case containercollection.EventTypeAddContainer:
if utils.IsHostContainer(notif.Container) {
return
}
if cpm.cfg.IgnoreContainer(notif.Container.K8s.Namespace, notif.Container.K8s.PodName, notif.Container.K8s.PodLabels) {
return
}
go cpm.addContainerWithTimeout(notif.Container)
case containercollection.EventTypeRemoveContainer:
if cpm.cfg.IgnoreContainer(notif.Container.K8s.Namespace, notif.Container.K8s.PodName, notif.Container.K8s.PodLabels) {
return
}
go cpm.deleteContainer(notif.Container)
}
}
// addContainerWithTimeout handles adding a container with a timeout to prevent hanging
func (cpm *ContainerProfileManager) addContainerWithTimeout(container *containercollection.Container) {
containerID := container.Runtime.ContainerID
// Create container entry early with nil watchedContainerData
entry := &ContainerEntry{
data: &containerData{},
ready: make(chan struct{}),
}
cpm.addContainerEntry(containerID, entry)
ctx, cancel := context.WithTimeout(context.Background(), MaxWaitForSharedContainerData)
defer cancel()
done := make(chan error, 1)
go func() {
done <- cpm.addContainer(container, ctx)
}()
select {
case err := <-done:
if err != nil {
logger.L().Error("failed to add container to the container profile manager", helpers.Error(err))
// Close ready channel and remove entry on error
entry.readyOnce.Do(func() {
close(entry.ready)
})
cpm.removeContainerEntry(containerID)
}
case <-ctx.Done():
logger.L().Error("timeout while adding container to the container profile manager",
helpers.String("containerID", container.Runtime.ContainerID),
helpers.String("containerName", container.Runtime.ContainerName),
helpers.String("podName", container.K8s.PodName),
helpers.String("namespace", container.K8s.Namespace))
// Close ready channel and remove entry on timeout
entry.readyOnce.Do(func() {
close(entry.ready)
})
cpm.removeContainerEntry(containerID)
}
}
// addContainer adds a container to the container profile manager
func (cpm *ContainerProfileManager) addContainer(container *containercollection.Container, ctx context.Context) error {
containerID := container.Runtime.ContainerID
// Wait for shared container data with timeout
sharedData, err := cpm.waitForSharedContainerData(containerID, ctx)
if err != nil {
// Close ready channel and remove the container entry if we fail
if entry, exists := cpm.getContainerEntry(containerID); exists {
entry.readyOnce.Do(func() {
close(entry.ready)
})
}
cpm.removeContainerEntry(containerID)
return fmt.Errorf("failed to get shared data for container %s: %w", containerID, err)
}
// Check if the container should use a user-defined profile.
// When both an ApplicationProfile and a NetworkNeighborhood are
// user-provided, skip ALL recording — there is nothing to learn.
if sharedData.UserDefinedProfile != "" {
logger.L().Debug("ignoring container with a user-defined profile",
helpers.String("containerID", containerID),
helpers.String("containerName", container.Runtime.ContainerName),
helpers.String("podName", container.K8s.PodName),
helpers.String("namespace", container.K8s.Namespace),
helpers.String("userDefinedProfile", sharedData.UserDefinedProfile),
helpers.String("userDefinedNetwork", sharedData.UserDefinedNetwork))
// Close ready channel before removing entry
if entry, exists := cpm.getContainerEntry(containerID); exists {
entry.readyOnce.Do(func() {
close(entry.ready)
})
}
cpm.removeContainerEntry(containerID)
return nil
}
// Ignore ephemeral containers
if sharedData.ContainerType == objectcache.EphemeralContainer {
logger.L().Debug("ignoring ephemeral container",
helpers.String("containerID", containerID),
helpers.String("containerName", container.Runtime.ContainerName),
helpers.String("podName", container.K8s.PodName),
helpers.String("namespace", container.K8s.Namespace))
// Close ready channel before removing entry
if entry, exists := cpm.getContainerEntry(containerID); exists {
entry.readyOnce.Do(func() {
close(entry.ready)
})
}
cpm.removeContainerEntry(containerID)
return nil
}
if sharedData.PreRunningContainer && !(cpm.cfg.EnableRuntimeDetection || cpm.cfg.EnablePartialProfileGeneration) {
logger.L().Debug("ignoring pre-running container without runtime detection or partial profile generation",
helpers.String("containerID", containerID),
helpers.String("containerName", container.Runtime.ContainerName),
helpers.String("podName", container.K8s.PodName),
helpers.String("namespace", container.K8s.Namespace))
// Close ready channel before removing entry
if entry, exists := cpm.getContainerEntry(containerID); exists {
entry.readyOnce.Do(func() {
close(entry.ready)
})
}
cpm.removeContainerEntry(containerID)
return nil
}
// Update the existing container entry with watchedContainerData
entry, exists := cpm.getContainerEntry(containerID)
if !exists || entry.data == nil {
// Should not happen, but guard just in case
return fmt.Errorf("container entry missing for %s after shared data ready", containerID)
}
entry.mu.Lock()
entry.data.watchedContainerData = sharedData
entry.mu.Unlock()
// Set container data fields
cpm.setContainerData(container, sharedData)
// Setup monitoring timer
sniffingTime := cpm.calculateSniffingTime(container)
timer := time.AfterFunc(sniffingTime, func() {
cpm.handleContainerMaxTime(container)
})
// Store timer in container data for cleanup
entry.mu.Lock()
entry.data.timer = timer
entry.mu.Unlock()
// Start monitoring in separate goroutine
go cpm.startContainerMonitoring(container, sharedData)
// Signal that the container entry is ready
entry.readyOnce.Do(func() {
close(entry.ready)
})
logger.L().Debug("container added to container profile manager",
helpers.String("containerID", containerID),
helpers.String("workloadID", sharedData.Wlid),
helpers.String("containerName", container.Runtime.ContainerName),
helpers.String("podName", container.K8s.PodName),
helpers.String("namespace", container.K8s.Namespace))
return nil
}
// calculateSniffingTime determines how long to monitor a container
func (cpm *ContainerProfileManager) calculateSniffingTime(container *containercollection.Container) time.Duration {
sniffingTime := utils.AddJitter(cpm.cfg.MaxSniffingTime, cpm.cfg.MaxJitterPercentage)
if podLabelMaxSniffingTime, ok := container.K8s.PodLabels[MaxSniffingTimeLabel]; ok {
if duration, err := time.ParseDuration(podLabelMaxSniffingTime); err == nil {
sniffingTime = duration
} else {
logger.L().Debug("failed to parse pod label for max sniffing time",
helpers.String("podName", container.K8s.PodName),
helpers.String("namespace", container.K8s.Namespace),
helpers.String("podLabelMaxSniffingTime", podLabelMaxSniffingTime),
helpers.Error(err))
}
}
return sniffingTime
}
// handleContainerMaxTime handles when a container reaches its maximum sniffing time
func (cpm *ContainerProfileManager) handleContainerMaxTime(container *containercollection.Container) {
containerID := container.Runtime.ContainerID
logger.L().Debug("reached max sniffing time for container",
helpers.String("containerID", containerID),
helpers.String("containerName", container.Runtime.ContainerName),
helpers.String("podName", container.K8s.PodName),
helpers.String("namespace", container.K8s.Namespace))
var ackChan chan struct{}
err := cpm.withContainerNoSizeUpdate(containerID, func(data *containerData) error {
if data.watchedContainerData != nil {
// Send container max time signal (blocking send, safe because monitoring goroutine is always running)
data.watchedContainerData.SyncChannel <- ContainerReachedMaxTime
ackChan = data.watchedContainerData.AckChan
}
return nil
})
if ackChan != nil {
select {
case <-ackChan:
// Ack received
case <-time.After(MaxWaitForAck):
logger.L().Warning("timeout waiting for ack from monitoring goroutine after max time",
helpers.String("containerID", containerID))
}
}
if err == nil {
cpm.notifyContainerEndOfLife(container)
cpm.deleteContainer(container)
}
}
// deleteContainer removes a container from the container profile manager
func (cpm *ContainerProfileManager) deleteContainer(container *containercollection.Container) {
containerID := container.Runtime.ContainerID
// Get the container entry
entry, exists := cpm.getContainerEntry(containerID)
if !exists {
logger.L().Debug("container not found in container profile manager, skipping delete",
helpers.String("containerID", containerID),
helpers.String("containerName", container.Runtime.ContainerName),
helpers.String("podName", container.K8s.PodName),
helpers.String("namespace", container.K8s.Namespace))
return
}
// Wait for shared data to be available, this is needed to avoid race condition in case the container is deleted before the shared data is available
ctx, cancel := context.WithTimeout(context.Background(), MaxWaitForSharedContainerData)
defer cancel()
// Wait for either the container to be ready or timeout
select {
case <-entry.ready:
// Container is ready, proceed with deletion
case <-ctx.Done():
logger.L().Debug("timeout waiting for container to be ready, proceeding with deletion",
helpers.String("containerID", containerID),
helpers.String("containerName", container.Runtime.ContainerName),
helpers.String("podName", container.K8s.PodName),
helpers.String("namespace", container.K8s.Namespace))
}
var ackChan chan struct{}
// Clean up container resources
entry.mu.Lock()
if entry.data != nil {
// Stop timer if still running
if entry.data.timer != nil {
entry.data.timer.Stop()
entry.data.timer = nil
}
// Signal termination if monitoring is active
if entry.data.watchedContainerData != nil &&
entry.data.watchedContainerData.GetStatus() != objectcache.WatchedContainerStatusCompleted &&
entry.data.watchedContainerData.GetStatus() != objectcache.WatchedContainerStatusTooLarge {
// Set exit code based status if applicable
if objectcache.GetTerminationExitCode(cpm.k8sObjectCache, container.K8s.Namespace,
container.K8s.PodName, container.K8s.ContainerName, containerID) == 0 {
entry.data.watchedContainerData.SetStatus(objectcache.WatchedContainerStatusCompleted)
} else {
entry.data.watchedContainerData.SetStatus(objectcache.WatchedContainerStatusFailed)
}
// Send container termination signal (blocking send, safe because monitoring goroutine is always running)
entry.data.watchedContainerData.SyncChannel <- ContainerHasTerminatedError
ackChan = entry.data.watchedContainerData.AckChan
}
}
entry.mu.Unlock()
if ackChan != nil {
select {
case <-ackChan:
// Ack received
case <-time.After(MaxWaitForAck):
logger.L().Warning("timeout waiting for ack from monitoring goroutine after termination",
helpers.String("containerID", containerID))
}
}
// Remove the container entry from the map
cpm.removeContainerEntry(containerID)
entry.mu.Lock()
if entry.data != nil {
entry.data = nil // Clear data to free resources
}
entry.mu.Unlock()
logger.L().Debug("container deleted from container profile manager",
helpers.String("containerID", containerID))
}
// startContainerMonitoring starts monitoring a container
func (cpm *ContainerProfileManager) startContainerMonitoring(container *containercollection.Container, sharedData *objectcache.WatchedContainerData) {
if err := cpm.monitorContainer(container, sharedData); err != nil {
logger.L().Info("stopped recording container profile",
helpers.String("reason", err.Error()),
helpers.String("containerID", container.Runtime.ContainerID),
helpers.String("containerName", container.Runtime.ContainerName),
helpers.String("podName", container.K8s.PodName),
helpers.String("namespace", container.K8s.Namespace))
}
}
// setContainerData sets the container data for the container profile manager
func (cpm *ContainerProfileManager) setContainerData(container *containercollection.Container, sharedData *objectcache.WatchedContainerData) {
// Set completion status & status as soon as we start monitoring the container
if sharedData.PreRunningContainer {
sharedData.SetCompletionStatus(objectcache.WatchedContainerCompletionStatusPartial)
} else {
sharedData.SetCompletionStatus(objectcache.WatchedContainerCompletionStatusFull)
}
sharedData.SetStatus(objectcache.WatchedContainerStatusInitializing)
// Set series ID for the container
if sharedData.SeriesID == "" {
sharedData.SeriesID = createUUID()
}
// Set the sync channel
if sharedData.SyncChannel == nil {
sharedData.SyncChannel = make(chan error, 3) // 2 for (ContainerReachedMaxTime, ContainerHasTerminatedError) and 1 for queue errors
}
// Set the ack channel
if sharedData.AckChan == nil {
sharedData.AckChan = make(chan struct{}, 1) // 1 for (ContainerReachedMaxTime, ContainerHasTerminatedError)
}
// Set the update data ticker
if sharedData.UpdateDataTicker == nil {
sharedData.UpdateDataTicker = time.NewTicker(utils.AddJitter(cpm.cfg.InitialDelay, cpm.cfg.MaxJitterPercentage))
}
// Set the initial delay expired to false
sharedData.InitialDelayExpired = false
// Set the container id
if sharedData.ContainerID == "" {
sharedData.ContainerID = container.Runtime.ContainerID
}
// Set the mount namespace ID
if sharedData.NsMntId == 0 {
sharedData.NsMntId = container.Mntns
}
}
// waitForSharedContainerData waits for shared container data to be available
func (cpm *ContainerProfileManager) waitForSharedContainerData(containerID string, ctx context.Context) (*objectcache.WatchedContainerData, error) {
return backoff.Retry(ctx, func() (*objectcache.WatchedContainerData, error) {
if sharedData := cpm.k8sObjectCache.GetSharedContainerData(containerID); sharedData != nil {
return sharedData, nil
}
return nil, fmt.Errorf("container %s not found in shared data", containerID)
}, backoff.WithBackOff(backoff.NewExponentialBackOff()))
}
// RegisterForContainerEndOfLife registers a channel to receive notifications when a container reaches its max sniffing time
func (cpm *ContainerProfileManager) RegisterForContainerEndOfLife(notificationChannel chan *containercollection.Container) {
if notificationChannel == nil {
logger.L().Error("nil channel provided for container end of life notifications")
return
}
cpm.notificationMu.Lock()
defer cpm.notificationMu.Unlock()
cpm.maxSniffTimeNotificationChan = append(cpm.maxSniffTimeNotificationChan, notificationChannel)
logger.L().Debug("registered for container end of life notifications",
helpers.Int("currentChannelCount", len(cpm.maxSniffTimeNotificationChan)))
}
// notifyContainerEndOfLife notifies all registered channels about the end of life
func (cpm *ContainerProfileManager) notifyContainerEndOfLife(container *containercollection.Container) {
cpm.notificationMu.RLock()
channels := make([]chan *containercollection.Container, len(cpm.maxSniffTimeNotificationChan))
copy(channels, cpm.maxSniffTimeNotificationChan)
cpm.notificationMu.RUnlock()
for _, notifChan := range channels {
select {
case notifChan <- container:
default:
logger.L().Warning("notification channel for container end of life is full, skipping notification",
helpers.String("containerID", container.Runtime.ContainerID),
helpers.String("containerName", container.Runtime.ContainerName),
helpers.String("podName", container.K8s.PodName),
helpers.String("namespace", container.K8s.Namespace))
}
}
}