diff --git a/pkg/antctl/raw/supportbundle/command.go b/pkg/antctl/raw/supportbundle/command.go index 1e2550f0333..d5787929dd2 100644 --- a/pkg/antctl/raw/supportbundle/command.go +++ b/pkg/antctl/raw/supportbundle/command.go @@ -120,13 +120,10 @@ func init() { } } -var getSupportBundleClient func(cmd *cobra.Command) (systemclientset.SupportBundleInterface, error) = setupSupportBundleClient +var getSupportBundleClient func() (systemclientset.SupportBundleInterface, error) = setupSupportBundleClient -func setupSupportBundleClient(cmd *cobra.Command) (systemclientset.SupportBundleInterface, error) { - kubeconfig, err := raw.ResolveKubeconfig(cmd) - if err != nil { - return nil, err - } +func setupSupportBundleClient() (systemclientset.SupportBundleInterface, error) { + kubeconfig := &rest.Config{} raw.SetupLocalKubeconfig(kubeconfig) client, err := systemclientset.NewForConfig(kubeconfig) return client.SupportBundles(), err @@ -134,7 +131,7 @@ func setupSupportBundleClient(cmd *cobra.Command) (systemclientset.SupportBundle func localSupportBundleRequest(cmd *cobra.Command, mode string, writer io.Writer) error { ctx := cmd.Context() - client, err := getSupportBundleClient(cmd) + client, err := getSupportBundleClient() if err != nil { return fmt.Errorf("error when creating system client: %w", err) } diff --git a/pkg/antctl/raw/supportbundle/command_test.go b/pkg/antctl/raw/supportbundle/command_test.go index a8e9e89d818..cbca643e7f2 100644 --- a/pkg/antctl/raw/supportbundle/command_test.go +++ b/pkg/antctl/raw/supportbundle/command_test.go @@ -136,7 +136,7 @@ func createFakeSupportBundleClient() systemclientset.SupportBundleInterface { } func TestLocalSupportBundleRequest(t *testing.T) { - getSupportBundleClient = func(cmd *cobra.Command) (systemclientset.SupportBundleInterface, error) { + getSupportBundleClient = func() (systemclientset.SupportBundleInterface, error) { return createFakeSupportBundleClient(), nil } defer func() { diff --git a/pkg/antctl/runtime/runtime.go b/pkg/antctl/runtime/runtime.go index 93b181bf0f8..9edcad98c27 100644 --- a/pkg/antctl/runtime/runtime.go +++ b/pkg/antctl/runtime/runtime.go @@ -15,6 +15,7 @@ package runtime import ( + "fmt" "os" "strings" @@ -47,10 +48,24 @@ func ResolveKubeconfig(path string) (*rest.Config, error) { path = clientcmd.RecommendedHomeFile } } - if _, err = os.Stat(path); path == clientcmd.RecommendedHomeFile && os.IsNotExist(err) { - return rest.InClusterConfig() + if _, err := os.Stat(path); os.IsNotExist(err) { + if path == clientcmd.RecommendedHomeFile { + config, inClusterErr := rest.InClusterConfig() + if inClusterErr == nil { + return config, nil + } + return nil, fmt.Errorf( + "failed to resolve kubeconfig: neither a valid kubeconfig file was found at '%s', nor could InClusterConfig be used: %w", + path, inClusterErr, + ) + } + return nil, fmt.Errorf("failed to resolve kubeconfig: kubeconfig file does not exist at path '%s'", path) + } + config, err := clientcmd.BuildConfigFromFlags("", path) + if err != nil { + return nil, fmt.Errorf("failed to build kubeconfig from file at path '%s': %w", path, err) } - return clientcmd.BuildConfigFromFlags("", path) + return config, nil } func init() {