Skip to content

Commit

Permalink
fix: use kubeconfig file when user specify it (#605)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
jianzhangbjz committed Aug 19, 2023
1 parent 1a0ae1a commit e3b21ec
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()

if kubeconfig != "" {
loadingRules.ExplicitPath = kubeconfig
}
var err error

if kubeconfig != "" {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
loadingRules.ExplicitPath = kubeconfig
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loadingRules,
&clientcmd.ConfigOverrides{
Expand All @@ -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
Expand Down

0 comments on commit e3b21ec

Please sign in to comment.