Skip to content

Commit

Permalink
Fixes #20
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Schneppenheim committed Jan 2, 2020
1 parent 3d32c0f commit b8f6c11
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
debug

.vscode
.idea
12 changes: 11 additions & 1 deletion collector/node_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type nodeResourcesCollector struct {
// Resource usage
usageCPUCoresDesc *prometheus.Desc
usageMemoryBytesDesc *prometheus.Desc
usagePodCount *prometheus.Desc
}

func init() {
Expand Down Expand Up @@ -92,6 +93,12 @@ func newNodeResourcesCollector(opts *options.Options) (Collector, error) {
labels,
prometheus.Labels{},
),
usagePodCount: prometheus.NewDesc(
prometheus.BuildFQName(opts.Namespace, subsystem, "usage_pod_count"),
"Total number of running pods for each kubernetes node",
labels,
prometheus.Labels{},
),
}, nil
}

Expand Down Expand Up @@ -163,6 +170,7 @@ func (c *nodeResourcesCollector) updateMetrics(ch chan<- prometheus.Metric) erro
ch <- prometheus.MustNewConstMetric(c.requestMemoryBytesDesc, prometheus.GaugeValue, float64(podMetrics.requestedMemoryBytes), n.Name)
ch <- prometheus.MustNewConstMetric(c.limitCPUCoresDesc, prometheus.GaugeValue, podMetrics.limitCPUCores, n.Name)
ch <- prometheus.MustNewConstMetric(c.limitMemoryBytesDesc, prometheus.GaugeValue, float64(podMetrics.limitMemoryBytes), n.Name)
ch <- prometheus.MustNewConstMetric(c.usagePodCount, prometheus.GaugeValue, float64(podMetrics.podCount), n.Name)
}

return nil
Expand Down Expand Up @@ -194,14 +202,16 @@ func getAggregatedPodMetricsByNodeName(pods *corev1.PodList) map[string]aggregat
// Iterate through all pod definitions to sum and group pods' resource requests and limits by node name
for _, podInfo := range pods.Items {
nodeName := podInfo.Spec.NodeName
podCount := podMetrics[nodeName].podCount + 1

// skip not running pods (e. g. failed/succeeded jobs, evicted pods etc.)
podPhase := podInfo.Status.Phase
if podPhase == corev1.PodFailed || podPhase == corev1.PodSucceeded {
continue
}

// Don't increment this counter for failed / non running pods
podCount := podMetrics[nodeName].podCount + 1

for _, c := range podInfo.Spec.Containers {
requestedCPUCores := float64(c.Resources.Requests.Cpu().MilliValue()) / 1000
requestedMemoryBytes := c.Resources.Requests.Memory().MilliValue() / 1000
Expand Down

0 comments on commit b8f6c11

Please sign in to comment.