forked from grafana/grafana-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller_config.go
283 lines (246 loc) · 7.82 KB
/
controller_config.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
package config
import (
"fmt"
"sync"
"time"
"github.com/grafana-operator/grafana-operator/v4/api/integreatly/v1alpha1"
)
const (
ConfigGrafanaImage = "grafana.image.url"
ConfigGrafanaImageTag = "grafana.image.tag"
ConfigPluginsInitContainerImage = "grafana.plugins.init.container.image.url"
ConfigPluginsInitContainerTag = "grafana.plugins.init.container.image.tag"
ConfigOperatorNamespace = "grafana.operator.namespace"
ConfigDashboardLabelSelector = "grafana.dashboard.selector"
ConfigOpenshift = "mode.openshift"
ConfigJsonnetBasePath = "grafonnet.location"
GrafanaDataPath = "/var/lib/grafana"
GrafanaLogsPath = "/var/log/grafana"
GrafanaPluginsPath = "/var/lib/grafana/plugins"
GrafanaProvisioningPath = "/etc/grafana/provisioning/"
GrafanaProvisioningPluginsPath = "/etc/grafana/provisioning/plugins"
GrafanaProvisioningDashboardsPath = "/etc/grafana/provisioning/dashboards"
GrafanaProvisioningNotifiersPath = "/etc/grafana/provisioning/notifiers"
PluginsInitContainerImage = "quay.io/grafana-operator/grafana_plugins_init"
PluginsInitContainerTag = "0.1.0"
PluginsUrl = "https://grafana.com/api/plugins/%s/versions/%s"
SecretsMountDir = "/etc/grafana-secrets/" // #nosec G101
ConfigMapsMountDir = "/etc/grafana-configmaps/"
ConfigRouteWatch = "watch.routes"
ConfigGrafanaDashboardsSynced = "grafana.dashboards.synced"
ConfigGrafanaFoldersSynced = "grafana.dashboard.folders.synced"
ConfigGrafanaNotificationChannelsSynced = "grafana.notificationchannels.synced"
JsonnetBasePath = "/opt/jsonnet"
)
type ControllerConfig struct {
*sync.Mutex
Values map[string]interface{}
Plugins map[string]v1alpha1.PluginList
Dashboards map[string][]*v1alpha1.GrafanaDashboardRef
Folders map[string][]*v1alpha1.GrafanaFolderRef
RequeueDelay time.Duration
}
var instance *ControllerConfig
var once sync.Once
func GetControllerConfig() *ControllerConfig {
once.Do(func() {
instance = &ControllerConfig{
Mutex: &sync.Mutex{},
Values: map[string]interface{}{},
Plugins: map[string]v1alpha1.PluginList{},
Dashboards: map[string][]*v1alpha1.GrafanaDashboardRef{},
Folders: map[string][]*v1alpha1.GrafanaFolderRef{},
RequeueDelay: time.Second * 10,
}
})
return instance
}
func (c *ControllerConfig) GetDashboardId(namespace, name string) string {
return fmt.Sprintf("%v/%v", namespace, name)
}
func (c *ControllerConfig) GetAllPlugins() v1alpha1.PluginList {
c.Lock()
defer c.Unlock()
var plugins v1alpha1.PluginList
for _, v := range GetControllerConfig().Plugins {
plugins = append(plugins, v...)
}
return plugins
}
func (c *ControllerConfig) GetPluginsFor(dashboard *v1alpha1.GrafanaDashboard) v1alpha1.PluginList {
c.Lock()
defer c.Unlock()
return c.Plugins[c.GetDashboardId(dashboard.Namespace, dashboard.Name)]
}
func (c *ControllerConfig) SetPluginsFor(dashboard *v1alpha1.GrafanaDashboard) {
id := c.GetDashboardId(dashboard.Namespace, dashboard.Name)
c.Lock()
defer c.Unlock()
c.Plugins[id] = dashboard.Spec.Plugins
}
func (c *ControllerConfig) RemovePluginsFor(dashboard *v1alpha1.GrafanaDashboardRef) {
id := c.GetDashboardId(dashboard.Namespace, dashboard.Name)
delete(c.Plugins, id)
}
func (c *ControllerConfig) AddFolder(folder *v1alpha1.GrafanaFolder) {
namespace := folder.Namespace
c.Lock()
defer c.Unlock()
if i, exists := c.HasFolder(namespace, folder.Spec.FolderName); !exists {
c.Folders[namespace] = append(c.Folders[namespace], &v1alpha1.GrafanaFolderRef{
Name: folder.Spec.FolderName,
Namespace: namespace,
Hash: folder.Hash(),
})
} else {
c.Folders[namespace][i] = &v1alpha1.GrafanaFolderRef{
Name: folder.Spec.FolderName,
Namespace: namespace,
Hash: folder.Hash(),
}
}
}
func (c *ControllerConfig) AddDashboard(dashboard *v1alpha1.GrafanaDashboard, folderId *int64, folderName string) {
ns := dashboard.Namespace
if i, exists := c.HasDashboard(ns, dashboard.UID()); !exists {
c.Lock()
defer c.Unlock()
c.Dashboards[ns] = append(c.Dashboards[ns], &v1alpha1.GrafanaDashboardRef{
Name: dashboard.Name,
Namespace: ns,
UID: dashboard.UID(),
Hash: dashboard.Hash(),
FolderId: folderId,
FolderName: dashboard.Spec.CustomFolderName,
})
} else {
c.Lock()
defer c.Unlock()
c.Dashboards[ns][i] = &v1alpha1.GrafanaDashboardRef{
Name: dashboard.Name,
Namespace: ns,
UID: dashboard.UID(),
Hash: dashboard.Hash(),
FolderId: folderId,
FolderName: folderName,
}
}
}
func (c *ControllerConfig) HasFolder(namespace, name string) (int, bool) {
for i, folder := range c.Folders[namespace] {
if folder.Name == name {
return i, true
}
}
return -1, false
}
func (c *ControllerConfig) HasDashboard(ns, uid string) (int, bool) {
for i, v := range c.Dashboards[ns] {
if v.UID == uid {
return i, true
}
}
return -1, false
}
func (c *ControllerConfig) InvalidateDashboards() {
c.Lock()
defer c.Unlock()
for _, ds := range c.Dashboards {
for _, v := range ds {
v.Hash = ""
}
}
}
func (c *ControllerConfig) RemoveDashboard(hash string) {
for ns := range c.Dashboards {
if i, exists := c.HasDashboard(ns, hash); exists {
c.Lock()
defer c.Unlock()
list := c.Dashboards[ns]
list[i] = list[len(list)-1]
list = list[:len(list)-1]
c.Dashboards[ns] = list
}
}
}
func (c *ControllerConfig) GetFolders(namespace string) []*v1alpha1.GrafanaFolderRef {
c.Lock()
defer c.Unlock()
if c.Folders[namespace] != nil {
return c.Folders[namespace]
}
folders := []*v1alpha1.GrafanaFolderRef{}
if namespace == "" {
for _, folder := range c.Folders {
folders = append(folders, folder...)
}
}
return folders
}
func (c *ControllerConfig) GetDashboards(namespace string) []*v1alpha1.GrafanaDashboardRef {
c.Lock()
defer c.Unlock()
if c.Dashboards[namespace] != nil {
return c.Dashboards[namespace]
}
dashboards := []*v1alpha1.GrafanaDashboardRef{}
// The periodic resync in grafanadashboard.GrafanaDashboardReconciler rely on the convention
// that an empty namespace means all of them, so we follow that rule here.
if namespace == "" {
for _, ds := range c.Dashboards {
dashboards = append(dashboards, ds...)
}
return dashboards
}
return dashboards
}
func (c *ControllerConfig) AddConfigItem(key string, value interface{}) {
c.Lock()
defer c.Unlock()
if key != "" && value != nil && value != "" {
c.Values[key] = value
}
}
func (c *ControllerConfig) RemoveConfigItem(key string) {
c.Lock()
defer c.Unlock()
delete(c.Values, key)
}
func (c *ControllerConfig) GetConfigItem(key string, defaultValue interface{}) interface{} {
if c.HasConfigItem(key) {
return c.Values[key]
}
return defaultValue
}
func (c *ControllerConfig) GetConfigString(key, defaultValue string) string {
if c.HasConfigItem(key) {
return c.Values[key].(string)
}
return defaultValue
}
func (c *ControllerConfig) GetConfigBool(key string, defaultValue bool) bool {
if c.HasConfigItem(key) {
return c.Values[key].(bool)
}
return defaultValue
}
func (c *ControllerConfig) GetConfigTimestamp(key string, defaultValue time.Time) time.Time {
if c.HasConfigItem(key) {
return c.Values[key].(time.Time)
}
return defaultValue
}
func (c *ControllerConfig) HasConfigItem(key string) bool {
c.Lock()
defer c.Unlock()
_, ok := c.Values[key]
return ok
}
func (c *ControllerConfig) Cleanup(plugins bool) {
c.Lock()
defer c.Unlock()
c.Dashboards = map[string][]*v1alpha1.GrafanaDashboardRef{}
if plugins {
c.Plugins = map[string]v1alpha1.PluginList{}
}
}