-
Notifications
You must be signed in to change notification settings - Fork 289
/
controller.go
286 lines (244 loc) · 8.16 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
/*
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 filewatch
import (
"context"
"fmt"
"sync"
"time"
"github.com/jonboulle/clockwork"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"
"github.com/tilt-dev/fsnotify"
"github.com/tilt-dev/tilt/internal/controllers/apicmp"
"github.com/tilt-dev/tilt/internal/controllers/apis/configmap"
"github.com/tilt-dev/tilt/internal/controllers/core/filewatch/fsevent"
"github.com/tilt-dev/tilt/internal/controllers/indexer"
"github.com/tilt-dev/tilt/internal/ignore"
"github.com/tilt-dev/tilt/internal/store"
"github.com/tilt-dev/tilt/internal/store/filewatches"
"github.com/tilt-dev/tilt/internal/watch"
"github.com/tilt-dev/tilt/pkg/apis"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/logger"
)
// Controller reconciles a FileWatch object
type Controller struct {
ctrlclient.Client
Store store.RStore
targetWatches map[types.NamespacedName]*watcher
fsWatcherMaker fsevent.WatcherMaker
timerMaker fsevent.TimerMaker
mu sync.Mutex
clock clockwork.Clock
indexer *indexer.Indexer
requeuer *indexer.Requeuer
}
func NewController(client ctrlclient.Client, store store.RStore, fsWatcherMaker fsevent.WatcherMaker, timerMaker fsevent.TimerMaker, scheme *runtime.Scheme, clock clockwork.Clock) *Controller {
return &Controller{
Client: client,
Store: store,
targetWatches: make(map[types.NamespacedName]*watcher),
fsWatcherMaker: fsWatcherMaker,
timerMaker: timerMaker,
indexer: indexer.NewIndexer(scheme, indexFw),
requeuer: indexer.NewRequeuer(),
clock: clock,
}
}
func (c *Controller) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
c.mu.Lock()
defer c.mu.Unlock()
existing, hasExisting := c.targetWatches[req.NamespacedName]
var fw v1alpha1.FileWatch
err := c.Client.Get(ctx, req.NamespacedName, &fw)
c.indexer.OnReconcile(req.NamespacedName, &fw)
if err != nil && !apierrors.IsNotFound(err) {
return ctrl.Result{}, err
}
if apierrors.IsNotFound(err) || !fw.ObjectMeta.DeletionTimestamp.IsZero() {
if hasExisting {
existing.cleanupWatch(ctx)
c.removeWatch(existing)
}
c.Store.Dispatch(filewatches.NewFileWatchDeleteAction(req.NamespacedName.Name))
return ctrl.Result{}, nil
}
// The apiserver is the source of truth, and will ensure the engine state is up to date.
c.Store.Dispatch(filewatches.NewFileWatchUpsertAction(&fw))
ctx = store.MustObjectLogHandler(ctx, c.Store, &fw)
// Get configmap's disable status
disableStatus, err := configmap.MaybeNewDisableStatus(ctx, c.Client, fw.Spec.DisableSource, fw.Status.DisableStatus)
if err != nil {
return ctrl.Result{}, err
}
// Clean up existing filewatches if it's disabled
result := ctrl.Result{}
if disableStatus.State == v1alpha1.DisableStateDisabled {
if hasExisting {
existing.cleanupWatch(ctx)
c.removeWatch(existing)
}
} else {
// Determine if we the filewatch needs to be refreshed.
shouldRestart := !hasExisting || !apicmp.DeepEqual(existing.spec, fw.Spec)
if hasExisting && !shouldRestart {
shouldRestart, result = existing.shouldRestart()
}
if shouldRestart {
c.addOrReplace(ctx, req.NamespacedName, &fw)
}
}
watch, ok := c.targetWatches[req.NamespacedName]
status := &v1alpha1.FileWatchStatus{DisableStatus: disableStatus}
if ok {
status = watch.copyStatus()
status.DisableStatus = disableStatus
}
err = c.maybeUpdateObjectStatus(ctx, &fw, status)
if err != nil {
return ctrl.Result{}, err
}
return result, nil
}
func (c *Controller) maybeUpdateObjectStatus(ctx context.Context, fw *v1alpha1.FileWatch, newStatus *v1alpha1.FileWatchStatus) error {
if apicmp.DeepEqual(newStatus, &fw.Status) {
return nil
}
oldError := fw.Status.Error
update := fw.DeepCopy()
update.Status = *newStatus
err := c.Client.Status().Update(ctx, update)
if err != nil {
return err
}
if update.Status.Error != "" && oldError != update.Status.Error {
logger.Get(ctx).Errorf("filewatch %s: %s", fw.Name, update.Status.Error)
}
c.Store.Dispatch(NewFileWatchUpdateStatusAction(update))
return nil
}
func (c *Controller) CreateBuilder(mgr ctrl.Manager) (*builder.Builder, error) {
b := ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.FileWatch{}).
Watches(&source.Kind{Type: &v1alpha1.ConfigMap{}},
handler.EnqueueRequestsFromMapFunc((c.indexer.Enqueue))).
Watches(c.requeuer, handler.Funcs{})
return b, nil
}
// removeWatch removes a watch from the map. It does NOT stop the watcher or free up resources.
//
// mu must be held before calling.
func (c *Controller) removeWatch(tw *watcher) {
if entry, ok := c.targetWatches[tw.name]; ok && tw == entry {
delete(c.targetWatches, tw.name)
}
}
func (c *Controller) addOrReplace(ctx context.Context, name types.NamespacedName, fw *v1alpha1.FileWatch) {
existing, hasExisting := c.targetWatches[name]
status := &v1alpha1.FileWatchStatus{}
w := &watcher{
name: name,
spec: *fw.Spec.DeepCopy(),
clock: c.clock,
restartBackoff: time.Second,
}
if hasExisting && apicmp.DeepEqual(existing.spec, w.spec) {
w.restartBackoff = existing.restartBackoff
status.Error = existing.status.Error
}
ignoreMatcher := ignore.CreateFileChangeFilter(fw.Spec.Ignores)
notify, err := c.fsWatcherMaker(
append([]string{}, fw.Spec.WatchedPaths...),
ignoreMatcher,
logger.Get(ctx))
if err != nil {
status.Error = fmt.Sprintf("failed to initialize filesystem watch: %v", err)
} else if err := notify.Start(); err != nil {
status.Error = fmt.Sprintf("failed to initialize filesystem watch: %v", err)
}
if hasExisting {
// Clean up the existing watch AFTER the new watch has been started.
existing.cleanupWatch(ctx)
}
if notify != nil {
w.notify = notify
status.MonitorStartTime = apis.NowMicro()
ctx, cancel := context.WithCancel(ctx)
w.cancel = cancel
go c.dispatchFileChangesLoop(ctx, w)
}
w.status = status
c.targetWatches[name] = w
}
func (c *Controller) dispatchFileChangesLoop(ctx context.Context, w *watcher) {
eventsCh := fsevent.Coalesce(c.timerMaker, w.notify.Events())
defer func() {
c.mu.Lock()
defer c.mu.Unlock()
w.cleanupWatch(ctx)
c.requeuer.Add(w.name)
}()
for {
select {
case err, ok := <-w.notify.Errors():
if !ok {
return
}
if watch.IsWindowsShortReadError(err) {
w.recordError(fmt.Errorf("Windows I/O overflow.\n"+
"You may be able to fix this by setting the env var %s.\n"+
"Current buffer size: %d\n"+
"More details: https://github.com/tilt-dev/tilt/issues/3556\n"+
"Caused by: %v",
watch.WindowsBufferSizeEnvVar,
watch.DesiredWindowsBufferSize(),
err))
} else if err.Error() == fsnotify.ErrEventOverflow.Error() {
w.recordError(fmt.Errorf("%s\nerror: %v", DetectedOverflowErrMsg, err))
} else {
w.recordError(err)
}
c.requeuer.Add(w.name)
case <-ctx.Done():
return
case fsEvents, ok := <-eventsCh:
if !ok {
return
}
w.recordEvent(fsEvents)
c.requeuer.Add(w.name)
}
}
}
// Find all the objects to watch based on the Filewatch model
func indexFw(obj ctrlclient.Object) []indexer.Key {
fw := obj.(*v1alpha1.FileWatch)
result := []indexer.Key{}
if fw.Spec.DisableSource != nil {
cm := fw.Spec.DisableSource.ConfigMap
if cm != nil {
gvk := v1alpha1.SchemeGroupVersion.WithKind("ConfigMap")
result = append(result, indexer.Key{
Name: types.NamespacedName{Name: cm.Name},
GVK: gvk,
})
}
}
return result
}