Skip to content

Commit

Permalink
check for container running before following logs
Browse files Browse the repository at this point in the history
Signed-off-by: Ashish Amarnath <[email protected]>
  • Loading branch information
ashish-amarnath committed May 5, 2019
1 parent 3bef2a4 commit 2c66c06
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/client/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io"
"sync"
"time"

"github.com/pkg/errors"
"k8s.io/api/core/v1"
Expand Down Expand Up @@ -180,8 +181,35 @@ type logStreamer struct {
client kubernetes.Interface
}

func (l *logStreamer) waitForContainerRunning(pod *v1.Pod) error {
backoffSeconds := 1 * time.Second
for {
for _, cs := range pod.Status.ContainerStatuses {
if cs.Name == l.container && cs.State.Running != nil {
return nil
}
}

//TODO: add a time out
time.Sleep(backoffSeconds)
backoffSeconds *= 2
}
}

// stream will open a connection to the pod's logs and push messages onto a fan-in channel.
func (l *logStreamer) stream() {
if l.logOpts.Follow {
pod, err := l.client.CoreV1().Pods(l.ns).Get(l.pod, metav1.GetOptions{})
if err != nil {
l.errc <- errors.Wrapf(err, "failed to get pod [%s/%s]", l.ns, l.pod)
return
}
err = l.waitForContainerRunning(pod)
if err != nil {
l.errc <- errors.Wrapf(err, "failed to waiting for container [%v] in pod [%s/%s] to start running", l.container, l.ns, l.pod)
return
}
}
req := l.client.CoreV1().Pods(l.ns).GetLogs(l.pod, l.logOpts)
readCloser, err := req.Stream()
if err != nil {
Expand Down

0 comments on commit 2c66c06

Please sign in to comment.