Skip to content

Commit

Permalink
potential fixes for #15 and #16
Browse files Browse the repository at this point in the history
  • Loading branch information
robscott committed Jun 26, 2019
1 parent 18d479f commit ca03747
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 29 deletions.
20 changes: 17 additions & 3 deletions pkg/capacity/capacity.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,20 @@ func FetchAndPrint(showContainers, showPods, showUtil bool, podLabels, nodeLabel

podList, nodeList := getPodsAndNodes(clientset, podLabels, nodeLabels, namespaceLabels)
pmList := &v1beta1.PodMetricsList{}
nmList := &v1beta1.NodeMetricsList{}

if showUtil {
mClientset, err := kube.NewMetricsClientSet(kubeContext)
if err != nil {
fmt.Printf("Error connecting to Metrics API: %v\n", err)
os.Exit(4)
}

pmList = getMetrics(mClientset)
pmList = getPodMetrics(mClientset)
nmList = getNodeMetrics(mClientset)
}

cm := buildClusterMetric(podList, pmList, nodeList)
cm := buildClusterMetric(podList, pmList, nodeList, nmList)
printList(&cm, showContainers, showPods, showUtil, output, sortBy)
}

Expand Down Expand Up @@ -115,7 +118,7 @@ func getPodsAndNodes(clientset kubernetes.Interface, podLabels, nodeLabels, name
return podList, nodeList
}

func getMetrics(mClientset *metrics.Clientset) *v1beta1.PodMetricsList {
func getPodMetrics(mClientset *metrics.Clientset) *v1beta1.PodMetricsList {
pmList, err := mClientset.MetricsV1beta1().PodMetricses("").List(metav1.ListOptions{})
if err != nil {
fmt.Printf("Error getting Pod Metrics: %v\n", err)
Expand All @@ -125,3 +128,14 @@ func getMetrics(mClientset *metrics.Clientset) *v1beta1.PodMetricsList {

return pmList
}

func getNodeMetrics(mClientset *metrics.Clientset) *v1beta1.NodeMetricsList {
nmList, err := mClientset.MetricsV1beta1().NodeMetricses().List(metav1.ListOptions{})
if err != nil {
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(7)
}

return nmList
}
12 changes: 12 additions & 0 deletions pkg/capacity/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,18 @@ func getTestClusterMetric() clusterMetric {
},
},
},
}, &v1beta1.NodeMetricsList{
Items: []v1beta1.NodeMetrics{
{
ObjectMeta: metav1.ObjectMeta{
Name: "example-node-1",
},
Usage: corev1.ResourceList{
"cpu": resource.MustParse("63m"),
"memory": resource.MustParse("439Mi"),
},
},
},
},
)
}
40 changes: 19 additions & 21 deletions pkg/capacity/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ type containerMetric struct {
memory *resourceMetric
}

func buildClusterMetric(podList *corev1.PodList, pmList *v1beta1.PodMetricsList, nodeList *corev1.NodeList) clusterMetric {
func buildClusterMetric(podList *corev1.PodList, pmList *v1beta1.PodMetricsList,
nodeList *corev1.NodeList, nmList *v1beta1.NodeMetricsList) clusterMetric {
cm := clusterMetric{
cpu: &resourceMetric{resourceType: "cpu"},
memory: &resourceMetric{resourceType: "memory"},
Expand All @@ -90,9 +91,11 @@ func buildClusterMetric(podList *corev1.PodList, pmList *v1beta1.PodMetricsList,
},
podMetrics: map[string]*podMetric{},
}
}

cm.cpu.allocatable.Add(node.Status.Allocatable["cpu"])
cm.memory.allocatable.Add(node.Status.Allocatable["memory"])
for _, nm := range nmList.Items {
cm.nodeMetrics[nm.Name].cpu.utilization = nm.Usage["cpu"]
cm.nodeMetrics[nm.Name].memory.utilization = nm.Usage["memory"]
}

podMetrics := map[string]v1beta1.PodMetrics{}
Expand All @@ -106,6 +109,13 @@ func buildClusterMetric(podList *corev1.PodList, pmList *v1beta1.PodMetricsList,
}
}

for _, node := range nodeList.Items {
nm := cm.nodeMetrics[node.Name]
if nm != nil {
cm.addNodeMetric(cm.nodeMetrics[node.Name])
}
}

return cm
}

Expand Down Expand Up @@ -156,11 +166,6 @@ func (cm *clusterMetric) addPodMetric(pod *corev1.Pod, podMetrics v1beta1.PodMet
}

if nm != nil {
cm.cpu.request.Add(req["cpu"])
cm.cpu.limit.Add(limit["cpu"])
cm.memory.request.Add(req["memory"])
cm.memory.limit.Add(limit["memory"])

nm.podMetrics[key] = pm
nm.podMetrics[key].cpu.allocatable = nm.cpu.allocatable
nm.podMetrics[key].memory.allocatable = nm.memory.allocatable
Expand All @@ -172,20 +177,13 @@ func (cm *clusterMetric) addPodMetric(pod *corev1.Pod, podMetrics v1beta1.PodMet
}

for _, container := range podMetrics.Containers {
pm.containerMetrics[container.Name].cpu.utilization = container.Usage["cpu"]
pm.cpu.utilization.Add(container.Usage["cpu"])
pm.containerMetrics[container.Name].memory.utilization = container.Usage["memory"]
pm.memory.utilization.Add(container.Usage["memory"])

if nm == nil {
continue
cm := pm.containerMetrics[container.Name]
if cm != nil {
pm.containerMetrics[container.Name].cpu.utilization = container.Usage["cpu"]
pm.cpu.utilization.Add(container.Usage["cpu"])
pm.containerMetrics[container.Name].memory.utilization = container.Usage["memory"]
pm.memory.utilization.Add(container.Usage["memory"])
}

nm.cpu.utilization.Add(container.Usage["cpu"])
nm.memory.utilization.Add(container.Usage["memory"])

cm.cpu.utilization.Add(container.Usage["cpu"])
cm.memory.utilization.Add(container.Usage["memory"])
}
}

Expand Down
18 changes: 15 additions & 3 deletions pkg/capacity/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

func TestBuildClusterMetricEmpty(t *testing.T) {
cm := buildClusterMetric(
&corev1.PodList{}, &v1beta1.PodMetricsList{}, &corev1.NodeList{},
&corev1.PodList{}, &v1beta1.PodMetricsList{}, &corev1.NodeList{}, &v1beta1.NodeMetricsList{},
)

expected := clusterMetric{
Expand Down Expand Up @@ -129,21 +129,33 @@ func TestBuildClusterMetricFull(t *testing.T) {
},
},
},
}, &v1beta1.NodeMetricsList{
Items: []v1beta1.NodeMetrics{
{
ObjectMeta: metav1.ObjectMeta{
Name: "example-node-1",
},
Usage: corev1.ResourceList{
"cpu": resource.MustParse("43m"),
"memory": resource.MustParse("349Mi"),
},
},
},
},
)

cpuExpected := &resourceMetric{
allocatable: resource.MustParse("1000m"),
request: resource.MustParse("350m"),
limit: resource.MustParse("400m"),
utilization: resource.MustParse("23m"),
utilization: resource.MustParse("43m"),
}

memoryExpected := &resourceMetric{
allocatable: resource.MustParse("4000Mi"),
request: resource.MustParse("400Mi"),
limit: resource.MustParse("700Mi"),
utilization: resource.MustParse("299Mi"),
utilization: resource.MustParse("349Mi"),
}

assert.NotNil(t, cm.cpu)
Expand Down
5 changes: 4 additions & 1 deletion pkg/capacity/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ func (tp *tablePrinter) Print() {

if len(sortedNodeMetrics) > 1 {
tp.printClusterLine()
tp.printLine(&tableLine{})
}

for _, nm := range sortedNodeMetrics {
if tp.showPods || tp.showContainers {
tp.printLine(&tableLine{})
}

tp.printNodeLine(nm.name, nm)

if tp.showPods || tp.showContainers {
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.3.1")
fmt.Println("kube-capacity version 0.3.2")
},
}

0 comments on commit ca03747

Please sign in to comment.