-
Notifications
You must be signed in to change notification settings - Fork 2
/
grafanadashboard_types.go
124 lines (101 loc) · 3.73 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
package v1alpha1
import (
"crypto/sha1"
"crypto/sha256"
"encoding/json"
"fmt"
"io"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const GrafanaDashboardKind = "GrafanaDashboard"
// 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 {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
Json string `json:"json"`
Jsonnet string `json:"jsonnet"`
Name string `json:"name"`
Plugins PluginList `json:"plugins,omitempty"`
Url string `json:"url,omitempty"`
ConfigMapRef *corev1.ConfigMapKeySelector `json:"configMapRef,omitempty"`
Datasources []GrafanaDashboardDatasource `json:"datasources,omitempty"`
CustomFolderName string `json:"customFolderName,omitempty"`
}
type GrafanaDashboardDatasource struct {
InputName string `json:"inputName"`
DatasourceName string `json:"datasourceName"`
}
// Used to keep a dashboard reference without having access to the dashboard
// struct itself
type GrafanaDashboardRef struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
UID string `json:"uid"`
Hash string `json:"hash"`
FolderId *int64 `json:"folderId"`
FolderName string `json:"folderName"`
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// GrafanaDashboard is the Schema for the grafanadashboards API
// +k8s:openapi-gen=true
type GrafanaDashboard struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec GrafanaDashboardSpec `json:"spec,omitempty"`
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// GrafanaDashboardList contains a list of GrafanaDashboard
type GrafanaDashboardList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []GrafanaDashboard `json:"items"`
}
type GrafanaDashboardStatusMessage struct {
Message string `json:"message"`
Timestamp string `json:"timestamp"`
}
func init() {
SchemeBuilder.Register(&GrafanaDashboard{}, &GrafanaDashboardList{})
}
func (d *GrafanaDashboard) Hash() string {
hash := sha256.New()
for _, input := range d.Spec.Datasources {
io.WriteString(hash, input.DatasourceName)
io.WriteString(hash, input.InputName)
}
io.WriteString(hash, d.Spec.Json)
io.WriteString(hash, d.Spec.Url)
io.WriteString(hash, d.Spec.Jsonnet)
io.WriteString(hash, d.Namespace)
io.WriteString(hash, d.Spec.CustomFolderName)
if d.Spec.ConfigMapRef != nil {
io.WriteString(hash, d.Spec.ConfigMapRef.Name)
io.WriteString(hash, d.Spec.ConfigMapRef.Key)
}
return fmt.Sprintf("%x", hash.Sum(nil))
}
func (d *GrafanaDashboard) Parse(optional string) (map[string]interface{}, error) {
var dashboardBytes = []byte(d.Spec.Json)
if optional != "" {
dashboardBytes = []byte(optional)
}
var parsed = make(map[string]interface{})
err := json.Unmarshal(dashboardBytes, &parsed)
return parsed, err
}
func (d *GrafanaDashboard) UID() string {
content, err := d.Parse("")
if err == nil {
// Check if the user has defined an uid and if that's the
// case, use that
if content["uid"] != nil && content["uid"] != "" {
return content["uid"].(string)
}
}
// Use sha1 to keep the hash limit at 40 bytes which is what
// Grafana allows for UIDs
return fmt.Sprintf("%x", sha1.Sum([]byte(d.Namespace+d.Name)))
}