fix: use kubeconfig file when user specify it (#605)

If user specify `--kubeconfig` when running k8sgpt, it should use the
kubeconfig file to login the corresponding cluster instead of getting auth info via SA.

Closes #604

Signed-off-by: Jian Zhang <jiazha@redhat.com>
This commit is contained in:
Jian Zhang
2023-08-20 03:11:11 +08:00
committed by GitHub
parent 1a0ae1a086
commit e3b21ec5ec

View File

@@ -36,14 +36,11 @@ func (c *Client) GetRestClient() rest.Interface {
func NewClient(kubecontext string, kubeconfig string) (*Client, error) {
var config *rest.Config
config, err := rest.InClusterConfig()
if err != nil {
var err error
if kubeconfig != "" {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
if kubeconfig != "" {
loadingRules.ExplicitPath = kubeconfig
}
loadingRules.ExplicitPath = kubeconfig
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loadingRules,
&clientcmd.ConfigOverrides{
@@ -54,7 +51,13 @@ func NewClient(kubecontext string, kubeconfig string) (*Client, error) {
if err != nil {
return nil, err
}
} else {
config, err = rest.InClusterConfig()
if err != nil {
return nil, err
}
}
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err