/
mco_dashboard.go
72 lines (62 loc) · 1.5 KB
/
mco_dashboard.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
// Copyright (c) Red Hat, Inc.
// Copyright Contributors to the Open Cluster Management project
// Licensed under the Apache License 2.0
package utils
import (
"crypto/tls"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"k8s.io/klog"
)
func ContainDashboard(opt TestOptions, title string) (error, bool) {
grafanaConsoleURL := GetGrafanaURL(opt)
path := "/api/search?"
queryParams := url.PathEscape(fmt.Sprintf("query=%s", title))
req, err := http.NewRequest(
"GET",
grafanaConsoleURL+path+queryParams,
nil)
if err != nil {
return err, false
}
client := &http.Client{}
if os.Getenv("IS_KIND_ENV") != "true" {
tr := &http.Transport{
// #nosec G402 -- Used in test.
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client = &http.Client{Transport: tr}
token, err := FetchBearerToken(opt)
if err != nil {
return err, false
}
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
req.Host = opt.HubCluster.GrafanaHost
}
resp, err := client.Do(req)
if err != nil {
return err, false
}
if resp.StatusCode != http.StatusOK {
klog.Errorf("resp: %+v\n", resp)
klog.Errorf("err: %+v\n", err)
return errors.New("failed to access grafana api"), false
}
result, err := io.ReadAll(resp.Body)
klog.V(1).Infof("result: %s\n", result)
if err != nil {
return err, false
}
if !strings.Contains(string(result), fmt.Sprintf(`"title":"%s"`, title)) {
return errors.New("failed to find the dashboard"), false
} else {
return nil, true
}
}