Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion cmd/eks-node-viewer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"log"
"os"
"os/exec"
"strings"

"github.com/aws/aws-sdk-go-v2/config"
Expand Down Expand Up @@ -73,7 +74,20 @@ func main() {
if err != nil {
log.Fatalf("creating style, %s", err)
}
m := model.NewUIModel(strings.Split(flags.ExtraLabels, ","), flags.NodeSort, style)

kubeconfig := ""
if flags.Kubeconfig != "" {
kubeconfig = flags.Kubeconfig
} else {
kubeconfig = "~/.kube/config"
}

out, err := exec.Command("kubectl", "config", "current-context", "--kubeconfig", kubeconfig).Output()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be possible to pull this from the kubeconfig file, I would greatly prefer that to running the kubectl binary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, would be better to just parse the json config file. I'll do that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can get it via something like this:

-func getConfig(kubeconfig, context string) (*rest.Config, error) {
-	// use the current context in kubeconfig
-	return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
+func getConfig(kubeconfig, context string) (string, *rest.Config, error) {
+	loadCfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
 		&clientcmd.ClientConfigLoadingRules{Precedence: strings.Split(kubeconfig, ":")},
-		&clientcmd.ConfigOverrides{CurrentContext: context}).ClientConfig()
+		&clientcmd.ConfigOverrides{CurrentContext: context})
+	raw, err := loadCfg.RawConfig()
+	if err != nil {
+		return "", nil, err
+	}
+	cfg, err := loadCfg.ClientConfig()
+	if err != nil {
+		return "", nil, err
+	}
+	return raw.CurrentContext, cfg, err
 }

if err != nil {
log.Fatal(err)
}

m := model.NewUIModel(strings.Split(flags.ExtraLabels, ","), flags.NodeSort, style, string(out))
m.DisablePricing = flags.DisablePricing
m.SetResources(strings.FieldsFunc(flags.Resources, func(r rune) bool { return r == ',' }))

Expand Down
4 changes: 3 additions & 1 deletion pkg/model/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ type Cluster struct {
nodes map[string]*Node
pods map[objectKey]*Pod
resources []v1.ResourceName
name string
}

func NewCluster() *Cluster {
func NewCluster(context string) *Cluster {
return &Cluster{
nodes: map[string]*Node{},
pods: map[objectKey]*Pod{},
resources: []v1.ResourceName{v1.ResourceCPU},
name: context,
}
}
func (c *Cluster) AddNode(node *Node) *Node {
Expand Down
12 changes: 6 additions & 6 deletions pkg/model/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

func TestClusterAddNode(t *testing.T) {
cluster := model.NewCluster()
cluster := model.NewCluster("mycontext-name")

if got := len(cluster.Stats().Nodes); got != 0 {
t.Errorf("expected 0 nodes, got %d", got)
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestClusterAddNode(t *testing.T) {
}

func TestClusterGetNodeByProviderID(t *testing.T) {
cluster := model.NewCluster()
cluster := model.NewCluster("mycontext-name")

_, ok := cluster.GetNode("mynode-id")
if ok {
Expand All @@ -88,7 +88,7 @@ func TestClusterGetNodeByProviderID(t *testing.T) {
}

func TestClusterGetNodeByName(t *testing.T) {
cluster := model.NewCluster()
cluster := model.NewCluster("mycontext-name")

_, ok := cluster.GetNodeByName("mynode")
if ok {
Expand All @@ -105,7 +105,7 @@ func TestClusterGetNodeByName(t *testing.T) {
}

func TestClusterUpdateNode(t *testing.T) {
cluster := model.NewCluster()
cluster := model.NewCluster("mycontext-name")

n1 := testNode("mynode")
n1.Status.Allocatable = v1.ResourceList{
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestClusterUpdateNode(t *testing.T) {
}

func TestClusterAddPod(t *testing.T) {
cluster := model.NewCluster()
cluster := model.NewCluster("mycontext-name")

n := testNode("mynode")
n.Spec.ProviderID = "mynode-id"
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestClusterAddPod(t *testing.T) {
}

func TestClusterDeleteNodeDeletesPods(t *testing.T) {
cluster := model.NewCluster()
cluster := model.NewCluster("mycontext-name")

// add a node and pod bound to that node
n := testNode("mynode")
Expand Down
9 changes: 7 additions & 2 deletions pkg/model/uimodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ type UIModel struct {
DisablePricing bool
}

func NewUIModel(extraLabels []string, nodeSort string, style *Style) *UIModel {
func NewUIModel(extraLabels []string, nodeSort string, style *Style, context string) *UIModel {
pager := paginator.New()
pager.Type = paginator.Dots
pager.ActiveDot = activeDot
pager.InactiveDot = inactiveDot
return &UIModel{
// red to green
progress: progress.New(style.gradient),
cluster: NewCluster(),
cluster: NewCluster(context),
extraLabels: extraLabels,
paginator: pager,
nodeSorter: makeNodeSorter(nodeSort),
Expand All @@ -88,6 +88,10 @@ func (u *UIModel) View() string {
})

ctw := text.NewColorTabWriter(&b, 0, 8, 1)

fmt.Fprintln(&b, "Viewing cluster: ", u.cluster.name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think taking up a new line on the screen is too much, can it be fit anywhere else? Maybe after the "q: quit" message?

Copy link
Author

@ianis-c ianis-c Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in my use case it's a pretty important information that's why I put it up.
But I can move it at the bottom under the controls description.

fmt.Fprintln(&b)

u.writeClusterSummary(u.cluster.resources, stats, ctw)
ctw.Flush()
u.progress.ShowPercentage = true
Expand Down Expand Up @@ -122,6 +126,7 @@ func (u *UIModel) View() string {

fmt.Fprintln(&b, u.paginator.View())
fmt.Fprintln(&b, helpStyle("←/→ page • q: quit"))

return b.String()
}

Expand Down