Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show UsedBy for PriorityClasses #1666

Merged
merged 1 commit into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/dao/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ func (c *CronJob) Scan(ctx context.Context, gvr, fqn string, wait bool) (Refs, e
GVR: c.GVR(),
FQN: client.FQN(cj.Namespace, cj.Name),
})
case "scheduling.k8s.io/v1/priorityclasses":
if !hasPC(&cj.Spec.JobTemplate.Spec.Template.Spec, n) {
continue
}
refs = append(refs, Ref{
GVR: c.GVR(),
FQN: client.FQN(cj.Namespace, cj.Name),
})
}
}

Expand Down
13 changes: 13 additions & 0 deletions internal/dao/dp.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,16 @@ func (d *Deployment) Scan(ctx context.Context, gvr, fqn string, wait bool) (Refs
GVR: d.GVR(),
FQN: client.FQN(dp.Namespace, dp.Name),
})
case "scheduling.k8s.io/v1/priorityclasses":
if !hasPC(&dp.Spec.Template.Spec, n) {
continue
}
refs = append(refs, Ref{
GVR: d.GVR(),
FQN: client.FQN(dp.Namespace, dp.Name),
})
}

}

return refs, nil
Expand Down Expand Up @@ -276,6 +285,10 @@ func hasPVC(spec *v1.PodSpec, name string) bool {
return false
}

func hasPC(spec *v1.PodSpec, name string) bool {
return spec.PriorityClassName == name
}

func hasConfigMap(spec *v1.PodSpec, name string) bool {
for _, c := range spec.InitContainers {
if containerHasConfigMap(c, name) {
Expand Down
8 changes: 8 additions & 0 deletions internal/dao/ds.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ func (d *DaemonSet) Scan(ctx context.Context, gvr, fqn string, wait bool) (Refs,
GVR: d.GVR(),
FQN: client.FQN(ds.Namespace, ds.Name),
})
case "scheduling.k8s.io/v1/priorityclasses":
if !hasPC(&ds.Spec.Template.Spec, n) {
continue
}
refs = append(refs, Ref{
GVR: d.GVR(),
FQN: client.FQN(ds.Namespace, ds.Name),
})
}
}

Expand Down
8 changes: 8 additions & 0 deletions internal/dao/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ func (j *Job) Scan(ctx context.Context, gvr, fqn string, wait bool) (Refs, error
GVR: j.GVR(),
FQN: client.FQN(job.Namespace, job.Name),
})
case "scheduling.k8s.io/v1/priorityclasses":
if !hasPC(&job.Spec.Template.Spec, n) {
continue
}
refs = append(refs, Ref{
GVR: j.GVR(),
FQN: client.FQN(job.Namespace, job.Name),
})
}
}

Expand Down
8 changes: 8 additions & 0 deletions internal/dao/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ func (p *Pod) Scan(ctx context.Context, gvr, fqn string, wait bool) (Refs, error
GVR: p.GVR(),
FQN: client.FQN(pod.Namespace, pod.Name),
})
case "scheduling.k8s.io/v1/priorityclasses":
if !hasPC(&pod.Spec, n) {
continue
}
refs = append(refs, Ref{
GVR: p.GVR(),
FQN: client.FQN(pod.Namespace, pod.Name),
})
}
}

Expand Down
9 changes: 9 additions & 0 deletions internal/dao/sts.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ func (s *StatefulSet) Scan(ctx context.Context, gvr, fqn string, wait bool) (Ref
GVR: s.GVR(),
FQN: client.FQN(sts.Namespace, sts.Name),
})
case "scheduling.k8s.io/v1/priorityclasses":
if !hasPC(&sts.Spec.Template.Spec, n) {
continue
}
refs = append(refs, Ref{
GVR: s.GVR(),
FQN: client.FQN(sts.Namespace, sts.Name),
})

}
}

Expand Down
32 changes: 32 additions & 0 deletions internal/view/priorityclass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package view

import (
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/ui"
"github.com/gdamore/tcell/v2"
)

// PriorityClass presents a priority class viewer.
type PriorityClass struct {
ResourceViewer
}

// NewPriorityClass returns a new viewer.
func NewPriorityClass(gvr client.GVR) ResourceViewer {
s := PriorityClass{
ResourceViewer: NewBrowser(gvr),
}
s.AddBindKeysFn(s.bindKeys)

return &s
}

func (s *PriorityClass) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
ui.KeyU: ui.NewKeyAction("UsedBy", s.refCmd, true),
})
}

func (s *PriorityClass) refCmd(evt *tcell.EventKey) *tcell.EventKey {
return scanRefs(evt, s.App(), s.GetTable(), "scheduling.k8s.io/v1/priorityclasses")
}
17 changes: 17 additions & 0 deletions internal/view/priorityclass_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package view_test

import (
"testing"

"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/view"
"github.com/stretchr/testify/assert"
)

func TestPriorityClassNew(t *testing.T) {
s := view.NewPriorityClass(client.NewGVR("scheduling.k8s.io/v1/priorityclasses"))

assert.Nil(t, s.Init(makeCtx()))
assert.Equal(t, "PriorityClass", s.Name())
assert.Equal(t, 6, len(s.Hints()))
}
3 changes: 3 additions & 0 deletions internal/view/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func coreViewers(vv MetaViewers) {
vv[client.NewGVR("v1/secrets")] = MetaViewer{
viewerFn: NewSecret,
}
vv[client.NewGVR("scheduling.k8s.io/v1/priorityclasses")] = MetaViewer{
viewerFn: NewPriorityClass,
}
vv[client.NewGVR("v1/configmaps")] = MetaViewer{
viewerFn: NewConfigMap,
}
Expand Down
8 changes: 8 additions & 0 deletions internal/view/svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ func init() {
Verbs: []string{"get", "list", "watch", "delete"},
Categories: []string{"k9s"},
})
dao.MetaAccess.RegisterMeta("scheduling.k8s.io/v1/priorityclasses", metav1.APIResource{
Name: "priorityclasses",
SingularName: "priorityclass",
Namespaced: false,
Kind: "PriorityClass",
Verbs: []string{"get", "list", "watch", "delete"},
Categories: []string{"k9s"},
})
dao.MetaAccess.RegisterMeta("v1/configmaps", metav1.APIResource{
Name: "configmaps",
SingularName: "configmap",
Expand Down