Skip to content

Commit

Permalink
feat: 在启动时读取 kubectl context 中默认的 namespace (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
elfgzp authored Mar 31, 2021
1 parent 5d2cc83 commit d56709d
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 25 deletions.
18 changes: 9 additions & 9 deletions pkg/app/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/app/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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. ")
)
6 changes: 3 additions & 3 deletions pkg/app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/app/panel.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/app/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 11 additions & 10 deletions pkg/app/statekey.go
Original file line number Diff line number Diff line change
@@ -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
)
11 changes: 10 additions & 1 deletion pkg/kubecli/config/current_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubecli/kubecli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d56709d

Please sign in to comment.