Skip to content

Commit

Permalink
Allow KUBECONFIG path to be set via flag
Browse files Browse the repository at this point in the history
  • Loading branch information
laurybueno committed Aug 9, 2020
1 parent 0b98c12 commit 1e29c5c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand All @@ -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`
Expand Down
41 changes: 19 additions & 22 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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)
}

0 comments on commit 1e29c5c

Please sign in to comment.