forked from grafana/grafana-operator
/
grafananotificationchannel_controller.go
391 lines (335 loc) · 13.9 KB
/
grafananotificationchannel_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
/*
Copyright 2021.
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 grafananotificationchannel
import (
"context"
"crypto/tls"
"encoding/json"
defaultErrors "errors"
"fmt"
"github.com/go-logr/logr"
grafanav1alpha1 "github.com/grafana-operator/grafana-operator/v4/api/integreatly/v1alpha1"
"github.com/grafana-operator/grafana-operator/v4/controllers/constants"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log"
"net/http"
"os"
"time"
"github.com/grafana-operator/grafana-operator/v4/controllers/common"
"github.com/grafana-operator/grafana-operator/v4/controllers/config"
"k8s.io/apimachinery/pkg/api/errors"
"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"
"sigs.k8s.io/controller-runtime/pkg/source"
)
const (
ControllerName = "controller_grafananotificationchannel"
)
type GrafanaChannel struct {
ID *uint `json:"id"`
UID *string `json:"uid"`
Name *string `json:"name"`
Type *string `json:"type"`
IsDefault *bool `json:"isDefault"`
SendReminder *bool `json:"sendReminder"`
DisableResolveMessage *bool `json:"disableResolveMessage"`
}
// NewReconciler returns a new reconcile.Reconciler
func NewReconciler(mgr manager.Manager) reconcile.Reconciler {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
return &GrafanaNotificationChannelReconciler{
client: mgr.GetClient(),
transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, //nolint:gosec
},
},
config: config.GetNotificationControllerConfig(),
context: ctx,
cancel: cancel,
recorder: mgr.GetEventRecorderFor(ControllerName),
state: common.ControllerState{},
Log: mgr.GetLogger(),
}
}
// Add creates a new GrafanaNotificationChannel 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, namespace string) error {
return SetupWithManager(mgr, NewReconciler(mgr), namespace)
}
// +kubebuilder:rbac:groups=integreatly.org,resources=grafananotificationchannels,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=integreatly.org,resources=grafananotificationchannels/status,verbs=get;update;patch
// SetupWithManager sets up the controller with the Manager.
func SetupWithManager(mgr ctrl.Manager, r reconcile.Reconciler, namespace string) error {
c, err := controller.New("grafananotificationchannel-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
// Watch for changes to primary resource GrafanaNotificationChannel
err = c.Watch(&source.Kind{Type: &grafanav1alpha1.GrafanaNotificationChannel{}}, &handler.EnqueueRequestForObject{})
if err == nil {
log.Log.Info("Starting notificationchannel controller")
}
var ref = r.(*GrafanaNotificationChannelReconciler)
ticker := time.NewTicker(config.GetControllerConfig().RequeueDelay)
sendEmptyRequest := func() {
request := reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: namespace,
Name: "",
},
}
_, err := r.Reconcile(ref.context, request)
if err != nil {
return
}
}
go func() {
for range ticker.C {
log.Log.Info("running periodic notificationchannel resync")
sendEmptyRequest()
}
}()
go func() {
for stateChange := range common.ControllerEvents {
// Controller state updated
ref.state = stateChange
}
}()
return err
}
var _ reconcile.Reconciler = &GrafanaNotificationChannelReconciler{}
// GrafanaNotificationChannelReconciler reconciles a GrafanaNotificationChannel object
type GrafanaNotificationChannelReconciler 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
transport *http.Transport
config *config.NotificationControllerConfig
context context.Context
cancel context.CancelFunc
recorder record.EventRecorder
state common.ControllerState
Log logr.Logger
}
// Reconcile , 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 *GrafanaNotificationChannelReconciler) Reconcile(context context.Context, request reconcile.Request) (ctrl.Result, error) {
logger := r.Log.WithValues(ControllerName, request.NamespacedName)
// If Grafana is not running there is no need to continue
if !r.state.GrafanaReady {
logger.Info("no grafana instance available")
return reconcile.Result{Requeue: false}, nil
}
grafanaClient, err := r.getClient()
if err != nil {
// we handle the error by requeing, safe to ignore nilerr return
return reconcile.Result{RequeueAfter: config.GetControllerConfig().RequeueDelay}, nil //nolint:nilerr
}
// Initial request?
if request.Name == "" {
return r.reconcileNotificationChannels(request, grafanaClient)
}
// Check if the label selectors are available yet. If not then the grafana controller
// has not finished initializing and we can't continue. Reschedule for later.
if r.state.DashboardSelectors == nil {
return reconcile.Result{RequeueAfter: config.GetControllerConfig().RequeueDelay}, nil
}
// Fetch the GrafanaNotificationChannel instance
instance := &grafanav1alpha1.GrafanaNotificationChannel{}
err = r.client.Get(r.context, request.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
// If some notificationchannel has been deleted, then always re sync the world
logger.Info(fmt.Sprintf("deleting notificationchannel %v/%v", request.Namespace, request.Name))
return r.reconcileNotificationChannels(request, grafanaClient)
}
// Error reading the object - requeue the request.
return reconcile.Result{}, err
}
// If the notificationchannel does not match the label selectors then we ignore it
cr := instance.DeepCopy()
if !r.isMatch(cr) {
logger.Info(fmt.Sprintf("notificationchannel %v/%v found but selectors do not match",
cr.Namespace, cr.Name))
return reconcile.Result{}, nil
}
// Otherwise always re sync all notificationchannels in the namespace
return r.reconcileNotificationChannels(request, grafanaClient)
}
//nolint:funlen
func (r *GrafanaNotificationChannelReconciler) reconcileNotificationChannels(request reconcile.Request, client GrafanaClient) (reconcile.Result, error) { //nolint:cyclop
// Collect known and namespace notificationchannels
knownNotificationChannels := r.config.GetNotificationChannels(request.Namespace)
namespaceNotificationChannels := &grafanav1alpha1.GrafanaNotificationChannelList{}
err := r.client.List(r.context, namespaceNotificationChannels)
if err != nil {
return reconcile.Result{}, err
}
// Prepare lists
var notificationchannelsToDelete []*grafanav1alpha1.GrafanaNotificationChannelRef
// Check if a given notificationchannel (by name) is present in the list of
// notificationchannels in the namespace
inNamespace := func(item string) bool {
for _, notificationchannel := range namespaceNotificationChannels.Items {
if notificationchannel.Name == item {
return true
}
}
return false
}
// Returns the hash of a notificationchannel if it is known
findHash := func(item *grafanav1alpha1.GrafanaNotificationChannel) string {
for _, d := range knownNotificationChannels {
if item.Name == d.Name {
return d.Hash
}
}
return ""
}
// NotificationChannels to delete: notificationchannels that are known but not found
// any longer in the namespace
for _, notificationchannel := range knownNotificationChannels {
if !inNamespace(notificationchannel.Name) {
notificationchannelsToDelete = append(notificationchannelsToDelete, notificationchannel)
}
}
// Process new/updated notificationchannels
for index, notificationchannel := range namespaceNotificationChannels.Items {
// Is this a notificationchannel we care about (matches the label selectors)?
if !r.isMatch(&namespaceNotificationChannels.Items[index]) {
r.Log.Info(fmt.Sprintf("notificationchannel %v/%v found but selectors do not match",
notificationchannel.Namespace, notificationchannel.Name))
continue
}
// Process the notificationchannel. Use the known hash of an existing notificationchannel
// to determine if an update is required
knownHash := findHash(&namespaceNotificationChannels.Items[index])
pipeline := NewNotificationChannelPipeline(r.client, &namespaceNotificationChannels.Items[index])
processed, err := pipeline.ProcessNotificationChannel(knownHash)
if err != nil {
r.Log.Error(err, fmt.Sprintf("cannot process notificationchannel %v/%v", notificationchannel.Namespace, notificationchannel.Name))
r.manageError(&namespaceNotificationChannels.Items[index], err)
continue
}
if processed == nil {
continue
}
var status GrafanaResponse
// Create or Update notification channel
rawJson := GrafanaChannel{}
if err := json.Unmarshal(processed, &rawJson); err != nil {
return reconcile.Result{}, err
}
if rawJson.UID == nil {
r.Log.Info(fmt.Sprintf("cannot process notificationchannel %v/%v, UID is nil", notificationchannel.Namespace, notificationchannel.Name))
return reconcile.Result{}, nil
}
if _, err = client.GetNotificationChannel(*rawJson.UID); err != nil {
status, err = client.CreateNotificationChannel(processed)
} else {
status, err = client.UpdateNotificationChannel(processed, *rawJson.UID)
}
if err != nil {
r.Log.Info(fmt.Sprintf("cannot submit notificationchannel %v/%v", notificationchannel.Namespace, notificationchannel.Name))
r.manageError(&namespaceNotificationChannels.Items[index], err)
continue
}
err = r.manageSuccess(&namespaceNotificationChannels.Items[index], status, pipeline.NewHash())
if err != nil {
r.manageError(&namespaceNotificationChannels.Items[index], err)
}
}
for _, notificationchannel := range notificationchannelsToDelete {
status, err := client.DeleteNotificationChannelByUID(notificationchannel.UID)
if err != nil {
r.Log.Error(err, fmt.Sprintf("error deleting notificationchannel %v, status was %v/%v",
notificationchannel.UID,
*status.Message,
*status.UID))
}
r.Log.Info(fmt.Sprintf("delete result was %v", *status.UID))
r.config.RemoveNotificationChannel(notificationchannel.Namespace, notificationchannel.Name)
}
// Mark the notificationchannels as synced so that the current state can be written
// to the Grafana CR by the grafana controller
r.config.AddConfigItem(config.ConfigGrafanaNotificationChannelsSynced, true)
return reconcile.Result{Requeue: false}, nil
}
// Handle success case: update notificationchannel metadata (id, uid) and update the list
// of plugins
func (r *GrafanaNotificationChannelReconciler) manageSuccess(notificationchannel *grafanav1alpha1.GrafanaNotificationChannel, status GrafanaResponse, hash string) error {
msg := fmt.Sprintf("notificationchannel %v/%v successfully submitted",
notificationchannel.Namespace,
notificationchannel.Name)
r.recorder.Event(notificationchannel, "Normal", "Success", msg)
r.Log.Info(msg)
notificationchannel.Status.UID = *status.UID
notificationchannel.Status.ID = *status.ID
notificationchannel.Status.Phase = grafanav1alpha1.PhaseReconciling
notificationchannel.Status.Hash = hash
notificationchannel.Status.Message = constants.GrafanaSuccessMsg
r.config.AddNotificationChannel(notificationchannel)
return r.client.Status().Update(r.context, notificationchannel)
}
// Handle error case: update notificationchannel with error message and status
func (r *GrafanaNotificationChannelReconciler) manageError(notificationchannel *grafanav1alpha1.GrafanaNotificationChannel, issue error) {
r.recorder.Event(notificationchannel, "Warning", "ProcessingError", issue.Error())
notificationchannel.Status.Phase = grafanav1alpha1.PhaseFailing
notificationchannel.Status.Message = issue.Error()
err := r.client.Status().Update(r.context, notificationchannel)
if err != nil {
// Ignore conclicts. Resource might just be outdated.
if errors.IsConflict(err) {
return
}
r.Log.Error(err, "error updating notificationchannel status")
}
}
// Get an authenticated grafana API client
func (r *GrafanaNotificationChannelReconciler) getClient() (GrafanaClient, error) {
url := r.state.AdminUrl
if url == "" {
return nil, defaultErrors.New("cannot get grafana admin url")
}
username := os.Getenv(constants.GrafanaAdminUserEnvVar)
if username == "" {
return nil, defaultErrors.New("invalid credentials (username)")
}
password := os.Getenv(constants.GrafanaAdminPasswordEnvVar)
if password == "" {
return nil, defaultErrors.New("invalid credentials (password)")
}
duration := time.Duration(r.state.ClientTimeout)
return NewGrafanaClient(url, username, password, r.transport, duration), nil
}
// Test if a given notificationchannel matches an array of label selectors
func (r *GrafanaNotificationChannelReconciler) isMatch(item *grafanav1alpha1.GrafanaNotificationChannel) bool {
if r.state.DashboardSelectors == nil {
return false
}
match, err := item.MatchesSelectors(r.state.DashboardSelectors)
if err != nil {
r.Log.Error(err, fmt.Sprintf("error matching selectors against %v/%v",
item.Namespace,
item.Name))
return false
}
return match
}