Skip to content

Commit

Permalink
Merge pull request #2559 from RiRa12621/main
Browse files Browse the repository at this point in the history
fix(kube_pod_tolerations): deduplicate tolerations before creating metric
  • Loading branch information
k8s-ci-robot authored Jan 20, 2025
2 parents 88ef004 + 20f3023 commit 288b3cf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
18 changes: 17 additions & 1 deletion internal/store/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -1642,6 +1642,21 @@ func createPodStatusUnschedulableFamilyGenerator() generator.FamilyGenerator {
)
}

// getUniqueTolerations takes an array
func getUniqueTolerations(tolerations []v1.Toleration) []v1.Toleration {
uniqueTolerationsMap := make(map[v1.Toleration]struct{})
var uniqueTolerations []v1.Toleration

for _, toleration := range tolerations {
_, exists := uniqueTolerationsMap[toleration]
if !exists {
uniqueTolerationsMap[toleration] = struct{}{}
uniqueTolerations = append(uniqueTolerations, toleration)
}
}
return uniqueTolerations
}

func createPodTolerationsFamilyGenerator() generator.FamilyGenerator {
return *generator.NewFamilyGeneratorWithStability(
"kube_pod_tolerations",
Expand All @@ -1651,8 +1666,9 @@ func createPodTolerationsFamilyGenerator() generator.FamilyGenerator {
"",
wrapPodFunc(func(p *v1.Pod) *metric.Family {
var ms []*metric.Metric
uniqueTolerations := getUniqueTolerations(p.Spec.Tolerations)

for _, t := range p.Spec.Tolerations {
for _, t := range uniqueTolerations {
var key, operator, value, effect, tolerationSeconds string

key = t.Key
Expand Down
6 changes: 6 additions & 0 deletions internal/store/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,12 @@ func TestPodStore(t *testing.T) {
Operator: v1.TolerationOpEqual,
Value: "value3",
},
{
// Duplicate toleration, to ensure that doesn't result in a duplicate metric
Key: "key3",
Operator: v1.TolerationOpEqual,
Value: "value3",
},
{
// an empty toleration to ensure that an empty toleration does not result in a metric
},
Expand Down

0 comments on commit 288b3cf

Please sign in to comment.