Skip to content

Commit

Permalink
adding the f command to pf extender view
Browse files Browse the repository at this point in the history
  • Loading branch information
wjiec committed Jan 29, 2024
1 parent 2070690 commit 9877ac6
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 32 deletions.
2 changes: 1 addition & 1 deletion internal/view/dp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ func TestDeploy(t *testing.T) {

assert.Nil(t, v.Init(makeCtx()))
assert.Equal(t, "Deployments", v.Name())
assert.Equal(t, 14, len(v.Hints()))
assert.Equal(t, 15, len(v.Hints()))
}
2 changes: 1 addition & 1 deletion internal/view/ds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ func TestDaemonSet(t *testing.T) {

assert.Nil(t, v.Init(makeCtx()))
assert.Equal(t, "DaemonSets", v.Name())
assert.Equal(t, 15, len(v.Hints()))
assert.Equal(t, 16, len(v.Hints()))
}
38 changes: 38 additions & 0 deletions internal/view/pf_extender.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
package view

import (
"context"
"fmt"
"strings"

"github.com/derailed/k9s/internal"
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/dao"
"github.com/derailed/k9s/internal/port"
"github.com/derailed/k9s/internal/ui"
Expand Down Expand Up @@ -35,6 +38,7 @@ func NewPortForwardExtender(r ResourceViewer) ResourceViewer {

func (p *PortForwardExtender) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
ui.KeyF: ui.NewKeyAction("Show PortForward", p.showPFCmd, true),
ui.KeyShiftF: ui.NewKeyAction("Port-Forward", p.portFwdCmd, true),
})
}
Expand All @@ -61,6 +65,32 @@ func (p *PortForwardExtender) portFwdCmd(evt *tcell.EventKey) *tcell.EventKey {
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) {
res, err := dao.AccessorFor(p.App().factory, p.GVR())
if err != nil {
Expand All @@ -74,6 +104,14 @@ func (p *PortForwardExtender) fetchPodName(path string) (string, error) {
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...

Expand Down
28 changes: 0 additions & 28 deletions internal/view/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ func (p *Pod) bindKeys(aa ui.KeyActions) {

aa.Add(ui.KeyActions{
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.KeyShiftT: ui.NewKeyAction("Sort Restart", p.GetTable().SortColCmd("RESTARTS", false), false),
ui.KeyShiftS: ui.NewKeyAction("Sort Status", p.GetTable().SortColCmd(statusCol, true), false),
Expand Down Expand Up @@ -196,33 +195,6 @@ func (p *Pod) showNode(evt *tcell.EventKey) *tcell.EventKey {
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 {
selections := p.GetTable().GetSelectedItems()
if len(selections) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion internal/view/sts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ func TestStatefulSetNew(t *testing.T) {

assert.Nil(t, s.Init(makeCtx()))
assert.Equal(t, "StatefulSets", s.Name())
assert.Equal(t, 12, len(s.Hints()))
assert.Equal(t, 13, len(s.Hints()))
}
2 changes: 1 addition & 1 deletion internal/view/svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,5 @@ func TestServiceNew(t *testing.T) {

assert.Nil(t, s.Init(makeCtx()))
assert.Equal(t, "Services", s.Name())
assert.Equal(t, 10, len(s.Hints()))
assert.Equal(t, 11, len(s.Hints()))
}

0 comments on commit 9877ac6

Please sign in to comment.