add container selection
parent
ad0b9e49d0
commit
045f39cd88
|
|
@ -103,7 +103,7 @@ func (c *Container) attachCmd(evt *tcell.EventKey) *tcell.EventKey {
|
||||||
|
|
||||||
c.Stop()
|
c.Stop()
|
||||||
defer c.Start()
|
defer c.Start()
|
||||||
attachIn(c.App(), c.GetTable().Path)
|
attachIn(c.App(), c.GetTable().Path, sel)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ func TestHelp(t *testing.T) {
|
||||||
v := view.NewHelp()
|
v := view.NewHelp()
|
||||||
|
|
||||||
assert.Nil(t, v.Init(ctx))
|
assert.Nil(t, v.Init(ctx))
|
||||||
assert.Equal(t, 20, v.GetRowCount())
|
assert.Equal(t, 21, v.GetRowCount())
|
||||||
assert.Equal(t, 8, v.GetColumnCount())
|
assert.Equal(t, 8, v.GetColumnCount())
|
||||||
assert.Equal(t, "<a>", strings.TrimSpace(v.GetCell(1, 0).Text))
|
assert.Equal(t, "<a>", strings.TrimSpace(v.GetCell(1, 0).Text))
|
||||||
assert.Equal(t, "Attach", strings.TrimSpace(v.GetCell(1, 1).Text))
|
assert.Equal(t, "Attach", strings.TrimSpace(v.GetCell(1, 1).Text))
|
||||||
|
|
|
||||||
|
|
@ -132,32 +132,19 @@ func (p *Pod) shellCmd(evt *tcell.EventKey) *tcell.EventKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pod) attachCmd(evt *tcell.EventKey) *tcell.EventKey {
|
func (p *Pod) attachCmd(evt *tcell.EventKey) *tcell.EventKey {
|
||||||
sel := p.GetTable().GetSelectedItem()
|
path := p.GetTable().GetSelectedItem()
|
||||||
if sel == "" {
|
if path == "" {
|
||||||
return evt
|
return evt
|
||||||
}
|
}
|
||||||
|
|
||||||
row := p.GetTable().GetSelectedRowIndex()
|
row := p.GetTable().GetSelectedRowIndex()
|
||||||
status := ui.TrimCell(p.GetTable().SelectTable, row, p.GetTable().NameColIndex()+2)
|
status := ui.TrimCell(p.GetTable().SelectTable, row, p.GetTable().NameColIndex()+2)
|
||||||
if status != render.Running {
|
if status != render.Running {
|
||||||
p.App().Flash().Errf("%s is not in a running state", sel)
|
p.App().Flash().Errf("%s is not in a running state", path)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
cc, err := fetchContainers(p.App().factory, sel, false)
|
|
||||||
if err != nil {
|
if err := containerAttachIn(p.App(), p, path, ""); 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)
|
|
||||||
})
|
|
||||||
if err := p.App().inject(picker); err != nil {
|
|
||||||
p.App().Flash().Err(err)
|
p.App().Flash().Err(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -170,9 +157,9 @@ func (p *Pod) shellIn(path, co string) {
|
||||||
p.Start()
|
p.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pod) attachIn(path string) {
|
func (p *Pod) attachIn(path, co string) {
|
||||||
p.Stop()
|
p.Stop()
|
||||||
attachIn(p.App(), path)
|
attachIn(p.App(), path, co)
|
||||||
p.Start()
|
p.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -221,12 +208,47 @@ func shellIn(a *App, path, co string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func attachIn(a *App, path string) {
|
func containerAttachIn(a *App, comp model.Component, path, co string) error {
|
||||||
args := computeAttachArgs(path, a.Config.K9s.CurrentContext, a.Conn().Config().Flags().KubeConfig)
|
if co != "" {
|
||||||
log.Debug().Msgf("Attach args %v", args)
|
resumeAttachIn(a, comp, path, co)
|
||||||
if !runK(true, a, args...) {
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
cc, err := fetchContainers(a.factory, path, false)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(cc) == 1 {
|
||||||
|
resumeAttachIn(a, comp, path, cc[0])
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
picker := NewPicker()
|
||||||
|
picker.populate(cc)
|
||||||
|
picker.SetSelectedFunc(func(_ int, co, _ string, _ rune) {
|
||||||
|
resumeAttachIn(a, comp, path, co)
|
||||||
|
})
|
||||||
|
if err := a.inject(picker); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func resumeAttachIn(a *App, c model.Component, path, co string) {
|
||||||
|
c.Stop()
|
||||||
|
defer c.Start()
|
||||||
|
|
||||||
|
attachIn(a, path, co)
|
||||||
|
}
|
||||||
|
|
||||||
|
func attachIn(a *App, path, co string) {
|
||||||
|
args := computeAttachArgs(path, co, a.Config.K9s.CurrentContext, a.Conn().Config().Flags().KubeConfig)
|
||||||
|
|
||||||
|
c := color.New(color.BgGreen).Add(color.FgBlack).Add(color.Bold)
|
||||||
|
if !runK(a, shellOpts{clear: true, banner: c.Sprintf(bannerFmt, path, co), args: args}) {
|
||||||
a.Flash().Err(errors.New("Attach exec failed"))
|
a.Flash().Err(errors.New("Attach exec failed"))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func computeShellArgs(path, co, context string, kcfg *string) []string {
|
func computeShellArgs(path, co, context string, kcfg *string) []string {
|
||||||
|
|
@ -246,7 +268,7 @@ func computeShellArgs(path, co, context string, kcfg *string) []string {
|
||||||
return append(args, "--", "sh", "-c", shellCheck)
|
return append(args, "--", "sh", "-c", shellCheck)
|
||||||
}
|
}
|
||||||
|
|
||||||
func computeAttachArgs(path, context string, kcfg *string) []string {
|
func computeAttachArgs(path, co, context string, kcfg *string) []string {
|
||||||
args := make([]string, 0, 15)
|
args := make([]string, 0, 15)
|
||||||
args = append(args, "attach", "-it")
|
args = append(args, "attach", "-it")
|
||||||
args = append(args, "--context", context)
|
args = append(args, "--context", context)
|
||||||
|
|
@ -256,6 +278,10 @@ func computeAttachArgs(path, context string, kcfg *string) []string {
|
||||||
if kcfg != nil && *kcfg != "" {
|
if kcfg != nil && *kcfg != "" {
|
||||||
args = append(args, "--kubeconfig", *kcfg)
|
args = append(args, "--kubeconfig", *kcfg)
|
||||||
}
|
}
|
||||||
|
if co != "" {
|
||||||
|
args = append(args, "-c", co)
|
||||||
|
}
|
||||||
|
|
||||||
return args
|
return args
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ func TestPodNew(t *testing.T) {
|
||||||
|
|
||||||
assert.Nil(t, po.Init(makeCtx()))
|
assert.Nil(t, po.Init(makeCtx()))
|
||||||
assert.Equal(t, "Pods", po.Name())
|
assert.Equal(t, "Pods", po.Name())
|
||||||
assert.Equal(t, 19, len(po.Hints()))
|
assert.Equal(t, 20, len(po.Hints()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helpers...
|
// Helpers...
|
||||||
|
|
|
||||||
|
|
@ -296,28 +296,32 @@ func (x *Xray) shellCmd(evt *tcell.EventKey) *tcell.EventKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *Xray) attachCmd(evt *tcell.EventKey) *tcell.EventKey {
|
func (x *Xray) attachCmd(evt *tcell.EventKey) *tcell.EventKey {
|
||||||
ref := x.selectedSpec()
|
|
||||||
if ref == nil {
|
spec := x.selectedSpec()
|
||||||
|
if spec == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if ref.Status != "" {
|
if spec.Status() != "ok" {
|
||||||
x.app.Flash().Errf("%s is not in a running state", ref.Path)
|
x.app.Flash().Errf("%s is not in a running state", spec.Path())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if ref.Parent != nil {
|
path, co := spec.Path(), ""
|
||||||
x.attachIn(ref.Parent.Path)
|
if spec.GVR() == "containers" {
|
||||||
} else {
|
path = *spec.ParentPath()
|
||||||
log.Error().Msgf("No parent found on container node %q", ref.Path)
|
}
|
||||||
|
|
||||||
|
if err := containerAttachIn(x.app, x, path, co); err != nil {
|
||||||
|
x.app.Flash().Err(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *Xray) attachIn(path string) {
|
func (x *Xray) attachIn(path, co string) {
|
||||||
x.Stop()
|
x.Stop()
|
||||||
attachIn(x.app, path)
|
attachIn(x.app, path, co)
|
||||||
x.Start()
|
x.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue