diff --git a/internal/resource/hpa_v2beta1.go b/internal/resource/hpa_v2beta1.go index a91bd3b2..ec81625e 100644 --- a/internal/resource/hpa_v2beta1.go +++ b/internal/resource/hpa_v2beta1.go @@ -109,31 +109,13 @@ func (r *HorizontalPodAutoscalerV2Beta1) toMetrics(specs []autoscalingv2beta1.Me return "" } - list, max, more, count := []string{}, 2, false, 0 + list, count := []string{}, 0 for i, spec := range specs { - current := "" - - switch spec.Type { - case autoscalingv2beta1.ExternalMetricSourceType: - list = append(list, r.externalMetrics(i, spec, statuses)) - case autoscalingv2beta1.PodsMetricSourceType: - if len(statuses) > i && statuses[i].Pods != nil { - current = statuses[i].Pods.CurrentAverageValue.String() - } - list = append(list, current+"/"+spec.Pods.TargetAverageValue.String()) - case autoscalingv2beta1.ObjectMetricSourceType: - if len(statuses) > i && statuses[i].Object != nil { - current = statuses[i].Object.CurrentValue.String() - } - list = append(list, current+"/"+spec.Object.TargetValue.String()) - case autoscalingv2beta1.ResourceMetricSourceType: - list = append(list, r.resourceMetrics(i, spec, statuses)) - default: - list = append(list, "") - } + list = append(list, r.checkHPAType(i, spec, statuses)) count++ } + max, more := 2, false if count > max { list, more = list[:max], true } @@ -146,6 +128,29 @@ func (r *HorizontalPodAutoscalerV2Beta1) toMetrics(specs []autoscalingv2beta1.Me return ret } +func (r *HorizontalPodAutoscalerV2Beta1) checkHPAType(i int, spec autoscalingv2beta1.MetricSpec, statuses []autoscalingv2beta1.MetricStatus) string { + current := "" + + switch spec.Type { + case autoscalingv2beta1.ExternalMetricSourceType: + return r.externalMetrics(i, spec, statuses) + case autoscalingv2beta1.PodsMetricSourceType: + if len(statuses) > i && statuses[i].Pods != nil { + current = statuses[i].Pods.CurrentAverageValue.String() + } + return current + "/" + spec.Pods.TargetAverageValue.String() + case autoscalingv2beta1.ObjectMetricSourceType: + if len(statuses) > i && statuses[i].Object != nil { + current = statuses[i].Object.CurrentValue.String() + } + return current + "/" + spec.Object.TargetValue.String() + case autoscalingv2beta1.ResourceMetricSourceType: + return r.resourceMetrics(i, spec, statuses) + } + + return "" +} + func (*HorizontalPodAutoscalerV2Beta1) externalMetrics(i int, spec autoscalingv2beta1.MetricSpec, statuses []autoscalingv2beta1.MetricStatus) string { current := "" diff --git a/internal/views/app.go b/internal/views/app.go index a6aac0bd..bdbd7d4b 100644 --- a/internal/views/app.go +++ b/internal/views/app.go @@ -79,22 +79,24 @@ func NewApp(cfg *config.Config) *appView { v.views["clusterInfo"] = newClusterInfoView(&v, k8s.NewMetricsServer(cfg.GetConnection())) v.SetInputCapture(v.keyboard) - v.registerActions() + v.bindKeys() return &v } -func (a *appView) registerActions() { - a.actions[KeyColon] = newKeyAction("Cmd", a.activateCmd, false) - a.actions[tcell.KeyCtrlR] = newKeyAction("Redraw", a.redrawCmd, false) - a.actions[tcell.KeyCtrlC] = newKeyAction("Quit", a.quitCmd, false) - a.actions[KeyHelp] = newKeyAction("Help", a.helpCmd, false) - a.actions[tcell.KeyCtrlA] = newKeyAction("Aliases", a.aliasCmd, true) - a.actions[tcell.KeyEscape] = newKeyAction("Escape", a.escapeCmd, false) - a.actions[tcell.KeyEnter] = newKeyAction("Goto", a.gotoCmd, false) - a.actions[tcell.KeyBackspace2] = newKeyAction("Erase", a.eraseCmd, false) - a.actions[tcell.KeyBackspace] = newKeyAction("Erase", a.eraseCmd, false) - a.actions[tcell.KeyDelete] = newKeyAction("Erase", a.eraseCmd, false) +func (a *appView) bindKeys() { + a.actions = keyActions{ + KeyColon: newKeyAction("Cmd", a.activateCmd, false), + tcell.KeyCtrlR: newKeyAction("Redraw", a.redrawCmd, false), + tcell.KeyCtrlC: newKeyAction("Quit", a.quitCmd, false), + KeyHelp: newKeyAction("Help", a.helpCmd, false), + tcell.KeyCtrlA: newKeyAction("Aliases", a.aliasCmd, true), + tcell.KeyEscape: newKeyAction("Escape", a.escapeCmd, false), + tcell.KeyEnter: newKeyAction("Goto", a.gotoCmd, false), + tcell.KeyBackspace2: newKeyAction("Erase", a.eraseCmd, false), + tcell.KeyBackspace: newKeyAction("Erase", a.eraseCmd, false), + tcell.KeyDelete: newKeyAction("Erase", a.eraseCmd, false), + } } func (a *appView) Init(version string, rate int) { @@ -212,14 +214,6 @@ func (a *appView) Run() { } } -func (a *appView) crumbs() *crumbsView { - return a.views["crumbs"].(*crumbsView) -} - -func (a *appView) logo() *logoView { - return a.views["logo"].(*logoView) -} - func (a *appView) statusReset() { a.logo().reset() a.Draw() @@ -262,11 +256,6 @@ func (a *appView) keyboard(evt *tcell.EventKey) *tcell.EventKey { return evt } -func (a *appView) rbacCmd(evt *tcell.EventKey) *tcell.EventKey { - a.inject(newRBACView(a, "", "aa_k9s", clusterRole)) - return evt -} - func (a *appView) redrawCmd(evt *tcell.EventKey) *tcell.EventKey { a.Draw() return evt @@ -347,23 +336,10 @@ func (a *appView) aliasCmd(evt *tcell.EventKey) *tcell.EventKey { return nil } -func (a *appView) fwdCmd(evt *tcell.EventKey) *tcell.EventKey { - if a.inCmdMode() { - return evt - } - - a.inject(newForwardView("", a, nil)) +func noopCmd(*tcell.EventKey) *tcell.EventKey { return nil } -func (a *appView) noopCmd(*tcell.EventKey) *tcell.EventKey { - return nil -} - -func (a *appView) puntCmd(evt *tcell.EventKey) *tcell.EventKey { - return evt -} - func (a *appView) gotoResource(res string, record bool) bool { if a.cancel != nil { a.cancel() @@ -390,26 +366,32 @@ func (a *appView) inject(i igniter) { a.SetFocus(i) } -func (a *appView) flash() *flashView { - return a.views["flash"].(*flashView) +func (a *appView) inCmdMode() bool { + return a.cmd().inCmdMode() } func (a *appView) setHints(h hints) { a.views["menu"].(*menuView).populateMenu(h) } +// View Accessors... + +func (a *appView) crumbs() *crumbsView { + return a.views["crumbs"].(*crumbsView) +} + +func (a *appView) logo() *logoView { + return a.views["logo"].(*logoView) +} + func (a *appView) clusterInfo() *clusterInfoView { return a.views["clusterInfo"].(*clusterInfoView) } -func (a *appView) clusterInfoRefresh() { - a.clusterInfo().refresh() +func (a *appView) flash() *flashView { + return a.views["flash"].(*flashView) } func (a *appView) cmd() *cmdView { return a.views["cmd"].(*cmdView) } - -func (a *appView) inCmdMode() bool { - return a.cmd().inCmdMode() -} diff --git a/internal/views/dump.go b/internal/views/dump.go index 0ea03190..2676d8ea 100644 --- a/internal/views/dump.go +++ b/internal/views/dump.go @@ -91,7 +91,7 @@ func (v *dumpView) registerActions() { v.actions[KeyP] = newKeyAction("Previous", v.app.prevCmd, false) v.actions[tcell.KeyEnter] = newKeyAction("Enter", v.enterCmd, true) v.actions[tcell.KeyCtrlD] = newKeyAction("Delete", v.deleteCmd, true) - v.actions[tcell.KeyCtrlS] = newKeyAction("Save", v.app.noopCmd, false) + v.actions[tcell.KeyCtrlS] = newKeyAction("Save", noopCmd, false) vu := v.getTV() vu.setActions(v.actions) diff --git a/internal/views/log.go b/internal/views/log.go index 2204f3e5..8d9e56c7 100644 --- a/internal/views/log.go +++ b/internal/views/log.go @@ -135,17 +135,29 @@ func (v *logView) update() { // Actions... func (v *logView) saveCmd(evt *tcell.EventKey) *tcell.EventKey { - dir := filepath.Join(config.K9sDumpDir, v.app.config.K9s.CurrentCluster) - if err := os.MkdirAll(dir, 0744); err != nil { - log.Error().Err(err).Msgf("Mkdir K9s dump") - return nil + if path, err := saveData(v.app.config.K9s.CurrentCluster, v.path, v.logs.GetText(true)); err != nil { + v.app.flash().err(err) + } else { + v.app.flash().infof("Log %s saved successfully!", path) + } + return nil +} + +func ensureDir(dir string) error { + return os.MkdirAll(dir, 0744) +} + +func saveData(cluster, name, data string) (string, error) { + dir := filepath.Join(config.K9sDumpDir, cluster) + if err := ensureDir(dir); err != nil { + return "", err } now := time.Now().UnixNano() - fName := fmt.Sprintf("%s-%d.log", strings.Replace(v.path, "/", "-", -1), now) + fName := fmt.Sprintf("%s-%d.log", strings.Replace(name, "/", "-", -1), now) path := filepath.Join(dir, fName) - mod := os.O_CREATE | os.O_APPEND | os.O_WRONLY + mod := os.O_CREATE | os.O_WRONLY file, err := os.OpenFile(path, mod, 0644) defer func() { if file != nil { @@ -154,16 +166,13 @@ func (v *logView) saveCmd(evt *tcell.EventKey) *tcell.EventKey { }() if err != nil { log.Error().Err(err).Msgf("LogFile create %s", path) - return nil + return "", nil + } + if _, err := fmt.Fprintf(file, data); err != nil { + return "", err } - if _, err := fmt.Fprintf(file, v.logs.GetText(true)); err != nil { - log.Error().Err(err).Msgf("Log dump %s", v.path) - } - v.app.flash().infof("Log %s saved successfully!", path) - log.Debug().Msgf("Log %s saved successfully!", path) - - return nil + return path, nil } func (v *logView) toggleScrollCmd(evt *tcell.EventKey) *tcell.EventKey { diff --git a/internal/views/master_detail.go b/internal/views/master_detail.go index 1b818abe..050714bc 100644 --- a/internal/views/master_detail.go +++ b/internal/views/master_detail.go @@ -119,7 +119,7 @@ func (v *masterDetail) selectItem(r, c int) { } func (v *masterDetail) defaultActions() { - v.actions[KeyHelp] = newKeyAction("Help", v.app.noopCmd, false) + v.actions[KeyHelp] = newKeyAction("Help", noopCmd, false) v.actions[KeyP] = newKeyAction("Previous", v.app.prevCmd, false) if v.extraActionsFn != nil { diff --git a/internal/views/resource.go b/internal/views/resource.go index 0abebb28..fb568d0d 100644 --- a/internal/views/resource.go +++ b/internal/views/resource.go @@ -57,7 +57,7 @@ func (v *resourceView) init(ctx context.Context, ns string) { v.masterPage().setColorer(colorer) v.update(vctx) - v.app.clusterInfoRefresh() + v.app.clusterInfo().refresh() v.refresh() tv := v.masterPage() diff --git a/internal/views/table.go b/internal/views/table.go index 0f13a336..c7a2b671 100644 --- a/internal/views/table.go +++ b/internal/views/table.go @@ -155,7 +155,7 @@ const ( func (v *tableView) saveCmd(evt *tcell.EventKey) *tcell.EventKey { dir := filepath.Join(config.K9sDumpDir, v.app.config.K9s.CurrentCluster) - if err := os.MkdirAll(dir, 0744); err != nil { + if err := ensureDir(dir); err != nil { log.Error().Err(err).Msgf("Mkdir K9s dump") return nil } @@ -170,7 +170,7 @@ func (v *tableView) saveCmd(evt *tcell.EventKey) *tcell.EventKey { } path := filepath.Join(dir, fName) - mod := os.O_CREATE | os.O_APPEND | os.O_WRONLY + mod := os.O_CREATE | os.O_WRONLY file, err := os.OpenFile(path, mod, 0644) defer func() { if file != nil { @@ -191,9 +191,8 @@ func (v *tableView) saveCmd(evt *tcell.EventKey) *tcell.EventKey { if err := w.Error(); err != nil { log.Error().Err(err).Msgf("Screen dump %s", v.baseTitle) } - v.app.flash().infof("File %s saved successfully!", path) - log.Debug().Msgf("File %s saved successfully!", path) + v.app.flash().infof("File %s saved successfully!", path) return nil }