mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-07-02 02:02:08 +00:00
Merge pull request #155 from k8sgpt-ai/fix/kubectx
fix: now supports different kubeconfig and kubectx
This commit is contained in:
commit
2acab541bc
24
cmd/root.go
24
cmd/root.go
@ -2,8 +2,10 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/k8sgpt-ai/k8sgpt/cmd/generate"
|
"github.com/k8sgpt-ai/k8sgpt/cmd/generate"
|
||||||
|
"k8s.io/client-go/util/homedir"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/k8sgpt-ai/k8sgpt/cmd/analyze"
|
"github.com/k8sgpt-ai/k8sgpt/cmd/analyze"
|
||||||
@ -14,10 +16,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cfgFile string
|
cfgFile string
|
||||||
masterURL string
|
kubecontext string
|
||||||
kubeconfig string
|
kubeconfig string
|
||||||
version string
|
version string
|
||||||
)
|
)
|
||||||
|
|
||||||
// rootCmd represents the base command when called without any subcommands
|
// rootCmd represents the base command when called without any subcommands
|
||||||
@ -43,23 +45,25 @@ func Execute(v string) {
|
|||||||
func init() {
|
func init() {
|
||||||
cobra.OnInitialize(initConfig)
|
cobra.OnInitialize(initConfig)
|
||||||
|
|
||||||
// Here you will define your flags and configuration settings.
|
var kubeconfigPath string
|
||||||
// Cobra supports persistent flags, which, if defined here,
|
if home := homedir.HomeDir(); home != "" {
|
||||||
// will be global for your application.
|
kubeconfigPath = filepath.Join(home, ".kube", "config")
|
||||||
|
}
|
||||||
rootCmd.AddCommand(auth.AuthCmd)
|
rootCmd.AddCommand(auth.AuthCmd)
|
||||||
rootCmd.AddCommand(analyze.AnalyzeCmd)
|
rootCmd.AddCommand(analyze.AnalyzeCmd)
|
||||||
rootCmd.AddCommand(generate.GenerateCmd)
|
rootCmd.AddCommand(generate.GenerateCmd)
|
||||||
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(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. 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", "", "Path to a kubeconfig. Only required if out-of-cluster.")
|
rootCmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", kubeconfigPath, "Path to a kubeconfig. Only required if out-of-cluster.")
|
||||||
// Cobra also supports local flags, which will only run
|
// Cobra also supports local flags, which will only run
|
||||||
// when this action is called directly.
|
// when this action is called directly.
|
||||||
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
|
|
||||||
//Initialise the kubeconfig
|
//Initialise the kubeconfig
|
||||||
kubernetesClient, err := kubernetes.NewClient(masterURL, kubeconfig)
|
kubernetesClient, err := kubernetes.NewClient(kubecontext, kubeconfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
color.Red("Error initialising kubernetes client: %v", err)
|
color.Red("Error initialising kubernetes client: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
viper.Set("kubernetesClient", kubernetesClient)
|
viper.Set("kubernetesClient", kubernetesClient)
|
||||||
|
@ -2,7 +2,6 @@ package kubernetes
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
"k8s.io/client-go/rest"
|
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -14,21 +13,23 @@ func (c *Client) GetClient() *kubernetes.Clientset {
|
|||||||
return c.client
|
return c.client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(masterURL string, kubeconfig string) (*Client, error) {
|
func NewClient(kubecontext string, kubeconfig string) (*Client, error) {
|
||||||
|
|
||||||
config, err := rest.InClusterConfig()
|
config := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
|
||||||
if err != nil {
|
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
|
||||||
kubeconfig :=
|
&clientcmd.ConfigOverrides{
|
||||||
clientcmd.NewDefaultClientConfigLoadingRules().GetDefaultFilename()
|
CurrentContext: kubecontext,
|
||||||
config, err = clientcmd.BuildConfigFromFlags(masterURL, kubeconfig)
|
})
|
||||||
if err != nil {
|
// create the clientset
|
||||||
return nil, err
|
c, err := config.ClientConfig()
|
||||||
}
|
|
||||||
}
|
|
||||||
clientSet, err := kubernetes.NewForConfig(config)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
clientSet, err := kubernetes.NewForConfig(c)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
client: clientSet,
|
client: clientSet,
|
||||||
}, nil
|
}, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user