Skip to content

Commit

Permalink
Create new kubeconfig for SupportBundleClient
Browse files Browse the repository at this point in the history
Fixes: #6687

* Create a new kubeconfig for local support bundle requests to avoid errors caused
by the absence of this file.
* For ResolveKubeconfig, detailed error messages are returned, differentiating
between kubeconfig file errors and InClusterConfig errors.

Signed-off-by: Shuyang Xin <[email protected]>
  • Loading branch information
XinShuYang committed Dec 10, 2024
1 parent 18e9111 commit 480ed83
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
11 changes: 4 additions & 7 deletions pkg/antctl/raw/supportbundle/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,18 @@ 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
}

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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/antctl/raw/supportbundle/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
21 changes: 18 additions & 3 deletions pkg/antctl/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package runtime

import (
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 480ed83

Please sign in to comment.