From 3a312692f5005e30935647d5737fc5fb12d5e538 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rogelio=20Dom=C3=ADnguez=20Hern=C3=A1ndez?= Date: Wed, 14 May 2025 09:52:02 -0600 Subject: [PATCH 1/2] honor KUBECONFIG environment variable --- k8s/nodes.go | 11 ++++++++--- k8s/pods.go | 5 ++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/k8s/nodes.go b/k8s/nodes.go index 7288359..3cd6358 100644 --- a/k8s/nodes.go +++ b/k8s/nodes.go @@ -100,7 +100,10 @@ type KubeletStats struct { } func ClusterInfo() Cluster { - kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config") + kubeconfig := os.Getenv("KUBECONFIG") + if kubeconfig == "" { + kubeconfig = filepath.Join(os.Getenv("HOME"), ".kube", "config") + } confvar := clientcmd.GetConfigFromFileOrDie(kubeconfig) K8sinfo := Cluster{} @@ -348,11 +351,13 @@ func getPodStats(clientset *kubernetes.Clientset, node *core.Node, podName strin } func Nodes(inputs *utils.Inputs) (NodeStatsList []Node) { - metric := inputs.Metrics utils.InitLogger() - kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config") + kubeconfig := os.Getenv("KUBECONFIG") + if kubeconfig == "" { + kubeconfig = filepath.Join(os.Getenv("HOME"), ".kube", "config") + } config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) if err != nil { diff --git a/k8s/pods.go b/k8s/pods.go index 182c9d8..194dd79 100644 --- a/k8s/pods.go +++ b/k8s/pods.go @@ -43,7 +43,10 @@ func Pods(inputs *utils.Inputs) (PodStatsList []Pod) { metric := inputs.Metrics utils.InitLogger() - kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config") + kubeconfig := os.Getenv("KUBECONFIG") + if kubeconfig == "" { + kubeconfig = filepath.Join(os.Getenv("HOME"), ".kube", "config") + } config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) if err != nil { From ae2a92c15d4b401303e2dde9115ad8bfd749a562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rogelio=20Dom=C3=ADnguez=20Hern=C3=A1ndez?= Date: Wed, 14 May 2025 10:05:11 -0600 Subject: [PATCH 2/2] update README to document usage of KUBECONFIG envvar --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b195880..158efb0 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ You no longer have to wait for that `prometheus` and `grafana` - Just check the ![Alt text](assets/KubeNodeUsage3.0.4.gif) -KubeNodeUsage use your local KubeConfig file to connect to the cluster and use Kubernetes API directly using Kubernetes GO SDK +KubeNodeUsage use your local kubeconfig file to connect to the cluster and use Kubernetes API directly using Kubernetes GO SDK It fetches the Node and Pod Metrics from Kubernetes API and apply filters and aggreations to display it in a nice human readable Graphical format as Bar Charts. @@ -123,17 +123,17 @@ I have tested KubeNodeUsage starting from **1.19 - 1.29** ( the latest stable ve KubeNodeUsage is written in `GoLang` and uses `client-go` and `kubernetes` SDK libraries -When you start KubeNodeUsage - It try to read the `$HOME/.kube/config` file in your HOME directory +When you start KubeNodeUsage - It first checks for the `KUBECONFIG` environment variable. If set, it will use that path for the Kubernetes configuration. If not set, it falls back to reading the `$HOME/.kube/config` file in your HOME directory. -KubeNodeUsage connects to the Default cluster set by the `CurrentContext` on the ./kube/config file +KubeNodeUsage connects to the Default cluster set by the `CurrentContext` on the kubeconfig file. You can manually edit this file and update it but the recommended way to update current-context is to use `kubectl config use-context` -KubeNodeUsage does not use `kubectl` directly and it relies on the `.kube/config` file and the authentication method defined in there +KubeNodeUsage does not use `kubectl` directly and it relies on the kubeconfig file and the authentication method defined in there -KubeNodeUsage works the same way - kubectl works based on the configuration information found on .kube/config file +KubeNodeUsage works the same way - kubectl works based on the configuration information found on the kubeconfig file -If the `kubectl`commands are not working - its highly likely KubeNodeUsage would fail too - In this case you have to check your Kube config file +If the `kubectl` commands are not working - its highly likely KubeNodeUsage would fail too - In this case you have to check your kubeconfig file