@@ -121,6 +121,9 @@ type podRecords map[types.UID]*podRecord
121
121
func NewGenericPLEG (logger klog.Logger , runtime kubecontainer.Runtime , eventChannel chan * PodLifecycleEvent ,
122
122
relistDuration * RelistDuration , cache kubecontainer.Cache ,
123
123
clock clock.Clock ) PodLifecycleEventGenerator {
124
+ if cache == nil {
125
+ panic ("cache cannot be nil" )
126
+ }
124
127
return & GenericPLEG {
125
128
logger : logger ,
126
129
relistDuration : relistDuration ,
@@ -265,45 +268,42 @@ func (g *GenericPLEG) Relist() {
265
268
}
266
269
}
267
270
268
- var needsReinspection map [types.UID ]* kubecontainer.Pod
269
- if g .cacheEnabled () {
270
- needsReinspection = make (map [types.UID ]* kubecontainer.Pod )
271
- }
271
+ needsReinspection := make (map [types.UID ]* kubecontainer.Pod )
272
272
273
273
// If there are events associated with a pod, we should update the
274
274
// podCache.
275
275
for pid , events := range eventsByPodID {
276
276
pod := g .podRecords .getCurrent (pid )
277
- if g .cacheEnabled () {
278
- // updateCache() will inspect the pod and update the cache. If an
279
- // error occurs during the inspection, we want PLEG to retry again
280
- // in the next relist. To achieve this, we do not update the
281
- // associated podRecord of the pod, so that the change will be
282
- // detect again in the next relist.
283
- // TODO: If many pods changed during the same relist period,
284
- // inspecting the pod and getting the PodStatus to update the cache
285
- // serially may take a while. We should be aware of this and
286
- // parallelize if needed.
287
- if err , updated := g .updateCache (ctx , pod , pid ); err != nil {
288
- // Rely on updateCache calling GetPodStatus to log the actual error.
289
- g .logger .V (4 ).Error (err , "PLEG: Ignoring events for pod" , "pod" , klog .KRef (pod .Namespace , pod .Name ))
290
277
291
- // make sure we try to reinspect the pod during the next relisting
292
- needsReinspection [pid ] = pod
278
+ // updateCache() will inspect the pod and update the cache. If an
279
+ // error occurs during the inspection, we want PLEG to retry again
280
+ // in the next relist. To achieve this, we do not update the
281
+ // associated podRecord of the pod, so that the change will be
282
+ // detect again in the next relist.
283
+ // TODO: If many pods changed during the same relist period,
284
+ // inspecting the pod and getting the PodStatus to update the cache
285
+ // serially may take a while. We should be aware of this and
286
+ // parallelize if needed.
287
+ if err , updated := g .updateCache (ctx , pod , pid ); err != nil {
288
+ // Rely on updateCache calling GetPodStatus to log the actual error.
289
+ g .logger .V (4 ).Error (err , "PLEG: Ignoring events for pod" , "pod" , klog .KRef (pod .Namespace , pod .Name ))
290
+
291
+ // make sure we try to reinspect the pod during the next relisting
292
+ needsReinspection [pid ] = pod
293
293
294
- continue
295
- } else {
296
- // this pod was in the list to reinspect and we did so because it had events, so remove it
297
- // from the list (we don't want the reinspection code below to inspect it a second time in
298
- // this relist execution)
299
- delete (g .podsToReinspect , pid )
300
- if utilfeature .DefaultFeatureGate .Enabled (features .EventedPLEG ) {
301
- if ! updated {
302
- continue
303
- }
294
+ continue
295
+ } else {
296
+ // this pod was in the list to reinspect and we did so because it had events, so remove it
297
+ // from the list (we don't want the reinspection code below to inspect it a second time in
298
+ // this relist execution)
299
+ delete (g .podsToReinspect , pid )
300
+ if utilfeature .DefaultFeatureGate .Enabled (features .EventedPLEG ) {
301
+ if ! updated {
302
+ continue
304
303
}
305
304
}
306
305
}
306
+
307
307
// Update the internal storage and send out the events.
308
308
g .podRecords .update (pid )
309
309
@@ -324,7 +324,7 @@ func (g *GenericPLEG) Relist() {
324
324
// Log exit code of containers when they finished in a particular event
325
325
if events [i ].Type == ContainerDied {
326
326
// Fill up containerExitCode map for ContainerDied event when first time appeared
327
- if len (containerExitCode ) == 0 && pod != nil && g . cache != nil {
327
+ if len (containerExitCode ) == 0 && pod != nil {
328
328
// Get updated podStatus
329
329
status , err := g .cache .Get (pod .ID )
330
330
if err == nil {
@@ -342,24 +342,22 @@ func (g *GenericPLEG) Relist() {
342
342
}
343
343
}
344
344
345
- if g .cacheEnabled () {
346
- // reinspect any pods that failed inspection during the previous relist
347
- if len (g .podsToReinspect ) > 0 {
348
- g .logger .V (5 ).Info ("GenericPLEG: Reinspecting pods that previously failed inspection" )
349
- for pid , pod := range g .podsToReinspect {
350
- if err , _ := g .updateCache (ctx , pod , pid ); err != nil {
351
- // Rely on updateCache calling GetPodStatus to log the actual error.
352
- g .logger .V (5 ).Error (err , "PLEG: pod failed reinspection" , "pod" , klog .KRef (pod .Namespace , pod .Name ))
353
- needsReinspection [pid ] = pod
354
- }
345
+ // reinspect any pods that failed inspection during the previous relist
346
+ if len (g .podsToReinspect ) > 0 {
347
+ g .logger .V (5 ).Info ("GenericPLEG: Reinspecting pods that previously failed inspection" )
348
+ for pid , pod := range g .podsToReinspect {
349
+ if err , _ := g .updateCache (ctx , pod , pid ); err != nil {
350
+ // Rely on updateCache calling GetPodStatus to log the actual error.
351
+ g .logger .V (5 ).Error (err , "PLEG: pod failed reinspection" , "pod" , klog .KRef (pod .Namespace , pod .Name ))
352
+ needsReinspection [pid ] = pod
355
353
}
356
354
}
357
-
358
- // Update the cache timestamp. This needs to happen *after*
359
- // all pods have been properly updated in the cache.
360
- g .cache .UpdateTime (timestamp )
361
355
}
362
356
357
+ // Update the cache timestamp. This needs to happen *after*
358
+ // all pods have been properly updated in the cache.
359
+ g .cache .UpdateTime (timestamp )
360
+
363
361
// make sure we retain the list of pods that need reinspecting the next time relist is called
364
362
g .podsToReinspect = needsReinspection
365
363
}
@@ -402,10 +400,6 @@ func computeEvents(logger klog.Logger, oldPod, newPod *kubecontainer.Pod, cid *k
402
400
return generateEvents (logger , pid , cid .ID , oldState , newState )
403
401
}
404
402
405
- func (g * GenericPLEG ) cacheEnabled () bool {
406
- return g .cache != nil
407
- }
408
-
409
403
// getPodIP preserves an older cached status' pod IP if the new status has no pod IPs
410
404
// and its sandboxes have exited
411
405
func (g * GenericPLEG ) getPodIPs (pid types.UID , status * kubecontainer.PodStatus ) []string {
@@ -488,9 +482,6 @@ func (g *GenericPLEG) updateCache(ctx context.Context, pod *kubecontainer.Pod, p
488
482
489
483
func (g * GenericPLEG ) UpdateCache (pod * kubecontainer.Pod , pid types.UID ) (error , bool ) {
490
484
ctx := context .Background ()
491
- if ! g .cacheEnabled () {
492
- return fmt .Errorf ("pod cache disabled" ), false
493
- }
494
485
if pod == nil {
495
486
return fmt .Errorf ("pod cannot be nil" ), false
496
487
}
0 commit comments