add 'attach' command

mine
Mike Hummel 2020-02-06 19:45:52 +01:00
parent b544852ec7
commit 01b77384d1
3 changed files with 103 additions and 0 deletions

View File

@ -43,6 +43,7 @@ func (c *Container) Name() string { return containerTitle }
func (c *Container) bindDangerousKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
ui.KeyS: ui.NewKeyAction("Shell", c.shellCmd, true),
ui.KeyA: ui.NewKeyAction("Attach", c.attachCmd, true),
})
}
@ -97,6 +98,19 @@ func (c *Container) shellCmd(evt *tcell.EventKey) *tcell.EventKey {
return nil
}
func (c *Container) attachCmd(evt *tcell.EventKey) *tcell.EventKey {
sel := c.GetTable().GetSelectedItem()
if sel == "" {
return evt
}
c.Stop()
defer c.Start()
attachIn(c.App(), c.GetTable().Path, sel)
return nil
}
func (c *Container) portFwdCmd(evt *tcell.EventKey) *tcell.EventKey {
path := c.GetTable().GetSelectedItem()
if path == "" {

View File

@ -39,6 +39,7 @@ func (p *Pod) bindDangerousKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
tcell.KeyCtrlK: ui.NewKeyAction("Kill", p.killCmd, true),
ui.KeyS: ui.NewKeyAction("Shell", p.shellCmd, true),
ui.KeyA: ui.NewKeyAction("Attach", p.attachCmd, true),
})
}
@ -140,12 +141,51 @@ func (p *Pod) shellCmd(evt *tcell.EventKey) *tcell.EventKey {
return evt
}
func (p *Pod) attachCmd(evt *tcell.EventKey) *tcell.EventKey {
sel := p.GetTable().GetSelectedItem()
if sel == "" {
return evt
}
row := p.GetTable().GetSelectedRowIndex()
status := ui.TrimCell(p.GetTable().SelectTable, row, p.GetTable().NameColIndex()+2)
if status != render.Running {
p.App().Flash().Errf("%s is not in a running state", sel)
return nil
}
cc, err := fetchContainers(p.App().factory, sel, false)
if err != nil {
p.App().Flash().Errf("Unable to retrieve containers %s", err)
return evt
}
if len(cc) == 1 {
p.attachIn(sel, "")
return nil
}
picker := NewPicker()
picker.populate(cc)
picker.SetSelectedFunc(func(i int, t, d string, r rune) {
p.attachIn(sel, t)
})
if err := p.App().inject(picker); err != nil {
p.App().Flash().Err(err)
}
return evt
}
func (p *Pod) shellIn(path, co string) {
p.Stop()
shellIn(p.App(), path, co)
p.Start()
}
func (p *Pod) attachIn(path, co string) {
p.Stop()
attachIn(p.App(), path, co)
p.Start()
}
// ----------------------------------------------------------------------------
// Helpers...
@ -181,6 +221,14 @@ func shellIn(a *App, path, co string) {
}
}
func attachIn(a *App, path, co string) {
args := computeAttachArgs(path, a.Config.K9s.CurrentContext, a.Conn().Config().Flags().KubeConfig)
log.Debug().Msgf("Attach args %v", args)
if !runK(true, a, args...) {
a.Flash().Err(errors.New("Attach exec failed"))
}
}
func computeShellArgs(path, co, context string, kcfg *string) []string {
args := make([]string, 0, 15)
args = append(args, "exec", "-it")
@ -197,3 +245,16 @@ func computeShellArgs(path, co, context string, kcfg *string) []string {
return append(args, "--", "sh", "-c", shellCheck)
}
func computeAttachArgs(path, context string, kcfg *string) []string {
args := make([]string, 0, 15)
args = append(args, "attach", "-it")
args = append(args, "--context", context)
ns, po := client.Namespaced(path)
args = append(args, "-n", ns)
args = append(args, po)
if kcfg != nil && *kcfg != "" {
args = append(args, "--kubeconfig", *kcfg)
}
return args
}

View File

@ -157,6 +157,7 @@ func (x *Xray) refreshActions() {
if ref.GVR == "containers" {
aa[ui.KeyS] = ui.NewKeyAction("Shell", x.shellCmd, true)
aa[ui.KeyA] = ui.NewKeyAction("Attach", x.attachCmd, true)
aa[ui.KeyL] = ui.NewKeyAction("Logs", x.logsCmd(false), true)
aa[ui.KeyShiftL] = ui.NewKeyAction("Logs Previous", x.logsCmd(true), true)
}
@ -256,6 +257,33 @@ func (x *Xray) shellIn(path, co string) {
x.Start()
}
func (x *Xray) attachCmd(evt *tcell.EventKey) *tcell.EventKey {
ref := x.selectedSpec()
if ref == nil {
return nil
}
if ref.Status != "" {
x.app.Flash().Errf("%s is not in a running state", ref.Path)
return nil
}
if ref.Parent != nil {
_, co := client.Namespaced(ref.Path)
x.attachIn(ref.Parent.Path, co)
} else {
log.Error().Msgf("No parent found on container node %q", ref.Path)
}
return nil
}
func (x *Xray) attachIn(path, co string) {
x.Stop()
attachIn(x.app, path, co)
x.Start()
}
func (x *Xray) viewCmd(evt *tcell.EventKey) *tcell.EventKey {
ref := x.selectedSpec()
if ref == nil {