fix busted logger ;(

mine
derailed 2019-06-21 16:27:54 -06:00
parent f0df99c488
commit cab842c510
2 changed files with 6 additions and 17 deletions

View File

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"io" "io"
"strconv" "strconv"
"sync/atomic"
"time" "time"
"github.com/derailed/k9s/internal/color" "github.com/derailed/k9s/internal/color"
@ -171,15 +170,13 @@ func tailLogs(ctx context.Context, res k8s.Loggable, c chan<- string, opts LogOp
log.Debug().Msgf("Tailing logs for %q/%q:%q", opts.Namespace, opts.Name, opts.Container) log.Debug().Msgf("Tailing logs for %q/%q:%q", opts.Namespace, opts.Name, opts.Container)
o := v1.PodLogOptions{ o := v1.PodLogOptions{
Container: opts.Container, Container: opts.Container,
Follow: true,
TailLines: &opts.Lines, TailLines: &opts.Lines,
Previous: opts.Previous, Previous: opts.Previous,
} }
req := res.Logs(opts.Namespace, opts.Name, &o) req := res.Logs(opts.Namespace, opts.Name, &o)
req.Context(ctx) req.Context(ctx)
var blocked int32 = 1
go logsTimeout(blocked, c, opts)
// This call will block if nothing is in the stream!! // This call will block if nothing is in the stream!!
stream, err := req.Stream() stream, err := req.Stream()
if err != nil { if err != nil {
@ -187,7 +184,7 @@ func tailLogs(ctx context.Context, res k8s.Loggable, c chan<- string, opts LogOp
return err return err
} }
atomic.StoreInt32(&blocked, 0) // atomic.StoreInt32(&blocked, 0)
go readLogs(ctx, stream, c, opts) go readLogs(ctx, stream, c, opts)
return nil return nil
@ -195,32 +192,22 @@ func tailLogs(ctx context.Context, res k8s.Loggable, c chan<- string, opts LogOp
func readLogs(ctx context.Context, stream io.ReadCloser, c chan<- string, opts LogOptions) { func readLogs(ctx context.Context, stream io.ReadCloser, c chan<- string, opts LogOptions) {
defer func() { defer func() {
log.Debug().Msgf("Closing stream `%s", opts.Path()) log.Debug().Msgf(">>> Closing stream `%s", opts.Path())
stream.Close() stream.Close()
}() }()
scanner, head := bufio.NewScanner(stream), opts.NormalizeName() scanner, head := bufio.NewScanner(stream), opts.NormalizeName()
for scanner.Scan() { for scanner.Scan() {
txt := scanner.Text()
select { select {
case <-ctx.Done(): case <-ctx.Done():
return return
case c <- head + txt: case c <- head + scanner.Text():
default: default:
// Ensures we get back to scanning // Ensures we get back to scanning
} }
} }
} }
func logsTimeout(blocked int32, c chan<- string, opts LogOptions) {
select {
case <-time.After(defaultTimeout):
if atomic.LoadInt32(&blocked) == 1 {
log.Debug().Msgf("Closing channel %s:%s", opts.Name, opts.Container)
}
}
}
// List resources for a given namespace. // List resources for a given namespace.
func (r *Pod) List(ns string) (Columnars, error) { func (r *Pod) List(ns string) (Columnars, error) {
pods, err := r.Resource.List(ns) pods, err := r.Resource.List(ns)

View File

@ -138,9 +138,11 @@ func updateLogs(c <-chan string, l *logView, buffSize int) {
select { select {
case line, ok := <-c: case line, ok := <-c:
if !ok { if !ok {
log.Debug().Msgf("Closed channel detected. Bailing out...")
l.flush(index, buff) l.flush(index, buff)
return return
} }
log.Debug().Msgf("Got line %s", line)
if index < buffSize { if index < buffSize {
buff[index] = line buff[index] = line
index++ index++