Skip to content

Commit 0fb7066

Browse files
committed
enter replicasets from deployments with r
Signed-off-by: Rudrakh Panigrahi <[email protected]>
1 parent bd4a8ca commit 0fb7066

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

internal/view/dp.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ package view
55

66
import (
77
"errors"
8-
98
"github.com/derailed/k9s/internal/client"
109
"github.com/derailed/k9s/internal/dao"
1110
"github.com/derailed/k9s/internal/ui"
11+
"github.com/derailed/tcell/v2"
1212
appsv1 "k8s.io/api/apps/v1"
1313
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1414
)
@@ -47,6 +47,7 @@ func (d *Deploy) bindKeys(aa *ui.KeyActions) {
4747
ui.KeyShiftR: ui.NewKeyAction("Sort Ready", d.GetTable().SortColCmd(readyCol, true), false),
4848
ui.KeyShiftU: ui.NewKeyAction("Sort UpToDate", d.GetTable().SortColCmd(uptodateCol, true), false),
4949
ui.KeyShiftL: ui.NewKeyAction("Sort Available", d.GetTable().SortColCmd(availCol, true), false),
50+
ui.KeyR: ui.NewKeyAction("Show Replicasets", d.replicaSetsCmd, true),
5051
})
5152
}
5253

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

67+
func (d *Deploy) replicaSetsCmd(evt *tcell.EventKey) *tcell.EventKey {
68+
dName := d.GetTable().GetSelectedItem()
69+
if dName == "" {
70+
return evt
71+
}
72+
dp, err := d.getInstance(dName)
73+
if err != nil {
74+
d.App().Flash().Err(err)
75+
return nil
76+
}
77+
showReplicasetsFromSelector(d.App(), dName, dp.Spec.Selector)
78+
return nil
79+
}
80+
6681
func (d *Deploy) showPods(app *App, model ui.Tabular, gvr client.GVR, fqn string) {
6782
dp, err := d.getInstance(fqn)
6883
if err != nil {
@@ -73,6 +88,16 @@ func (d *Deploy) showPods(app *App, model ui.Tabular, gvr client.GVR, fqn string
7388
showPodsFromSelector(app, fqn, dp.Spec.Selector)
7489
}
7590

91+
func (d *Deploy) showReplicasets(app *App, model ui.Tabular, gvr client.GVR, fqn string) {
92+
dp, err := d.getInstance(fqn)
93+
if err != nil {
94+
app.Flash().Err(err)
95+
return
96+
}
97+
98+
showReplicasetsFromSelector(app, fqn, dp.Spec.Selector)
99+
}
100+
76101
func (d *Deploy) getInstance(fqn string) (*appsv1.Deployment, error) {
77102
var dp dao.Deployment
78103
dp.Init(d.App().factory, d.GVR())
@@ -92,3 +117,13 @@ func showPodsFromSelector(app *App, path string, sel *metav1.LabelSelector) {
92117

93118
showPods(app, path, l.String(), "")
94119
}
120+
121+
func showReplicasetsFromSelector(app *App, path string, sel *metav1.LabelSelector) {
122+
l, err := metav1.LabelSelectorAsSelector(sel)
123+
if err != nil {
124+
app.Flash().Err(err)
125+
return
126+
}
127+
128+
showReplicasets(app, path, l.String(), "")
129+
}

internal/view/helpers.go

+21
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,27 @@ func podCtx(_ *App, path, fieldSel string) ContextFunc {
163163
}
164164
}
165165

166+
func showReplicasets(app *App, path, labelSel, fieldSel string) {
167+
v := NewReplicaSet(client.NewGVR("apps/v1/replicasets"))
168+
v.SetContextFn(replicasetCtx(app, path, fieldSel))
169+
v.SetLabelFilter(cmd.ToLabels(labelSel))
170+
171+
ns, _ := client.Namespaced(path)
172+
if err := app.Config.SetActiveNamespace(ns); err != nil {
173+
log.Error().Err(err).Msg("Config NS set failed!")
174+
}
175+
if err := app.inject(v, false); err != nil {
176+
app.Flash().Err(err)
177+
}
178+
}
179+
180+
func replicasetCtx(_ *App, path, fieldSel string) ContextFunc {
181+
return func(ctx context.Context) context.Context {
182+
ctx = context.WithValue(ctx, internal.KeyPath, path)
183+
return context.WithValue(ctx, internal.KeyFields, fieldSel)
184+
}
185+
}
186+
166187
func extractApp(ctx context.Context) (*App, error) {
167188
app, ok := ctx.Value(internal.KeyApp).(*App)
168189
if !ok {

0 commit comments

Comments
 (0)