mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2026-07-17 02:04:40 +00:00
fix: resolve EKS default kubeconfig path across platforms (#1686)
The EKS integration analyzer built the default kubeconfig path from
os.Getenv("HOME") when no explicit --kubeconfig was provided. On Windows
the HOME environment variable is normally unset (Windows exposes
USERPROFILE, and HOMEDRIVE plus HOMEPATH), so os.Getenv("HOME") returned
"" and filepath.Join collapsed to the relative path .kube/config resolved
against the current working directory rather than the user's home. The
analyzer then loaded an empty config and reported "EKS cluster was not
detected" even when a valid ~/.kube/config existed.
Resolve the home directory with os.UserHomeDir() instead, matching the
convention already used elsewhere in the codebase (cmd/root.go), so the
default kubeconfig path works on Windows as well as Unix. The path
resolution is extracted into a small getKubeconfigPath helper and covered
by a unit test.
Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
Co-authored-by: Alex Jones <1235925+AlexsJones@users.noreply.github.com>
This commit is contained in:
@@ -22,12 +22,9 @@ func (e *EKSAnalyzer) Analyze(analysis common.Analyzer) ([]common.Result, error)
|
||||
_ = map[string]common.PreAnalysis{}
|
||||
svc := eks.New(e.session)
|
||||
// Get the name of the current cluster
|
||||
var kubeconfig string
|
||||
kubeconfigFromPath := viper.GetString("kubeconfig")
|
||||
if kubeconfigFromPath != "" {
|
||||
kubeconfig = kubeconfigFromPath
|
||||
} else {
|
||||
kubeconfig = filepath.Join(os.Getenv("HOME"), ".kube", "config")
|
||||
kubeconfig, err := getKubeconfigPath()
|
||||
if err != nil {
|
||||
return cr, err
|
||||
}
|
||||
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
|
||||
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
|
||||
@@ -78,3 +75,21 @@ func (e *EKSAnalyzer) Analyze(analysis common.Analyzer) ([]common.Result, error)
|
||||
}
|
||||
return cr, nil
|
||||
}
|
||||
|
||||
// getKubeconfigPath returns the kubeconfig path to use for EKS cluster
|
||||
// detection. An explicit --kubeconfig (via viper) takes precedence; otherwise
|
||||
// it falls back to $HOME/.kube/config, resolving the home directory with
|
||||
// os.UserHomeDir() so the default works across platforms (on Windows the HOME
|
||||
// environment variable is typically unset).
|
||||
func getKubeconfigPath() (string, error) {
|
||||
if kubeconfig := viper.GetString("kubeconfig"); kubeconfig != "" {
|
||||
return kubeconfig, nil
|
||||
}
|
||||
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return filepath.Join(home, ".kube", "config"), nil
|
||||
}
|
||||
|
||||
42
pkg/integration/aws/eks_test.go
Normal file
42
pkg/integration/aws/eks_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package aws
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGetKubeconfigPath(t *testing.T) {
|
||||
t.Run("uses explicit kubeconfig from viper when set", func(t *testing.T) {
|
||||
viper.Reset()
|
||||
t.Cleanup(viper.Reset)
|
||||
explicit := filepath.Join("custom", "kubeconfig")
|
||||
viper.Set("kubeconfig", explicit)
|
||||
|
||||
got, err := getKubeconfigPath()
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, explicit, got)
|
||||
})
|
||||
|
||||
t.Run("falls back to the user home dir kubeconfig", func(t *testing.T) {
|
||||
viper.Reset()
|
||||
t.Cleanup(viper.Reset)
|
||||
|
||||
home, err := os.UserHomeDir()
|
||||
require.NoError(t, err)
|
||||
|
||||
got, err := getKubeconfigPath()
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, filepath.Join(home, ".kube", "config"), got)
|
||||
// The fallback must resolve against the user's home directory rather
|
||||
// than collapsing to a relative ".kube/config", which is what happened
|
||||
// on Windows where the HOME environment variable is typically unset.
|
||||
assert.True(t, filepath.IsAbs(got))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user