Simplify PLEG relist loops

This commit is contained in:
Tim Allclair 2024-10-30 11:58:17 -07:00
parent 2caf4eddd8
commit 07a9ab87bc

View File

@ -253,27 +253,26 @@ func (g *GenericPLEG) Relist() {
updateRunningPodAndContainerMetrics(pods) updateRunningPodAndContainerMetrics(pods)
g.podRecords.setCurrent(pods) g.podRecords.setCurrent(pods)
// Compare the old and the current pods, and generate events. needsReinspection := make(map[types.UID]*kubecontainer.Pod)
eventsByPodID := map[types.UID][]*PodLifecycleEvent{}
for pid := range g.podRecords { for pid := range g.podRecords {
// Compare the old and the current pods, and generate events.
oldPod := g.podRecords.getOld(pid) oldPod := g.podRecords.getOld(pid)
pod := g.podRecords.getCurrent(pid) pod := g.podRecords.getCurrent(pid)
// Get all containers in the old and the new pod. // Get all containers in the old and the new pod.
allContainers := getContainersFromPods(oldPod, pod) allContainers := getContainersFromPods(oldPod, pod)
var events []*PodLifecycleEvent
for _, container := range allContainers { for _, container := range allContainers {
events := computeEvents(g.logger, oldPod, pod, &container.ID) containerEvents := computeEvents(g.logger, oldPod, pod, &container.ID)
for _, e := range events { events = append(events, containerEvents...)
updateEvents(eventsByPodID, e)
}
} }
}
needsReinspection := make(map[types.UID]*kubecontainer.Pod) _, reinspect := g.podsToReinspect[pid]
// If there are events associated with a pod, we should update the if len(events) == 0 && !reinspect {
// podCache. // Nothing else needed for this pod.
for pid, events := range eventsByPodID { continue
pod := g.podRecords.getCurrent(pid) }
// updateCache() will inspect the pod and update the cache. If an // updateCache() will inspect the pod and update the cache. If an
// error occurs during the inspection, we want PLEG to retry again // error occurs during the inspection, we want PLEG to retry again
@ -293,10 +292,6 @@ func (g *GenericPLEG) Relist() {
continue continue
} else { } else {
// this pod was in the list to reinspect and we did so because it had events, so remove it
// from the list (we don't want the reinspection code below to inspect it a second time in
// this relist execution)
delete(g.podsToReinspect, pid)
if utilfeature.DefaultFeatureGate.Enabled(features.EventedPLEG) { if utilfeature.DefaultFeatureGate.Enabled(features.EventedPLEG) {
if !updated { if !updated {
continue continue
@ -342,18 +337,6 @@ func (g *GenericPLEG) Relist() {
} }
} }
// reinspect any pods that failed inspection during the previous relist
if len(g.podsToReinspect) > 0 {
g.logger.V(5).Info("GenericPLEG: Reinspecting pods that previously failed inspection")
for pid, pod := range g.podsToReinspect {
if err, _ := g.updateCache(ctx, pod, pid); err != nil {
// Rely on updateCache calling GetPodStatus to log the actual error.
g.logger.V(5).Error(err, "PLEG: pod failed reinspection", "pod", klog.KRef(pod.Namespace, pod.Name))
needsReinspection[pid] = pod
}
}
}
// Update the cache timestamp. This needs to happen *after* // Update the cache timestamp. This needs to happen *after*
// all pods have been properly updated in the cache. // all pods have been properly updated in the cache.
g.cache.UpdateTime(timestamp) g.cache.UpdateTime(timestamp)