From 7c64d4f342ec4d5446a4768b779f57ef22d87129 Mon Sep 17 00:00:00 2001 From: Vikram Raman Date: Thu, 17 Sep 2020 12:48:56 -0700 Subject: [PATCH] improved prefix handling --- Makefile | 2 +- cmd/wavefront-adapter/main.go | 3 ++- go.mod | 2 +- pkg/provider/provider_test.go | 4 +++- pkg/provider/translator.go | 13 ++++++++----- pkg/provider/translator_test.go | 17 +++++++++++++++++ 6 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 pkg/provider/translator_test.go diff --git a/Makefile b/Makefile index 8f189df..575b34e 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ OUT_DIR?=./_output DOCKER_REPO=wavefronthq DOCKER_IMAGE=wavefront-hpa-adapter -VERSION=0.9.5 +VERSION=0.9.6 GOLANG_VERSION?=1.13 BINARY_NAME=wavefront-adapter GIT_COMMIT:=$(shell git rev-parse --short HEAD) diff --git a/cmd/wavefront-adapter/main.go b/cmd/wavefront-adapter/main.go index ff752ca..b452bfc 100644 --- a/cmd/wavefront-adapter/main.go +++ b/cmd/wavefront-adapter/main.go @@ -8,6 +8,7 @@ import ( "net/url" "os" "runtime" + "strings" "time" log "github.com/sirupsen/logrus" @@ -78,7 +79,7 @@ func (a *WavefrontAdapter) makeProviderOrDie() customprovider.MetricsProvider { KubeClient: kubeClient, Mapper: mapper, WaveClient: waveClient, - Prefix: a.CustomMetricPrefix, + Prefix: strings.Trim(a.CustomMetricPrefix, "."), ListInterval: a.MetricsRelistInterval, ExternalCfg: a.AdapterConfigFile, }) diff --git a/go.mod b/go.mod index a1953dc..2b9af31 100644 --- a/go.mod +++ b/go.mod @@ -54,7 +54,7 @@ require ( github.com/sirupsen/logrus v1.4.2 github.com/spf13/cobra v0.0.2-0.20180319062004-c439c4fa0937 // indirect github.com/spf13/pflag v1.0.1 // indirect - github.com/stretchr/testify v1.4.0 // indirect + github.com/stretchr/testify v1.4.0 github.com/ugorji/go v0.0.0-20170107133203-ded73eae5db7 // indirect github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect go.etcd.io/bbolt v1.3.3 // indirect diff --git a/pkg/provider/provider_test.go b/pkg/provider/provider_test.go index 2c4ec57..1be40d1 100644 --- a/pkg/provider/provider_test.go +++ b/pkg/provider/provider_test.go @@ -5,13 +5,15 @@ package provider import ( "fmt" + "testing" + "github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider" "github.com/wavefronthq/wavefront-kubernetes-adapter/pkg/client" + "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/dynamic/fake" - "testing" ) func TestListAllMetrics(t *testing.T) { diff --git a/pkg/provider/translator.go b/pkg/provider/translator.go index 90dd91f..802faa5 100644 --- a/pkg/provider/translator.go +++ b/pkg/provider/translator.go @@ -84,7 +84,7 @@ func (t wavefrontTranslator) MatchValuesToNames(queryResult wave.QueryResult, gr func (t wavefrontTranslator) CustomMetricsFor(metricNames []string) []provider.CustomMetricInfo { var customMetrics []provider.CustomMetricInfo for _, metricName := range metricNames { - resourceName, metric := splitMetric(metricName) + resourceName, metric := splitMetric(t.prefix, metricName) if resourceName == "" || metric == "" { continue } @@ -219,12 +219,15 @@ func namespaced(resourceName string) bool { } // splits a metric such as "kubernetes.pod.cpu.limit" into "pod" and "cpu.limit" -func splitMetric(metricName string) (string, string) { - parts := strings.SplitN(metricName, ".", 3) - if len(parts) != 3 { +func splitMetric(prefix, metricName string) (string, string) { + if strings.HasPrefix(metricName, prefix) { + metricName = metricName[len(prefix)+1:] + } + parts := strings.SplitN(metricName, ".", 2) + if len(parts) != 2 { return "", "" } - return parts[1], parts[2] + return parts[0], parts[1] } // trims a float64 to 3 decimal digits diff --git a/pkg/provider/translator_test.go b/pkg/provider/translator_test.go new file mode 100644 index 0000000..3051983 --- /dev/null +++ b/pkg/provider/translator_test.go @@ -0,0 +1,17 @@ +package provider + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSplitMetric(t *testing.T) { + res, metric := splitMetric("kubernetes", "kubernetes.pod.cpu.usage") + assert.Equal(t, res, "pod") + assert.Equal(t, metric, "cpu.usage") + + res, metric = splitMetric("pks.kubernetes", "pks.kubernetes.pod.cpu.usage") + assert.Equal(t, res, "pod") + assert.Equal(t, metric, "cpu.usage") +}