Skip to content

Commit

Permalink
[e2e] Check errors in kube-state-metrics own metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierlemasle committed Mar 17, 2020
1 parent fdd2ef1 commit 47f507d
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 151 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
documented_metrics
code_metrics

# E2e tests output
/log
*.bak

# Created by https://www.gitignore.io/api/go

### Go ###
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/jsonnet-bundler/jsonnet-bundler v0.1.1-0.20190930114713-10e24cb86976
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v1.1.0
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4
github.com/prometheus/common v0.6.0
github.com/prometheus/prometheus v2.5.0+incompatible
github.com/robfig/cron/v3 v3.0.0
Expand Down
9 changes: 5 additions & 4 deletions tests/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ OS=${OS:-linux}

EXCLUDED_RESOURCE_REGEX="verticalpodautoscaler"

mkdir -p ${KUBE_STATE_METRICS_LOG_DIR}

function finish() {
echo "calling cleanup function"
# kill kubectl proxy in background
Expand Down Expand Up @@ -166,8 +164,11 @@ set -e
echo "kube-state-metrics is up and running"

echo "start e2e test for kube-state-metrics"
KSMURL='http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics:http-metrics/proxy'
go test -v ./tests/e2e/ --ksmurl=${KSMURL}
KSM_HTTP_METRICS_URL='http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics:http-metrics/proxy'
KSM_TELEMETRY_URL='http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics:telemetry/proxy'
go test -v ./tests/e2e/ --ksm-http-metrics-url=${KSM_HTTP_METRICS_URL} --ksm-telemetry-url=${KSM_TELEMETRY_URL}

mkdir -p ${KUBE_STATE_METRICS_LOG_DIR}

# TODO: re-implement the following test cases in Go with the goal of removing this file.
echo "access kube-state-metrics metrics endpoint"
Expand Down
12 changes: 12 additions & 0 deletions tests/e2e/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
To run these tests, you need to provide two CLI flags:

- `--ksm-http-metrics-url`: url to access the kube-state-metrics service
- `--ksm-telemetry-url`: url to access the kube-state-metrics telemetry endpoint

Example:

```
go test -v ./tests/e2e \
--ksm-http-metrics-url=http://localhost:8080/ \
--ksm-telemetry-url=http://localhost:8081/
```
131 changes: 0 additions & 131 deletions tests/e2e/framework.go

This file was deleted.

174 changes: 174 additions & 0 deletions tests/e2e/framework/framework.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
Copyright 2019 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package framework

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"strings"

"github.com/pkg/errors"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
)

const (
epHealthz = "/healthz"
epMetrics = "/metrics"
)

// The Framework stores a pointer to the KSMClient
type Framework struct {
KsmClient *KSMClient
}

// New returns a new Framework given the kube-state-metrics service URLs.
// It delegates the url validation errs to NewKSMClient func.
func New(ksmHTTPMetricsURL, ksmTelemetryURL string) (*Framework, error) {
ksmClient, err := NewKSMClient(ksmHTTPMetricsURL, ksmTelemetryURL)
if err != nil {
return nil, err
}

return &Framework{
KsmClient: ksmClient,
}, nil
}

// The KSMClient is the Kubernetes State Metric client.
type KSMClient struct {
httpMetricsEndpoint *url.URL
telemetryEndpoint *url.URL
client *http.Client
}

// NewKSMClient retrieves a new KSMClient the kube-state-metrics service URLs.
// In case of error parsing the provided addresses, it returns an error.
func NewKSMClient(ksmHTTPMetricsAddress, ksmTelemetryAddress string) (*KSMClient, error) {
ksmHTTPMetricsURL, err := validateURL(ksmHTTPMetricsAddress)
if err != nil {
return nil, err
}
ksmTelemetryURL, err := validateURL(ksmTelemetryAddress)
if err != nil {
return nil, err
}

return &KSMClient{
httpMetricsEndpoint: ksmHTTPMetricsURL,
telemetryEndpoint: ksmTelemetryURL,
client: &http.Client{},
}, nil
}

func validateURL(address string) (*url.URL, error) {
u, err := url.Parse(address)
if err != nil {
return nil, err
}
u.Path = strings.TrimRight(u.Path, "/")
return u, nil
}

// IsHealthz makes a request to the /healthz endpoint to get the health status.
func (k *KSMClient) IsHealthz() (bool, error) {
p := path.Join(k.httpMetricsEndpoint.Path, epHealthz)

u := *k.httpMetricsEndpoint
u.Path = p

req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return false, err
}

resp, err := k.client.Do(req)
if err != nil {
return false, err
}
defer func() {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}()

if resp.StatusCode != http.StatusOK {
return false, fmt.Errorf("server returned HTTP status %s", resp.Status)
}

return true, nil
}

func (k *KSMClient) writeMetrics(endpoint *url.URL, w io.Writer) error {
if endpoint == nil {
return errors.New("Endpoint is nil")
}

u := *endpoint
u.Path = path.Join(endpoint.Path, epMetrics)

req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return err
}

resp, err := k.client.Do(req)
if err != nil {
return err
}
defer func() {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("server returned HTTP status %s", resp.Status)
}

io.Copy(w, resp.Body)

return nil
}

// Metrics makes a request to the /metrics endpoint on the "http-metrics" port,
// and writes its content to the writer w.
func (k *KSMClient) Metrics(w io.Writer) error {
return k.writeMetrics(k.httpMetricsEndpoint, w)
}

// TelemetryMetrics makes a request to the /metrics endpoint on the "telemetry" port,
// and writes its content to the writer w.
func (k *KSMClient) TelemetryMetrics(w io.Writer) error {
return k.writeMetrics(k.telemetryEndpoint, w)
}

// ParseMetrics uses a prometheus TextParser to parse metrics, given a function
// that fetches and writes metrics.
func (f *Framework) ParseMetrics(metrics func(io.Writer) error) (map[string]*dto.MetricFamily, error) {
buf := &bytes.Buffer{}
err := metrics(buf)
if err != nil {
return nil, errors.Wrap(err, "Failed to get metrics")
}

parser := &expfmt.TextParser{}
return parser.TextToMetricFamilies(buf)
}
Loading

0 comments on commit 47f507d

Please sign in to comment.