/
grafana_controller.go
471 lines (390 loc) · 14.7 KB
/
grafana_controller.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package grafana
import (
"context"
"fmt"
i8ly "github.com/integr8ly/grafana-operator/pkg/apis/integreatly/v1alpha1"
"github.com/integr8ly/grafana-operator/pkg/controller/common"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/source"
)
var log = logf.Log.WithName("controller_grafana")
const (
PhaseConfigFiles int = iota
PhaseInstallGrafana
PhaseDone
PhaseReconcile
)
const OpenShiftOAuthRedirect = "serviceaccounts.openshift.io/oauth-redirectreference.primary"
/**
* USER ACTION REQUIRED: This is a scaffold file intended for the user to modify with their own Controller
* business logic. Delete these comments after modifying this file.*
*/
// Add creates a new Grafana Controller and adds it to the Manager. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager) error {
return add(mgr, newReconciler(mgr))
}
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
return &ReconcileGrafana{
client: mgr.GetClient(),
scheme: mgr.GetScheme(),
helper: common.NewKubeHelper(),
plugins: newPluginsHelper(),
config: common.GetControllerConfig(),
}
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {
// Create a new controller
c, err := controller.New("grafana-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
// Watch for changes to primary resource Grafana
return c.Watch(&source.Kind{Type: &i8ly.Grafana{}}, &handler.EnqueueRequestForObject{})
}
var _ reconcile.Reconciler = &ReconcileGrafana{}
// ReconcileGrafana reconciles a Grafana object
type ReconcileGrafana struct {
// This client, initialized using mgr.Client() above, is a split client
// that reads objects from the cache and writes to the apiserver
client client.Client
scheme *runtime.Scheme
helper *common.KubeHelperImpl
plugins *PluginsHelperImpl
config *common.ControllerConfig
}
// Reconcile reads that state of the cluster for a Grafana object and makes changes based on the state read
// and what is in the Grafana.Spec
func (r *ReconcileGrafana) Reconcile(request reconcile.Request) (reconcile.Result, error) {
instance := &i8ly.Grafana{}
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
// Stop the dashboard controller from reconciling when grafana is not installed
r.config.RemoveConfigItem(common.ConfigDashboardLabelSelector)
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
}
cr := instance.DeepCopy()
switch cr.Status.Phase {
case PhaseConfigFiles:
return r.createConfigFiles(cr)
case PhaseInstallGrafana:
return r.installGrafana(cr)
case PhaseDone:
log.Info("Grafana installation complete")
return r.updatePhase(cr, PhaseReconcile)
case PhaseReconcile:
return r.ReconcileGrafana(cr)
}
return reconcile.Result{}, nil
}
// ReconcileGrafana is constantly reconcile the grafana config and plugins
func (r *ReconcileGrafana) ReconcileGrafana(cr *i8ly.Grafana) (reconcile.Result, error) {
// Update the label selector and make it available to the dashboard controller
r.config.AddConfigItem(common.ConfigDashboardLabelSelector, cr.Spec.DashboardLabelSelector)
// Config updated?
newConfig := NewIniConfig(cr)
if err := newConfig.Build(); err != nil {
return reconcile.Result{}, err
}
if newConfig.DiffersFrom(cr.Status.LastConfig) {
if err := r.helper.UpdateGrafanaConfig(newConfig.Contents, cr); err != nil {
return reconcile.Result{}, err
}
// Store the new config hash
cr.Status.LastConfig = newConfig.Hash
if err := r.client.Update(context.TODO(), cr); err != nil {
return reconcile.Result{}, err
}
// Grafana needs to be restarted after a config change
if err := r.helper.UpdateGrafanaDeployment(cr.Status.LastConfig); err != nil {
return reconcile.Result{}, err
}
log.Info("grafana updated configuration hash due to config change")
// Skip plugins reconciliation while grafana is restarting
return reconcile.Result{RequeueAfter: common.RequeueDelay}, nil
}
// Plugins updated?
if err := r.ReconcileDashboardPlugins(cr); err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{RequeueAfter: common.RequeueDelay}, nil
}
// ReconcileDashboardPlugins is responsible for reconciling the grafana dashboards
func (r *ReconcileGrafana) ReconcileDashboardPlugins(cr *i8ly.Grafana) error {
// Waited long enough for dashboards to be ready?
if r.plugins.CanUpdatePlugins() == false {
return nil
}
// Fetch all plugins of all dashboards
var requestedPlugins i8ly.PluginList
for _, v := range common.GetControllerConfig().Plugins {
requestedPlugins = append(requestedPlugins, v...)
}
// Consolidate plugins and create the new list of plugin requirements
// If 'updated' is false then no changes have to be applied
filteredPlugins, updated := r.plugins.FilterPlugins(cr, requestedPlugins)
if updated {
r.ReconcilePlugins(cr, filteredPlugins)
// Update the dashboards that had their plugins modified
// to let the owners know about the status
err := r.updateDashboardMessages(filteredPlugins)
if err != nil {
return err
}
}
return nil
}
func (r *ReconcileGrafana) ReconcilePlugins(cr *i8ly.Grafana, plugins []i8ly.GrafanaPlugin) error {
var validPlugins []i8ly.GrafanaPlugin
var failedPlugins []i8ly.GrafanaPlugin
for _, plugin := range plugins {
if r.plugins.PluginExists(plugin) == false {
log.Info(fmt.Sprintf("invalid plugin: %s@%s", plugin.Name, plugin.Version))
failedPlugins = append(failedPlugins, plugin)
continue
}
log.Info(fmt.Sprintf("installing plugin: %s@%s", plugin.Name, plugin.Version))
validPlugins = append(validPlugins, plugin)
}
cr.Status.InstalledPlugins = validPlugins
cr.Status.FailedPlugins = failedPlugins
err := r.client.Update(context.TODO(), cr)
if err != nil {
return err
}
newEnv := r.plugins.BuildEnv(cr)
err = r.helper.UpdateGrafanaInitContainersDeployment(newEnv)
return err
}
func (r *ReconcileGrafana) updateDashboardMessages(plugins i8ly.PluginList) error {
for _, plugin := range plugins {
err := r.client.Update(context.TODO(), plugin.Origin)
if err != nil {
return err
}
}
return nil
}
// Initially create the config map that contains grafana.ini
func (r *ReconcileGrafana) createGrafanaConfig(cr *i8ly.Grafana) error {
grafanaIni := NewIniConfig(cr)
err := grafanaIni.Build()
if err != nil {
return err
}
configMap := core.ConfigMap{}
configMap.ObjectMeta = v1.ObjectMeta{
Name: common.GrafanaConfigMapName,
Namespace: cr.Namespace,
}
configMap.Data = map[string]string{}
configMap.Data[common.GrafanaConfigFileName] = grafanaIni.Contents
err = controllerutil.SetControllerReference(cr, &configMap, r.scheme)
if err != nil {
return err
}
cr.Status.LastConfig = grafanaIni.Hash
err = r.client.Create(context.TODO(), &configMap)
if err != nil {
// This might be called multiple times if creating one of the other config
// resources fails
if errors.IsAlreadyExists(err) {
return nil
}
}
return err
}
func (r *ReconcileGrafana) createConfigFiles(cr *i8ly.Grafana) (reconcile.Result, error) {
log.Info("Phase: Create Config Files")
ingressType := common.GrafanaIngressName
if common.GetControllerConfig().GetConfigBool(common.ConfigOpenshift, false) == true {
ingressType = common.GrafanaRouteName
}
err := r.createServiceAccount(cr, common.GrafanaServiceAccountName)
if err != nil {
return reconcile.Result{}, err
}
err = r.createGrafanaConfig(cr)
if err != nil {
return reconcile.Result{}, err
}
resources := []string{
common.GrafanaDashboardsConfigMapName,
common.GrafanaProvidersConfigMapName,
common.GrafanaDatasourcesConfigMapName,
common.GrafanaServiceName,
}
// Ingress / Route has to be enabled
if cr.Spec.Ingress.Enabled {
resources = append(resources, ingressType)
}
for _, resourceName := range resources {
log.Info(fmt.Sprintf("Creating the %s resource", resourceName))
if err := r.createResource(cr, resourceName); err != nil {
log.Info(fmt.Sprintf("Error in CreateConfigFiles, resourceName=%s : err=%s", resourceName, err))
// Requeue so it can be attempted again
return reconcile.Result{}, err
}
}
log.Info("Config files created")
return r.updatePhase(cr, PhaseInstallGrafana)
}
func (r *ReconcileGrafana) installGrafana(cr *i8ly.Grafana) (reconcile.Result, error) {
log.Info("Phase: Install Grafana")
if err := r.createDeployment(cr, common.GrafanaDeploymentName); err != nil {
return reconcile.Result{}, err
}
return r.updatePhase(cr, PhaseDone)
}
// Creates the deployment from the template and injects any specified extra containers before
// submitting it
func (r *ReconcileGrafana) createDeployment(cr *i8ly.Grafana, resourceName string) error {
resourceHelper := newResourceHelper(cr)
resource, err := resourceHelper.createResource(resourceName)
if err != nil {
return err
}
rawResource := newUnstructuredResourceMap(resource.(*unstructured.Unstructured))
var extraVolumeMounts []interface{}
// Extra secrets to be added as volumes?
if len(cr.Spec.Secrets) > 0 {
volumes := rawResource.access("spec").access("template").access("spec").get("volumes").([]interface{})
for _, secret := range cr.Spec.Secrets {
volumeName := fmt.Sprintf("secret-%s", secret)
log.Info(fmt.Sprintf("adding volume for secret '%s' as '%s'", secret, volumeName))
volumes = append(volumes, core.Volume{
Name: volumeName,
VolumeSource: core.VolumeSource{
Secret: &core.SecretVolumeSource{
SecretName: secret,
},
},
})
extraVolumeMounts = append(extraVolumeMounts, map[string]interface{}{
"name": volumeName,
"readOnly": true,
"mountPath": common.SecretsMountDir + secret,
})
}
rawResource.access("spec").access("template").access("spec").set("volumes", volumes)
}
// Extra config maps to be added as volumes?
if len(cr.Spec.ConfigMaps) > 0 {
volumes := rawResource.access("spec").access("template").access("spec").get("volumes").([]interface{})
for _, configmap := range cr.Spec.ConfigMaps {
volumeName := fmt.Sprintf("configmap-%s", configmap)
log.Info(fmt.Sprintf("adding volume for configmap '%s' as '%s'", configmap, volumeName))
volumes = append(volumes, core.Volume{
Name: volumeName,
VolumeSource: core.VolumeSource{
ConfigMap: &core.ConfigMapVolumeSource{
LocalObjectReference: core.LocalObjectReference{
Name: configmap,
},
},
},
})
extraVolumeMounts = append(extraVolumeMounts, map[string]interface{}{
"name": volumeName,
"readOnly": true,
"mountPath": common.ConfigMapsMountDir + configmap,
})
}
rawResource.access("spec").access("template").access("spec").set("volumes", volumes)
}
// Extra containers to add to the deployment?
if len(cr.Spec.Containers) > 0 {
// Otherwise append extra containers before submitting the resource
containers := rawResource.access("spec").access("template").access("spec").get("containers").([]interface{})
for _, container := range cr.Spec.Containers {
containers = append(containers, container)
log.Info(fmt.Sprintf("adding extra container '%v' to '%v'", container.Name, common.GrafanaDeploymentName))
}
rawResource.access("spec").access("template").access("spec").set("containers", containers)
}
// Append extra volume mounts to all containers
if len(extraVolumeMounts) > 0 {
containers := rawResource.access("spec").access("template").access("spec").get("containers").([]interface{})
for _, container := range containers {
volumeMounts := container.(map[string]interface{})["volumeMounts"].([]interface{})
volumeMounts = append(volumeMounts, extraVolumeMounts...)
container.(map[string]interface{})["volumeMounts"] = volumeMounts
}
}
return r.deployResource(cr, resource, resourceName)
}
func (r *ReconcileGrafana) createServiceAccount(cr *i8ly.Grafana, resourceName string) error {
resourceHelper := newResourceHelper(cr)
resource, err := resourceHelper.createResource(resourceName)
if err != nil {
return err
}
// Deploy the unmodified resource if not on OpenShift
if common.GetControllerConfig().GetConfigBool(common.ConfigOpenshift, false) == false {
return r.deployResource(cr, resource, resourceName)
}
// Otherwise add an annotation that allows using the OAuthProxy (and will have no
// effect otherwise)
annotations := make(map[string]string)
annotations[OpenShiftOAuthRedirect] = fmt.Sprintf(`{"kind":"OAuthRedirectReference","apiVersion":"v1","reference":{"kind":"Route","name":"%s"}}`, common.GrafanaRouteName)
rawResource := newUnstructuredResourceMap(resource.(*unstructured.Unstructured))
rawResource.access("metadata").set("annotations", annotations)
return r.deployResource(cr, resource, resourceName)
}
// Creates a generic kubernetes resource from a template
func (r *ReconcileGrafana) createResource(cr *i8ly.Grafana, resourceName string) error {
resourceHelper := newResourceHelper(cr)
resource, err := resourceHelper.createResource(resourceName)
if err != nil {
return err
}
return r.deployResource(cr, resource, resourceName)
}
// Deploys a resource given by a runtime object
func (r *ReconcileGrafana) deployResource(cr *i8ly.Grafana, resource runtime.Object, resourceName string) error {
// Try to find the resource, it may already exist
selector := types.NamespacedName{
Namespace: cr.Namespace,
Name: resourceName,
}
err := r.client.Get(context.TODO(), selector, resource)
// The resource exists, do nothing
if err == nil {
return nil
}
// Resource does not exist or something went wrong
if errors.IsNotFound(err) {
log.Info(fmt.Sprintf("Resource %s does not exist, creating now", resourceName))
} else {
return err
}
// Set the CR as the owner of this resource so that when
// the CR is deleted this resource also gets removed
err = controllerutil.SetControllerReference(cr, resource.(v1.Object), r.scheme)
if err != nil {
return err
}
return r.client.Create(context.TODO(), resource)
}
func (r *ReconcileGrafana) updatePhase(cr *i8ly.Grafana, phase int) (reconcile.Result, error) {
cr.Status.Phase = phase
err := r.client.Update(context.TODO(), cr)
return reconcile.Result{}, err
}