Merge pull request #111527 from mengjiao-liu/clean-prometheus-in-volume

Cleanup: remove prometheus dependencies for volume
This commit is contained in:
Kubernetes Prow Robot 2022-08-23 16:05:31 -07:00 committed by GitHub
commit 71e534c580
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 70 deletions

View File

@ -37,7 +37,6 @@ source "${KUBE_ROOT}/hack/lib/util.sh"
# See: https://github.com/kubernetes/kubernetes/issues/89267 # See: https://github.com/kubernetes/kubernetes/issues/89267
allowed_prometheus_importers=( allowed_prometheus_importers=(
./cluster/images/etcd-version-monitor/etcd-version-monitor.go ./cluster/images/etcd-version-monitor/etcd-version-monitor.go
./pkg/volume/util/operationexecutor/operation_generator_test.go
./staging/src/k8s.io/component-base/metrics/prometheusextension/timing_histogram.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_test.go
./staging/src/k8s.io/component-base/metrics/prometheusextension/timing_histogram_vec.go ./staging/src/k8s.io/component-base/metrics/prometheusextension/timing_histogram_vec.go

View File

@ -42,7 +42,8 @@ const (
* involves explicitly acknowledging support for the metric across multiple releases, in accordance with * involves explicitly acknowledging support for the metric across multiple releases, in accordance with
* the metric stability policy. * the metric stability policy.
*/ */
var storageOperationMetric = metrics.NewHistogramVec(
var StorageOperationMetric = metrics.NewHistogramVec(
&metrics.HistogramOpts{ &metrics.HistogramOpts{
Name: "storage_operation_duration_seconds", Name: "storage_operation_duration_seconds",
Help: "Storage operation duration", Help: "Storage operation duration",
@ -80,7 +81,7 @@ func init() {
func registerMetrics() { func registerMetrics() {
// legacyregistry is the internal k8s wrapper around the prometheus // legacyregistry is the internal k8s wrapper around the prometheus
// global registry, used specifically for metric stability enforcement // global registry, used specifically for metric stability enforcement
legacyregistry.MustRegister(storageOperationMetric) legacyregistry.MustRegister(StorageOperationMetric)
legacyregistry.MustRegister(storageOperationEndToEndLatencyMetric) legacyregistry.MustRegister(storageOperationEndToEndLatencyMetric)
legacyregistry.MustRegister(csiOperationsLatencyMetric) legacyregistry.MustRegister(csiOperationsLatencyMetric)
} }
@ -101,7 +102,7 @@ func OperationCompleteHook(plugin, operationName string) func(types.CompleteFunc
if c.Migrated != nil { if c.Migrated != nil {
migrated = *c.Migrated migrated = *c.Migrated
} }
storageOperationMetric.WithLabelValues(plugin, operationName, status, strconv.FormatBool(migrated)).Observe(timeTaken) StorageOperationMetric.WithLabelValues(plugin, operationName, status, strconv.FormatBool(migrated)).Observe(timeTaken)
} }
return opComplete return opComplete
} }

View File

@ -28,20 +28,20 @@ import (
"os" "os"
"testing" "testing"
io_prometheus_client "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/uuid" "k8s.io/apimachinery/pkg/util/uuid"
fakeclient "k8s.io/client-go/kubernetes/fake" fakeclient "k8s.io/client-go/kubernetes/fake"
"k8s.io/component-base/metrics/legacyregistry" "k8s.io/component-base/metrics/testutil"
"k8s.io/csi-translation-lib/plugins" "k8s.io/csi-translation-lib/plugins"
"k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume"
"k8s.io/kubernetes/pkg/volume/awsebs" "k8s.io/kubernetes/pkg/volume/awsebs"
csitesting "k8s.io/kubernetes/pkg/volume/csi/testing" csitesting "k8s.io/kubernetes/pkg/volume/csi/testing"
"k8s.io/kubernetes/pkg/volume/gcepd" "k8s.io/kubernetes/pkg/volume/gcepd"
volumetesting "k8s.io/kubernetes/pkg/volume/testing" volumetesting "k8s.io/kubernetes/pkg/volume/testing"
"k8s.io/kubernetes/pkg/volume/util"
volumetypes "k8s.io/kubernetes/pkg/volume/util/types" volumetypes "k8s.io/kubernetes/pkg/volume/util/types"
) )
@ -91,30 +91,15 @@ func TestOperationGenerator_GenerateUnmapVolumeFunc_PluginName(t *testing.T) {
t.Fatalf("Error occurred while generating unmapVolumeFunc: %v", e) t.Fatalf("Error occurred while generating unmapVolumeFunc: %v", e)
} }
metricFamilyName := "storage_operation_duration_seconds" m := util.StorageOperationMetric.WithLabelValues(expectedPluginName, "unmap_volume", "success", "false")
labelFilter := map[string]string{ storageOperationDurationSecondsMetricBefore, _ := testutil.GetHistogramMetricCount(m)
"migrated": "false",
"status": "success",
"operation_name": "unmap_volume",
"volume_plugin": expectedPluginName,
}
// compare the relative change of the metric because of the global state of the prometheus.DefaultGatherer.Gather()
storageOperationDurationSecondsMetricBefore := findMetricWithNameAndLabels(metricFamilyName, labelFilter)
var ee error var ee error
unmapVolumeFunc.CompleteFunc(volumetypes.CompleteFuncParam{Err: &ee}) unmapVolumeFunc.CompleteFunc(volumetypes.CompleteFuncParam{Err: &ee})
storageOperationDurationSecondsMetricAfter := findMetricWithNameAndLabels(metricFamilyName, labelFilter) storageOperationDurationSecondsMetricAfter, _ := testutil.GetHistogramMetricCount(m)
if storageOperationDurationSecondsMetricAfter == nil { metricValueDiff := storageOperationDurationSecondsMetricAfter - storageOperationDurationSecondsMetricBefore
t.Fatalf("Couldn't find the metric with name(%s) and labels(%v)", metricFamilyName, labelFilter) assert.Equal(t, uint64(1), metricValueDiff, tc.name)
}
if storageOperationDurationSecondsMetricBefore == nil {
assert.Equal(t, uint64(1), *storageOperationDurationSecondsMetricAfter.Histogram.SampleCount, tc.name)
} else {
metricValueDiff := *storageOperationDurationSecondsMetricAfter.Histogram.SampleCount - *storageOperationDurationSecondsMetricBefore.Histogram.SampleCount
assert.Equal(t, uint64(1), metricValueDiff, tc.name)
}
} }
} }
@ -406,40 +391,6 @@ func getTestPV(volumeName string, specSize string) *v1.PersistentVolume {
} }
} }
func findMetricWithNameAndLabels(metricFamilyName string, labelFilter map[string]string) *io_prometheus_client.Metric {
metricFamily := getMetricFamily(metricFamilyName)
if metricFamily == nil {
return nil
}
for _, metric := range metricFamily.GetMetric() {
if isLabelsMatchWithMetric(labelFilter, metric) {
return metric
}
}
return nil
}
func isLabelsMatchWithMetric(labelFilter map[string]string, metric *io_prometheus_client.Metric) bool {
if len(labelFilter) != len(metric.Label) {
return false
}
for labelName, labelValue := range labelFilter {
labelFound := false
for _, labelPair := range metric.Label {
if labelName == *labelPair.Name && labelValue == *labelPair.Value {
labelFound = true
break
}
}
if !labelFound {
return false
}
}
return true
}
func getTestOperationGenerator(volumePluginMgr *volume.VolumePluginMgr, objects ...runtime.Object) OperationGenerator { func getTestOperationGenerator(volumePluginMgr *volume.VolumePluginMgr, objects ...runtime.Object) OperationGenerator {
fakeKubeClient := fakeclient.NewSimpleClientset(objects...) fakeKubeClient := fakeclient.NewSimpleClientset(objects...)
fakeRecorder := &record.FakeRecorder{} fakeRecorder := &record.FakeRecorder{}
@ -492,16 +443,6 @@ func getTestVolumeToUnmount(pod *v1.Pod, pvSpec v1.PersistentVolumeSpec, pluginN
return volumeToUnmount return volumeToUnmount
} }
func getMetricFamily(metricFamilyName string) *io_prometheus_client.MetricFamily {
metricFamilies, _ := legacyregistry.DefaultGatherer.Gather()
for _, mf := range metricFamilies {
if *mf.Name == metricFamilyName {
return mf
}
}
return nil
}
func initTestPlugins(t *testing.T, plugs []volume.VolumePlugin, pluginName string) (*volume.VolumePluginMgr, string) { func initTestPlugins(t *testing.T, plugs []volume.VolumePlugin, pluginName string) (*volume.VolumePluginMgr, string) {
client := fakeclient.NewSimpleClientset() client := fakeclient.NewSimpleClientset()
pluginMgr, _, tmpDir := csitesting.NewTestPlugin(t, client) pluginMgr, _, tmpDir := csitesting.NewTestPlugin(t, client)