-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathmonitoring.go
73 lines (64 loc) · 2.18 KB
/
monitoring.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
package test
import (
"context"
"crypto/tls"
"fmt"
"net/http"
prom "github.com/prometheus/client_golang/api"
promv1 "github.com/prometheus/client_golang/api/prometheus/v1"
authenticationv1 "k8s.io/api/authentication/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
kubeclient "knative.dev/pkg/client/injection/kube/client"
"knative.dev/pkg/injection/clients/dynamicclient"
)
type authRoundtripper struct {
authorization string
inner http.RoundTripper
}
func (a *authRoundtripper) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set("Authorization", a.authorization)
return a.inner.RoundTrip(r)
}
func NewPrometheusClient(ctx context.Context) (promv1.API, error) {
host, err := getPrometheusHost(ctx)
if err != nil {
return nil, err
}
bToken, err := getBearerTokenForPrometheusAccount(ctx)
if err != nil {
return nil, err
}
rt := prom.DefaultRoundTripper.(*http.Transport).Clone()
rt.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client, err := prom.NewClient(prom.Config{
Address: "https://" + host,
RoundTripper: &authRoundtripper{
authorization: fmt.Sprintf("Bearer %s", bToken),
inner: rt,
},
})
if err != nil {
return nil, err
}
return promv1.NewAPI(client), nil
}
func getPrometheusHost(ctx context.Context) (string, error) {
routeGVR := schema.GroupVersionResource{Group: "route.openshift.io", Version: "v1", Resource: "routes"}
route, err := dynamicclient.Get(ctx).Resource(routeGVR).Namespace("openshift-monitoring").
Get(ctx, "prometheus-k8s", metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("unable to get route: %w", err)
}
host, _, _ := unstructured.NestedString(route.Object, "spec", "host")
return host, nil
}
func getBearerTokenForPrometheusAccount(ctx context.Context) (string, error) {
token, err := kubeclient.Get(ctx).CoreV1().ServiceAccounts("openshift-monitoring").
CreateToken(context.Background(), "prometheus-k8s", &authenticationv1.TokenRequest{}, metav1.CreateOptions{})
if err != nil {
return "", fmt.Errorf("failed to create prometheus token: %w", err)
}
return token.Status.Token, nil
}