From 529b6da9bb0f54a92ab0369f2b9a626715bada9b Mon Sep 17 00:00:00 2001 From: tanjunchen Date: Wed, 22 Apr 2020 14:07:53 +0800 Subject: [PATCH] remove prometheus dependencies from k/k and add testcases for LabelsMatch Kubernetes-commit: 6e986249ee4252f83037f229a8773869feaab15a --- go.mod | 18 ++++++++--------- go.sum | 4 ---- pkg/admission/metrics/testutil_test.go | 27 ++++++++++---------------- 3 files changed, 19 insertions(+), 30 deletions(-) diff --git a/go.mod b/go.mod index a2de1088b..4675d0c9a 100644 --- a/go.mod +++ b/go.mod @@ -27,7 +27,6 @@ require ( github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 github.com/pkg/errors v0.9.1 github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021 // indirect - github.com/prometheus/client_model v0.2.0 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.4.0 go.etcd.io/etcd v0.5.0-alpha.5.0.20200401174654-e694b7bb0875 @@ -41,10 +40,10 @@ require ( gopkg.in/natefinch/lumberjack.v2 v2.0.0 gopkg.in/square/go-jose.v2 v2.2.2 gopkg.in/yaml.v2 v2.2.8 - k8s.io/api v0.0.0-20200421221621-2ca06ba83edf - k8s.io/apimachinery v0.0.0-20200423195709-147f4cafe1e5 - k8s.io/client-go v0.0.0-20200424080147-0ad81dc3c85d - k8s.io/component-base v0.0.0-20200423061759-681caaeb5a1e + k8s.io/api v0.0.0 + k8s.io/apimachinery v0.0.0 + k8s.io/client-go v0.0.0 + k8s.io/component-base v0.0.0 k8s.io/klog v1.0.0 k8s.io/kube-openapi v0.0.0-20200403204345-e1beb1bd0f35 k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89 @@ -56,8 +55,9 @@ require ( replace ( golang.org/x/sys => golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a // pinned to release-branch.go1.13 golang.org/x/tools => golang.org/x/tools v0.0.0-20190821162956-65e3620a7ae7 // pinned to release-branch.go1.13 - k8s.io/api => k8s.io/api v0.0.0-20200421221621-2ca06ba83edf - k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20200423195709-147f4cafe1e5 - k8s.io/client-go => k8s.io/client-go v0.0.0-20200424080147-0ad81dc3c85d - k8s.io/component-base => k8s.io/component-base v0.0.0-20200423061759-681caaeb5a1e + k8s.io/api => ../api + k8s.io/apimachinery => ../apimachinery + k8s.io/apiserver => ../apiserver + k8s.io/client-go => ../client-go + k8s.io/component-base => ../component-base ) diff --git a/go.sum b/go.sum index 151216beb..af5d5e2ae 100644 --- a/go.sum +++ b/go.sum @@ -362,10 +362,6 @@ gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -k8s.io/api v0.0.0-20200421221621-2ca06ba83edf/go.mod h1:ulbB/6H6vVXAkqfLv6nAqwp3OAksqUGzbqvrQ8uWFCs= -k8s.io/apimachinery v0.0.0-20200423195709-147f4cafe1e5/go.mod h1:imoz42hIYwpLTRWXU8pdJ9IE8DbxUsnU9lyVN8Y1SNo= -k8s.io/client-go v0.0.0-20200424080147-0ad81dc3c85d/go.mod h1:6fX2/t8t2anrYnkj3wBvT0ObCPteMb7rnUL+W2Tn914= -k8s.io/component-base v0.0.0-20200423061759-681caaeb5a1e/go.mod h1:HZ3pegcljoPKhQyaHWt9RSDAwJjsWJXD5M5vQhy+iIE= k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0= k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk= k8s.io/klog v0.3.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk= diff --git a/pkg/admission/metrics/testutil_test.go b/pkg/admission/metrics/testutil_test.go index b31739a6c..121778b8e 100644 --- a/pkg/admission/metrics/testutil_test.go +++ b/pkg/admission/metrics/testutil_test.go @@ -19,43 +19,36 @@ package metrics import ( "testing" - ptype "github.com/prometheus/client_model/go" "k8s.io/component-base/metrics/legacyregistry" + "k8s.io/component-base/metrics/testutil" ) -func labelsMatch(metric *ptype.Metric, labelFilter map[string]string) bool { - for _, lp := range metric.GetLabel() { - if value, ok := labelFilter[lp.GetName()]; ok && lp.GetValue() != value { - return false - } - } - return true -} - // expectFindMetric find a metric with the given name nad labels or reports a fatal test error. -func expectFindMetric(t *testing.T, name string, expectedLabels map[string]string) *ptype.Metric { +func expectFindMetric(t *testing.T, name string, expectedLabels map[string]string) { metrics, err := legacyregistry.DefaultGatherer.Gather() if err != nil { t.Fatalf("Failed to gather metrics: %s", err) } + if len(metrics) == 0 { + t.Fatalf("No metric found with name %s and labels %#+v", name, expectedLabels) + } + for _, mf := range metrics { if mf.GetName() == name { for _, metric := range mf.GetMetric() { - if labelsMatch(metric, expectedLabels) { + if testutil.LabelsMatch(metric, expectedLabels) { gotLabelCount := len(metric.GetLabel()) wantLabelCount := len(expectedLabels) if wantLabelCount != gotLabelCount { t.Errorf("Got metric with %d labels, but wanted %d labels. Wanted %#+v for %s", gotLabelCount, wantLabelCount, expectedLabels, metric.String()) + continue } - return metric } } } } - t.Fatalf("No metric found with name %s and labels %#+v", name, expectedLabels) - return nil } // expectHistogramCountTotal ensures that the sum of counts of metrics matching the labelFilter is as @@ -72,7 +65,7 @@ func expectHistogramCountTotal(t *testing.T, name string, labelFilter map[string continue // Ignore other metrics. } for _, metric := range mf.GetMetric() { - if !labelsMatch(metric, labelFilter) { + if !testutil.LabelsMatch(metric, labelFilter) { continue } counterSum += int(metric.GetHistogram().GetSampleCount()) @@ -104,7 +97,7 @@ func expectCounterValue(t *testing.T, name string, labelFilter map[string]string continue // Ignore other metrics. } for _, metric := range mf.GetMetric() { - if !labelsMatch(metric, labelFilter) { + if !testutil.LabelsMatch(metric, labelFilter) { continue } counterSum += int(metric.GetCounter().GetValue())