From 3cfc4e860c3f764432fc770963839784d54bfb7f Mon Sep 17 00:00:00 2001 From: derailed Date: Thu, 28 May 2020 14:02:43 -0600 Subject: [PATCH] cleaning up --- internal/client/client.go | 3 ++- internal/config/config.go | 1 + internal/config/feature.go | 2 +- internal/dao/cluster.go | 2 ++ internal/dao/cronjob.go | 2 ++ internal/dao/dp.go | 2 ++ internal/dao/ds.go | 2 ++ internal/dao/job.go | 2 ++ internal/dao/pod.go | 2 ++ internal/dao/reference.go | 9 ++++++--- internal/dao/sts.go | 2 ++ internal/render/row_event.go | 3 +++ internal/view/cm.go | 3 +-- internal/view/meow.go | 5 +++-- internal/view/sa.go | 3 +-- 15 files changed, 32 insertions(+), 11 deletions(-) diff --git a/internal/client/client.go b/internal/client/client.go index ba26876d..db0c9c09 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -72,6 +72,7 @@ func InitConnection(config *Config) (*APIClient, error) { return &a, err } +// ConnectionOK returns connection status. func (a *APIClient) ConnectionOK() bool { return a.connOK } @@ -304,7 +305,7 @@ func (a *APIClient) Dial() (kubernetes.Interface, error) { return a.client, nil } -// RestConfigOrDie returns a rest api client. +// RestConfig returns a rest api client. func (a *APIClient) RestConfig() (*restclient.Config, error) { cfg, err := a.config.RESTConfig() if err != nil { diff --git a/internal/config/config.go b/internal/config/config.go index f7095839..4795f908 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -132,6 +132,7 @@ func (c *Config) ActiveNamespace() string { return "default" } +// ValidateFavorites ensure favorite ns are legit. func (c *Config) ValidateFavorites() { cl := c.K9s.ActiveCluster() if cl == nil { diff --git a/internal/config/feature.go b/internal/config/feature.go index c9741fae..52164ec5 100644 --- a/internal/config/feature.go +++ b/internal/config/feature.go @@ -5,7 +5,7 @@ type FeatureGates struct { NodeShell bool `yaml:"nodeShell"` } -// NewFeatureGate returns a new feature gate. +// NewFeatureGates returns a new feature gate. func NewFeatureGates() *FeatureGates { return &FeatureGates{} } diff --git a/internal/dao/cluster.go b/internal/dao/cluster.go index 99d4168e..ace3ab69 100644 --- a/internal/dao/cluster.go +++ b/internal/dao/cluster.go @@ -49,6 +49,7 @@ func scanners() map[string]RefScanner { } } +// ScanForRefs scans cluster resources for resource references. func ScanForRefs(ctx context.Context, f Factory) (Refs, error) { defer func(t time.Time) { log.Debug().Msgf("Cluster Scan %v", time.Since(t)) @@ -101,6 +102,7 @@ func ScanForRefs(ctx context.Context, f Factory) (Refs, error) { return res, nil } +// ScanForSARefs scans cluster resources for serviceaccount refs. func ScanForSARefs(ctx context.Context, f Factory) (Refs, error) { defer func(t time.Time) { log.Debug().Msgf("Cluster Scan %v", time.Since(t)) diff --git a/internal/dao/cronjob.go b/internal/dao/cronjob.go index c93fdc6d..001c73dd 100644 --- a/internal/dao/cronjob.go +++ b/internal/dao/cronjob.go @@ -75,6 +75,7 @@ func (c *CronJob) Run(path string) error { return err } +// ScanSA scans for serviceaccount refs. func (c *CronJob) ScanSA(ctx context.Context, fqn string, wait bool) (Refs, error) { ns, n := client.Namespaced(fqn) oo, err := c.Factory.List(c.GVR(), ns, wait, labels.Everything()) @@ -100,6 +101,7 @@ func (c *CronJob) ScanSA(ctx context.Context, fqn string, wait bool) (Refs, erro return refs, nil } +// Scan scans for cluster resource refs. func (c *CronJob) Scan(ctx context.Context, gvr, fqn string, wait bool) (Refs, error) { ns, n := client.Namespaced(fqn) oo, err := c.Factory.List(c.GVR(), ns, wait, labels.Everything()) diff --git a/internal/dao/dp.go b/internal/dao/dp.go index 1f425cb9..b409a1d8 100644 --- a/internal/dao/dp.go +++ b/internal/dao/dp.go @@ -134,6 +134,7 @@ func (*Deployment) Load(f Factory, fqn string) (*appsv1.Deployment, error) { return &dp, nil } +// ScanSA scans for serviceaccount refs. func (d *Deployment) ScanSA(ctx context.Context, fqn string, wait bool) (Refs, error) { ns, n := client.Namespaced(fqn) oo, err := d.Factory.List(d.GVR(), ns, wait, labels.Everything()) @@ -159,6 +160,7 @@ func (d *Deployment) ScanSA(ctx context.Context, fqn string, wait bool) (Refs, e return refs, nil } +// Scan scans for resource references. func (d *Deployment) Scan(ctx context.Context, gvr, fqn string, wait bool) (Refs, error) { ns, n := client.Namespaced(fqn) oo, err := d.Factory.List(d.GVR(), ns, wait, labels.Everything()) diff --git a/internal/dao/ds.go b/internal/dao/ds.go index a3ead752..a005ed35 100644 --- a/internal/dao/ds.go +++ b/internal/dao/ds.go @@ -148,6 +148,7 @@ func (d *DaemonSet) GetInstance(fqn string) (*appsv1.DaemonSet, error) { return &ds, nil } +// ScanSA scans for serviceaccount refs. func (d *DaemonSet) ScanSA(ctx context.Context, fqn string, wait bool) (Refs, error) { ns, n := client.Namespaced(fqn) oo, err := d.Factory.List(d.GVR(), ns, wait, labels.Everything()) @@ -173,6 +174,7 @@ func (d *DaemonSet) ScanSA(ctx context.Context, fqn string, wait bool) (Refs, er return refs, nil } +// Scan scans for cluster refs. func (d *DaemonSet) Scan(ctx context.Context, gvr, fqn string, wait bool) (Refs, error) { ns, n := client.Namespaced(fqn) oo, err := d.Factory.List(d.GVR(), ns, wait, labels.Everything()) diff --git a/internal/dao/job.go b/internal/dao/job.go index 4958f681..28ae2d1a 100644 --- a/internal/dao/job.go +++ b/internal/dao/job.go @@ -44,6 +44,7 @@ func (j *Job) TailLogs(ctx context.Context, c LogChan, opts LogOptions) error { return podLogs(ctx, c, job.Spec.Selector.MatchLabels, opts) } +// ScanSA scans for serviceaccount refs. func (j *Job) ScanSA(ctx context.Context, fqn string, wait bool) (Refs, error) { ns, n := client.Namespaced(fqn) oo, err := j.Factory.List(j.GVR(), ns, wait, labels.Everything()) @@ -69,6 +70,7 @@ func (j *Job) ScanSA(ctx context.Context, fqn string, wait bool) (Refs, error) { return refs, nil } +// Scan scans for resource references. func (j *Job) Scan(ctx context.Context, gvr, fqn string, wait bool) (Refs, error) { ns, n := client.Namespaced(fqn) oo, err := j.Factory.List(j.GVR(), ns, wait, labels.Everything()) diff --git a/internal/dao/pod.go b/internal/dao/pod.go index 4b624496..5b5518d2 100644 --- a/internal/dao/pod.go +++ b/internal/dao/pod.go @@ -230,6 +230,7 @@ func (p *Pod) TailLogs(ctx context.Context, c LogChan, opts LogOptions) error { return nil } +// ScanSA scans for serviceaccount refs. func (p *Pod) ScanSA(ctx context.Context, fqn string, wait bool) (Refs, error) { ns, n := client.Namespaced(fqn) oo, err := p.Factory.List(p.GVR(), ns, wait, labels.Everything()) @@ -259,6 +260,7 @@ func (p *Pod) ScanSA(ctx context.Context, fqn string, wait bool) (Refs, error) { return refs, nil } +// Scan scans for cluster resource refs. func (p *Pod) Scan(ctx context.Context, gvr, fqn string, wait bool) (Refs, error) { ns, n := client.Namespaced(fqn) oo, err := p.Factory.List(p.GVR(), ns, wait, labels.Everything()) diff --git a/internal/dao/reference.go b/internal/dao/reference.go index 3425892b..a1b99ed2 100644 --- a/internal/dao/reference.go +++ b/internal/dao/reference.go @@ -10,14 +10,14 @@ import ( "k8s.io/apimachinery/pkg/runtime" ) -var ( - _ Accessor = (*Reference)(nil) -) +var _ Accessor = (*Reference)(nil) +// Reference represents cluster resource references. type Reference struct { NonResource } +// List collects all references. func (r *Reference) List(ctx context.Context, ns string) ([]runtime.Object, error) { gvr, ok := ctx.Value(internal.KeyGVR).(string) if !ok { @@ -31,10 +31,12 @@ func (r *Reference) List(ctx context.Context, ns string) ([]runtime.Object, erro } } +// Get fetch a given reference. func (c *Reference) Get(ctx context.Context, path string) (runtime.Object, error) { panic("NYI") } +// Scan scan cluster resources for references. func (r *Reference) Scan(ctx context.Context) ([]runtime.Object, error) { refs, err := ScanForRefs(ctx, r.Factory) if err != nil { @@ -59,6 +61,7 @@ func (r *Reference) Scan(ctx context.Context) ([]runtime.Object, error) { return oo, nil } +// ScanSA scans for serviceaccount refs. func (r *Reference) ScanSA(ctx context.Context) ([]runtime.Object, error) { refs, err := ScanForSARefs(ctx, r.Factory) if err != nil { diff --git a/internal/dao/sts.go b/internal/dao/sts.go index fe693c34..3e32bca8 100644 --- a/internal/dao/sts.go +++ b/internal/dao/sts.go @@ -133,6 +133,7 @@ func (s *StatefulSet) getStatefulSet(fqn string) (*appsv1.StatefulSet, error) { return &sts, nil } +// ScanSA scans for serviceaccount refs. func (s *StatefulSet) ScanSA(ctx context.Context, fqn string, wait bool) (Refs, error) { ns, n := client.Namespaced(fqn) oo, err := s.Factory.List(s.GVR(), ns, wait, labels.Everything()) @@ -158,6 +159,7 @@ func (s *StatefulSet) ScanSA(ctx context.Context, fqn string, wait bool) (Refs, return refs, nil } +// Scan scans for cluster resource refs. func (s *StatefulSet) Scan(ctx context.Context, gvr, fqn string, wait bool) (Refs, error) { ns, n := client.Namespaced(fqn) oo, err := s.Factory.List(s.GVR(), ns, wait, labels.Everything()) diff --git a/internal/render/row_event.go b/internal/render/row_event.go index 3bfe04f0..2202eb83 100644 --- a/internal/render/row_event.go +++ b/internal/render/row_event.go @@ -75,6 +75,7 @@ func (r RowEvent) Customize(cols []int) RowEvent { } } +// ExtractHeaderLabels extract collection of fields into header. func (r RowEvent) ExtractHeaderLabels(labelCol int) []string { hh, _ := sortLabels(labelize(r.Row.Fields[labelCol])) return hh @@ -105,6 +106,7 @@ func (r RowEvent) Diff(re RowEvent, ageCol int) bool { // RowEvents a collection of row events. type RowEvents []RowEvent +// ExtractHeaderLabels extract header labels. func (r RowEvents) ExtractHeaderLabels(labelCol int) []string { ll := make([]string, 0, 10) for _, re := range r { @@ -114,6 +116,7 @@ func (r RowEvents) ExtractHeaderLabels(labelCol int) []string { return ll } +// Labelize converts labels into a row event. func (r RowEvents) Labelize(cols []int, labelCol int, labels []string) RowEvents { out := make(RowEvents, 0, len(r)) for _, re := range r { diff --git a/internal/view/cm.go b/internal/view/cm.go index a6540a4d..54d4d0d0 100644 --- a/internal/view/cm.go +++ b/internal/view/cm.go @@ -50,9 +50,8 @@ func scanRefs(evt *tcell.EventKey, a *App, t *Table, gvr string) *tcell.EventKey if len(refs) == 0 { a.Flash().Warnf("No references found at this time for %s::%s. Check again later!", gvr, path) return nil - } else { - a.Flash().Infof("Viewing references for %s::%s", gvr, path) } + a.Flash().Infof("Viewing references for %s::%s", gvr, path) view := NewReference(client.NewGVR("references")) view.SetContextFn(refContext(gvr, path, false)) if err := a.inject(view); err != nil { diff --git a/internal/view/meow.go b/internal/view/meow.go index 864287d2..f4e7c031 100644 --- a/internal/view/meow.go +++ b/internal/view/meow.go @@ -3,12 +3,13 @@ package view import ( "context" "fmt" + "strings" + "github.com/derailed/k9s/internal/config" "github.com/derailed/k9s/internal/model" "github.com/derailed/k9s/internal/ui" "github.com/derailed/tview" "github.com/gdamore/tcell" - "strings" ) // Meow represents a bomb viewer @@ -50,7 +51,7 @@ func (m *Meow) Init(_ context.Context) error { m.bindKeys() m.SetInputCapture(m.keyboard) - m.talk() + m.talk() return nil } diff --git a/internal/view/sa.go b/internal/view/sa.go index 4484c3c0..e9d18d99 100644 --- a/internal/view/sa.go +++ b/internal/view/sa.go @@ -49,9 +49,8 @@ func scanSARefs(evt *tcell.EventKey, a *App, t *Table, gvr string) *tcell.EventK if len(refs) == 0 { a.Flash().Warnf("No references found at this time for %s::%s. Check again later!", gvr, path) return nil - } else { - a.Flash().Infof("Viewing references for %s::%s", gvr, path) } + a.Flash().Infof("Viewing references for %s::%s", gvr, path) view := NewReference(client.NewGVR("references")) view.SetContextFn(refContext(gvr, path, false)) if err := a.inject(view); err != nil {