refactor + cleanup
parent
83a381d485
commit
7fe489cf02
|
|
@ -109,31 +109,13 @@ func (r *HorizontalPodAutoscalerV2Beta1) toMetrics(specs []autoscalingv2beta1.Me
|
|||
return "<none>"
|
||||
}
|
||||
|
||||
list, max, more, count := []string{}, 2, false, 0
|
||||
list, count := []string{}, 0
|
||||
for i, spec := range specs {
|
||||
current := "<unknown>"
|
||||
|
||||
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, "<unknown type>")
|
||||
}
|
||||
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 := "<unknown>"
|
||||
|
||||
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 "<unknown type>"
|
||||
}
|
||||
|
||||
func (*HorizontalPodAutoscalerV2Beta1) externalMetrics(i int, spec autoscalingv2beta1.MetricSpec, statuses []autoscalingv2beta1.MetricStatus) string {
|
||||
current := "<unknown>"
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue