UPSTREAM: 63160: kubelet: logs: do not wait when following terminated container
@@ -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
- }
}
}
}
