Compare commits

...

2 Commits

Author SHA1 Message Date
Mikkel Oscar Lyderik Larsen
be7567efea Fix handling of invalid/empty json response (#209)
Signed-off-by: Mikkel Oscar Lyderik Larsen <mikkel.larsen@zalando.de>
2020-10-02 10:57:53 +02:00
Mikkel Oscar Lyderik Larsen
b677e814be Update dependencies (#200)
Signed-off-by: Mikkel Oscar Lyderik Larsen <mikkel.larsen@zalando.de>
2020-10-02 10:55:42 +02:00
4 changed files with 221 additions and 26 deletions

16
go.mod
View File

@ -2,7 +2,7 @@ module github.com/zalando-incubator/kube-metrics-adapter
require (
github.com/NYTimes/gziphandler v1.0.1 // indirect
github.com/aws/aws-sdk-go v1.33.5
github.com/aws/aws-sdk-go v1.35.0
github.com/googleapis/gnostic v0.2.0 // indirect
github.com/influxdata/influxdb-client-go v0.1.5
github.com/kubernetes-incubator/custom-metrics-apiserver v0.0.0-20200618121405-54026617ec44
@ -10,8 +10,8 @@ require (
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/onsi/gomega v1.8.1 // indirect
github.com/prometheus/client_golang v1.7.1
github.com/prometheus/common v0.10.0
github.com/sirupsen/logrus v1.6.0
github.com/prometheus/common v0.14.0
github.com/sirupsen/logrus v1.7.0
github.com/spf13/cobra v0.0.7
github.com/spyzhov/ajson v0.4.2
github.com/stretchr/testify v1.6.1
@ -20,12 +20,12 @@ require (
golang.org/x/tools v0.0.0-20200204192400-7124308813f3 // indirect
gonum.org/v1/netlib v0.0.0-20190331212654-76723241ea4e // indirect
honnef.co/go/tools v0.0.1-2020.1.3 // indirect
k8s.io/api v0.18.5
k8s.io/apimachinery v0.18.5
k8s.io/client-go v0.18.5
k8s.io/component-base v0.18.5
k8s.io/api v0.18.8
k8s.io/apimachinery v0.18.8
k8s.io/client-go v0.18.8
k8s.io/component-base v0.18.8
k8s.io/klog v1.0.0
k8s.io/metrics v0.18.5
k8s.io/metrics v0.18.8
)
go 1.13

210
go.sum

File diff suppressed because it is too large Load Diff

View File

@ -72,7 +72,7 @@ func (g *JSONPathMetricsGetter) GetMetric(metricsURL url.URL) (float64, error) {
return 0, err
}
if len(nodes) > 1 {
if len(nodes) != 1 {
return 0, fmt.Errorf("unexpected json: expected single numeric or array value")
}

View File

@ -1,6 +1,7 @@
package httpmetrics
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
@ -27,6 +28,7 @@ func TestJSONPathMetricsGetter(t *testing.T) {
jsonPath string
result float64
aggregator AggregatorFunc
err error
}{
{
name: "basic single value",
@ -49,6 +51,18 @@ func TestJSONPathMetricsGetter(t *testing.T) {
result: 5,
aggregator: Average,
},
{
name: "json path not resulting in array or number should lead to error",
jsonResponse: []byte(`{"metric.value":5}`),
jsonPath: "$['invalid.metric.values']",
err: errors.New("unexpected json: expected single numeric or array value"),
},
{
name: "invalid json should error",
jsonResponse: []byte(`{`),
jsonPath: "$['invalid.metric.values']",
err: errors.New("unexpected end of file"),
},
} {
t.Run(tc.name, func(t *testing.T) {
server := makeTestHTTPServer(t, tc.jsonResponse)
@ -58,6 +72,11 @@ func TestJSONPathMetricsGetter(t *testing.T) {
url, err := url.Parse(fmt.Sprintf("%s/metrics", server.URL))
require.NoError(t, err)
metric, err := getter.GetMetric(*url)
if tc.err != nil {
require.Error(t, err)
require.Equal(t, tc.err.Error(), err.Error())
return
}
require.NoError(t, err)
require.Equal(t, tc.result, metric)
})