Skip to content

Commit

Permalink
Merge pull request #1616 from olivierlemasle/rebase-950
Browse files Browse the repository at this point in the history
[e2e] check all default collector metrics are available
  • Loading branch information
k8s-ci-robot authored Oct 27, 2021
2 parents 25066f2 + ce21166 commit 8c8f384
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
9 changes: 0 additions & 9 deletions tests/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ SUDO=${SUDO:-}
OS=$(uname -s | awk '{print tolower($0)}')
OS=${OS:-linux}

EXCLUDED_RESOURCE_REGEX="verticalpodautoscaler"

function finish() {
echo "calling cleanup function"
# kill kubectl proxy in background
Expand Down Expand Up @@ -165,13 +163,6 @@ mkdir -p ${KUBE_STATE_METRICS_LOG_DIR}
echo "access kube-state-metrics metrics endpoint"
curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics:http-metrics/proxy/metrics" >${KUBE_STATE_METRICS_LOG_DIR}/metrics

resources=$(find internal/store/ -maxdepth 1 -name "*.go" -not -name "*_test.go" -not -name "builder.go" -not -name "testutils.go" -not -name "utils.go" -print0 | xargs -0 -n1 basename | awk -F. '{print $1}'| grep -v "$EXCLUDED_RESOURCE_REGEX")
echo "available resources: $resources"
for resource in ${resources}; do
echo "checking that kube_${resource}* metrics exists"
grep "^kube_${resource}_" ${KUBE_STATE_METRICS_LOG_DIR}/metrics
done

KUBE_STATE_METRICS_STATUS=$(curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics:http-metrics/proxy/healthz")
if [[ "${KUBE_STATE_METRICS_STATUS}" == "OK" ]]; then
echo "kube-state-metrics is still running after accessing metrics endpoint"
Expand Down
57 changes: 57 additions & 0 deletions tests/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"path"
"regexp"
"sort"
"strings"
"testing"

Expand Down Expand Up @@ -242,3 +243,59 @@ func prettyPrintCounter(name string, metric *dto.Metric) string {
}
return fmt.Sprintf("%s{%s} %d", name, strings.Join(labelStrings, ","), int(metric.GetCounter().GetValue()))
}

func TestDefaultCollectorMetricsAvailable(t *testing.T) {
buf := &bytes.Buffer{}

err := framework.KsmClient.Metrics(buf)
if err != nil {
t.Fatalf("failed to get metrics from kube-state-metrics: %v", err)
}

resources := map[string]struct{}{}
files, err := os.ReadDir("../../internal/store/")
if err != nil {
t.Fatalf("failed to read dir to get all resouces name: %v", err)
}

re := regexp.MustCompile(`^([a-z]+).go$`)
for _, file := range files {
params := re.FindStringSubmatch(file.Name())
if len(params) != 2 {
continue
}
if params[1] == "builder" || params[1] == "utils" || params[1] == "testutils" {
// Non resource file
continue
}
if params[1] == "verticalpodautoscaler" {
// Resource disabled by default
continue
}
resources[params[1]] = struct{}{}
}

re = regexp.MustCompile(`^kube_([a-z]+)_`)
scanner := bufio.NewScanner(buf)
for scanner.Scan() {
params := re.FindStringSubmatch(scanner.Text())
if len(params) != 2 {
continue
}
delete(resources, params[1])
}

err = scanner.Err()
if err != nil {
t.Fatalf("failed to scan metrics: %v", err)
}

if len(resources) != 0 {
s := []string{}
for k := range resources {
s = append(s, k)
}
sort.Strings(s)
t.Fatalf("failed to find metrics of resources: %s", strings.Join(s, ", "))
}
}

0 comments on commit 8c8f384

Please sign in to comment.