Skip to content

Commit

Permalink
Improve kubeconfig resolution with detailed error
Browse files Browse the repository at this point in the history
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 Nov 29, 2024
1 parent 18e9111 commit fca53f1
Showing 1 changed file with 18 additions and 3 deletions.
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 fca53f1

Please sign in to comment.