Skip to content
Merged
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
17 changes: 14 additions & 3 deletions internal/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package utils

import (
"fmt"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"os"
"path/filepath"
"strings"

clientcmdapi "k8s.io/client-go/tools/clientcmd/api"

"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
Expand All @@ -47,16 +49,25 @@ func NewNamespacedContext(namespace, context string) *NamespacedContext {
}
}

func loadKubeconfig() (*clientcmdapi.Config, error) {
func getKubeconfigPath() (string, error) {
kubeconfigPath := os.Getenv("KUBECONFIG")
if kubeconfigPath == "" {
home := homedir.HomeDir()
if home == "" {
return nil, fmt.Errorf("could not find home directory")
return "", fmt.Errorf("could not find home directory")
}
kubeconfigPath = filepath.Join(home, ".kube", "config")
}
// If KUBECONFIG contains multiple paths, use the first one.
parts := strings.Split(kubeconfigPath, ":")
return parts[0], nil
}

func loadKubeconfig() (*clientcmdapi.Config, error) {
kubeconfigPath, err := getKubeconfigPath()
if err != nil {
return nil, err
}
return clientcmd.LoadFromFile(kubeconfigPath)
}

Expand Down
59 changes: 59 additions & 0 deletions internal/utils/kubernetes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2025 Michele Zanotti <m.zanotti019@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package utils

import (
"testing"
)

func TestGetKubeconfigPath(t *testing.T) {

t.Run("Test with KUBECONFIG set", func(t *testing.T) {
t.Setenv("KUBECONFIG", "/tmp/kubeconfig")
kubeconfigPath, err := getKubeconfigPath()
if err != nil {
t.Fatalf("Failed to get kubeconfig path: %v", err)
}
if kubeconfigPath != "/tmp/kubeconfig" {
t.Fatalf("Expected /tmp/kubeconfig, got %s", kubeconfigPath)
}
})

t.Run("Test with KUBECONFIG with multiple parts", func(t *testing.T) {
t.Setenv("KUBECONFIG", "/tmp/kubeconfig:/tmp/kubeconfig2")
kubeconfigPath, err := getKubeconfigPath()
if err != nil {
t.Fatalf("Failed to get kubeconfig path: %v", err)
}
if kubeconfigPath != "/tmp/kubeconfig" {
t.Fatalf("Expected /tmp/kubeconfig, got %s", kubeconfigPath)
}
})

t.Run("Test with KUBECONFIG not set", func(t *testing.T) {
t.Setenv("HOME", "/tmp")
t.Setenv("KUBECONFIG", "")
kubeconfigPath, err := getKubeconfigPath()
if err != nil {
t.Fatalf("Failed to get kubeconfig path: %v", err)
}
expectedPath := "/tmp/.kube/config"
if kubeconfigPath != expectedPath {
t.Fatalf("Expected %s, got %s", expectedPath, kubeconfigPath)
}
})
}
Loading