forked from kyma-project/kyma
/
prometheus.go
481 lines (406 loc) · 13.5 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package backupe2e
// Test http request to the prometheus api before backup and after restore.
// An expected result at a specific point in time is considered a succeeded test.
//
// {
// "status":"success",
// "data":{
// "resultType":"vector",
// "result":[
// {
// "metric":{},
// "value":[
// 1551421406.195,
// "1.661"
// ]
// }
// ]
// }
// }
//
// {success {vector [{map[] [1.551424874014e+09 1.661]}]}}
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
v1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
prometheusClient "github.com/coreos/prometheus-operator/pkg/client/versioned"
"github.com/sirupsen/logrus"
"github.com/google/uuid"
"github.com/kyma-project/kyma/tests/end-to-end/backup-restore-test/utils/config"
. "github.com/smartystreets/goconvey/convey"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
const (
domain = "http://monitoring-prometheus.kyma-system"
prometheusNS = "kyma-system"
api = "/api/v1/query?"
metricsQuery = "max(sum(kube_pod_container_resource_requests_cpu_cores) by (instance))"
port = "9090"
metricName = "kube_pod_container_resource_requests_cpu_cores"
prometheusName = "monitoring"
prometheusPodName = "prometheus-monitoring-0"
prometheusServiceName = "monitoring-prometheus"
prometheusStatefulsetName = "prometheus-monitoring"
prometheusPvcName = "prometheus-monitoring-db-prometheus-monitoring-0"
prometheusLabelSelector = "app=prometheus"
)
type queryResponse struct {
Status string `json:"status"`
Data responseData `json:"data"`
}
type responseData struct {
ResultType string `json:"resultType"`
Result []dataResult `json:"result"`
}
type dataResult struct {
Metric interface{} `json:"metric,omitempty"`
Value []interface{} `json:"value,omitempty"`
}
type prometheusTest struct {
metricName, uuid string
coreClient *kubernetes.Clientset
prometheusClient *prometheusClient.Clientset
response queryResponse
beforeBackup bool
expectedResult string
finalResult string
apiQuery
pointInTime
log logrus.FieldLogger
}
type pointInTime struct {
floatValue float64
timeValue time.Time
formmattedValue string
}
type apiQuery struct {
domain string
prometheusNS string
api string
metricQuery string
port string
}
var backedupPrometheus *v1.Prometheus
func NewPrometheusTest() (*prometheusTest, error) {
restConfig, err := config.NewRestClientConfig()
if err != nil {
return &prometheusTest{}, err
}
coreClient, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return &prometheusTest{}, err
}
pClient, err := prometheusClient.NewForConfig(restConfig)
if err != nil {
return &prometheusTest{}, err
}
queryToApi := apiQuery{api: api, domain: domain, metricQuery: metricsQuery, port: port, prometheusNS: prometheusNS}
return &prometheusTest{
coreClient: coreClient,
prometheusClient: pClient,
metricName: metricName,
uuid: uuid.New().String(),
apiQuery: queryToApi,
beforeBackup: true,
log: logrus.WithField("test", "prometheus"),
}, nil
}
func (point *pointInTime) pointInTime(f float64) {
point.floatValue = f
t := time.Unix(int64(f), 0) //gives unix time stamp in utc
point.timeValue = t
point.formmattedValue = t.Format(time.RFC3339)
}
type Connector interface {
connectToPrometheusApi(domain, port, api, query, pointInTime string) error
}
func (qresp *queryResponse) connectToPrometheusApi(domain, port, api, query, pointInTime string) error {
values := url.Values{}
values.Set("query", query)
if pointInTime != "" {
values.Set("time", pointInTime)
}
uri := domain + ":" + port + api
client := &http.Client{
Timeout: 15 * time.Second,
}
url := uri + values.Encode()
resp, err := client.Get(url)
if err != nil {
return fmt.Errorf("http request to the api (%s) failed with '%s'", uri, err)
}
defer func() {
err := resp.Body.Close()
So(err, ShouldBeNil)
}()
body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Unable to get a reponse from the api. \n http response was '%s' (%d) and not OK (200). Body:\n%s\n", resp.Status, resp.StatusCode, string(body))
}
return qresp.decodeQueryResponse(body)
}
func whatIsThisThing(something interface{}) (float64, string, error) {
switch i := something.(type) {
case float64:
return float64(i), "", nil
case string:
return float64(0), string(i), nil
default:
return float64(0), "", errors.New("unknown value is of incompatible type")
}
}
func (qresp *queryResponse) decodeQueryResponse(jresponse []byte) error {
err := json.Unmarshal(jresponse, &qresp)
if err != nil {
return fmt.Errorf("http response can't be Unmarshal: %v", err)
}
return nil
}
func (pt *prometheusTest) CreateResources(namespace string) {
qresp := &queryResponse{}
err := qresp.connectToPrometheusApi(pt.domain, pt.port, pt.api, pt.metricQuery, "")
So(err, ShouldBeNil)
pt.response = *qresp
point := pointInTime{}
if len(qresp.Data.Result) > 0 && len(qresp.Data.Result[0].Value) > 0 {
values := qresp.Data.Result[0].Value
for _, something := range values {
f, s, err := whatIsThisThing(something)
So(err, ShouldBeNil)
if f != float64(0) {
point.pointInTime(f)
pt.pointInTime = point
}
if s != "" {
pt.expectedResult = s
}
}
}
}
func (pt *prometheusTest) TestResources(namespace string) {
if !pt.beforeBackup {
err := pt.deletePrometheus(prometheusNS, prometheusName)
So(err, ShouldBeNil)
err = pt.deletePod(prometheusNS, prometheusPodName, prometheusLabelSelector)
So(err, ShouldBeNil)
err = pt.createPrometheusFromSavedResource()
So(err, ShouldBeNil)
}
pt.beforeBackup = false
err := pt.waitForPodPrometheus(10 * time.Minute)
So(err, ShouldBeNil)
qresp := &queryResponse{}
timeout := time.After(2 * time.Minute)
tick := time.Tick(2 * time.Second)
timedout := false
done := false
for {
select {
case <-timeout:
timedout = true
pt.log.Infof("Timedout: while hitting Prometheus API: %v", err)
case <-tick:
err = qresp.connectToPrometheusApi(pt.domain, pt.port, pt.api, pt.metricQuery, pt.pointInTime.formmattedValue)
if err == nil {
done = true
}
}
if done || timedout {
break
}
}
So(err, ShouldBeNil)
if len(qresp.Data.Result) > 0 && len(qresp.Data.Result[0].Value) > 0 {
values := qresp.Data.Result[0].Value
for _, something := range values {
_, s, err := whatIsThisThing(something)
So(err, ShouldBeNil)
if s != "" {
pt.finalResult = s
}
}
}
So(strings.TrimSpace(pt.finalResult), ShouldEqual, strings.TrimSpace(pt.expectedResult))
}
func (pt *prometheusTest) DeleteResources(namespace string) {
// It needs to be implemented for this test.
err := pt.waitForPodPrometheus(1 * time.Minute)
So(err, ShouldBeNil)
err = pt.deleteServices(prometheusNS, prometheusServiceName, prometheusLabelSelector)
So(err, ShouldBeNil)
err = pt.deletePrometheus(prometheusNS, prometheusName)
So(err, ShouldBeNil)
err = pt.deletePod(prometheusNS, prometheusPodName, prometheusLabelSelector)
So(err, ShouldBeNil)
err = pt.deletePVC(prometheusNS, prometheusPvcName, prometheusLabelSelector)
So(err, ShouldBeNil)
}
func (pt *prometheusTest) waitForPodPrometheus(waitmax time.Duration) error {
timeout := time.After(waitmax)
tick := time.Tick(2 * time.Second)
for {
select {
case <-timeout:
pod, err := pt.coreClient.CoreV1().Pods(prometheusNS).Get(prometheusPodName, metav1.GetOptions{})
if err != nil {
return err
}
return fmt.Errorf("Pod did not start within given time %v: %+v", waitmax, pod)
case <-tick:
pod, err := pt.coreClient.CoreV1().Pods(prometheusNS).Get(prometheusPodName, metav1.GetOptions{})
pt.log.Info("Waiting for prometheus pod to be up!")
if err != nil && strings.Contains(err.Error(), "not found") {
// If Pod is not there, we hope the pod will come soon
break
} else if err != nil {
return fmt.Errorf("Error in fetching Prometheus pod %s: ", err.Error())
}
// If Pod condition is not ready the for will continue until timeout
if len(pod.Status.Conditions) > 0 {
conditions := pod.Status.Conditions
for _, cond := range conditions {
if cond.Type == corev1.PodReady && cond.Status == corev1.ConditionTrue {
pt.log.Info("Prometheus pod is ready!")
return nil
}
}
}
// Succeeded or Failed or Unknoen are taken as a error
if pod.Status.Phase == corev1.PodSucceeded || pod.Status.Phase == corev1.PodFailed || pod.Status.Phase == corev1.PodUnknown {
return fmt.Errorf("Prometheus in state %v: \n%+v", pod.Status.Phase, pod)
}
}
}
}
func (pt *prometheusTest) deleteServices(namespace, serviceName, labelSelector string) error {
deletePolicy := metav1.DeletePropagationForeground
serviceList, err := pt.coreClient.CoreV1().Services(namespace).List(metav1.ListOptions{LabelSelector: labelSelector})
if err != nil {
pt.log.Errorf("Error while listing SVC for Prometheus: %v", err)
return err
}
for _, service := range serviceList.Items {
if service.Name == serviceName {
err := pt.coreClient.CoreV1().Services(namespace).Delete(serviceName, &metav1.DeleteOptions{
PropagationPolicy: &deletePolicy,
})
if err != nil {
pt.log.Errorf("Error while deleting SVC for Prometheus: %v", err)
return err
}
}
}
return nil
}
func (pt *prometheusTest) savePrometheusResource(namespace, name string) error {
backedupPrometheusLocal, err := pt.prometheusClient.MonitoringV1().Prometheuses(namespace).Get(name, metav1.GetOptions{})
if err != nil {
return err
}
backedupPrometheus = backedupPrometheusLocal
return nil
}
func (pt *prometheusTest) createPrometheusFromSavedResource() error {
backedupPrometheus.ObjectMeta.ResourceVersion = ""
backedupPrometheus.Status = nil
backedupPrometheus.Generation = 0
prometheusAnnotations := backedupPrometheus.Annotations
backedupPrometheus.ObjectMeta = metav1.ObjectMeta{
Annotations: prometheusAnnotations,
Name: backedupPrometheus.Name,
Labels: backedupPrometheus.Labels,
Namespace: backedupPrometheus.Namespace,
CreationTimestamp: backedupPrometheus.CreationTimestamp,
}
podAnnotations := backedupPrometheus.Spec.PodMetadata.Annotations
backedupPrometheus.Spec.PodMetadata = &metav1.ObjectMeta{
Annotations: podAnnotations,
CreationTimestamp: backedupPrometheus.CreationTimestamp,
}
backedupPrometheus.Spec.Storage.VolumeClaimTemplate.ObjectMeta = metav1.ObjectMeta{
CreationTimestamp: backedupPrometheus.CreationTimestamp,
}
for {
_, err := pt.prometheusClient.MonitoringV1().Prometheuses(backedupPrometheus.Namespace).Get(backedupPrometheus.Name, metav1.GetOptions{})
if err != nil && strings.Contains(err.Error(), "not found") {
break
}
}
_, err := pt.prometheusClient.MonitoringV1().Prometheuses(backedupPrometheus.ObjectMeta.Namespace).Create(backedupPrometheus)
if err != nil {
pt.log.Errorf("Error while creating Prometheus CR created from saved resource: %v", err)
return err
}
pt.log.Infoln("Prometheus CR created from saved resource!")
return nil
}
func (pt *prometheusTest) deletePrometheus(namespace, name string) error {
pt.savePrometheusResource(namespace, name)
deletePolicy := metav1.DeletePropagationForeground
err := pt.prometheusClient.MonitoringV1().Prometheuses(namespace).Delete(name, &metav1.DeleteOptions{
PropagationPolicy: &deletePolicy,
})
if err != nil {
pt.log.Errorf("Error while deleting Prometheus CR: %v", err)
return err
}
pt.log.Info("Deletion of Prometheus: monitoring is successful!")
return nil
}
func (pt *prometheusTest) deleteStatefulset(namespace, statefulsetName string) error {
deletePolicy := metav1.DeletePropagationForeground
collection := pt.coreClient.AppsV1().StatefulSets(namespace)
err := collection.Delete(statefulsetName, &metav1.DeleteOptions{
PropagationPolicy: &deletePolicy,
})
if err != nil {
pt.log.Errorf("Error while deleting monitoring sts: %v", err)
return err
}
pt.log.Info("Deletion of sts: monitoring is successful!")
return nil
}
func (pt *prometheusTest) deletePod(namespace, podName, labelSelector string) error {
podList, err := pt.coreClient.CoreV1().Pods(namespace).List(metav1.ListOptions{LabelSelector: labelSelector})
if err != nil {
pt.log.Errorf("Error while listing prometheus pods: %v", err)
return err
}
for _, pod := range podList.Items {
if pod.Name == podName {
// Delete Pod
err = pt.coreClient.CoreV1().Pods(namespace).Delete(podName, &metav1.DeleteOptions{})
if err != nil {
pt.log.Errorf("Error while deleting prometheus pods: %v", err)
return err
}
}
}
return nil
}
func (pt *prometheusTest) deletePVC(namespace, pvcName, labelSelector string) error {
pvcList, err := pt.coreClient.CoreV1().PersistentVolumeClaims(namespace).List(metav1.ListOptions{LabelSelector: labelSelector})
if err != nil {
pt.log.Errorf("Error while listing PVC for prometheus: %v", err)
return err
}
for _, pvc := range pvcList.Items {
if pvc.Name == pvcName {
err = pt.coreClient.CoreV1().PersistentVolumeClaims(namespace).Delete(pvcName, &metav1.DeleteOptions{})
if err != nil {
pt.log.Errorf("Error while deleting PVC for prometheus: %v", err)
return err
}
pt.log.Infof("Deletion of pvc: %v is successful!", pvcName)
}
}
return nil
}