Skip to content

Commit

Permalink
Make sure kanctl honors KUBECONFIG env and kubeconfig dir (#1247)
Browse files Browse the repository at this point in the history
* Make sure `kanctl` honors `KUBECONFIG` env and kubeconfig dir

kanctl in current implentation create kubeclient using `inClusterConfig`
it doesn't check if the `KUBECONFIG` env var is set or config file is
available at default location.
This has resulted into problem for some of the users. This PR makes the
change to prioritise creating kubeclient in below order

- KUBECONFIG env var
- kubeconfig file at deafult location `~/.kube/config`
- Use `inClusterConfig`

* Address review comments

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
viveksinghggits and mergify[bot] committed Feb 22, 2022
1 parent aad4b34 commit 22424ea
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package kube

import (
"os"

"github.com/pkg/errors"
crdclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/client-go/dynamic"
Expand Down Expand Up @@ -46,9 +48,19 @@ func ConfigNamespace() (string, error) {

// LoadConfig returns a kubernetes client config based on global settings.
func LoadConfig() (*rest.Config, error) {
kubeConfigEnv := os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
if len(kubeConfigEnv) != 0 {
return clientcmd.BuildConfigFromFlags("", kubeConfigEnv)
}

if c, err := clientcmd.BuildConfigFromFlags("", clientcmd.RecommendedHomeFile); err == nil {
return c, nil
}

if c, err := rest.InClusterConfig(); err == nil {
return c, nil
}

return newClientConfig().ClientConfig()
}

Expand Down

0 comments on commit 22424ea

Please sign in to comment.