diff --git a/README.md b/README.md index 45af2a7..4bbedbe 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ go get github.com/laurybueno/kubectl-hoggers ``` ## Usage -This plugin uses the `KUBECONFIG` environment variable to access cluster data. It must be set for everything to work. +This plugin checks the `KUBECONFIG` environment variable to find the cluster config file. You can also pass its path with the `--kubeconfig` flag. Check CPU and RAM reservations/limits for each node in a cluster ``` @@ -34,7 +34,7 @@ kubectl hoggers top Note: because of the way `kubectl` plugins work, running `kubectl hoggers` or `kubectl-hoggers` gives the same results. ## Roadmap -- [ ] allow `KUBECONFIG` to be set via command flag +- [x] allow `KUBECONFIG` to be set via command flag - [ ] output `report` to a file - [ ] add a namespace option to `report` - [ ] add table scroll in `report` diff --git a/cmd/root.go b/cmd/root.go index b288a98..5d2d8b9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -49,9 +49,8 @@ func Execute() { } } -func getClientSet() *kubernetes.Clientset { - // use the current context in kubeconfig - config, err := clientcmd.BuildConfigFromFlags("", viper.GetString("KUBECONFIG")) +func getClientSet(kubeconfig string) *kubernetes.Clientset { + config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) if err != nil { panic(err.Error()) } @@ -65,9 +64,8 @@ func getClientSet() *kubernetes.Clientset { return clientset } -func getMetricsClientset() *metricsv.Clientset { - // use the current context in kubeconfig - config, err := clientcmd.BuildConfigFromFlags("", viper.GetString("KUBECONFIG")) +func getMetricsClientset(kubeconfig string) *metricsv.Clientset { + config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) if err != nil { panic(err.Error()) } @@ -80,27 +78,26 @@ func getMetricsClientset() *metricsv.Clientset { return metricsClientset } -func init() { - cobra.OnInitialize(initConfig) - - // Here you will define your flags and configuration settings. - // Cobra supports persistent flags, which, if defined here, - // will be global for your application. +var kbFile string - // rootCmd.PersistentFlags().StringVar(&kbFile, "kubeconfig", "", "path to kubeconfig file (default is environment variable $KUBECONFIG or $HOME/kube/.config.yaml)") +func init() { + // Try to get a kubeconfig flag, if available + rootCmd.PersistentFlags().StringVar(&kbFile, "kubeconfig", "", "path to kubeconfig file (default is environment variable $KUBECONFIG") - // Cobra also supports local flags, which will only run - // when this action is called directly. - // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + cobra.OnInitialize(initConfig) } -// initConfig reads in ENV variables if set. func initConfig() { - viper.AutomaticEnv() // read in environment variables - if viper.GetString("KUBECONFIG") == "" { - panic("$KUBECONFIG environment variable is not set. Aborting") + // If kbFile is already set, then it has been passed as a command line flag + // If not, try the get its value from environment + if kbFile == "" { + viper.AutomaticEnv() // read in environment variables + if viper.GetString("KUBECONFIG") == "" { + panic("$KUBECONFIG environment variable is not set. Aborting") + } + kbFile = viper.GetString("KUBECONFIG") } - clientset = getClientSet() - metricsClientset = getMetricsClientset() + clientset = getClientSet(kbFile) + metricsClientset = getMetricsClientset(kbFile) }