-
Notifications
You must be signed in to change notification settings - Fork 10
/
controller.go
224 lines (186 loc) · 5.81 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
//
// Copyright 2021 IBM Corporation
//
// 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 controller
import (
"context"
"fmt"
"runtime/debug"
"time"
"github.com/IBM/argocd-interlace/pkg/interlace"
"github.com/IBM/argocd-interlace/pkg/utils"
appv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
appClientset "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apiruntime "k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
type controller struct {
applicationClientset appClientset.Interface
informer cache.SharedIndexInformer
appRefreshQueue workqueue.RateLimitingInterface
namespace string
}
func Start(ctx context.Context, config string, namespace string) {
_, cfg, err := utils.GetClient(config)
appClientset := appClientset.NewForConfigOrDie(cfg)
if err != nil {
log.Fatalf("Error in starting argocd interlace controller: %s", err.Error())
}
c := newController(appClientset, namespace)
c.Run(ctx)
}
func (ctrl *controller) newApplicationInformer(applicationClientset appClientset.Interface) cache.SharedIndexInformer {
informer := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (apiruntime.Object, error) {
return applicationClientset.ArgoprojV1alpha1().Applications(ctrl.namespace).List(context.TODO(), options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return applicationClientset.ArgoprojV1alpha1().Applications(ctrl.namespace).Watch(context.TODO(), options)
},
},
&appv1.Application{},
0,
cache.Indexers{},
)
return informer
}
func newController(applicationClientset appClientset.Interface, namespace string) *controller {
q := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
ctrl := &controller{
applicationClientset: applicationClientset,
appRefreshQueue: q,
namespace: namespace,
}
appInformer := ctrl.newApplicationInformer(applicationClientset)
appInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if !ctrl.canProcessApp(obj) {
return
}
key, err := cache.MetaNamespaceKeyFunc(obj)
app, ok := obj.(*appv1.Application)
if ok {
err := interlace.CreateEventHandler(app)
if err != nil {
log.Errorf("Error in handling create event: %s", err.Error())
}
}
if err == nil {
ctrl.appRefreshQueue.Add(key)
}
},
UpdateFunc: func(old, new interface{}) {
if !ctrl.canProcessApp(old) {
return
}
key, err := cache.MetaNamespaceKeyFunc(old)
oldApp, oldOK := old.(*appv1.Application)
newApp, newOK := new.(*appv1.Application)
if oldOK && newOK {
err := interlace.UpdateEventHandler(oldApp, newApp)
if err != nil {
log.Errorf("Error in handling update event: %s", err.Error())
}
}
if err == nil {
ctrl.appRefreshQueue.Add(key)
}
},
DeleteFunc: func(obj interface{}) {
if !ctrl.canProcessApp(obj) {
return
}
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err == nil {
log.Debug("Event received of type delete for key ", key)
//ctrl.appRefreshQueue.Add(key)
}
},
})
ctrl.informer = appInformer
return ctrl
}
func (c *controller) canProcessApp(obj interface{}) bool {
_, ok := obj.(*appv1.Application)
if ok {
return ok
}
return false
}
func (c *controller) Run(ctx context.Context) {
defer utilruntime.HandleCrash() //this will handle panic and won't crash the process
defer c.appRefreshQueue.ShutDown() //shutdown all workqueue and terminate all workers
log.Info("Starting argocd-interlace...")
go c.informer.Run(ctx.Done())
log.Info("Synchronizing events...")
//synchronize the cache before starting to process events
if !cache.WaitForCacheSync(ctx.Done(), c.informer.HasSynced) {
utilruntime.HandleError(fmt.Errorf("Timed out waiting for caches to sync"))
log.Info("synchronization failed...")
return
}
log.Info("Synchronization complete!")
log.Info("Ready to process events")
go wait.Until(func() {
for c.processNextItem() {
// continue looping
}
}, time.Second, ctx.Done())
<-ctx.Done()
}
func (c *controller) processNextItem() (processNext bool) {
log.Debug("Check if new events in queue ", c.appRefreshQueue.Len())
appKey, shutdown := c.appRefreshQueue.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.appRefreshQueue.Done(appKey)
}()
err := c.processItem(appKey.(string))
if err == nil {
c.appRefreshQueue.Forget(appKey)
return true
}
return true
}
func (c *controller) processItem(key string) error {
obj, exists, err := c.informer.GetIndexer().GetByKey(key)
if err != nil {
return fmt.Errorf("Error fetching object with key %s from store: %v", key, err)
}
if !exists {
// This happens after app was deleted, but the work queue still had an entry for it.
return nil
}
_, ok := obj.(*appv1.Application)
if !ok {
log.Warnf("Key '%s' in index is not an application", key)
return nil
}
return nil
}