-
Notifications
You must be signed in to change notification settings - Fork 397
/
datasource_controller.go
284 lines (243 loc) · 8.31 KB
/
datasource_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
package grafanadatasource
import (
"context"
"crypto/md5"
"fmt"
grafanav1alpha1 "github.com/integr8ly/grafana-operator/v3/pkg/apis/integreatly/v1alpha1"
"github.com/integr8ly/grafana-operator/v3/pkg/controller/common"
"github.com/integr8ly/grafana-operator/v3/pkg/controller/config"
"github.com/integr8ly/grafana-operator/v3/pkg/controller/model"
"io"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"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"
"sort"
"time"
)
const (
DatasourcesApiVersion = 1
ControllerName = "controller_grafanadatasource"
)
var log = logf.Log.WithName(ControllerName)
// Add creates a new GrafanaDataSource 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, _ chan schema.GroupVersionKind, namespace string) error {
return add(mgr, newReconciler(mgr), namespace)
}
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
return &ReconcileGrafanaDataSource{
client: mgr.GetClient(),
scheme: mgr.GetScheme(),
context: ctx,
cancel: cancel,
recorder: mgr.GetEventRecorderFor(ControllerName),
state: common.ControllerState{},
}
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler, namespace string) error {
// Create a new controller
c, err := controller.New("grafanadatasource-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
// Watch for changes to primary resource GrafanaDataSource
err = c.Watch(&source.Kind{Type: &grafanav1alpha1.GrafanaDataSource{}}, &handler.EnqueueRequestForObject{})
if err != nil {
return err
}
ref := r.(*ReconcileGrafanaDataSource)
// The datasources should not change very often, only revisit them
// half as often as the dashboards
ticker := time.NewTicker(config.RequeueDelay * 2)
sendEmptyRequest := func() {
request := reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: namespace,
Name: "",
},
}
r.Reconcile(request)
}
go func() {
for range ticker.C {
log.Info("running periodic datasource resync")
sendEmptyRequest()
}
}()
// Listen for config change events
go func() {
for stateChange := range common.ControllerEvents {
// Controller state updated
ref.state = stateChange
}
}()
return nil
}
var _ reconcile.Reconciler = &ReconcileGrafanaDataSource{}
// ReconcileGrafanaDataSource reconciles a GrafanaDataSource object
type ReconcileGrafanaDataSource 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
context context.Context
cancel context.CancelFunc
recorder record.EventRecorder
state common.ControllerState
}
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (r *ReconcileGrafanaDataSource) Reconcile(request reconcile.Request) (reconcile.Result, error) {
// Read the current state of known and cluster datasources
currentState := common.NewDataSourcesState()
err := currentState.Read(r.context, r.client, request.Namespace)
if err != nil {
return reconcile.Result{}, err
}
if currentState.KnownDataSources == nil {
log.Info(fmt.Sprintf("no datasources configmap found"))
return reconcile.Result{Requeue: false}, nil
}
// Reconcile all data sources
err = r.reconcileDataSources(currentState)
if err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{Requeue: false}, nil
}
func (r *ReconcileGrafanaDataSource) reconcileDataSources(state *common.DataSourcesState) error {
var dataSourcesToAddOrUpdate []grafanav1alpha1.GrafanaDataSource
var dataSourcesToDelete []string
// check if a given datasource (by its key) is found on the cluster
foundOnCluster := func(key string) bool {
for _, ds := range state.ClusterDataSources.Items {
if key == ds.Filename() {
return true
}
}
return false
}
// Data sources to add or update: we always update the config map and let
// Kubernetes figure out if any changes have to be applied
for _, ds := range state.ClusterDataSources.Items {
dataSourcesToAddOrUpdate = append(dataSourcesToAddOrUpdate, ds)
}
// Data sources to delete: if a datasourcedashboard is in the configmap but cannot
// be found on the cluster then we assume it has been deleted and remove
// it from the configmap
for ds, _ := range state.KnownDataSources.Data {
if !foundOnCluster(ds) {
dataSourcesToDelete = append(dataSourcesToDelete, ds)
}
}
// apply dataSourcesToDelete
for _, ds := range dataSourcesToDelete {
log.Info(fmt.Sprintf("deleting datasource %v", ds))
if state.KnownDataSources.Data != nil {
delete(state.KnownDataSources.Data, ds)
}
}
// apply dataSourcesToAddOrUpdate
updated := []grafanav1alpha1.GrafanaDataSource{}
for _, ds := range dataSourcesToAddOrUpdate {
pipeline := NewDatasourcePipeline(&ds)
err := pipeline.ProcessDatasource(state.KnownDataSources)
if err != nil {
r.manageError(&ds, err)
continue
}
updated = append(updated, ds)
}
// update the hash of the newly reconciled datasources
hash, err := r.updateHash(state.KnownDataSources)
if err != nil {
r.manageError(nil, err)
return err
}
if state.KnownDataSources.Annotations == nil {
state.KnownDataSources.Annotations = map[string]string{}
}
// Compare the last hash to the previous one, update if changed
lastHash := state.KnownDataSources.Annotations[model.LastConfigAnnotation]
if lastHash != hash {
state.KnownDataSources.Annotations[model.LastConfigAnnotation] = hash
// finally, update the configmap
err = r.client.Update(r.context, state.KnownDataSources)
if err != nil {
r.recorder.Event(state.KnownDataSources, "Warning", "UpdateError", err.Error())
} else {
r.manageSuccess(updated)
}
}
return nil
}
func (i *ReconcileGrafanaDataSource) updateHash(known *v1.ConfigMap) (string, error) {
if known == nil || known.Data == nil {
return "", nil
}
// Make sure that we always use the same order when creating the hash
var keys []string
for key, _ := range known.Data {
keys = append(keys, key)
}
sort.Strings(keys)
hash := md5.New()
for _, key := range keys {
_, err := io.WriteString(hash, key)
if err != nil {
return "", err
}
_, err = io.WriteString(hash, known.Data[key])
if err != nil {
return "", err
}
}
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}
// Handle error case: update datasource with error message and status
func (r *ReconcileGrafanaDataSource) manageError(datasource *grafanav1alpha1.GrafanaDataSource, issue error) {
r.recorder.Event(datasource, "Warning", "ProcessingError", issue.Error())
// datasource deleted
if datasource == nil {
return
}
datasource.Status.Phase = grafanav1alpha1.PhaseFailing
datasource.Status.Message = issue.Error()
err := r.client.Status().Update(r.context, datasource)
if err != nil {
// Ignore conclicts. Resource might just be outdated.
if errors.IsConflict(err) {
return
}
log.Error(err, "error updating datasource status")
}
}
// manage success case: datasource has been imported successfully and the configmap
// is updated
func (r *ReconcileGrafanaDataSource) manageSuccess(datasources []grafanav1alpha1.GrafanaDataSource) {
for _, datasource := range datasources {
log.Info(fmt.Sprintf("datasource %v/%v successfully imported",
datasource.Namespace,
datasource.Name))
datasource.Status.Phase = grafanav1alpha1.PhaseReconciling
datasource.Status.Message = "success"
err := r.client.Status().Update(r.context, &datasource)
if err != nil {
r.recorder.Event(&datasource, "Warning", "UpdateError", err.Error())
}
}
}