This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
controller.go
378 lines (336 loc) · 11.8 KB
/
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
package controller
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"runtime/debug"
"time"
"github.com/argoproj-labs/argocd-notifications/expr"
"github.com/argoproj-labs/argocd-notifications/pkg"
"github.com/argoproj-labs/argocd-notifications/pkg/services"
"github.com/argoproj-labs/argocd-notifications/pkg/subscriptions"
"github.com/argoproj-labs/argocd-notifications/pkg/triggers"
"github.com/argoproj-labs/argocd-notifications/shared/k8s"
"github.com/argoproj-labs/argocd-notifications/shared/legacy"
"github.com/argoproj-labs/argocd-notifications/shared/settings"
log "github.com/sirupsen/logrus"
v1 "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"
runtimeutil "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
const (
resyncPeriod = 60 * time.Second
notifiedHistoryMaxSize = 100
)
var (
notifiedAnnotationKey = "notified." + subscriptions.AnnotationPrefix
)
type NotificationController interface {
Run(ctx context.Context, processors int)
Init(ctx context.Context) error
}
func NewController(
client dynamic.Interface,
namespace string,
cfg settings.Config,
appLabelSelector string,
metricsRegistry *controllerRegistry,
) (NotificationController, error) {
appClient := k8s.NewAppClient(client, namespace)
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
appInformer := newInformer(appClient, appLabelSelector)
appInformer.AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err == nil {
queue.Add(key)
}
},
UpdateFunc: func(old, new interface{}) {
key, err := cache.MetaNamespaceKeyFunc(new)
if err == nil {
queue.Add(key)
}
},
},
)
appProjInformer := newInformer(k8s.NewAppProjClient(client, namespace), "")
return ¬ificationController{
appClient: appClient,
appInformer: appInformer,
appProjInformer: appProjInformer,
refreshQueue: queue,
cfg: cfg,
metricsRegistry: metricsRegistry,
}, nil
}
func newInformer(resClient dynamic.ResourceInterface, selector string) cache.SharedIndexInformer {
informer := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options v1.ListOptions) (object runtime.Object, err error) {
options.LabelSelector = selector
return resClient.List(context.Background(), options)
},
WatchFunc: func(options v1.ListOptions) (watch.Interface, error) {
options.LabelSelector = selector
return resClient.Watch(context.Background(), options)
},
},
&unstructured.Unstructured{},
resyncPeriod,
cache.Indexers{},
)
return informer
}
type notificationController struct {
appClient dynamic.ResourceInterface
appInformer cache.SharedIndexInformer
appProjInformer cache.SharedIndexInformer
refreshQueue workqueue.RateLimitingInterface
cfg settings.Config
metricsRegistry *controllerRegistry
}
func (c *notificationController) Init(ctx context.Context) error {
go c.appInformer.Run(ctx.Done())
go c.appProjInformer.Run(ctx.Done())
if !cache.WaitForCacheSync(ctx.Done(), c.appInformer.HasSynced, c.appProjInformer.HasSynced) {
return errors.New("Timed out waiting for caches to sync")
}
return nil
}
func (c *notificationController) Run(ctx context.Context, processors int) {
defer runtimeutil.HandleCrash()
defer c.refreshQueue.ShutDown()
log.Warn("Controller is running.")
for i := 0; i < processors; i++ {
go wait.Until(func() {
for c.processQueueItem() {
}
}, time.Second, ctx.Done())
}
<-ctx.Done()
log.Warn("Controller has stopped.")
}
func ensureAnnotations(obj *unstructured.Unstructured) {
if obj.GetAnnotations() == nil {
obj.SetAnnotations(map[string]string{})
}
}
func (c *notificationController) processApp(app *unstructured.Unstructured, logEntry *log.Entry) error {
refreshed := false
ensureAnnotations(app)
state := triggers.NewState(app.GetAnnotations()[notifiedAnnotationKey])
// changes state of specified trigger/destination and returns if state has changed or not
setAlreadyNotified := func(trigger string, result triggers.ConditionResult, dest services.Destination, isNotified bool) (bool, error) {
changed := state.SetAlreadyNotified(trigger, result, dest, isNotified)
// if state changes reload application
if changed && !refreshed {
refreshedApp, err := c.appClient.Get(context.Background(), app.GetName(), v1.GetOptions{})
if err != nil {
return false, err
}
ensureAnnotations(refreshedApp)
app.GetAnnotations()[notifiedAnnotationKey] = refreshedApp.GetAnnotations()[notifiedAnnotationKey]
state = triggers.NewState(app.GetAnnotations()[notifiedAnnotationKey])
refreshed = true
return state.SetAlreadyNotified(trigger, result, dest, isNotified), nil
}
return changed, nil
}
for trigger, destinations := range c.getSubscriptions(app) {
res, err := c.cfg.API.RunTrigger(trigger, expr.Spawn(app, c.cfg.ArgoCDService, map[string]interface{}{"app": app.Object}))
if err != nil {
logEntry.Debugf("Failed to execute condition of trigger %s: %v", trigger, err)
}
logEntry.Infof("Trigger %s result: %v", trigger, res)
for _, cr := range res {
if !cr.Triggered {
for _, to := range destinations {
if _, err := setAlreadyNotified(trigger, cr, to, false); err != nil {
return err
}
}
continue
}
for _, to := range destinations {
if changed, err := setAlreadyNotified(trigger, cr, to, true); err != nil {
return err
} else if !changed {
logEntry.Infof("Notification about condition '%s.%s' already sent to '%v'", trigger, cr.Key, to)
continue // move to the next recipient
}
logEntry.Infof("Sending notification about condition '%s.%s' to '%v'", trigger, cr.Key, to)
vars := expr.Spawn(app, c.cfg.ArgoCDService, map[string]interface{}{
"app": app.Object,
"context": legacy.InjectLegacyVar(c.cfg.Context, to.Service),
})
if err := c.cfg.API.Send(vars, cr.Templates, to); err != nil {
logEntry.Errorf("Failed to notify recipient %s defined in app %s/%s: %v",
to, app.GetNamespace(), app.GetName(), err)
_ = state.SetAlreadyNotified(trigger, cr, to, false)
c.metricsRegistry.IncDeliveriesCounter(trigger, to.Service, false)
} else {
logEntry.Debugf("Notification %s was sent", to.Recipient)
c.metricsRegistry.IncDeliveriesCounter(trigger, to.Service, true)
}
}
}
}
state.Truncate(notifiedHistoryMaxSize)
annotations := app.GetAnnotations()
if len(state) == 0 {
delete(annotations, notifiedAnnotationKey)
} else {
stateJson, err := json.Marshal(state)
if err != nil {
return err
}
annotations[notifiedAnnotationKey] = string(stateJson)
}
app.SetAnnotations(annotations)
return nil
}
func (c *notificationController) getAppProj(app *unstructured.Unstructured) *unstructured.Unstructured {
projName, ok, err := unstructured.NestedString(app.Object, "spec", "project")
if !ok || err != nil {
return nil
}
projObj, ok, err := c.appProjInformer.GetIndexer().GetByKey(fmt.Sprintf("%s/%s", app.GetNamespace(), projName))
if !ok || err != nil {
return nil
}
proj, ok := projObj.(*unstructured.Unstructured)
if !ok {
return nil
}
ensureAnnotations(proj)
return proj
}
func (c *notificationController) getSubscriptions(app *unstructured.Unstructured) pkg.Subscriptions {
res := c.cfg.GetGlobalSubscriptions(app.GetLabels())
res.Merge(subscriptions.Annotations(app.GetAnnotations()).GetAll(c.cfg.DefaultTriggers...))
res.Merge(legacy.GetSubscriptions(app.GetAnnotations(), c.cfg.DefaultTriggers...))
if proj := c.getAppProj(app); proj != nil {
res.Merge(subscriptions.Annotations(proj.GetAnnotations()).GetAll(c.cfg.DefaultTriggers...))
res.Merge(legacy.GetSubscriptions(proj.GetAnnotations(), c.cfg.DefaultTriggers...))
}
return res.Dedup()
}
// Checks if the application SyncStatus has been refreshed by Argo CD after an operation has completed
func (c *notificationController) isAppSyncStatusRefreshed(app *unstructured.Unstructured, logEntry *log.Entry) bool {
_, ok, err := unstructured.NestedMap(app.Object, "status", "operationState")
if !ok || err != nil {
logEntry.Debug("No OperationState found, SyncStatus is assumed to be up-to-date")
return true
}
phase, ok, err := unstructured.NestedString(app.Object, "status", "operationState", "phase")
if !ok || err != nil {
logEntry.Debug("No OperationPhase found, SyncStatus is assumed to be up-to-date")
return true
}
switch phase {
case "Failed", "Error", "Succeeded":
finishedAtRaw, ok, err := unstructured.NestedString(app.Object, "status", "operationState", "finishedAt")
if !ok || err != nil {
logEntry.Debugf("No FinishedAt found for completed phase '%s', SyncStatus is assumed to be out-of-date", phase)
return false
}
finishedAt, err := time.Parse(time.RFC3339, finishedAtRaw)
if err != nil {
logEntry.Warnf("Failed to parse FinishedAt '%s'", finishedAtRaw)
return false
}
var reconciledAt, observedAt time.Time
reconciledAtRaw, ok, err := unstructured.NestedString(app.Object, "status", "reconciledAt")
if ok && err == nil {
reconciledAt, _ = time.Parse(time.RFC3339, reconciledAtRaw)
}
observedAtRaw, ok, err := unstructured.NestedString(app.Object, "status", "observedAt")
if ok && err == nil {
observedAt, _ = time.Parse(time.RFC3339, observedAtRaw)
}
if finishedAt.After(reconciledAt) && finishedAt.After(observedAt) {
logEntry.Debugf("SyncStatus out-of-date (FinishedAt=%v, ReconciledAt=%v, Observed=%v", finishedAt, reconciledAt, observedAt)
return false
}
logEntry.Debugf("SyncStatus up-to-date (FinishedAt=%v, ReconciledAt=%v, Observed=%v", finishedAt, reconciledAt, observedAt)
default:
logEntry.Debugf("Found phase '%s', SyncStatus is assumed to be up-to-date", phase)
}
return true
}
func (c *notificationController) processQueueItem() (processNext bool) {
key, shutdown := c.refreshQueue.Get()
if shutdown {
processNext = false
return
}
processNext = true
defer func() {
if r := recover(); r != nil {
log.Errorf("Recovered from panic: %+v\n%s", r, debug.Stack())
}
c.refreshQueue.Done(key)
}()
obj, exists, err := c.appInformer.GetIndexer().GetByKey(key.(string))
if err != nil {
log.Errorf("Failed to get app '%s' from appInformer index: %+v", key, err)
return
}
if !exists {
// This happens after app was deleted, but the work queue still had an entry for it.
return
}
app, ok := obj.(*unstructured.Unstructured)
if !ok {
log.Errorf("Failed to get app '%s' from appInformer index: %+v", key, err)
return
}
appCopy := app.DeepCopy()
logEntry := log.WithField("app", key)
logEntry.Info("Start processing")
if refreshed := c.isAppSyncStatusRefreshed(appCopy, logEntry); !refreshed {
logEntry.Info("Processing skipped, sync status out of date")
return
}
err = c.processApp(appCopy, logEntry)
if err != nil {
logEntry.Errorf("Failed to process: %v", err)
return
}
if !reflect.DeepEqual(app.GetAnnotations(), appCopy.GetAnnotations()) {
annotationsPatch := make(map[string]interface{})
for k, v := range appCopy.GetAnnotations() {
annotationsPatch[k] = v
}
for k := range app.GetAnnotations() {
if _, ok = appCopy.GetAnnotations()[k]; !ok {
annotationsPatch[k] = nil
}
}
patchData, err := json.Marshal(map[string]map[string]interface{}{
"metadata": {"annotations": annotationsPatch},
})
if err != nil {
logEntry.Errorf("Failed to marshal app patch: %v", err)
return
}
_, err = c.appClient.Patch(context.Background(), app.GetName(), types.MergePatchType, patchData, v1.PatchOptions{})
if err != nil {
logEntry.Errorf("Failed to patch app: %v", err)
return
}
}
logEntry.Info("Processing completed")
return
}