@@ -356,9 +356,35 @@ func (cgc *containerGC) evictPodLogsDirectories(allSourcesReady bool) error {
356
356
logSymlinks , _ := osInterface .Glob (filepath .Join (legacyContainerLogsDir , fmt .Sprintf ("*.%s" , legacyLogSuffix )))
357
357
for _ , logSymlink := range logSymlinks {
358
358
if _ , err := osInterface .Stat (logSymlink ); os .IsNotExist (err ) {
359
+ if containerID , err := getContainerIDFromLegacyLogSymlink (logSymlink ); err == nil {
360
+ status , err := cgc .manager .runtimeService .ContainerStatus (containerID )
361
+ if err != nil {
362
+ // TODO: we should handle container not found (i.e. container was deleted) case differently
363
+ // once https://github.com/kubernetes/kubernetes/issues/63336 is resolved
364
+ klog .Infof ("Error getting ContainerStatus for containerID %q: %v" , containerID , err )
365
+ } else if status .State != runtimeapi .ContainerState_CONTAINER_EXITED {
366
+ // Here is how container log rotation works (see containerLogManager#rotateLatestLog):
367
+ //
368
+ // 1. rename current log to rotated log file whose filename contains current timestamp (fmt.Sprintf("%s.%s", log, timestamp))
369
+ // 2. reopen the container log
370
+ // 3. if #2 fails, rename rotated log file back to container log
371
+ //
372
+ // There is small but indeterministic amount of time during which log file doesn't exist (between steps #1 and #2, between #1 and #3).
373
+ // Hence the symlink may be deemed unhealthy during that period.
374
+ // See https://github.com/kubernetes/kubernetes/issues/52172
375
+ //
376
+ // We only remove unhealthy symlink for dead containers
377
+ klog .V (5 ).Infof ("Container %q is still running, not removing symlink %q." , containerID , logSymlink )
378
+ continue
379
+ }
380
+ } else {
381
+ klog .V (4 ).Infof ("unable to obtain container Id: %v" , err )
382
+ }
359
383
err := osInterface .Remove (logSymlink )
360
384
if err != nil {
361
385
klog .Errorf ("Failed to remove container log dead symlink %q: %v" , logSymlink , err )
386
+ } else {
387
+ klog .V (4 ).Infof ("removed symlink %s" , logSymlink )
362
388
}
363
389
}
364
390
}
0 commit comments