Skip to content

Commit 288b3cf

Browse files
authored
Merge pull request #2559 from RiRa12621/main
fix(kube_pod_tolerations): deduplicate tolerations before creating metric
2 parents 88ef004 + 20f3023 commit 288b3cf

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

internal/store/pod.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,21 @@ func createPodStatusUnschedulableFamilyGenerator() generator.FamilyGenerator {
16421642
)
16431643
}
16441644

1645+
// getUniqueTolerations takes an array
1646+
func getUniqueTolerations(tolerations []v1.Toleration) []v1.Toleration {
1647+
uniqueTolerationsMap := make(map[v1.Toleration]struct{})
1648+
var uniqueTolerations []v1.Toleration
1649+
1650+
for _, toleration := range tolerations {
1651+
_, exists := uniqueTolerationsMap[toleration]
1652+
if !exists {
1653+
uniqueTolerationsMap[toleration] = struct{}{}
1654+
uniqueTolerations = append(uniqueTolerations, toleration)
1655+
}
1656+
}
1657+
return uniqueTolerations
1658+
}
1659+
16451660
func createPodTolerationsFamilyGenerator() generator.FamilyGenerator {
16461661
return *generator.NewFamilyGeneratorWithStability(
16471662
"kube_pod_tolerations",
@@ -1651,8 +1666,9 @@ func createPodTolerationsFamilyGenerator() generator.FamilyGenerator {
16511666
"",
16521667
wrapPodFunc(func(p *v1.Pod) *metric.Family {
16531668
var ms []*metric.Metric
1669+
uniqueTolerations := getUniqueTolerations(p.Spec.Tolerations)
16541670

1655-
for _, t := range p.Spec.Tolerations {
1671+
for _, t := range uniqueTolerations {
16561672
var key, operator, value, effect, tolerationSeconds string
16571673

16581674
key = t.Key

internal/store/pod_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -2114,6 +2114,12 @@ func TestPodStore(t *testing.T) {
21142114
Operator: v1.TolerationOpEqual,
21152115
Value: "value3",
21162116
},
2117+
{
2118+
// Duplicate toleration, to ensure that doesn't result in a duplicate metric
2119+
Key: "key3",
2120+
Operator: v1.TolerationOpEqual,
2121+
Value: "value3",
2122+
},
21172123
{
21182124
// an empty toleration to ensure that an empty toleration does not result in a metric
21192125
},

0 commit comments

Comments
 (0)