From 4a82b999abcc609170a261dfbaefc9093e41e69c Mon Sep 17 00:00:00 2001 From: derailed Date: Thu, 2 Jul 2020 22:10:16 -0600 Subject: [PATCH] fix issue #790 --- internal/client/client.go | 4 ++-- internal/client/config.go | 10 ---------- internal/dao/dir_test.go | 19 +++++++++++++++++++ internal/dao/pod.go | 1 + internal/dao/testdata/dir/a.yml | 0 internal/dao/testdata/dir/a/b.yml | 0 internal/model/log.go | 1 - internal/view/dir_test.go | 16 ++++++++++++++++ internal/view/log.go | 7 ++++--- internal/view/log_int_test.go | 2 +- internal/view/svc_test.go | 6 ++++++ 11 files changed, 49 insertions(+), 17 deletions(-) create mode 100644 internal/dao/dir_test.go create mode 100644 internal/dao/testdata/dir/a.yml create mode 100644 internal/dao/testdata/dir/a/b.yml create mode 100644 internal/view/dir_test.go diff --git a/internal/client/client.go b/internal/client/client.go index 3728ef7f..5ca0eb70 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -231,6 +231,7 @@ func (a *APIClient) CheckConnectivity() bool { a.connOK = false return a.connOK } + cfg.Timeout = defaultCallTimeoutDuration client, err := kubernetes.NewForConfig(cfg) if err != nil { log.Error().Err(err).Msgf("Unable to connect to api server") @@ -278,8 +279,7 @@ func (a *APIClient) HasMetrics() bool { timeout, err := time.ParseDuration(*a.config.flags.Timeout) if err != nil { - log.Error().Err(err).Msgf("parsing duration") - return false + timeout = defaultCallTimeoutDuration } ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() diff --git a/internal/client/config.go b/internal/client/config.go index ed6b9b50..708de64e 100644 --- a/internal/client/config.go +++ b/internal/client/config.go @@ -18,7 +18,6 @@ const ( defaultQPS = 50 defaultBurst = 50 defaultCallTimeoutDuration time.Duration = 5 * time.Second - defaultCallTimeout = "5s" ) // Config tracks a kubernetes configuration. @@ -32,15 +31,6 @@ type Config struct { // NewConfig returns a new k8s config or an error if the flags are invalid. func NewConfig(f *genericclioptions.ConfigFlags) *Config { - timeout := defaultCallTimeout - if f.Timeout == nil { - f.Timeout = &timeout - } else { - _, err := time.ParseDuration(*f.Timeout) - if err != nil { - f.Timeout = &timeout - } - } return &Config{ flags: f, mutex: &sync.RWMutex{}, diff --git a/internal/dao/dir_test.go b/internal/dao/dir_test.go new file mode 100644 index 00000000..cd604484 --- /dev/null +++ b/internal/dao/dir_test.go @@ -0,0 +1,19 @@ +package dao_test + +import ( + "context" + "testing" + + "github.com/derailed/k9s/internal" + "github.com/derailed/k9s/internal/dao" + "github.com/stretchr/testify/assert" +) + +func TestNewDir(t *testing.T) { + d := dao.NewDir(nil) + ctx := context.WithValue(context.Background(), internal.KeyPath, "testdata/dir") + oo, err := d.List(ctx, "") + + assert.Nil(t, err) + assert.Equal(t, 2, len(oo)) +} diff --git a/internal/dao/pod.go b/internal/dao/pod.go index 79511b65..b03dabeb 100644 --- a/internal/dao/pod.go +++ b/internal/dao/pod.go @@ -127,6 +127,7 @@ func (p *Pod) Logs(path string, opts *v1.PodLogOptions) (*restclient.Request, er if err != nil { return nil, err } + ns, n := client.Namespaced(path) return dial.CoreV1().Pods(ns).GetLogs(n, opts), nil } diff --git a/internal/dao/testdata/dir/a.yml b/internal/dao/testdata/dir/a.yml new file mode 100644 index 00000000..e69de29b diff --git a/internal/dao/testdata/dir/a/b.yml b/internal/dao/testdata/dir/a/b.yml new file mode 100644 index 00000000..e69de29b diff --git a/internal/model/log.go b/internal/model/log.go index 7bfcf680..487d7608 100644 --- a/internal/model/log.go +++ b/internal/model/log.go @@ -161,7 +161,6 @@ func (l *Log) ClearFilter() { // Filter filters th:e model using either fuzzy or regexp. func (l *Log) Filter(q string) { - log.Debug().Msgf("Filter %q", q) l.mx.Lock() defer l.mx.Unlock() diff --git a/internal/view/dir_test.go b/internal/view/dir_test.go new file mode 100644 index 00000000..b47f12b1 --- /dev/null +++ b/internal/view/dir_test.go @@ -0,0 +1,16 @@ +package view_test + +import ( + "testing" + + "github.com/derailed/k9s/internal/view" + "github.com/stretchr/testify/assert" +) + +func TestDir(t *testing.T) { + v := view.NewDir("/fred") + + assert.Nil(t, v.Init(makeCtx())) + assert.Equal(t, "Directory", v.Name()) + assert.Equal(t, 7, len(v.Hints())) +} diff --git a/internal/view/log.go b/internal/view/log.go index ceffd670..e77911dd 100644 --- a/internal/view/log.go +++ b/internal/view/log.go @@ -243,13 +243,14 @@ func (l *Log) Logs() *Details { } // EOL tracks end of lines. -var EOL = []byte("\n") +var EOL = []byte{'\n'} // Flush write logs to viewer. func (l *Log) Flush(lines dao.LogItems) { - showTime := l.Indicator().showTime + log.Debug().Msgf("Flushing %d", len(lines)) ll := make([][]byte, len(lines)) - lines.Render(showTime, ll) + lines.Render(l.Indicator().showTime, ll) + _, _ = l.ansiWriter.Write(EOL) if _, err := l.ansiWriter.Write(bytes.Join(ll, EOL)); err != nil { log.Error().Err(err).Msgf("write log failed") } diff --git a/internal/view/log_int_test.go b/internal/view/log_int_test.go index 1c89ce69..d71413f9 100644 --- a/internal/view/log_int_test.go +++ b/internal/view/log_int_test.go @@ -66,7 +66,7 @@ func TestLogTimestamp(t *testing.T) { l.Logs().Clear() l.Flush(buff) - assert.Equal(t, fmt.Sprintf("%-30s %s", "ttt", "fred/blee:c1 Testing 1, 2, 3"), l.Logs().GetText(true)) + assert.Equal(t, fmt.Sprintf("\n%-30s %s", "ttt", "fred/blee:c1 Testing 1, 2, 3"), l.Logs().GetText(true)) assert.Equal(t, 2, list.change) assert.Equal(t, 2, list.clear) assert.Equal(t, 0, list.fail) diff --git a/internal/view/svc_test.go b/internal/view/svc_test.go index 07142fea..32fd6a2c 100644 --- a/internal/view/svc_test.go +++ b/internal/view/svc_test.go @@ -11,6 +11,12 @@ import ( ) func init() { + dao.MetaAccess.RegisterMeta("dir", metav1.APIResource{ + Name: "dir", + SingularName: "dir", + Kind: "Directory", + Categories: []string{"k9s"}, + }) dao.MetaAccess.RegisterMeta("v1/pods", metav1.APIResource{ Name: "pods", SingularName: "pod",