Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new kubeconfig for SupportBundleClient #6840

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{}
antoninbas marked this conversation as resolved.
Show resolved Hide resolved
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
36 changes: 28 additions & 8 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 All @@ -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() {
Expand Down
Loading