mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-08-11 12:41:39 +00:00
fix: take KUBECONFIG
env variable into consideration (#340)
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>
This commit is contained in:
parent
f8fa35cf9d
commit
ee85d13d59
12
cmd/root.go
12
cmd/root.go
@ -17,18 +17,18 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/adrg/xdg"
|
"github.com/adrg/xdg"
|
||||||
"github.com/fatih/color"
|
"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/analyze"
|
||||||
"github.com/k8sgpt-ai/k8sgpt/cmd/auth"
|
"github.com/k8sgpt-ai/k8sgpt/cmd/auth"
|
||||||
"github.com/k8sgpt-ai/k8sgpt/cmd/filters"
|
"github.com/k8sgpt-ai/k8sgpt/cmd/filters"
|
||||||
"github.com/k8sgpt-ai/k8sgpt/cmd/generate"
|
"github.com/k8sgpt-ai/k8sgpt/cmd/generate"
|
||||||
"github.com/k8sgpt-ai/k8sgpt/cmd/integration"
|
"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/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"k8s.io/client-go/util/homedir"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -63,10 +63,6 @@ func init() {
|
|||||||
|
|
||||||
cobra.OnInitialize(initConfig)
|
cobra.OnInitialize(initConfig)
|
||||||
|
|
||||||
var kubeconfigPath string
|
|
||||||
if home := homedir.HomeDir(); home != "" {
|
|
||||||
kubeconfigPath = filepath.Join(home, ".kube", "config")
|
|
||||||
}
|
|
||||||
rootCmd.AddCommand(auth.AuthCmd)
|
rootCmd.AddCommand(auth.AuthCmd)
|
||||||
rootCmd.AddCommand(analyze.AnalyzeCmd)
|
rootCmd.AddCommand(analyze.AnalyzeCmd)
|
||||||
rootCmd.AddCommand(filters.FiltersCmd)
|
rootCmd.AddCommand(filters.FiltersCmd)
|
||||||
@ -75,7 +71,7 @@ func init() {
|
|||||||
rootCmd.AddCommand(serve.ServeCmd)
|
rootCmd.AddCommand(serve.ServeCmd)
|
||||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.k8sgpt.yaml)")
|
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(&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.
|
// initConfig reads in config file and ENV variables if set.
|
||||||
|
@ -44,8 +44,14 @@ func NewClient(kubecontext string, kubeconfig string) (*Client, error) {
|
|||||||
var config *rest.Config
|
var config *rest.Config
|
||||||
config, err := rest.InClusterConfig()
|
config, err := rest.InClusterConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
|
||||||
|
|
||||||
|
if kubeconfig != "" {
|
||||||
|
loadingRules.ExplicitPath = kubeconfig
|
||||||
|
}
|
||||||
|
|
||||||
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
|
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
|
||||||
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
|
loadingRules,
|
||||||
&clientcmd.ConfigOverrides{
|
&clientcmd.ConfigOverrides{
|
||||||
CurrentContext: kubecontext,
|
CurrentContext: kubecontext,
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user