Skip to content

Commit

Permalink
fix: Crash on pod labels
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreZiviani committed Jun 27, 2023
1 parent 1b90c8b commit 823c35b
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion exporter/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package exporter

import (
"context"
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -75,7 +76,12 @@ func (m *Metrics) Collect(ch chan<- prometheus.Metric) {
}

for _, pod := range m.Pods {
podLabelValues := []string{pod.Name, pod.Namespace, pod.Node.Name, pod.Node.Instance.Type, pod.Node.Cost.Type}
podLabelValues, err := getPodLabels(pod)
if err != nil {
log.Errorf("%w", err)
continue
}

for _, l := range m.addPodLabels {
podLabelValues = append(podLabelValues, pod.Labels[l])
}
Expand Down Expand Up @@ -193,3 +199,14 @@ func timeTrack(start time.Time, name string) {
elapsed := time.Since(start)
log.Infof("%s took %s", name, elapsed)
}

func getPodLabels(pod *Pod) ([]string, error) {
if pod.Node == nil {
return []string{}, fmt.Errorf("pod %s/%s does not have a node associated!", pod.Namespace, pod.Name)
}
if pod.Node.Instance == nil {
return []string{}, fmt.Errorf("node %s does not have an instance associated!", pod.Node.Name)
}

return []string{pod.Name, pod.Namespace, pod.Node.Name, pod.Node.Instance.Type, pod.Node.Cost.Type}, nil
}

0 comments on commit 823c35b

Please sign in to comment.