Skip to content

Commit

Permalink
Merge branch 'main' of github.com:TNK-Studio/lazykube
Browse files Browse the repository at this point in the history
  • Loading branch information
elfgzp committed Sep 30, 2021
2 parents c3a3259 + 7bf320e commit a054738
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
7 changes: 6 additions & 1 deletion pkg/app/panel.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"context"
"errors"
"github.com/TNK-Studio/lazykube/pkg/config"
guilib "github.com/TNK-Studio/lazykube/pkg/gui"
Expand Down Expand Up @@ -175,6 +176,10 @@ var (
Clickable: true,
OnRender: namespaceRender,
OnSelectedLineChange: func(gui *guilib.Gui, view *guilib.View, selectedLine string) error {
if !kubecli.Cli.HasNamespacePermission(context.Background()) {
return nil
}

formatted := formatResourceName(selectedLine, 0)
if notResourceSelected(formatted) {
formatted = ""
Expand Down Expand Up @@ -542,4 +547,4 @@ func resourceLogAble(resource string) bool {
}
}
return false
}
}
4 changes: 2 additions & 2 deletions pkg/app/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var (
)

func notResourceSelected(selectedName string) bool {
if selectedName == "" || selectedName == "NAME" || selectedName == "NAMESPACE" || selectedName == "No" {
if selectedName == "" {
return true
}
return false
Expand Down Expand Up @@ -299,7 +299,7 @@ func topNodesRender(_ *guilib.Gui, view *guilib.View) error {

func namespaceRender(_ *guilib.Gui, view *guilib.View) error {
view.Clear()
kubecli.Cli.Get(viewStreams(view), "namespaces").Run()
kubecli.Cli.Get(viewStreams(view), namespaceResource).Run()
return nil
}

Expand Down
34 changes: 34 additions & 0 deletions pkg/kubecli/get_namespaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package kubecli

import (
"context"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

func (cli *KubeCLI) GetNamespaces(ctx context.Context, opts metav1.ListOptions) (*v1.NamespaceList, error) {
config, err := cli.factory.ToRESTConfig()
if err != nil {
return nil, err
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return client.CoreV1().Namespaces().List(ctx, opts)
}

func (cli *KubeCLI) HasNamespacePermission(ctx context.Context) bool {
_, err := cli.GetNamespaces(ctx, metav1.ListOptions{})
if err == nil {
return true
}

if errors.IsForbidden(err) {
return false
}

return true
}
6 changes: 3 additions & 3 deletions pkg/kubecli/kubecli.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func init() {
}

type KubeCLI struct {
factory util.Factory
namespace *string
context *string
factory util.Factory
namespace *string
context *string
}

type Cmd struct {
Expand Down

0 comments on commit a054738

Please sign in to comment.