Skip to content

Commit 9f16159

Browse files
committed
metrics testing: add type aliases to avoid direct prometheus imports
In tests it is sometimes unavoidable to use the Prometheus types directly, for example when writing a custom gatherer which needs to normalize data before testing it. device_taint_eviction_test.go does this to strip out unpredictable data in a histogram. With type aliases in a package that is explicitly meant for tests we can avoid adding exceptions for such tests to the global exception list.
1 parent 37b47f4 commit 9f16159

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

hack/verify-prometheus-imports.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ source "${KUBE_ROOT}/hack/lib/util.sh"
3737
# See: https://github.com/kubernetes/kubernetes/issues/89267
3838
allowed_prometheus_importers=(
3939
./cluster/images/etcd-version-monitor/etcd-version-monitor.go
40-
./pkg/controller/devicetainteviction/device_taint_eviction_test.go
4140
./staging/src/k8s.io/component-base/metrics/prometheusextension/timing_histogram.go
4241
./staging/src/k8s.io/component-base/metrics/prometheusextension/timing_histogram_test.go
4342
./staging/src/k8s.io/component-base/metrics/prometheusextension/timing_histogram_vec.go

pkg/controller/devicetainteviction/device_taint_eviction_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ import (
3232
"github.com/onsi/gomega"
3333
"github.com/onsi/gomega/gstruct"
3434
gomegatypes "github.com/onsi/gomega/types"
35-
"github.com/prometheus/client_golang/prometheus"
36-
dto "github.com/prometheus/client_model/go"
3735
"github.com/stretchr/testify/assert"
3836
"github.com/stretchr/testify/require"
3937

@@ -1205,7 +1203,7 @@ device_taint_eviction_controller_pod_deletions_total %[1]d
12051203
controller.metrics.PodDeletionsTotal.FQName(),
12061204
controller.metrics.PodDeletionsLatency.FQName(),
12071205
}
1208-
gather := func() ([]*dto.MetricFamily, error) {
1206+
gather := func() ([]*metricstestutil.MetricFamily, error) {
12091207
got, err := controller.metrics.Gather()
12101208
for _, mf := range got {
12111209
for _, m := range mf.Metric {
@@ -1218,7 +1216,7 @@ device_taint_eviction_controller_pod_deletions_total %[1]d
12181216
return got, err
12191217
}
12201218

1221-
return metricstestutil.GatherAndCompare(prometheus.GathererFunc(gather), strings.NewReader(expectedMetric), names...)
1219+
return metricstestutil.GatherAndCompare(metricstestutil.GathererFunc(gather), strings.NewReader(expectedMetric), names...)
12221220
}
12231221

12241222
// TestEviction runs through the full flow of starting the controller and evicting one pod.

staging/src/k8s.io/component-base/metrics/testutil/testutil.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import (
2020
"fmt"
2121
"io"
2222

23+
"github.com/prometheus/client_golang/prometheus"
2324
"github.com/prometheus/client_golang/prometheus/testutil"
25+
dto "github.com/prometheus/client_model/go"
2426

2527
apimachineryversion "k8s.io/apimachinery/pkg/version"
2628
"k8s.io/component-base/metrics"
@@ -33,6 +35,14 @@ type TB interface {
3335
Fatalf(format string, args ...any)
3436
}
3537

38+
// MetricFamily is a type alias which enables writing gatherers in tests
39+
// without importing prometheus directly (https://github.com/kubernetes/kubernetes/issues/99876).
40+
type MetricFamily = dto.MetricFamily
41+
42+
// GathererFunc is a type alias which enables writing gatherers as a function in tests
43+
// without importing prometheus directly (https://github.com/kubernetes/kubernetes/issues/99876).
44+
type GathererFunc = prometheus.GathererFunc
45+
3646
// CollectAndCompare registers the provided Collector with a newly created
3747
// pedantic Registry. It then does the same as GatherAndCompare, gathering the
3848
// metrics from the pedantic Registry.

0 commit comments

Comments
 (0)