From e11daf11e249d89ae201e8b141c6abb064eb1e2b Mon Sep 17 00:00:00 2001 From: Shuyang Xin Date: Fri, 29 Nov 2024 15:25:42 +0800 Subject: [PATCH] Create new kubeconfig for SupportBundleClient Fixes: #6687 * Create a new kubeconfig for local support bundle requests to avoid errors caused by the absence of this file. * Refactored ResolveKubeconfig, detailed error messages are returned, differentiating between kubeconfig file errors and InClusterConfig errors. Signed-off-by: Shuyang Xin --- pkg/antctl/raw/supportbundle/command.go | 11 +++--- pkg/antctl/raw/supportbundle/command_test.go | 2 +- pkg/antctl/runtime/runtime.go | 36 +++++++++++++++----- 3 files changed, 33 insertions(+), 16 deletions(-) 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..5b637341a20 100644 --- a/pkg/antctl/runtime/runtime.go +++ b/pkg/antctl/runtime/runtime.go @@ -15,6 +15,7 @@ package runtime import ( + "fmt" "os" "strings" @@ -39,18 +40,37 @@ var ( ) func ResolveKubeconfig(path string) (*rest.Config, error) { - var err error - if len(path) == 0 { - var hasIt bool - path, hasIt = os.LookupEnv("KUBECONFIG") - if !hasIt || len(strings.TrimSpace(path)) == 0 { + withExplicitPath := path != "" + if !withExplicitPath { + path = strings.TrimSpace(os.Getenv("KUBECONFIG")) + if path == "" { path = clientcmd.RecommendedHomeFile } } - if _, err = os.Stat(path); path == clientcmd.RecommendedHomeFile && os.IsNotExist(err) { - return rest.InClusterConfig() + if _, err := os.Stat(path); err != nil { + if !os.IsNotExist(err) { + return nil, err + } + if withExplicitPath { + return nil, fmt.Errorf("failed to resolve kubeconfig: kubeconfig file does not exist at path '%s'", path) + } + 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, + ) + } + + config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( + &clientcmd.ClientConfigLoadingRules{ExplicitPath: path}, + &clientcmd.ConfigOverrides{}).ClientConfig() + 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() {