Skip to content

Commit aafcf4e

Browse files
authored
Merge pull request kubernetes#128453 from tallclair/cacheless-pleg
Cleanup unused cacheless PLEG code
2 parents 648717c + 96aa71c commit aafcf4e

File tree

2 files changed

+52
-58
lines changed

2 files changed

+52
-58
lines changed

pkg/kubelet/pleg/generic.go

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ type podRecords map[types.UID]*podRecord
121121
func NewGenericPLEG(logger klog.Logger, runtime kubecontainer.Runtime, eventChannel chan *PodLifecycleEvent,
122122
relistDuration *RelistDuration, cache kubecontainer.Cache,
123123
clock clock.Clock) PodLifecycleEventGenerator {
124+
if cache == nil {
125+
panic("cache cannot be nil")
126+
}
124127
return &GenericPLEG{
125128
logger: logger,
126129
relistDuration: relistDuration,
@@ -265,45 +268,42 @@ func (g *GenericPLEG) Relist() {
265268
}
266269
}
267270

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)
272272

273273
// If there are events associated with a pod, we should update the
274274
// podCache.
275275
for pid, events := range eventsByPodID {
276276
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))
290277

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
293293

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
304303
}
305304
}
306305
}
306+
307307
// Update the internal storage and send out the events.
308308
g.podRecords.update(pid)
309309

@@ -324,7 +324,7 @@ func (g *GenericPLEG) Relist() {
324324
// Log exit code of containers when they finished in a particular event
325325
if events[i].Type == ContainerDied {
326326
// 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 {
328328
// Get updated podStatus
329329
status, err := g.cache.Get(pod.ID)
330330
if err == nil {
@@ -342,24 +342,22 @@ func (g *GenericPLEG) Relist() {
342342
}
343343
}
344344

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
355353
}
356354
}
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)
361355
}
362356

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+
363361
// make sure we retain the list of pods that need reinspecting the next time relist is called
364362
g.podsToReinspect = needsReinspection
365363
}
@@ -402,10 +400,6 @@ func computeEvents(logger klog.Logger, oldPod, newPod *kubecontainer.Pod, cid *k
402400
return generateEvents(logger, pid, cid.ID, oldState, newState)
403401
}
404402

405-
func (g *GenericPLEG) cacheEnabled() bool {
406-
return g.cache != nil
407-
}
408-
409403
// getPodIP preserves an older cached status' pod IP if the new status has no pod IPs
410404
// and its sandboxes have exited
411405
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
488482

489483
func (g *GenericPLEG) UpdateCache(pod *kubecontainer.Pod, pid types.UID) (error, bool) {
490484
ctx := context.Background()
491-
if !g.cacheEnabled() {
492-
return fmt.Errorf("pod cache disabled"), false
493-
}
494485
if pod == nil {
495486
return fmt.Errorf("pod cannot be nil"), false
496487
}

pkg/kubelet/pleg/generic_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
"k8s.io/apimachinery/pkg/types"
3434
"k8s.io/component-base/metrics/testutil"
35+
"k8s.io/klog/v2"
3536
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
3637
containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
3738
"k8s.io/kubernetes/pkg/kubelet/metrics"
@@ -57,16 +58,18 @@ func newTestGenericPLEG() *TestGenericPLEG {
5758

5859
func newTestGenericPLEGWithChannelSize(eventChannelCap int) *TestGenericPLEG {
5960
fakeRuntime := &containertest.FakeRuntime{}
61+
fakeCache := containertest.NewFakeCache(fakeRuntime)
6062
clock := testingclock.NewFakeClock(time.Time{})
6163
// The channel capacity should be large enough to hold all events in a
6264
// single test.
63-
pleg := &GenericPLEG{
64-
relistDuration: &RelistDuration{RelistPeriod: time.Hour, RelistThreshold: 3 * time.Minute},
65-
runtime: fakeRuntime,
66-
eventChannel: make(chan *PodLifecycleEvent, eventChannelCap),
67-
podRecords: make(podRecords),
68-
clock: clock,
69-
}
65+
pleg := NewGenericPLEG(
66+
klog.Logger{},
67+
fakeRuntime,
68+
make(chan *PodLifecycleEvent, eventChannelCap),
69+
&RelistDuration{RelistPeriod: time.Hour, RelistThreshold: 3 * time.Minute},
70+
fakeCache,
71+
clock,
72+
).(*GenericPLEG)
7073
return &TestGenericPLEG{pleg: pleg, runtime: fakeRuntime, clock: clock}
7174
}
7275

0 commit comments

Comments
 (0)