Skip to content

Commit

Permalink
adding support for --context flag, updating readme, bumping version t…
Browse files Browse the repository at this point in the history
…o 0.2.0
  • Loading branch information
robscott committed Feb 23, 2019
1 parent 3b15149 commit 9cfd78a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,28 @@ example-node-2 tiller tiller-deploy 140m (14%) 180m (18%)

It's worth noting that utilization numbers from pods will likely not add up to the total node utilization numbers. Unlike request and limit numbers where node and cluster level numbers represent a sum of pod values, node metrics come directly from metrics-server and will likely include other forms of resource utilization.

### Filtering By Labels
For more advanced usage, kube-capacity also supports filtering by pod, namespace, and/or node labels. The following examples show how to use these filters:

```
kube-capacity --pod-labels app=nginx
kube-capacity --namespace-labels team=api
kube-capacity --node-labels kubernetes.io/role=node
```

## Prerequisites
Any commands requesting cluster utilization are dependent on [metrics-server](https://github.com/kubernetes-incubator/metrics-server) running on your cluster. If it's not already installed, you can install it with the official [helm chart](https://github.com/helm/charts/tree/master/stable/metrics-server).

## Kubernetes Configuration
If a `KUBECONFIG` environment variable is specified, kube-capacity will attempt to use the config at that path, otherwise it will default to `~/.kube/config`.
## Flags Supported
```
--context string context to use for Kubernetes config
-h, --help help for kube-capacity
-n, --namespace-labels string labels to filter namespaces with
--node-labels string labels to filter nodes with
-l, --pod-labels string labels to filter pods with
-p, --pods includes pods in output
-u, --util includes resource utilization in output
```

## Similar Projects
There are already some great projects out there that have similar goals.
Expand Down
10 changes: 5 additions & 5 deletions pkg/capacity/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
)

// List gathers cluster resource data and outputs it
func List(showPods, showUtil bool, podLabels, nodeLabels, namespaceLabels string) {
clientset, err := kube.NewClientSet()
func List(showPods, showUtil bool, podLabels, nodeLabels, namespaceLabels, kubeContext string) {
clientset, err := kube.NewClientSet(kubeContext)
if err != nil {
fmt.Printf("Error connecting to Kubernetes: %v\n", err)
os.Exit(1)
Expand All @@ -38,7 +38,7 @@ func List(showPods, showUtil bool, podLabels, nodeLabels, namespaceLabels string
podList, nodeList := getPodsAndNodes(clientset, podLabels, nodeLabels, namespaceLabels)
pmList := &v1beta1.PodMetricsList{}
if showUtil {
mClientset, err := kube.NewMetricsClientSet()
mClientset, err := kube.NewMetricsClientSet(kubeContext)
if err != nil {
fmt.Printf("Error connecting to Metrics API: %v\n", err)
os.Exit(4)
Expand Down Expand Up @@ -75,7 +75,7 @@ func getPodsAndNodes(clientset kubernetes.Interface, podLabels, nodeLabels, name
}

for _, pod := range podList.Items {
if ! nodes[pod.Spec.NodeName] {
if !nodes[pod.Spec.NodeName] {
continue
}

Expand All @@ -101,7 +101,7 @@ func getPodsAndNodes(clientset kubernetes.Interface, podLabels, nodeLabels, name
newPodItems := []corev1.Pod{}

for _, pod := range podList.Items {
if ! namespaces[pod.GetNamespace()] {
if !namespaces[pod.GetNamespace()] {
continue
}

Expand Down
18 changes: 10 additions & 8 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,28 @@ var showUtil bool
var podLabels string
var nodeLabels string
var namespaceLabels string
var kubeContext string

var rootCmd = &cobra.Command{
Use: "kube-capacity",
Short: "kube-capacity provides an overview of the resource requests, limits, and utilization in a Kubernetes cluster",
Long: "kube-capacity provides an overview of the resource requests, limits, and utilization in a Kubernetes cluster",
Short: "kube-capacity provides an overview of the resource requests, limits, and utilization in a Kubernetes cluster.",
Long: "kube-capacity provides an overview of the resource requests, limits, and utilization in a Kubernetes cluster.",
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.ParseFlags(args); err != nil {
fmt.Printf("Error parsing flags: %v", err)
}

capacity.List(showPods, showUtil, podLabels, nodeLabels, namespaceLabels)
capacity.List(showPods, showUtil, podLabels, nodeLabels, namespaceLabels, kubeContext)
},
}

func init() {
rootCmd.PersistentFlags().BoolVarP(&showPods, "pods", "p", false, "Set this flag to include pods in output")
rootCmd.PersistentFlags().BoolVarP(&showUtil, "util", "u", false, "Set this flag to include resource utilization in output")
rootCmd.PersistentFlags().StringVarP(&podLabels, "pod-labels", "l", "", "Labels to filter pods with.")
rootCmd.PersistentFlags().StringVarP(&nodeLabels, "node-labels", "", "", "Labels to filter nodes with.")
rootCmd.PersistentFlags().StringVarP(&namespaceLabels, "namespace-labels", "n", "", "Labels to filter namespaces with.")
rootCmd.PersistentFlags().BoolVarP(&showPods, "pods", "p", false, "includes pods in output")
rootCmd.PersistentFlags().BoolVarP(&showUtil, "util", "u", false, "includes resource utilization in output")
rootCmd.PersistentFlags().StringVarP(&podLabels, "pod-labels", "l", "", "labels to filter pods with")
rootCmd.PersistentFlags().StringVarP(&nodeLabels, "node-labels", "", "", "labels to filter nodes with")
rootCmd.PersistentFlags().StringVarP(&namespaceLabels, "namespace-labels", "n", "", "labels to filter namespaces with")
rootCmd.PersistentFlags().StringVarP(&kubeContext, "context", "", "", "context to use for Kubernetes config")
}

// Execute is the primary entrypoint for this CLI
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of kube-capacity",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("kube-capacity version 0.1.3")
fmt.Println("kube-capacity version 0.2.0")
},
}
12 changes: 6 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() (*kubernetes.Clientset, error) {
config, err := getKubeConfig()
func NewClientSet(kubeContext string) (*kubernetes.Clientset, error) {
config, err := getKubeConfig(kubeContext)
if err != nil {
return nil, err
}
Expand All @@ -35,18 +35,18 @@ func NewClientSet() (*kubernetes.Clientset, error) {
}

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

return metrics.NewForConfig(config)
}

func getKubeConfig() (*rest.Config, error) {
func getKubeConfig(kubeContext string) (*rest.Config, error) {
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{},
&clientcmd.ConfigOverrides{CurrentContext: kubeContext},
).ClientConfig()
}

0 comments on commit 9cfd78a

Please sign in to comment.