mine
derailed 2020-05-05 18:28:54 -06:00
parent 6b65938dbc
commit 6bd186c377
16 changed files with 42 additions and 36 deletions

View File

@ -232,9 +232,9 @@ linters:
- govet
- funlen
- gocyclo
disable:
- maligned
- prealloc
disable:
- gosec
disable-all: false
presets:

View File

@ -173,14 +173,12 @@ func (a *APIClient) ValidNamespaces() ([]v1.Namespace, error) {
return nss, nil
}
}
log.Debug().Msgf(">>>>> Loading all namespaces")
ctx, cancel := context.WithTimeout(context.Background(), CallTimeout)
defer cancel()
nn, err := a.DialOrDie().CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
a.cache.Add("validNamespaces", nn.Items, cacheExpiry)
return nn.Items, nil

View File

@ -124,6 +124,13 @@ func (c *Config) CurrentCluster() *Cluster {
// ActiveNamespace returns the active namespace in the current cluster.
func (c *Config) ActiveNamespace() string {
if c.client != nil {
ns := c.client.ActiveNamespace()
if client.IsNamespaced(ns) {
return ns
}
}
if cl := c.CurrentCluster(); cl != nil {
if cl.Namespace != nil {
return cl.Namespace.Active

View File

@ -17,13 +17,13 @@ type Plugins struct {
// Plugin describes a K9s plugin
type Plugin struct {
ShortCut string `yaml:"shortCut"`
Confirm bool `yaml:"confirm"`
Scopes []string `yaml:"scopes"`
Args []string `yaml:"args"`
ShortCut string `yaml:"shortCut"`
Description string `yaml:"description"`
Command string `yaml:"command"`
Confirm bool `yaml:"confirm"`
Background bool `yaml:"background"`
Args []string `yaml:"args"`
}
// NewPlugins returns a new plugin.

View File

@ -129,7 +129,7 @@ func (h Header) Columns(wide bool) []string {
if len(h) == 0 {
return nil
}
var cc []string
cc := make([]string, 0, len(h))
for _, c := range h {
if !wide && c.Wide {
continue

View File

@ -76,7 +76,7 @@ func renderSubjects(ss []rbacv1.Subject) (kind string, subjects string) {
return NAValue, ""
}
var tt []string
tt := make([]string, 0, len(ss))
for _, s := range ss {
kind = toSubjectAlias(s.Kind)
tt = append(tt, s.Name)

View File

@ -43,7 +43,7 @@ func (s *SelectTable) GetSelectedItems() []string {
return []string{s.GetSelectedItem()}
}
var items []string
items := make([]string, 0, len(s.marks))
for item := range s.marks {
items = append(items, item)
}

View File

@ -29,21 +29,20 @@ type (
// Table represents tabular data.
type Table struct {
gvr client.GVR
sortCol SortColumn
header render.Header
Path string
Extras string
*SelectTable
actions KeyActions
gvr client.GVR
Path string
Extras string
cmdBuff *model.FishBuff
styles *config.Styles
viewSetting *config.ViewSetting
sortCol SortColumn
colorerFn render.ColorerFunc
decorateFn DecorateFunc
wide bool
toast bool
header render.Header
hasMetrics bool
}

View File

@ -160,7 +160,7 @@ func rxFilter(q string, data render.TableData) (render.TableData, error) {
func fuzzyFilter(q string, data render.TableData) render.TableData {
q = strings.TrimSpace(q)
var ss []string
ss := make([]string, 0, len(data.RowEvents))
for _, re := range data.RowEvents {
ss = append(ss, re.Row.ID)
}

View File

@ -21,6 +21,7 @@ import (
"github.com/derailed/tview"
"github.com/gdamore/tcell"
"github.com/rs/zerolog/log"
"k8s.io/apimachinery/pkg/labels"
)
// ExitStatus indicates UI exit conditions.
@ -36,18 +37,17 @@ const (
// App represents an application view.
type App struct {
version string
*ui.App
Content *PageStack
command *Command
factory *watch.Factory
version string
showHeader bool
cancelFn context.CancelFunc
conRetry int32
clusterModel *model.ClusterInfo
cmdHistory *model.History
filterHistory *model.History
conRetry int32
showHeader bool
}
// NewApp returns a K9s app instance.
@ -93,6 +93,9 @@ func (a *App) Init(version string, rate int) error {
}
a.factory = watch.NewFactory(a.Conn())
if !a.isValidNS(ns) {
return fmt.Errorf("Invalid namespace %s", ns)
}
a.initFactory(ns)
a.clusterModel = model.NewClusterInfo(a.factory, version)
@ -315,16 +318,9 @@ func (a *App) isValidNS(ns string) bool {
if ns == client.AllNamespaces || ns == client.NamespaceAll {
return true
}
nn, err := a.Conn().ValidNamespaces()
if err != nil {
return false
}
for _, n := range nn {
if n.Name == ns {
return true
}
}
return false
_, err := a.factory.Get("v1/namespaces", client.FQN(client.ClusterScope, ns), true, labels.Everything())
return err == nil
}
func (a *App) switchCtx(name string, loadPods bool) error {

View File

@ -76,7 +76,7 @@ func describeResource(app *App, model ui.Tabular, gvr, path string) {
}
func showPodsWithLabels(app *App, path string, sel map[string]string) {
var labels []string
labels := make([]string, 0, len(sel))
for k, v := range sel {
labels = append(labels, fmt.Sprintf("%s=%s", k, v))
}

View File

@ -53,8 +53,8 @@ func (n *Node) bindKeys(aa ui.KeyActions) {
}
}
func (n *Node) showPods(app *App, _ ui.Tabular, _, path string) {
showPods(app, n.GetTable().GetSelectedItem(), "", "spec.nodeName="+path)
func (n *Node) showPods(a *App, _ ui.Tabular, _, path string) {
showPods(a, n.GetTable().GetSelectedItem(), client.AllNamespaces, "spec.nodeName="+path)
}
func (n *Node) drainCmd(evt *tcell.EventKey) *tcell.EventKey {

View File

@ -73,7 +73,7 @@ func DismissPortForwards(v ResourceViewer, p *ui.Pages) {
// Helpers...
func extractPort(p string) string {
rx := regexp.MustCompile(`\A(\w+)/?(\w+)?:?(\d+)?(UDP)?\z`)
rx := regexp.MustCompile(`\A([\w|-]+)/?([\w|-]+)?:?(\d+)?(UDP)?\z`)
mm := rx.FindStringSubmatch(p)
if len(mm) != 5 {
return p

View File

@ -28,6 +28,12 @@ func TestExtractPort(t *testing.T) {
"unamed": {
"dns/53", "53",
},
"pod-dashed": {
"blee-fred/:5000", "5000",
},
"co-dashed": {
"blee/fred-doh:5000", "5000",
},
}
for k := range uu {

View File

@ -75,7 +75,7 @@ func (s *Service) locatePods(ctx context.Context, ns string, sel map[string]stri
return nil, fmt.Errorf("Expecting a factory but got %T", ctx.Value(internal.KeyFactory))
}
var ll []string
ll := make([]string, 0, len(sel))
for k, v := range sel {
ll = append(ll, fmt.Sprintf("%s=%s", k, v))
}

View File

@ -205,7 +205,7 @@ func (t *TreeNode) Spec() NodeSpec {
// Flatten returns a collection of node specs.
func (t *TreeNode) Flatten() []NodeSpec {
var refs []NodeSpec
refs := make([]NodeSpec, 0, len(t.Children))
for _, c := range t.Children {
if c.IsLeaf() {
refs = append(refs, c.Spec())