/
grafanadashboard_types.go
321 lines (253 loc) · 9.08 KB
/
grafanadashboard_types.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
/*
Copyright 2022.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1beta1
import (
"bytes"
"compress/gzip"
"io"
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type DashboardSourceType string
const (
DashboardSourceTypeRawJson DashboardSourceType = "json"
DashboardSourceTypeGzipJson DashboardSourceType = "gzipJson"
DashboardSourceJsonnetProject DashboardSourceType = "jsonnetProjectWithRuntimeRaw"
DashboardSourceTypeUrl DashboardSourceType = "url"
DashboardSourceTypeJsonnet DashboardSourceType = "jsonnet"
DashboardSourceTypeGrafanaCom DashboardSourceType = "grafana"
DashboardSourceConfigMap DashboardSourceType = "configmap"
DefaultResyncPeriod = "5m"
)
type GrafanaDashboardDatasource struct {
InputName string `json:"inputName"`
DatasourceName string `json:"datasourceName"`
}
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
// GrafanaDashboardSpec defines the desired state of GrafanaDashboard
type GrafanaDashboardSpec struct {
// dashboard json
// +optional
Json string `json:"json,omitempty"`
// GzipJson the dashboard's JSON compressed with Gzip. Base64-encoded when in YAML.
// +optional
GzipJson []byte `json:"gzipJson,omitempty"`
// dashboard url
// +optional
Url string `json:"url,omitempty"`
// Jsonnet
// +optional
Jsonnet string `json:"jsonnet,omitempty"`
// Jsonnet project build
JsonnetProjectBuild *JsonnetProjectBuild `json:"jsonnetLib,omitempty"`
// grafana.com/dashboards
// +optional
GrafanaCom *GrafanaComDashboardReference `json:"grafanaCom,omitempty"`
// dashboard from configmap
// +optional
ConfigMapRef *v1.ConfigMapKeySelector `json:"configMapRef,omitempty"`
// selects Grafanas for import
InstanceSelector *metav1.LabelSelector `json:"instanceSelector"`
// folder assignment for dashboard
// +optional
FolderTitle string `json:"folder,omitempty"`
// plugins
// +optional
Plugins PluginList `json:"plugins,omitempty"`
// Cache duration for dashboards fetched from URLs
// +optional
ContentCacheDuration metav1.Duration `json:"contentCacheDuration,omitempty"`
// how often the dashboard is refreshed, defaults to 5m if not set
// +optional
ResyncPeriod string `json:"resyncPeriod,omitempty"`
// maps required data sources to existing ones
// +optional
Datasources []GrafanaDashboardDatasource `json:"datasources,omitempty"`
// allow to import this resources from an operator in a different namespace
// +optional
AllowCrossNamespaceImport *bool `json:"allowCrossNamespaceImport,omitempty"`
// environments variables as a map
// +optional
Envs []GrafanaDashboardEnv `json:"envs,omitempty"`
// environments variables from secrets or config maps
// +optional
EnvsFrom []GrafanaDashboardEnvFromSource `json:"envFrom,omitempty"`
}
type GrafanaDashboardEnv struct {
Name string `json:"name"`
// Inline evn value
// +optional
Value string `json:"value,omitempty"`
// Reference on value source, might be the reference on a secret or config map
// +optional
ValueFrom GrafanaDashboardEnvFromSource `json:"valueFrom,omitempty"`
}
type GrafanaDashboardEnvFromSource struct {
// Selects a key of a ConfigMap.
// +optional
ConfigMapKeyRef *v1.ConfigMapKeySelector `json:"configMapKeyRef,omitempty"`
// Selects a key of a Secret.
// +optional
SecretKeyRef *v1.SecretKeySelector `json:"secretKeyRef,omitempty"`
}
type JsonnetProjectBuild struct {
JPath []string `json:"jPath,omitempty"`
FileName string `json:"fileName"`
GzipJsonnetProject []byte `json:"gzipJsonnetProject"`
}
// GrafanaComDashbooardReference is a reference to a dashboard on grafana.com/dashboards
type GrafanaComDashboardReference struct {
Id int `json:"id"`
Revision *int `json:"revision,omitempty"`
}
// GrafanaDashboardStatus defines the observed state of GrafanaDashboard
type GrafanaDashboardStatus struct {
ContentCache []byte `json:"contentCache,omitempty"`
ContentTimestamp metav1.Time `json:"contentTimestamp,omitempty"`
ContentUrl string `json:"contentUrl,omitempty"`
Hash string `json:"hash,omitempty"`
// The dashboard instanceSelector can't find matching grafana instances
NoMatchingInstances bool `json:"NoMatchingInstances,omitempty"`
// Last time the dashboard was resynced
LastResync metav1.Time `json:"lastResync,omitempty"`
UID string `json:"uid,omitempty"`
}
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
// GrafanaDashboard is the Schema for the grafanadashboards API
// +kubebuilder:printcolumn:name="No matching instances",type="boolean",JSONPath=".status.NoMatchingInstances",description=""
// +kubebuilder:printcolumn:name="Last resync",type="date",format="date-time",JSONPath=".status.lastResync",description=""
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description=""
type GrafanaDashboard struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec GrafanaDashboardSpec `json:"spec,omitempty"`
Status GrafanaDashboardStatus `json:"status,omitempty"`
}
//+kubebuilder:object:root=true
// GrafanaDashboardList contains a list of GrafanaDashboard
type GrafanaDashboardList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []GrafanaDashboard `json:"items"`
}
func (in *GrafanaDashboard) Unchanged(hash string) bool {
return in.Status.Hash == hash
}
func (in *GrafanaDashboard) ResyncPeriodHasElapsed() bool {
deadline := in.Status.LastResync.Add(in.GetResyncPeriod())
return time.Now().After(deadline)
}
func (in *GrafanaDashboard) GetResyncPeriod() time.Duration {
if in.Spec.ResyncPeriod == "" {
in.Spec.ResyncPeriod = DefaultResyncPeriod
return in.GetResyncPeriod()
}
duration, err := time.ParseDuration(in.Spec.ResyncPeriod)
if err != nil {
in.Spec.ResyncPeriod = DefaultResyncPeriod
return in.GetResyncPeriod()
}
return duration
}
func (in *GrafanaDashboard) GetSourceTypes() []DashboardSourceType {
var sourceTypes []DashboardSourceType
if in.Spec.Json != "" {
sourceTypes = append(sourceTypes, DashboardSourceTypeRawJson)
}
if in.Spec.GzipJson != nil {
sourceTypes = append(sourceTypes, DashboardSourceTypeGzipJson)
}
if in.Spec.Url != "" {
sourceTypes = append(sourceTypes, DashboardSourceTypeUrl)
}
if in.Spec.Jsonnet != "" {
sourceTypes = append(sourceTypes, DashboardSourceTypeJsonnet)
}
if in.Spec.GrafanaCom != nil {
sourceTypes = append(sourceTypes, DashboardSourceTypeGrafanaCom)
}
if in.Spec.ConfigMapRef != nil {
sourceTypes = append(sourceTypes, DashboardSourceConfigMap)
}
if in.Spec.JsonnetProjectBuild != nil {
sourceTypes = append(sourceTypes, DashboardSourceJsonnetProject)
}
return sourceTypes
}
func (in *GrafanaDashboard) GetContentCache() []byte {
return in.Status.getContentCache(in.Spec.Url, in.Spec.ContentCacheDuration.Duration)
}
// getContentCache returns content cache when the following conditions are met: url is the same, data is not expired, gzipped data is not corrupted
func (in *GrafanaDashboardStatus) getContentCache(url string, cacheDuration time.Duration) []byte {
if in.ContentUrl != url {
return []byte{}
}
notExpired := cacheDuration <= 0 || in.ContentTimestamp.Add(cacheDuration).After(time.Now())
if !notExpired {
return []byte{}
}
cache, err := Gunzip(in.ContentCache)
if err != nil {
return []byte{}
}
return cache
}
func (in *GrafanaDashboard) IsAllowCrossNamespaceImport() bool {
if in.Spec.AllowCrossNamespaceImport != nil {
return *in.Spec.AllowCrossNamespaceImport
}
return false
}
func (in *GrafanaDashboard) IsUpdatedUID(uid string) bool {
// Dashboard has just been created, status is not yet updated
if in.Status.UID == "" {
return false
}
if uid == "" {
uid = string(in.UID)
}
return in.Status.UID != uid
}
func Gunzip(compressed []byte) ([]byte, error) {
gz, err := gzip.NewReader(bytes.NewReader(compressed))
if err != nil {
return nil, err
}
return io.ReadAll(gz)
}
func Gzip(content []byte) ([]byte, error) {
buf := bytes.NewBuffer([]byte{})
gz := gzip.NewWriter(buf)
_, err := gz.Write(content)
if err != nil {
return nil, err
}
if err := gz.Close(); err != nil {
return nil, err
}
return io.ReadAll(buf)
}
func (in *GrafanaDashboardList) Find(namespace string, name string) *GrafanaDashboard {
for _, dashboard := range in.Items {
if dashboard.Namespace == namespace && dashboard.Name == name {
return &dashboard
}
}
return nil
}
func init() {
SchemeBuilder.Register(&GrafanaDashboard{}, &GrafanaDashboardList{})
}