mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2026-07-26 15:37:14 +00:00
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>
43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
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))
|
|
})
|
|
}
|