Skip to content

Commit

Permalink
fix: take KUBECONFIG env variable into consideration (#340)
Browse files Browse the repository at this point in the history
Before, the default value set for the `--kubeconfig` flag prevented the
`KUBECONFIG` env variable to be ever taken into consideration. This
behavior has now been fixed.

If `--kubeconfig` flag is set, it takes precedence over the `KUBECONFIG` env
variable.

fixes #331

Signed-off-by: Patrick Pichler <git@patrickpichler.dev>
Co-authored-by: Patrick Pichler <git@patrickpichler.dev>
  • Loading branch information
patrickpichler and Patrick Pichler committed Apr 26, 2023
1 parent f8fa35c commit ee85d13
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
12 changes: 4 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ import (
"fmt"
"os"
"path/filepath"

"github.com/adrg/xdg"
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/cmd/serve"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
"github.com/k8sgpt-ai/k8sgpt/cmd/analyze"
"github.com/k8sgpt-ai/k8sgpt/cmd/auth"
"github.com/k8sgpt-ai/k8sgpt/cmd/filters"
"github.com/k8sgpt-ai/k8sgpt/cmd/generate"
"github.com/k8sgpt-ai/k8sgpt/cmd/integration"
"github.com/k8sgpt-ai/k8sgpt/cmd/serve"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/client-go/util/homedir"
)

var (
Expand Down Expand Up @@ -63,10 +63,6 @@ func init() {

cobra.OnInitialize(initConfig)

var kubeconfigPath string
if home := homedir.HomeDir(); home != "" {
kubeconfigPath = filepath.Join(home, ".kube", "config")
}
rootCmd.AddCommand(auth.AuthCmd)
rootCmd.AddCommand(analyze.AnalyzeCmd)
rootCmd.AddCommand(filters.FiltersCmd)
Expand All @@ -75,7 +71,7 @@ func init() {
rootCmd.AddCommand(serve.ServeCmd)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.k8sgpt.yaml)")
rootCmd.PersistentFlags().StringVar(&kubecontext, "kubecontext", "", "Kubernetes context to use. Only required if out-of-cluster.")
rootCmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", kubeconfigPath, "Path to a kubeconfig. Only required if out-of-cluster.")
rootCmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
}

// initConfig reads in config file and ENV variables if set.
Expand Down
8 changes: 7 additions & 1 deletion pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ 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
}

clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
loadingRules,
&clientcmd.ConfigOverrides{
CurrentContext: kubecontext,
})
Expand Down

0 comments on commit ee85d13

Please sign in to comment.