/
prometheus.go
100 lines (84 loc) · 3.04 KB
/
prometheus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package fakerp
import (
"context"
"crypto/tls"
"encoding/json"
"net/http"
"os"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/openshift/openshift-azure/test/clients/azure"
"github.com/openshift/openshift-azure/test/sanity"
)
type target struct {
DiscoveredLabels map[string]string `json:"discoveredLabels"`
Labels map[string]string `json:"labels"`
ScrapeURL string `json:"scrapeURL"`
LastError string `json:"lastError"`
LastScrape string `json:"lastScrape"`
Health string `json:"health"`
}
type targetsResponse struct {
Status string `json:"status"`
Data struct {
ActiveTargets []target `json:"activeTargets"`
DroppedTargets []target `json:"droppedTargets"`
} `json:"data"`
}
var _ = Describe("Prometheus E2E tests ", func() {
It("should register all the necessary prometheus targets", func() {
token, err := sanity.Checker.Client.Admin.GetServiceAccountToken("openshift-monitoring", "prometheus-k8s")
Expect(err).NotTo(HaveOccurred())
route, err := sanity.Checker.Client.Admin.RouteV1.Routes("openshift-monitoring").Get("prometheus-k8s", meta_v1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
req, err := http.NewRequest(http.MethodGet, "https://"+route.Spec.Host+"/api/v1/targets", nil)
Expect(err).NotTo(HaveOccurred())
req.Header.Add("Authorization", "Bearer "+string(token))
cli := &http.Client{
Transport: &http.Transport{
/* #nosec - connecting to internal API of a self created cluster */
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
resp, err := cli.Do(req)
Expect(err).NotTo(HaveOccurred())
defer resp.Body.Close()
Expect(resp.StatusCode).To(Equal(http.StatusOK))
var tr targetsResponse
d := json.NewDecoder(resp.Body)
err = d.Decode(&tr)
Expect(err).NotTo(HaveOccurred())
healthyTargets := map[string]int{}
for _, t := range tr.Data.ActiveTargets {
if t.Health == "up" {
healthyTargets[t.Labels["job"]]++
}
}
cs, err := azure.RPClient.OpenShiftManagedClusters.Get(context.Background(), os.Getenv("RESOURCEGROUP"), os.Getenv("RESOURCEGROUP"))
Expect(err).NotTo(HaveOccurred())
nodes, masters := int(*cs.Properties.MasterPoolProfile.Count), int(*cs.Properties.MasterPoolProfile.Count)
for _, app := range cs.Properties.AgentPoolProfiles {
nodes += int(*app.Count)
}
Expect(healthyTargets).To(Equal(map[string]int{
"alertmanager-main": 3,
"apiserver": masters,
"canary": 2,
"etcd-metrics": 3,
"router-stats": 3,
"cluster-monitoring-operator": 1,
"kube-controllers": masters,
"kube-state-metrics": 2,
"kubelet": nodes * 2,
"node-exporter": nodes,
"prometheus-k8s": 2,
"prometheus-operator": 1,
"sync": 1,
"azure-controllers": 1,
"metrics-bridge": 1,
}))
})
})