From d56709de3e8cc5b93ee5d263558e8235a34bbef0 Mon Sep 17 00:00:00 2001 From: Elf Date: Wed, 31 Mar 2021 12:58:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9C=A8=E5=90=AF=E5=8A=A8=E6=97=B6?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=20kubectl=20context=20=E4=B8=AD=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E7=9A=84=20namespace=20(#59)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/app/action.go | 18 +++++++++--------- pkg/app/error.go | 1 + pkg/app/handler.go | 6 +++--- pkg/app/panel.go | 13 +++++++++++++ pkg/app/render.go | 3 ++- pkg/app/statekey.go | 21 +++++++++++---------- pkg/kubecli/config/current_context.go | 11 ++++++++++- pkg/kubecli/kubecli.go | 2 +- 8 files changed, 50 insertions(+), 25 deletions(-) diff --git a/pkg/app/action.go b/pkg/app/action.go index 9079e9c..cd5c772 100644 --- a/pkg/app/action.go +++ b/pkg/app/action.go @@ -170,17 +170,17 @@ var ( } tailLogsAction = &guilib.Action{ - Keys: keyMap[tailLogsActionName], - Name: tailLogsActionName, - Handler: tailLogsHandler, - Mod: gocui.ModNone, + Keys: keyMap[tailLogsActionName], + Name: tailLogsActionName, + Handler: tailLogsHandler, + Mod: gocui.ModNone, } scrollLogsAction = &guilib.Action{ - Keys: keyMap[scrollLogsActionName], - Name: scrollLogsActionName, - Handler: scrollLogsHandler, - Mod: gocui.ModNone, + Keys: keyMap[scrollLogsActionName], + Name: scrollLogsActionName, + Handler: scrollLogsHandler, + Mod: gocui.ModNone, } runPodAction = &guilib.Action{ @@ -373,7 +373,7 @@ func switchNamespace(gui *guilib.Gui, selectedNamespaceLine string) { log.Logger.Warningf("switchNamespace - detailView.SetOrigin(0, 0) error %s", err) } gui.ReRenderViews(resizeableViews...) - gui.ReRenderViews(navigationViewName, detailViewName) + gui.ReRenderViews(clusterInfoViewName, navigationViewName, detailViewName) } func newMoreActions(moreActions []*moreAction) *guilib.Action { diff --git a/pkg/app/error.go b/pkg/app/error.go index ddb04e3..22d221b 100644 --- a/pkg/app/error.go +++ b/pkg/app/error.go @@ -5,4 +5,5 @@ import "errors" var ( resourceNotFoundErr = errors.New("Resource not found. ") noResourceSelectedErr = errors.New("No resource selected. ") + noNamespaceSelectedErr = errors.New("No namespace selected. ") ) diff --git a/pkg/app/handler.go b/pkg/app/handler.go index 8fbecfa..f76ef6e 100644 --- a/pkg/app/handler.go +++ b/pkg/app/handler.go @@ -234,16 +234,16 @@ func getResourceNamespaceAndName(gui *guilib.Gui, resourceView *guilib.View) (na if notResourceSelected(resourceName) { return "", "", noResourceSelectedErr } - return namespace, resourceName, nil + return kubecli.Cli.Namespace(), resourceName, nil } namespace = formatResourceName(selected, 0) resourceName = formatResourceName(selected, 1) if notResourceSelected(resourceName) { - return "", "", noResourceSelectedErr + return kubecli.Cli.Namespace(), "", noResourceSelectedErr } - if namespace == "" { + if notResourceSelected(namespace) { namespace = kubecli.Cli.Namespace() } diff --git a/pkg/app/panel.go b/pkg/app/panel.go index 2ef8ab1..91b9f11 100644 --- a/pkg/app/panel.go +++ b/pkg/app/panel.go @@ -1,6 +1,7 @@ package app import ( + "errors" "github.com/TNK-Studio/lazykube/pkg/config" guilib "github.com/TNK-Studio/lazykube/pkg/gui" "github.com/TNK-Studio/lazykube/pkg/kubecli" @@ -179,6 +180,18 @@ var ( formatted = "" } + _, err := view.GetState(iniDefaultNamespaceKey) + if err != nil { + if errors.Is(err, guilib.StateKeyError) { + ns := kubecli.Cli.Namespace() + if err := view.SetState(iniDefaultNamespaceKey, ns, false); err != nil { + return err + } + return nil + } + return err + } + if formatted == "" { switchNamespace(gui, "") return nil diff --git a/pkg/app/render.go b/pkg/app/render.go index 4c8ea48..a8ca06d 100644 --- a/pkg/app/render.go +++ b/pkg/app/render.go @@ -259,8 +259,9 @@ func navigationOnClick(gui *guilib.Gui, view *guilib.View) error { func renderClusterInfo(_ *guilib.Gui, view *guilib.View) error { view.Clear() currentContext := kubecli.Cli.CurrentContext() + currentNs := kubecli.Cli.Namespace() - if _, err := fmt.Fprintf(view, "Current Context: %s", color.Green.Sprint(currentContext)); err != nil { + if _, err := fmt.Fprintf(view, "Current Context: %s Namespace: %s", color.Green.Sprint(currentContext), color.Green.Sprint(currentNs)); err != nil { return err } return nil diff --git a/pkg/app/statekey.go b/pkg/app/statekey.go index ae6a37c..a722833 100644 --- a/pkg/app/statekey.go +++ b/pkg/app/statekey.go @@ -1,14 +1,15 @@ package app const ( - viewLastRenderTimeStateKey = "viewLastRenderTime" // value type: time.Time - cpuPlotStateKey = "cpuPlot" // value type: *gui.Plot - memoryPlotStateKey = "memoryPlot" // value type: *gui.Plot - moreActionTriggerViewStateKey = "triggerView" // value type: *gui.View - filterInputValueStateKey = "filterInputValue" // value type: string - confirmValueStateKey = "confirmValue" // value type: string - logSinceTimeStateKey = "logSinceTime" // value type: time.Time - ScrollingLogsStateKey = "scrollingLogs" // value type: boolean - podContainersStateKey = "podContainers" // value type: []string - logContainerStateKey = "logContainer" // value type: string + viewLastRenderTimeStateKey = "viewLastRenderTime" // value type: time.Time + cpuPlotStateKey = "cpuPlot" // value type: *gui.Plot + memoryPlotStateKey = "memoryPlot" // value type: *gui.Plot + moreActionTriggerViewStateKey = "triggerView" // value type: *gui.View + filterInputValueStateKey = "filterInputValue" // value type: string + confirmValueStateKey = "confirmValue" // value type: string + logSinceTimeStateKey = "logSinceTime" // value type: time.Time + ScrollingLogsStateKey = "scrollingLogs" // value type: boolean + podContainersStateKey = "podContainers" // value type: []string + logContainerStateKey = "logContainer" // value type: string + iniDefaultNamespaceKey = "iniDefaultNamespace" // value type: string ) diff --git a/pkg/kubecli/config/current_context.go b/pkg/kubecli/config/current_context.go index 2929bcd..908d43f 100644 --- a/pkg/kubecli/config/current_context.go +++ b/pkg/kubecli/config/current_context.go @@ -26,9 +26,18 @@ func SetCurrentContext(context string) { config.CurrentContext = context } +func ContextNamespace() string { + ctx, ok := config.Contexts[config.CurrentContext] + if !ok { + return "" + } + ns := ctx.Namespace + return ns +} + func ListContexts() []string { contexts := make([]string, 0) - for name, _ := range config.Contexts { + for name := range config.Contexts { contexts = append(contexts, name) } return contexts diff --git a/pkg/kubecli/kubecli.go b/pkg/kubecli/kubecli.go index 6cac54f..7219310 100644 --- a/pkg/kubecli/kubecli.go +++ b/pkg/kubecli/kubecli.go @@ -63,7 +63,7 @@ func (c *Cmd) SetFlag(name, value string) *Cmd { } func NewKubeCLI() *KubeCLI { - namespace := "" + namespace := config.ContextNamespace() context := config.CurrentContext() kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag() kubeConfigFlags.Namespace = &namespace