Skip to content

Commit 07a9ab8

Browse files
committed
Simplify PLEG relist loops
1 parent 2caf4ed commit 07a9ab8

File tree

1 file changed

+11
-28
lines changed

1 file changed

+11
-28
lines changed

pkg/kubelet/pleg/generic.go

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -253,27 +253,26 @@ func (g *GenericPLEG) Relist() {
253253
updateRunningPodAndContainerMetrics(pods)
254254
g.podRecords.setCurrent(pods)
255255

256-
// Compare the old and the current pods, and generate events.
257-
eventsByPodID := map[types.UID][]*PodLifecycleEvent{}
256+
needsReinspection := make(map[types.UID]*kubecontainer.Pod)
257+
258258
for pid := range g.podRecords {
259+
// Compare the old and the current pods, and generate events.
259260
oldPod := g.podRecords.getOld(pid)
260261
pod := g.podRecords.getCurrent(pid)
261262
// Get all containers in the old and the new pod.
262263
allContainers := getContainersFromPods(oldPod, pod)
264+
var events []*PodLifecycleEvent
263265
for _, container := range allContainers {
264-
events := computeEvents(g.logger, oldPod, pod, &container.ID)
265-
for _, e := range events {
266-
updateEvents(eventsByPodID, e)
267-
}
266+
containerEvents := computeEvents(g.logger, oldPod, pod, &container.ID)
267+
events = append(events, containerEvents...)
268268
}
269-
}
270269

271-
needsReinspection := make(map[types.UID]*kubecontainer.Pod)
270+
_, reinspect := g.podsToReinspect[pid]
272271

273-
// If there are events associated with a pod, we should update the
274-
// podCache.
275-
for pid, events := range eventsByPodID {
276-
pod := g.podRecords.getCurrent(pid)
272+
if len(events) == 0 && !reinspect {
273+
// Nothing else needed for this pod.
274+
continue
275+
}
277276

278277
// updateCache() will inspect the pod and update the cache. If an
279278
// error occurs during the inspection, we want PLEG to retry again
@@ -293,10 +292,6 @@ func (g *GenericPLEG) Relist() {
293292

294293
continue
295294
} 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)
300295
if utilfeature.DefaultFeatureGate.Enabled(features.EventedPLEG) {
301296
if !updated {
302297
continue
@@ -342,18 +337,6 @@ func (g *GenericPLEG) Relist() {
342337
}
343338
}
344339

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
353-
}
354-
}
355-
}
356-
357340
// Update the cache timestamp. This needs to happen *after*
358341
// all pods have been properly updated in the cache.
359342
g.cache.UpdateTime(timestamp)

0 commit comments

Comments
 (0)