From 4045cb56a4200de7590f196a24ab81e4c92898e2 Mon Sep 17 00:00:00 2001 From: derailed Date: Wed, 28 Oct 2020 19:10:29 -0600 Subject: [PATCH] cleaning up --- internal/dao/patch_test.go | 33 ++++++++++++---------- internal/dao/pod.go | 11 ++++---- internal/model/log.go | 2 +- internal/render/helpers.go | 58 -------------------------------------- internal/render/pod.go | 10 +++---- internal/view/browser.go | 8 ++++-- internal/view/helpers.go | 3 -- internal/view/live_view.go | 7 +++-- internal/view/logger.go | 5 ++-- 9 files changed, 43 insertions(+), 94 deletions(-) diff --git a/internal/dao/patch_test.go b/internal/dao/patch_test.go index 439a3fc7..0fbda32a 100644 --- a/internal/dao/patch_test.go +++ b/internal/dao/patch_test.go @@ -1,15 +1,16 @@ package dao import ( - "github.com/stretchr/testify/require" "testing" + + "github.com/stretchr/testify/require" ) func TestGetTemplateJsonPatch(t *testing.T) { type args struct { imageSpecs ImageSpecs } - tests := map[string]struct { + uu := map[string]struct { args args want string wantErr bool @@ -35,14 +36,15 @@ func TestGetTemplateJsonPatch(t *testing.T) { wantErr: false, }, } - for name, tt := range tests { - t.Run(name, func(t *testing.T) { - got, err := GetTemplateJsonPatch(tt.args.imageSpecs) - if (err != nil) != tt.wantErr { - t.Errorf("GetTemplateJsonPatch() error = %v, wantErr %v", err, tt.wantErr) + for k := range uu { + u := uu[k] + t.Run(k, func(t *testing.T) { + got, err := GetTemplateJsonPatch(u.args.imageSpecs) + if (err != nil) != u.wantErr { + t.Errorf("GetTemplateJsonPatch() error = %v, wantErr %v", err, u.wantErr) return } - require.JSONEq(t, tt.want, string(got), "Json strings should be equal") + require.JSONEq(t, u.want, string(got), "Json strings should be equal") }) } } @@ -51,7 +53,7 @@ func TestGetJsonPatch(t *testing.T) { type args struct { imageSpecs ImageSpecs } - tests := map[string]struct { + uu := map[string]struct { args args want string wantErr bool @@ -77,14 +79,15 @@ func TestGetJsonPatch(t *testing.T) { wantErr: false, }, } - for name, tt := range tests { - t.Run(name, func(t *testing.T) { - got, err := GetJsonPatch(tt.args.imageSpecs) - if (err != nil) != tt.wantErr { - t.Errorf("GetTemplateJsonPatch() error = %v, wantErr %v", err, tt.wantErr) + for k := range uu { + u := uu[k] + t.Run(k, func(t *testing.T) { + got, err := GetJsonPatch(u.args.imageSpecs) + if (err != nil) != u.wantErr { + t.Errorf("GetTemplateJsonPatch() error = %v, wantErr %v", err, u.wantErr) return } - require.JSONEq(t, tt.want, string(got), "Json strings should be equal") + require.JSONEq(t, u.want, string(got), "Json strings should be equal") }) } } diff --git a/internal/dao/pod.go b/internal/dao/pod.go index 58457d6b..1ca85bf5 100644 --- a/internal/dao/pod.go +++ b/internal/dao/pod.go @@ -471,10 +471,11 @@ func (p *Pod) SetImages(ctx context.Context, path string, imageSpecs ImageSpecs) if !auth { return fmt.Errorf("user is not authorized to patch a deployment") } - if manager, isManaged, err := p.isControlled(path); isManaged { - if err != nil { - return err - } + manager, isManaged, err := p.isControlled(path) + if err != nil { + return err + } + if isManaged { return fmt.Errorf("Unable to set image. This pod is managed by %s. Please set the image on the controller", manager) } jsonPatch, err := GetJsonPatch(imageSpecs) @@ -501,7 +502,7 @@ func (p *Pod) isControlled(path string) (string, bool, error) { return "", false, err } references := pod.GetObjectMeta().GetOwnerReferences() - if len(references) != 0 && *references[0].Controller == true { + if len(references) > 0 { return fmt.Sprintf("%s/%s", references[0].Kind, references[0].Name), true, nil } return "", false, nil diff --git a/internal/model/log.go b/internal/model/log.go index ed5d959f..ede3968f 100644 --- a/internal/model/log.go +++ b/internal/model/log.go @@ -350,7 +350,7 @@ func (l *Log) applyFilter(q string) ([][]byte, error) { } func (l *Log) fireLogBuffChanged(lines dao.LogItems) { - ll := make([][]byte, len(l.lines)) + ll := make([][]byte, len(lines)) if l.filter == "" { l.lines.Render(l.logOptions.ShowTimestamp, ll) } else { diff --git a/internal/render/helpers.go b/internal/render/helpers.go index 994a949d..5b830995 100644 --- a/internal/render/helpers.go +++ b/internal/render/helpers.go @@ -13,7 +13,6 @@ import ( "github.com/rs/zerolog/log" "golang.org/x/text/language" "golang.org/x/text/message" - v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/duration" @@ -151,10 +150,6 @@ func AsPerc(p string) string { return "(" + p + ")" } -func printValAndPerc(v string, p int) string { - return strconv.Itoa(p) + "% (" + v + ")" -} - // PrintPerc prints a number as percentage. func PrintPerc(p int) string { return strconv.Itoa(p) + "%" @@ -262,25 +257,6 @@ func mapToIfc(m interface{}) (s string) { return } -func toCPUPerc(cpu, acpu *resource.Quantity) string { - c, ac := cpu.MilliValue(), acpu.MilliValue() - return toMc(c) + " " + AsPerc(strconv.Itoa(client.ToPercentage(c, ac))) -} - -func toMEMPerc(mem, amem *resource.Quantity) string { - m, am := mem.MilliValue(), amem.MilliValue() - return toMi(m) + " " + AsPerc(strconv.Itoa(client.ToPercentage(m, am))) -} - -func toResourcesMcPerc(res resources, a v1.ResourceList) string { - cpu := a.Cpu().MilliValue() - rcpu, lcpu := toResourcesMc(res) - return rcpu + - AsPerc(strconv.Itoa(client.ToPercentage(res[requestCPU].MilliValue(), cpu))) + ":" + - lcpu + - AsPerc(strconv.Itoa(client.ToPercentage(res[limitCPU].MilliValue(), cpu))) -} - func toMcPerc(v1, v2 *resource.Quantity) string { m := v1.MilliValue() return toMc(m) + " (" + @@ -309,40 +285,6 @@ func toMi(v int64) string { return p.Sprintf("%d", client.ToMB(v)) } -func toResourcesMc(res resources) (string, string) { - var v1, v2 int64 - if v, ok := res[requestCPU]; ok && v != nil { - v1 = v.MilliValue() - } - if v, ok := res[limitCPU]; ok && v != nil { - v2 = v.MilliValue() - } - if v1 == 0 && v2 == 0 { - return NAValue, NAValue - } - return toMc(v1), toMc(v2) -} - -func asMcStr(q *resource.Quantity) string { - if q == nil { - return ZeroValue - } - return toMc(q.MilliValue()) -} - -func asMiStr(q *resource.Quantity) string { - if q == nil { - return ZeroValue - } - return toMi(q.MilliValue()) -} - -// // ToMi returns the megabytes unit. -// func ToMi(v int64) string { -// p := message.NewPrinter(language.English) -// return p.Sprintf("%dMi", bytesToMb(v)) -// } - func boolPtrToStr(b *bool) string { if b == nil { return "false" diff --git a/internal/render/pod.go b/internal/render/pod.go index 07755873..c81f0982 100644 --- a/internal/render/pod.go +++ b/internal/render/pod.go @@ -18,9 +18,9 @@ import ( const ( requestCPU qualifiedResource = "rcpu" - requestMEM = "rmem" - limitCPU = "lcpu" - limitMEM = "lmem" + requestMEM qualifiedResource = "rmem" + limitCPU qualifiedResource = "lcpu" + limitMEM qualifiedResource = "lmem" ) type ( @@ -274,8 +274,8 @@ func newResourceList(cpu, mem *resource.Quantity) v1.ResourceList { func podRequests(spec v1.PodSpec) v1.ResourceList { cpu, mem := new(resource.Quantity), new(resource.Quantity) - for _, co := range spec.Containers { - rl := containerRequests(&co) + for i := range spec.Containers { + rl := containerRequests(&spec.Containers[i]) if rl.Cpu() != nil { cpu.Add(*rl.Cpu()) } diff --git a/internal/view/browser.go b/internal/view/browser.go index d8247b04..14e17461 100644 --- a/internal/view/browser.go +++ b/internal/view/browser.go @@ -127,7 +127,9 @@ func (b *Browser) Start() { b.GetModel().AddListener(b) b.Table.Start() b.CmdBuff().AddListener(b) - b.GetModel().Watch(b.prepareContext()) + if err := b.GetModel().Watch(b.prepareContext()); err != nil { + log.Error().Err(err).Msgf("Watcher failed for %s", b.GVR()) + } } // Stop terminates browser updates. @@ -158,7 +160,9 @@ func (b *Browser) BufferActive(state bool, k model.BufferKind) { if state { return } - b.GetModel().Refresh(b.prepareContext()) + if err := b.GetModel().Refresh(b.prepareContext()); err != nil { + log.Error().Err(err).Msgf("Refresh failed for %s", b.GVR()) + } b.app.QueueUpdateDraw(func() { b.Update(b.GetModel().Peek()) if b.GetRowCount() > 1 { diff --git a/internal/view/helpers.go b/internal/view/helpers.go index 5ee5f02f..15cf0470 100644 --- a/internal/view/helpers.go +++ b/internal/view/helpers.go @@ -61,9 +61,6 @@ func defaultEnv(c *client.Config, path string, header render.Header, row render. } func describeResource(app *App, m ui.Tabular, gvr, path string) { - ctx := context.Background() - ctx = context.WithValue(ctx, internal.KeyFactory, app.factory) - v := NewLiveView(app, "Describe", model.NewDescribe(client.NewGVR(gvr), path)) if err := app.inject(v); err != nil { app.Flash().Err(err) diff --git a/internal/view/live_view.go b/internal/view/live_view.go index e7470929..5d95aa4c 100644 --- a/internal/view/live_view.go +++ b/internal/view/live_view.go @@ -13,6 +13,7 @@ import ( "github.com/derailed/k9s/internal/ui" "github.com/derailed/tview" "github.com/gdamore/tcell" + "github.com/rs/zerolog/log" "github.com/sahilm/fuzzy" ) @@ -170,7 +171,9 @@ func (v *LiveView) Start() { var ctx context.Context ctx, v.cancel = context.WithCancel(v.defaultCtx()) - v.model.Watch(ctx) + if err := v.model.Watch(ctx); err != nil { + log.Error().Err(err).Msgf("LiveView watcher failed") + } } func (v *LiveView) defaultCtx() context.Context { @@ -316,7 +319,7 @@ func (v *LiveView) updateTitle() { if v.title == "" { return } - fmat := fmt.Sprintf(detailsTitleFmt, v.title, v.model.GetPath()) + fmat := fmt.Sprintf(liveViewTitleFmt, v.title, v.model.GetPath()) buff := v.cmdBuff.GetText() if buff == "" { diff --git a/internal/view/logger.go b/internal/view/logger.go index 8105f8c0..972b50bb 100644 --- a/internal/view/logger.go +++ b/internal/view/logger.go @@ -2,6 +2,7 @@ package view import ( "context" + "github.com/atotto/clipboard" "github.com/derailed/k9s/internal/config" "github.com/derailed/k9s/internal/model" @@ -10,8 +11,6 @@ import ( "github.com/gdamore/tcell" ) -const loggerTitleFmt = "[fg:bg:b] %s([hilite:bg:b]%s[fg:bg:-])[fg:bg:-] " - // Logger represents a generic log viewer. type Logger struct { *tview.TextView @@ -170,4 +169,4 @@ func (l *Logger) cpCmd(evt *tcell.EventKey) *tcell.EventKey { } return nil -} \ No newline at end of file +}