Skip to content

Commit

Permalink
Merge pull request #2 from robscott/rs/metrics-fixes
Browse files Browse the repository at this point in the history
Fixing some of the rough spots around fetching utilization metrics
  • Loading branch information
robscott authored Feb 5, 2019
2 parents aefec2c + 61f8c7c commit 34beab9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ 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.

## 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`.

Expand Down
33 changes: 20 additions & 13 deletions pkg/capacity/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package capacity

import (
"fmt"
"os"

"github.com/robscott/kube-capacity/pkg/kube"
corev1 "k8s.io/api/core/v1"
Expand All @@ -26,28 +27,32 @@ import (
// List gathers cluster resource data and outputs it
func List(args []string, showPods bool, showUtil bool) {
podList, nodeList := getPodsAndNodes()
pmList, nmList := getMetrics()
pmList := &v1beta1.PodMetricsList{}
nmList := &v1beta1.NodeMetricsList{}
if showUtil {
pmList, nmList = getMetrics()
}
cm := buildClusterMetric(podList, pmList, nodeList, nmList)
printList(&cm, showPods, showUtil)
}

func getPodsAndNodes() (*corev1.PodList, *corev1.NodeList) {
clientset, err := kube.NewClientSet()
if err != nil {
fmt.Println("Error connecting to Kubernetes")
panic(err.Error())
fmt.Printf("Error connecting to Kubernetes: %v\n", err)
os.Exit(1)
}

nodeList, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
fmt.Println("Error listing Nodes")
panic(err.Error())
fmt.Printf("Error listing Nodes: %v\n", err)
os.Exit(2)
}

podList, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
if err != nil {
fmt.Println("Error listing Nodes")
panic(err.Error())
fmt.Printf("Error listing Pods: %v\n", err)
os.Exit(3)
}

return podList, nodeList
Expand All @@ -56,20 +61,22 @@ func getPodsAndNodes() (*corev1.PodList, *corev1.NodeList) {
func getMetrics() (*v1beta1.PodMetricsList, *v1beta1.NodeMetricsList) {
mClientset, err := kube.NewMetricsClientSet()
if err != nil {
fmt.Println("Error connecting to Metrics Server")
panic(err.Error())
fmt.Printf("Error connecting to Metrics API: %v\n", err)
os.Exit(4)
}

nmList, err := mClientset.MetricsV1beta1().NodeMetricses().List(metav1.ListOptions{})
if err != nil {
fmt.Println("Error getting metrics")
panic(err.Error())
fmt.Printf("Error getting Node Metrics: %v\n", err)
fmt.Println("For this to work, metrics-server needs to be running in your cluster")
os.Exit(5)
}

pmList, err := mClientset.MetricsV1beta1().PodMetricses("").List(metav1.ListOptions{})
if err != nil {
fmt.Println("Error getting metrics")
panic(err.Error())
fmt.Printf("Error getting Pod Metrics: %v\n", err)
fmt.Println("For this to work, metrics-server needs to be running in your cluster")
os.Exit(6)
}

return pmList, nmList
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.1")
fmt.Println("kube-capacity version 0.1.2")
},
}

0 comments on commit 34beab9

Please sign in to comment.