/
grafana_client.go
171 lines (147 loc) · 4.32 KB
/
grafana_client.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
package client
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/grafana/grafana-operator/v5/controllers/metrics"
v1 "k8s.io/api/core/v1"
genapi "github.com/grafana/grafana-openapi-client-go/client"
"github.com/grafana/grafana-operator/v5/api/v1beta1"
"github.com/grafana/grafana-operator/v5/controllers/config"
"github.com/grafana/grafana-operator/v5/controllers/model"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type grafanaAdminCredentials struct {
username string
password string
apikey string
}
func getAdminCredentials(ctx context.Context, c client.Client, grafana *v1beta1.Grafana) (*grafanaAdminCredentials, error) {
credentials := &grafanaAdminCredentials{}
getValueFromSecret := func(ref *v1.SecretKeySelector) ([]byte, error) {
secret := &v1.Secret{}
selector := client.ObjectKey{
Name: ref.Name,
Namespace: grafana.Namespace,
}
err := c.Get(ctx, selector, secret)
if err != nil {
return nil, err
}
if secret.Data == nil {
return nil, fmt.Errorf("empty credential secret: %v/%v", grafana.Namespace, ref.Name)
}
if val, ok := secret.Data[ref.Key]; ok {
return val, nil
}
return nil, fmt.Errorf("admin credentials not found: %v/%v", grafana.Namespace, ref.Name)
}
if grafana.IsExternal() {
// prefer api key if present
if grafana.Spec.External.ApiKey != nil {
apikey, err := getValueFromSecret(grafana.Spec.External.ApiKey)
if err != nil {
return nil, err
}
credentials.apikey = string(apikey)
return credentials, nil
}
// rely on username and password otherwise
username, err := getValueFromSecret(grafana.Spec.External.AdminUser)
if err != nil {
return nil, err
}
password, err := getValueFromSecret(grafana.Spec.External.AdminPassword)
if err != nil {
return nil, err
}
credentials.username = string(username)
credentials.password = string(password)
return credentials, nil
}
deployment := model.GetGrafanaDeployment(grafana, nil)
selector := client.ObjectKey{
Namespace: deployment.Namespace,
Name: deployment.Name,
}
err := c.Get(ctx, selector, deployment)
if err != nil {
return nil, err
}
for _, container := range deployment.Spec.Template.Spec.Containers {
for _, env := range container.Env {
if env.Name == config.GrafanaAdminUserEnvVar {
if env.Value != "" {
credentials.username = env.Value
continue
}
if env.ValueFrom != nil {
if env.ValueFrom.SecretKeyRef != nil {
usernameFromSecret, err := getValueFromSecret(env.ValueFrom.SecretKeyRef)
if err != nil {
return nil, err
}
credentials.username = string(usernameFromSecret)
}
}
}
if env.Name == config.GrafanaAdminPasswordEnvVar {
if env.Value != "" {
credentials.password = env.Value
continue
}
if env.ValueFrom != nil {
if env.ValueFrom.SecretKeyRef != nil {
passwordFromSecret, err := getValueFromSecret(env.ValueFrom.SecretKeyRef)
if err != nil {
return nil, err
}
credentials.password = string(passwordFromSecret)
}
}
}
}
}
return credentials, nil
}
func NewGeneratedGrafanaClient(ctx context.Context, c client.Client, grafana *v1beta1.Grafana) (*genapi.GrafanaHTTPAPI, error) {
var timeout time.Duration
if grafana.Spec.Client != nil && grafana.Spec.Client.TimeoutSeconds != nil {
timeout = time.Duration(*grafana.Spec.Client.TimeoutSeconds)
if timeout < 0 {
timeout = 0
}
} else {
timeout = 10
}
credentials, err := getAdminCredentials(ctx, c, grafana)
if err != nil {
return nil, err
}
gURL, err := url.Parse(grafana.Status.AdminUrl)
if err != nil {
return nil, fmt.Errorf("parsing url for client: %w", err)
}
transport := NewInstrumentedRoundTripper(grafana.Name, metrics.GrafanaApiRequests, grafana.IsExternal())
client := &http.Client{
Transport: transport,
Timeout: timeout * time.Second,
}
cfg := &genapi.TransportConfig{
Schemes: []string{gURL.Scheme},
BasePath: "/api",
Host: gURL.Host,
// APIKey is an optional API key or service account token.
APIKey: credentials.apikey,
// NumRetries contains the optional number of attempted retries
NumRetries: 0,
Client: client,
}
if credentials.username != "" {
cfg.BasicAuth = url.UserPassword(credentials.username, credentials.password)
}
cl := genapi.NewHTTPClientWithConfig(nil, cfg)
return cl, nil
}