Skip to content

Commit

Permalink
enter replicasets from deployments with r
Browse files Browse the repository at this point in the history
Signed-off-by: Rudrakh Panigrahi <rudrakh97@gmail.com>
  • Loading branch information
rudrakhp committed Feb 16, 2025
1 parent bd4a8ca commit 0fb7066
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
37 changes: 36 additions & 1 deletion internal/view/dp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package view

import (
"errors"

"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/dao"
"github.com/derailed/k9s/internal/ui"
"github.com/derailed/tcell/v2"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -47,6 +47,7 @@ func (d *Deploy) bindKeys(aa *ui.KeyActions) {
ui.KeyShiftR: ui.NewKeyAction("Sort Ready", d.GetTable().SortColCmd(readyCol, true), false),
ui.KeyShiftU: ui.NewKeyAction("Sort UpToDate", d.GetTable().SortColCmd(uptodateCol, true), false),
ui.KeyShiftL: ui.NewKeyAction("Sort Available", d.GetTable().SortColCmd(availCol, true), false),
ui.KeyR: ui.NewKeyAction("Show Replicasets", d.replicaSetsCmd, true),
})
}

Expand All @@ -63,6 +64,20 @@ func (d *Deploy) logOptions(prev bool) (*dao.LogOptions, error) {
return podLogOptions(d.App(), path, prev, dp.ObjectMeta, dp.Spec.Template.Spec), nil
}

func (d *Deploy) replicaSetsCmd(evt *tcell.EventKey) *tcell.EventKey {
dName := d.GetTable().GetSelectedItem()
if dName == "" {
return evt
}
dp, err := d.getInstance(dName)
if err != nil {
d.App().Flash().Err(err)
return nil
}
showReplicasetsFromSelector(d.App(), dName, dp.Spec.Selector)
return nil
}

func (d *Deploy) showPods(app *App, model ui.Tabular, gvr client.GVR, fqn string) {
dp, err := d.getInstance(fqn)
if err != nil {
Expand All @@ -73,6 +88,16 @@ func (d *Deploy) showPods(app *App, model ui.Tabular, gvr client.GVR, fqn string
showPodsFromSelector(app, fqn, dp.Spec.Selector)
}

func (d *Deploy) showReplicasets(app *App, model ui.Tabular, gvr client.GVR, fqn string) {
dp, err := d.getInstance(fqn)
if err != nil {
app.Flash().Err(err)
return
}

showReplicasetsFromSelector(app, fqn, dp.Spec.Selector)
}

func (d *Deploy) getInstance(fqn string) (*appsv1.Deployment, error) {
var dp dao.Deployment
dp.Init(d.App().factory, d.GVR())
Expand All @@ -92,3 +117,13 @@ func showPodsFromSelector(app *App, path string, sel *metav1.LabelSelector) {

showPods(app, path, l.String(), "")
}

func showReplicasetsFromSelector(app *App, path string, sel *metav1.LabelSelector) {
l, err := metav1.LabelSelectorAsSelector(sel)
if err != nil {
app.Flash().Err(err)
return
}

showReplicasets(app, path, l.String(), "")
}
21 changes: 21 additions & 0 deletions internal/view/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,27 @@ func podCtx(_ *App, path, fieldSel string) ContextFunc {
}
}

func showReplicasets(app *App, path, labelSel, fieldSel string) {
v := NewReplicaSet(client.NewGVR("apps/v1/replicasets"))
v.SetContextFn(replicasetCtx(app, path, fieldSel))
v.SetLabelFilter(cmd.ToLabels(labelSel))

ns, _ := client.Namespaced(path)
if err := app.Config.SetActiveNamespace(ns); err != nil {
log.Error().Err(err).Msg("Config NS set failed!")
}
if err := app.inject(v, false); err != nil {
app.Flash().Err(err)
}
}

func replicasetCtx(_ *App, path, fieldSel string) ContextFunc {
return func(ctx context.Context) context.Context {
ctx = context.WithValue(ctx, internal.KeyPath, path)
return context.WithValue(ctx, internal.KeyFields, fieldSel)
}
}

func extractApp(ctx context.Context) (*App, error) {
app, ok := ctx.Value(internal.KeyApp).(*App)
if !ok {
Expand Down

0 comments on commit 0fb7066

Please sign in to comment.