Merge pull request #63160 from sjenning/no-waitlogs-stopped-pod

Automatic merge from submit-queue (batch tested with PRs 63252, 63160). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

kubelet: logs: do not wait when following terminated container

Currently, a `kubectl logs -f` on a terminated container will output the logs, wait 5 seconds (`stateCheckPeriod`), then return.  The 5 seconds delay should not occur as the container is terminated and unable to generate additional log messages.

This PR puts a check at the beginning of `waitLogs()` to avoid doing the wait when the container is not running.

@derekwaynecarr @smarterclayton
This commit is contained in:
Kubernetes Submit Queue 2018-04-27 12:27:05 -07:00 committed by GitHub
commit 284e8182a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -354,9 +354,28 @@ func ReadLogs(path, containerID string, opts *LogOptions, runtimeService interna
}
}
func isContainerRunning(id string, r internalapi.RuntimeService) (bool, error) {
s, err := r.ContainerStatus(id)
if err != nil {
return false, err
}
// Only keep following container log when it is running.
if s.State != runtimeapi.ContainerState_CONTAINER_RUNNING {
glog.V(5).Infof("Container %q is not running (state=%q)", id, s.State)
// Do not return error because it's normal that the container stops
// during waiting.
return false, nil
}
return true, nil
}
// waitLogs wait for the next log write. It returns a boolean and an error. The boolean
// indicates whether a new log is found; the error is error happens during waiting new logs.
func waitLogs(id string, w *fsnotify.Watcher, runtimeService internalapi.RuntimeService) (bool, error) {
// no need to wait if the pod is not running
if running, err := isContainerRunning(id, runtimeService); !running {
return false, err
}
errRetry := 5
for {
select {
@ -374,17 +393,9 @@ func waitLogs(id string, w *fsnotify.Watcher, runtimeService internalapi.Runtime
}
errRetry--
case <-time.After(stateCheckPeriod):
s, err := runtimeService.ContainerStatus(id)
if err != nil {
if running, err := isContainerRunning(id, runtimeService); !running {
return false, err
}
// Only keep following container log when it is running.
if s.State != runtimeapi.ContainerState_CONTAINER_RUNNING {
glog.V(5).Infof("Container %q is not running (state=%q)", id, s.State)
// Do not return error because it's normal that the container stops
// during waiting.
return false, nil
}
}
}
}