...
1 package sse
2
3 import (
4 "context"
5
6 "github.com/fsnotify/fsnotify"
7 "k8s.io/klog"
8 )
9
10 func (n *Notifier) watchDevfileChanges(ctx context.Context, devfileFiles []string) error {
11 devfileWatcher, err := fsnotify.NewWatcher()
12 if err != nil {
13 return err
14 }
15
16 go func() {
17 for {
18 select {
19 case <-ctx.Done():
20 klog.V(2).Infof("context done, reason: %v", ctx.Err())
21 cErr := devfileWatcher.Close()
22 if cErr != nil {
23 klog.V(2).Infof("error closing devfileWater: %v", cErr)
24 }
25 return
26 case ev, ok := <-devfileWatcher.Events:
27 if !ok {
28 return
29 }
30 klog.V(7).Infof("event: %v", ev)
31 devfileContent, rErr := n.fsys.ReadFile(n.devfilePath)
32 if rErr != nil {
33 klog.V(1).Infof("unable to read Devfile at path %q: %v", n.devfilePath, rErr)
34 continue
35 }
36 n.eventsChan <- Event{
37 eventType: DevfileUpdated,
38 data: map[string]string{
39 "path": ev.Name,
40 "operation": ev.Op.String(),
41 "content": string(devfileContent),
42 },
43 }
44 if ev.Has(fsnotify.Remove) {
45
46
47
48 wErr := devfileWatcher.Remove(ev.Name)
49 if wErr != nil {
50 klog.V(7).Infof("error removing file watch: %v", wErr)
51 }
52 wErr = devfileWatcher.Add(ev.Name)
53 if wErr != nil {
54 klog.V(0).Infof("error re-adding file watch: %v", wErr)
55 }
56 }
57 case wErr, ok := <-devfileWatcher.Errors:
58 if !ok {
59 return
60 }
61 klog.V(0).Infof("error on file watch: %v", wErr)
62 }
63 }
64 }()
65
66 for _, f := range devfileFiles {
67 err = devfileWatcher.Add(f)
68 if err != nil {
69 klog.V(0).Infof("error adding watcher for path %q: %v", f, err)
70 } else {
71 klog.V(7).Infof("added watcher for path %q", f)
72 }
73 }
74
75 return nil
76 }
77
View as plain text