@@ -24,7 +24,6 @@ import (
24
24
type Watcher struct {
25
25
watcher * fsnotify.Watcher
26
26
filesCache map [string ]fs.FileInfo
27
- eventCache map [string ]fsnotify.Event
28
27
workqueue workqueue.TypedDelayingInterface [string ]
29
28
}
30
29
@@ -37,7 +36,6 @@ func CreateWatcher() (*Watcher, error) {
37
36
return & Watcher {
38
37
watcher : watcher ,
39
38
filesCache : make (map [string ]fs.FileInfo ),
40
- eventCache : make (map [string ]fsnotify.Event ),
41
39
workqueue : workqueue .TypedNewDelayingQueue [string ](),
42
40
}, nil
43
41
}
@@ -141,132 +139,79 @@ func (w *Watcher) processNextEventForImages(ctx context.Context, cfg *config.Nod
141
139
func (w * Watcher ) processImageEvent (ctx context.Context , key string , cfg * config.Node , client * containerd.Client , imageClient runtimeapi.ImageServiceClient ) error {
142
140
defer w .workqueue .Done (key )
143
141
144
- event , ok := w .eventCache [key ]
145
- if ! ok {
146
- return nil
147
- }
148
-
149
- if event .Has (fsnotify .Write ) {
150
- newStateFile , err := os .Stat (event .Name )
151
- if err != nil {
152
- logrus .Errorf ("Failed to get file %s info for image event WRITE: %v" , key , err )
153
- return err
154
- }
155
-
156
- // we do not want to handle directorys, only files
157
- if newStateFile .IsDir () {
142
+ file , err := os .Stat (key )
143
+ // if the file does not exists, we assume that the event was RENAMED or REMOVED
144
+ if os .IsNotExist (err ) {
145
+ if key == cfg .Images {
146
+ w .ClearMap ()
158
147
return nil
159
148
}
160
149
161
- if ! isFileSupported (event . Name ) {
150
+ if ! isFileSupported (key ) {
162
151
return nil
163
152
}
164
153
165
- lastStateFile := w .filesCache [event .Name ]
166
- w .filesCache [event .Name ] = newStateFile
167
- if lastStateFile == nil || (newStateFile .Size () != lastStateFile .Size ()) && newStateFile .ModTime ().After (lastStateFile .ModTime ()) {
168
- logrus .Debugf ("File met the requirements for import to containerd image store: %s" , event .Name )
169
- start := time .Now ()
170
- if err := preloadFile (ctx , cfg , client , imageClient , event .Name ); err != nil {
171
- logrus .Errorf ("Failed to import %s: %v" , event .Name , err )
172
- return err
173
- }
174
- logrus .Infof ("Imported images from %s in %s" , event .Name , time .Since (start ))
175
- }
154
+ delete (w .filesCache , key )
155
+ logrus .Debugf ("File removed from the image watcher controller: %s" , key )
156
+ return nil
157
+ } else if err != nil {
158
+ logrus .Errorf ("Failed to get file %s info for image event: %v" , key , err )
159
+ return err
176
160
}
177
161
178
- if event .Has (fsnotify .Create ) {
179
- info , err := os .Stat (event .Name )
180
- if err != nil {
181
- logrus .Errorf ("Failed to get file %s info for image event CREATE: %v" , event .Name , err )
182
- return err
183
- }
162
+ if file .IsDir () {
163
+ // only add the image watcher, populate and search for images when it is the images folder
164
+ if key == cfg .Images {
165
+ if err := w .HandleWatch (cfg .Images ); err != nil {
166
+ logrus .Errorf ("Failed to watch %s: %v" , cfg .Images , err )
167
+ return err
168
+ }
184
169
185
- if info .IsDir () {
186
- // only add the image watcher, populate and search for images when it is the images folder
187
- if event .Name == cfg .Images {
188
- if err := w .HandleWatch (cfg .Images ); err != nil {
189
- logrus .Errorf ("Failed to watch %s: %v" , cfg .Images , err )
190
- return err
191
- }
170
+ if err := w .Populate (cfg .Images ); err != nil {
171
+ logrus .Errorf ("Failed to populate %s files: %v" , cfg .Images , err )
172
+ return err
173
+ }
192
174
193
- if err := w .Populate (cfg .Images ); err != nil {
194
- logrus .Errorf ("Failed to populate %s files: %v" , cfg .Images , err )
195
- return err
196
- }
175
+ // Read the directory to see if the created folder has files inside
176
+ fileInfos , err := os .ReadDir (cfg .Images )
177
+ if err != nil {
178
+ logrus .Errorf ("Unable to read images in %s: %v" , cfg .Images , err )
179
+ return err
180
+ }
197
181
198
- // Read the directory to see if the created folder has files inside
199
- fileInfos , err := os .ReadDir (cfg .Images )
200
- if err != nil {
201
- logrus .Errorf ("Unable to read images in %s: %v" , cfg .Images , err )
202
- return err
182
+ for _ , fileInfo := range fileInfos {
183
+ if fileInfo .IsDir () {
184
+ continue
203
185
}
204
186
205
- for _ , fileInfo := range fileInfos {
206
- if fileInfo .IsDir () {
207
- continue
208
- }
209
-
210
- start := time .Now ()
211
- filePath := filepath .Join (cfg .Images , fileInfo .Name ())
187
+ start := time .Now ()
188
+ filePath := filepath .Join (cfg .Images , fileInfo .Name ())
212
189
213
- if err := preloadFile (ctx , cfg , client , imageClient , filePath ); err != nil {
214
- logrus .Errorf ("Error encountered while importing %s: %v" , filePath , err )
215
- continue
216
- }
217
- logrus .Infof ("Imported images from %s in %s" , filePath , time .Since (start ))
190
+ if err := preloadFile (ctx , cfg , client , imageClient , filePath ); err != nil {
191
+ logrus .Errorf ("Error encountered while importing %s: %v" , filePath , err )
192
+ continue
218
193
}
194
+ logrus .Infof ("Imported images from %s in %s" , filePath , time .Since (start ))
219
195
}
220
-
221
- return nil
222
196
}
223
197
224
- if ! isFileSupported (event .Name ) {
225
- return nil
226
- }
227
-
228
- w .filesCache [event .Name ] = info
229
- logrus .Debugf ("File added to watcher controller: %s" , event .Name )
230
- start := time .Now ()
231
- if err := preloadFile (ctx , cfg , client , imageClient , event .Name ); err != nil {
232
- logrus .Errorf ("Error encountered while importing %s: %v" , event .Name , err )
233
- return err
234
- }
235
- logrus .Infof ("Imported images from %s in %s" , event .Name , time .Since (start ))
198
+ return nil
236
199
}
237
200
238
- if event .Has (fsnotify .Remove ) {
239
- // Clear the map when cfg.Images directory is removed, it does not need to remove the watcher
240
- // because the fsnotify already removes
241
- if event .Name == cfg .Images {
242
- w .ClearMap ()
243
- return nil
244
- }
245
-
246
- if ! isFileSupported (event .Name ) {
247
- return nil
248
- }
249
-
250
- // delete the file from the file map and the event to clean the caches
251
- delete (w .filesCache , event .Name )
252
- logrus .Debugf ("Removed file from the image watcher controller: %s" , event .Name )
201
+ if ! isFileSupported (key ) {
202
+ return nil
253
203
}
254
204
255
- if event .Has (fsnotify .Rename ) {
256
- // Clear the map when cfg.Images directory is renamed, it does not need to remove the watcher
257
- // because the fsnotify already removes
258
- if event .Name == cfg .Images {
259
- w .ClearMap ()
260
- return nil
261
- }
262
-
263
- if ! isFileSupported (event .Name ) {
264
- return nil
205
+ lastStateFile := w .filesCache [key ]
206
+ w .filesCache [key ] = file
207
+ if lastStateFile == nil || (file .Size () != lastStateFile .Size ()) && file .ModTime ().After (lastStateFile .ModTime ()) {
208
+ logrus .Debugf ("File met the requirements for import to containerd image store: %s" , key )
209
+ start := time .Now ()
210
+ if err := preloadFile (ctx , cfg , client , imageClient , key ); err != nil {
211
+ logrus .Errorf ("Failed to import %s: %v" , key , err )
212
+ return err
265
213
}
266
-
267
- // delete the file from the file map
268
- delete (w .filesCache , event .Name )
269
- logrus .Debugf ("Removed file from the image watcher controller: %s" , key )
214
+ logrus .Infof ("Imported images from %s in %s" , key , time .Since (start ))
270
215
}
271
216
272
217
return nil
@@ -324,7 +269,6 @@ func watchImages(ctx context.Context, cfg *config.Node) {
324
269
325
270
// this part is to specify to only get events that were from /agent/images
326
271
if strings .Contains (event .Name , "/agent/images" ) {
327
- w .eventCache [event .Name ] = event
328
272
w .workqueue .AddAfter (event .Name , 2 * time .Second )
329
273
}
330
274
0 commit comments