Skip to content

Commit

Permalink
Evict pods metrics if node is dead (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcgrath207 authored Nov 23, 2024
1 parent 61b8248 commit c19635f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
15 changes: 9 additions & 6 deletions pkg/node/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package node

import (
"fmt"
"math"

"github.com/jmcgrath207/k8s-ephemeral-storage-metrics/pkg/pod"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog/log"
"math"
)

var (
Expand Down Expand Up @@ -93,11 +93,14 @@ func (n *Node) SetMetrics(nodeName string, availableBytes float64, capacityBytes

func (n *Node) evict(node string) {
n.Set.Remove(node)
nodeAvailableGaugeVec.DeletePartialMatch(prometheus.Labels{"node_name": node})
nodeCapacityGaugeVec.DeletePartialMatch(prometheus.Labels{"node_name": node})
nodePercentageGaugeVec.DeletePartialMatch(prometheus.Labels{"node_name": node})
deleteLabel := prometheus.Labels{"node_name": node}

nodeAvailableGaugeVec.DeletePartialMatch(deleteLabel)
nodeCapacityGaugeVec.DeletePartialMatch(deleteLabel)
nodePercentageGaugeVec.DeletePartialMatch(deleteLabel)
if n.AdjustedPollingRate {
AdjustedPollingRateGaugeVec.DeletePartialMatch(prometheus.Labels{"node_name": node})
AdjustedPollingRateGaugeVec.DeletePartialMatch(deleteLabel)
}
pod.EvictPodByNode(&deleteLabel)
log.Info().Msgf("Node %s does not exist or is unresponsive. Removed from monitoring", node)
}
2 changes: 1 addition & 1 deletion pkg/pod/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (cr Collector) podWatch() {
cr.lookupMutex.Lock()
delete(*cr.lookup, p.Name)
cr.lookupMutex.Unlock()
evictPodFromMetrics(*p)
evictPodByName(*p)
},
}

Expand Down
14 changes: 12 additions & 2 deletions pkg/pod/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,22 @@ func (cr Collector) SetMetrics(podName string, podNamespace string, nodeName str
}
}

func evictPodFromMetrics(p v1.Pod) {

// Evicts exporter metrics by pod and container name
func evictPodByName(p v1.Pod) {
podGaugeVec.DeletePartialMatch(prometheus.Labels{"pod_name": p.Name})
// TODO: Look into removing this for loop and delete by pod_name
// e.g. containerVolumeUsageVec.DeletePartialMatch(prometheus.Labels{"pod_name": p.Name})
for _, c := range p.Spec.Containers {
containerVolumeUsageVec.DeletePartialMatch(prometheus.Labels{"container": c.Name})
containerPercentageLimitsVec.DeletePartialMatch(prometheus.Labels{"container": c.Name})
containerPercentageVolumeLimitsVec.DeletePartialMatch(prometheus.Labels{"container": c.Name})
}
}

// EvictPodByNode Evicts exporter metrics by Node
func EvictPodByNode(deleteLabel *prometheus.Labels) {
podGaugeVec.DeletePartialMatch(*deleteLabel)
containerVolumeUsageVec.DeletePartialMatch(*deleteLabel)
containerPercentageLimitsVec.DeletePartialMatch(*deleteLabel)
containerPercentageVolumeLimitsVec.DeletePartialMatch(*deleteLabel)
}

0 comments on commit c19635f

Please sign in to comment.