From f247b3ffb73613fdf76ab761d0594cee718012a9 Mon Sep 17 00:00:00 2001 From: Anas Khan Date: Tue, 14 Jul 2026 13:39:55 +0530 Subject: [PATCH] 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> --- pkg/integration/aws/eks.go | 27 ++++++++++++++++----- pkg/integration/aws/eks_test.go | 42 +++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 pkg/integration/aws/eks_test.go diff --git a/pkg/integration/aws/eks.go b/pkg/integration/aws/eks.go index 21bd6f01..ee9760c3 100644 --- a/pkg/integration/aws/eks.go +++ b/pkg/integration/aws/eks.go @@ -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 +} diff --git a/pkg/integration/aws/eks_test.go b/pkg/integration/aws/eks_test.go new file mode 100644 index 00000000..cc74a1d2 --- /dev/null +++ b/pkg/integration/aws/eks_test.go @@ -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)) + }) +}