adding the f command to pf extender view (#2511)

mine
Jayson Wang 2024-02-06 07:56:21 +08:00 committed by GitHub
parent 5a673e20b3
commit 763a6b0e00
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 42 additions and 32 deletions

View File

@ -16,5 +16,5 @@ func TestDeploy(t *testing.T) {
assert.Nil(t, v.Init(makeCtx())) assert.Nil(t, v.Init(makeCtx()))
assert.Equal(t, "Deployments", v.Name()) assert.Equal(t, "Deployments", v.Name())
assert.Equal(t, 14, len(v.Hints())) assert.Equal(t, 15, len(v.Hints()))
} }

View File

@ -16,5 +16,5 @@ func TestDaemonSet(t *testing.T) {
assert.Nil(t, v.Init(makeCtx())) assert.Nil(t, v.Init(makeCtx()))
assert.Equal(t, "DaemonSets", v.Name()) assert.Equal(t, "DaemonSets", v.Name())
assert.Equal(t, 15, len(v.Hints())) assert.Equal(t, 16, len(v.Hints()))
} }

View File

@ -4,9 +4,12 @@
package view package view
import ( import (
"context"
"fmt" "fmt"
"strings" "strings"
"github.com/derailed/k9s/internal"
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/dao" "github.com/derailed/k9s/internal/dao"
"github.com/derailed/k9s/internal/port" "github.com/derailed/k9s/internal/port"
"github.com/derailed/k9s/internal/ui" "github.com/derailed/k9s/internal/ui"
@ -35,6 +38,7 @@ func NewPortForwardExtender(r ResourceViewer) ResourceViewer {
func (p *PortForwardExtender) bindKeys(aa ui.KeyActions) { func (p *PortForwardExtender) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{ aa.Add(ui.KeyActions{
ui.KeyF: ui.NewKeyAction("Show PortForward", p.showPFCmd, true),
ui.KeyShiftF: ui.NewKeyAction("Port-Forward", p.portFwdCmd, true), ui.KeyShiftF: ui.NewKeyAction("Port-Forward", p.portFwdCmd, true),
}) })
} }
@ -61,6 +65,32 @@ func (p *PortForwardExtender) portFwdCmd(evt *tcell.EventKey) *tcell.EventKey {
return nil return nil
} }
func (p *PortForwardExtender) showPFCmd(evt *tcell.EventKey) *tcell.EventKey {
path := p.GetTable().GetSelectedItem()
if path == "" {
return evt
}
podName, err := p.fetchPodName(path)
if err != nil {
p.App().Flash().Err(err)
return nil
}
if !p.App().factory.Forwarders().IsPodForwarded(podName) {
p.App().Flash().Errf("no port-forward defined")
return nil
}
pf := NewPortForward(client.NewGVR("portforwards"))
pf.SetContextFn(p.portForwardContext)
if err := p.App().inject(pf, false); err != nil {
p.App().Flash().Err(err)
}
return nil
}
func (p *PortForwardExtender) fetchPodName(path string) (string, error) { func (p *PortForwardExtender) fetchPodName(path string) (string, error) {
res, err := dao.AccessorFor(p.App().factory, p.GVR()) res, err := dao.AccessorFor(p.App().factory, p.GVR())
if err != nil { if err != nil {
@ -74,6 +104,14 @@ func (p *PortForwardExtender) fetchPodName(path string) (string, error) {
return ctrl.Pod(path) return ctrl.Pod(path)
} }
func (p *PortForwardExtender) portForwardContext(ctx context.Context) context.Context {
if bc := p.App().BenchFile; bc != "" {
ctx = context.WithValue(ctx, internal.KeyBenchCfg, p.App().BenchFile)
}
return context.WithValue(ctx, internal.KeyPath, p.GetTable().GetSelectedItem())
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Helpers... // Helpers...

View File

@ -117,7 +117,6 @@ func (p *Pod) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{ aa.Add(ui.KeyActions{
ui.KeyN: ui.NewKeyAction("Show Node", p.showNode, true), ui.KeyN: ui.NewKeyAction("Show Node", p.showNode, true),
ui.KeyF: ui.NewKeyAction("Show PortForward", p.showPFCmd, true),
ui.KeyShiftR: ui.NewKeyAction("Sort Ready", p.GetTable().SortColCmd(readyCol, true), false), ui.KeyShiftR: ui.NewKeyAction("Sort Ready", p.GetTable().SortColCmd(readyCol, true), false),
ui.KeyShiftT: ui.NewKeyAction("Sort Restart", p.GetTable().SortColCmd("RESTARTS", false), false), ui.KeyShiftT: ui.NewKeyAction("Sort Restart", p.GetTable().SortColCmd("RESTARTS", false), false),
ui.KeyShiftS: ui.NewKeyAction("Sort Status", p.GetTable().SortColCmd(statusCol, true), false), ui.KeyShiftS: ui.NewKeyAction("Sort Status", p.GetTable().SortColCmd(statusCol, true), false),
@ -196,33 +195,6 @@ func (p *Pod) showNode(evt *tcell.EventKey) *tcell.EventKey {
return nil return nil
} }
func (p *Pod) showPFCmd(evt *tcell.EventKey) *tcell.EventKey {
path := p.GetTable().GetSelectedItem()
if path == "" {
return evt
}
if !p.App().factory.Forwarders().IsPodForwarded(path) {
p.App().Flash().Errf("no port-forward defined")
return nil
}
pf := NewPortForward(client.NewGVR("portforwards"))
pf.SetContextFn(p.portForwardContext)
if err := p.App().inject(pf, false); err != nil {
p.App().Flash().Err(err)
}
return nil
}
func (p *Pod) portForwardContext(ctx context.Context) context.Context {
if bc := p.App().BenchFile; bc != "" {
ctx = context.WithValue(ctx, internal.KeyBenchCfg, p.App().BenchFile)
}
return context.WithValue(ctx, internal.KeyPath, p.GetTable().GetSelectedItem())
}
func (p *Pod) killCmd(evt *tcell.EventKey) *tcell.EventKey { func (p *Pod) killCmd(evt *tcell.EventKey) *tcell.EventKey {
selections := p.GetTable().GetSelectedItems() selections := p.GetTable().GetSelectedItems()
if len(selections) == 0 { if len(selections) == 0 {

View File

@ -16,5 +16,5 @@ func TestStatefulSetNew(t *testing.T) {
assert.Nil(t, s.Init(makeCtx())) assert.Nil(t, s.Init(makeCtx()))
assert.Equal(t, "StatefulSets", s.Name()) assert.Equal(t, "StatefulSets", s.Name())
assert.Equal(t, 12, len(s.Hints())) assert.Equal(t, 13, len(s.Hints()))
} }

View File

@ -173,5 +173,5 @@ func TestServiceNew(t *testing.T) {
assert.Nil(t, s.Init(makeCtx())) assert.Nil(t, s.Init(makeCtx()))
assert.Equal(t, "Services", s.Name()) assert.Equal(t, "Services", s.Name())
assert.Equal(t, 10, len(s.Hints())) assert.Equal(t, 11, len(s.Hints()))
} }