Skip to content

Commit

Permalink
allow specifying kubeconfig file to use via cmd line arg
Browse files Browse the repository at this point in the history
  • Loading branch information
clive-jevons authored and robscott committed Apr 26, 2021
1 parent 9a0ca53 commit 706067b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
6 changes: 3 additions & 3 deletions pkg/capacity/capacity.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (
)

// FetchAndPrint gathers cluster resource data and outputs it
func FetchAndPrint(showContainers, showPods, showUtil, availableFormat bool, podLabels, nodeLabels, namespaceLabels, namespace, kubeContext, output, sortBy string) {
clientset, err := kube.NewClientSet(kubeContext)
func FetchAndPrint(showContainers, showPods, showUtil, availableFormat bool, podLabels, nodeLabels, namespaceLabels, namespace, kubeContext, kubeConfig, output, sortBy string) {
clientset, err := kube.NewClientSet(kubeContext, kubeConfig)
if err != nil {
fmt.Printf("Error connecting to Kubernetes: %v\n", err)
os.Exit(1)
Expand All @@ -41,7 +41,7 @@ func FetchAndPrint(showContainers, showPods, showUtil, availableFormat bool, pod
nmList := &v1beta1.NodeMetricsList{}

if showUtil {
mClientset, err := kube.NewMetricsClientSet(kubeContext)
mClientset, err := kube.NewMetricsClientSet(kubeContext, kubeConfig)
if err != nil {
fmt.Printf("Error connecting to Metrics API: %v\n", err)
os.Exit(4)
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var nodeLabels string
var namespaceLabels string
var namespace string
var kubeContext string
var kubeConfig string
var outputFormat string
var sortBy string
var availableFormat bool
Expand All @@ -48,7 +49,7 @@ var rootCmd = &cobra.Command{
os.Exit(1)
}

capacity.FetchAndPrint(showContainers, showPods, showUtil, availableFormat, podLabels, nodeLabels, namespaceLabels, namespace, kubeContext, outputFormat, sortBy)
capacity.FetchAndPrint(showContainers, showPods, showUtil, availableFormat, podLabels, nodeLabels, namespaceLabels, namespace, kubeContext, kubeConfig, outputFormat, sortBy)
},
}

Expand All @@ -71,6 +72,8 @@ func init() {
"namespace", "n", "", "only include pods from this namespace")
rootCmd.PersistentFlags().StringVarP(&kubeContext,
"context", "", "", "context to use for Kubernetes config")
rootCmd.PersistentFlags().StringVarP(&kubeConfig,
"kubeconfig", "", "", "kubeconfig file to use for Kubernetes config")
rootCmd.PersistentFlags().StringVarP(&sortBy,
"sort", "", "name",
fmt.Sprintf("attribute to sort results be (supports: %v)", capacity.SupportedSortAttributes))
Expand Down
16 changes: 10 additions & 6 deletions pkg/kube/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
)

// NewClientSet returns a new Kubernetes clientset
func NewClientSet(kubeContext string) (*kubernetes.Clientset, error) {
config, err := getKubeConfig(kubeContext)
func NewClientSet(kubeContext, kubeConfig string) (*kubernetes.Clientset, error) {
config, err := getKubeConfig(kubeContext, kubeConfig)
if err != nil {
return nil, err
}
Expand All @@ -35,18 +35,22 @@ func NewClientSet(kubeContext string) (*kubernetes.Clientset, error) {
}

// NewMetricsClientSet returns a new clientset for Kubernetes metrics
func NewMetricsClientSet(kubeContext string) (*metrics.Clientset, error) {
config, err := getKubeConfig(kubeContext)
func NewMetricsClientSet(kubeContext, kubeConfig string) (*metrics.Clientset, error) {
config, err := getKubeConfig(kubeContext, kubeConfig)
if err != nil {
return nil, err
}

return metrics.NewForConfig(config)
}

func getKubeConfig(kubeContext string) (*rest.Config, error) {
func getKubeConfig(kubeContext, kubeConfig string) (*rest.Config, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
if kubeConfig != "" {
loadingRules.ExplicitPath = kubeConfig
}
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
loadingRules,
&clientcmd.ConfigOverrides{CurrentContext: kubeContext},
).ClientConfig()
}

0 comments on commit 706067b

Please sign in to comment.