Skip to content

Commit

Permalink
Show owner of pod, rs (#1747)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitolicious committed Nov 12, 2023
1 parent 152ace0 commit a83fe75
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/view/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestHelp(t *testing.T) {
v := view.NewHelp(app)

assert.Nil(t, v.Init(ctx))
assert.Equal(t, 26, v.GetRowCount())
assert.Equal(t, 27, v.GetRowCount())
assert.Equal(t, 6, v.GetColumnCount())
assert.Equal(t, "<a>", strings.TrimSpace(v.GetCell(1, 0).Text))
assert.Equal(t, "Attach", strings.TrimSpace(v.GetCell(1, 1).Text))
Expand Down
45 changes: 45 additions & 0 deletions internal/view/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ 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.KeyO: ui.NewKeyAction("Show Owner", p.showOwner, 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 @@ -153,6 +154,50 @@ func (p *Pod) showNode(evt *tcell.EventKey) *tcell.EventKey {
return nil
}

func (p *Pod) showOwner(evt *tcell.EventKey) *tcell.EventKey {
path := p.GetTable().GetSelectedItem()
if path == "" {
return evt
}
pod, err := fetchPod(p.App().factory, path)
if err != nil {
p.App().Flash().Err(err)
return nil
}
if len(pod.GetObjectMeta().GetOwnerReferences()) == 0 {
p.App().Flash().Err(errors.New("no owner reference found"))
return nil
}

var owner model.Component
for _, ownerReference := range pod.GetObjectMeta().GetOwnerReferences() {
if ownerReference.Kind == "ReplicaSet" {
rs := NewReplicaSet(client.NewGVR("apps/v1/replicasets"))
rs.SetInstance(pod.Namespace + "/" + rs.Name())

owner = rs
break
} else if ownerReference.Kind == "DaemonSet" {
ds := NewDaemonSet(client.NewGVR("apps/v1/daemonsets"))
ds.SetInstance(pod.Namespace + "/" + ds.Name())

owner = ds
break
}

}
if owner == nil {
p.App().Flash().Err(errors.New("no ReplicaSet or DaemonSet owner reference found"))
return nil
}

if err := p.App().inject(owner, false); err != nil {
p.App().Flash().Err(err)
}

return nil
}

func (p *Pod) showPFCmd(evt *tcell.EventKey) *tcell.EventKey {
path := p.GetTable().GetSelectedItem()
if path == "" {
Expand Down
2 changes: 1 addition & 1 deletion internal/view/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestPodNew(t *testing.T) {

assert.Nil(t, po.Init(makeCtx()))
assert.Equal(t, "Pods", po.Name())
assert.Equal(t, 25, len(po.Hints()))
assert.Equal(t, 26, len(po.Hints()))
}

// Helpers...
Expand Down
60 changes: 60 additions & 0 deletions internal/view/rs.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package view

import (
"errors"
"fmt"

"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/dao"
"github.com/derailed/k9s/internal/model"
"github.com/derailed/k9s/internal/ui"
"github.com/derailed/tcell/v2"
"github.com/derailed/tview"
v1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)

// ReplicaSet presents a replicaset viewer.
Expand All @@ -28,6 +34,7 @@ func NewReplicaSet(gvr client.GVR) ResourceViewer {

func (r *ReplicaSet) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
ui.KeyO: ui.NewKeyAction("Show Owner", r.showOwner, true),
ui.KeyShiftD: ui.NewKeyAction("Sort Desired", r.GetTable().SortColCmd("DESIRED", true), false),
ui.KeyShiftC: ui.NewKeyAction("Sort Current", r.GetTable().SortColCmd("CURRENT", true), false),
ui.KeyShiftR: ui.NewKeyAction("Sort Ready", r.GetTable().SortColCmd(readyCol, true), false),
Expand All @@ -46,6 +53,44 @@ func (r *ReplicaSet) showPods(app *App, model ui.Tabular, gvr, path string) {
showPodsFromSelector(app, path, rs.Spec.Selector)
}

func (r *ReplicaSet) showOwner(evt *tcell.EventKey) *tcell.EventKey {
path := r.GetTable().GetSelectedItem()
if path == "" {
return evt
}
rs, err := fetchRs(r.App().factory, path)
if err != nil {
r.App().Flash().Err(err)
return nil
}
if len(rs.GetObjectMeta().GetOwnerReferences()) == 0 {
r.App().Flash().Err(errors.New("no owner reference found"))
return nil
}

var owner model.Component
for _, ownerReference := range rs.GetObjectMeta().GetOwnerReferences() {
if ownerReference.Kind == "Deployment" {
dp := NewDeploy(client.NewGVR("apps/v1/deployments"))
dp.SetInstance(rs.Namespace + "/" + dp.Name())

owner = dp
break
}
}

if owner == nil {
r.App().Flash().Err(errors.New("no Deployment owner reference found"))
return nil
}

if err := r.App().inject(owner, false); err != nil {
r.App().Flash().Err(err)
}

return nil
}

func (r *ReplicaSet) rollbackCmd(evt *tcell.EventKey) *tcell.EventKey {
path := r.GetTable().GetSelectedItem()
if path == "" {
Expand Down Expand Up @@ -87,3 +132,18 @@ func (r *ReplicaSet) showModal(msg string, done func(int, string)) {
r.App().Content.AddPage("confirm", confirm, false, false)
r.App().Content.ShowPage("confirm")
}

func fetchRs(f dao.Factory, path string) (*v1.ReplicaSet, error) {
o, err := f.Get("apps/v1/replicasets", path, true, labels.Everything())
if err != nil {
return nil, err
}

var rs v1.ReplicaSet
err = runtime.DefaultUnstructuredConverter.FromUnstructured(o.(*unstructured.Unstructured).Object, &rs)
if err != nil {
return nil, err
}

return &rs, nil
}

0 comments on commit a83fe75

Please sign in to comment.