From 9f161590bee774e9aefb0ca89442fe9993cbee87 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Tue, 18 Mar 2025 08:56:53 +0100 Subject: [PATCH] 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. --- hack/verify-prometheus-imports.sh | 1 - .../devicetainteviction/device_taint_eviction_test.go | 6 ++---- .../k8s.io/component-base/metrics/testutil/testutil.go | 10 ++++++++++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/hack/verify-prometheus-imports.sh b/hack/verify-prometheus-imports.sh index 18a67e77922..ba1321e1e41 100755 --- a/hack/verify-prometheus-imports.sh +++ b/hack/verify-prometheus-imports.sh @@ -37,7 +37,6 @@ source "${KUBE_ROOT}/hack/lib/util.sh" # See: https://github.com/kubernetes/kubernetes/issues/89267 allowed_prometheus_importers=( ./cluster/images/etcd-version-monitor/etcd-version-monitor.go - ./pkg/controller/devicetainteviction/device_taint_eviction_test.go ./staging/src/k8s.io/component-base/metrics/prometheusextension/timing_histogram.go ./staging/src/k8s.io/component-base/metrics/prometheusextension/timing_histogram_test.go ./staging/src/k8s.io/component-base/metrics/prometheusextension/timing_histogram_vec.go diff --git a/pkg/controller/devicetainteviction/device_taint_eviction_test.go b/pkg/controller/devicetainteviction/device_taint_eviction_test.go index 74afd0e9c66..ee1b5439fc5 100644 --- a/pkg/controller/devicetainteviction/device_taint_eviction_test.go +++ b/pkg/controller/devicetainteviction/device_taint_eviction_test.go @@ -32,8 +32,6 @@ import ( "github.com/onsi/gomega" "github.com/onsi/gomega/gstruct" gomegatypes "github.com/onsi/gomega/types" - "github.com/prometheus/client_golang/prometheus" - dto "github.com/prometheus/client_model/go" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -1205,7 +1203,7 @@ device_taint_eviction_controller_pod_deletions_total %[1]d controller.metrics.PodDeletionsTotal.FQName(), controller.metrics.PodDeletionsLatency.FQName(), } - gather := func() ([]*dto.MetricFamily, error) { + gather := func() ([]*metricstestutil.MetricFamily, error) { got, err := controller.metrics.Gather() for _, mf := range got { for _, m := range mf.Metric { @@ -1218,7 +1216,7 @@ device_taint_eviction_controller_pod_deletions_total %[1]d return got, err } - return metricstestutil.GatherAndCompare(prometheus.GathererFunc(gather), strings.NewReader(expectedMetric), names...) + return metricstestutil.GatherAndCompare(metricstestutil.GathererFunc(gather), strings.NewReader(expectedMetric), names...) } // TestEviction runs through the full flow of starting the controller and evicting one pod. diff --git a/staging/src/k8s.io/component-base/metrics/testutil/testutil.go b/staging/src/k8s.io/component-base/metrics/testutil/testutil.go index b00f949c376..d023a0dea8e 100644 --- a/staging/src/k8s.io/component-base/metrics/testutil/testutil.go +++ b/staging/src/k8s.io/component-base/metrics/testutil/testutil.go @@ -20,7 +20,9 @@ import ( "fmt" "io" + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/testutil" + dto "github.com/prometheus/client_model/go" apimachineryversion "k8s.io/apimachinery/pkg/version" "k8s.io/component-base/metrics" @@ -33,6 +35,14 @@ type TB interface { Fatalf(format string, args ...any) } +// MetricFamily is a type alias which enables writing gatherers in tests +// without importing prometheus directly (https://github.com/kubernetes/kubernetes/issues/99876). +type MetricFamily = dto.MetricFamily + +// GathererFunc is a type alias which enables writing gatherers as a function in tests +// without importing prometheus directly (https://github.com/kubernetes/kubernetes/issues/99876). +type GathererFunc = prometheus.GathererFunc + // CollectAndCompare registers the provided Collector with a newly created // pedantic Registry. It then does the same as GatherAndCompare, gathering the // metrics from the pedantic Registry.