-
Notifications
You must be signed in to change notification settings - Fork 55
/
filepath_sync.go
176 lines (154 loc) · 4.68 KB
/
filepath_sync.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
package file
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
msync "sync"
"gopkg.in/yaml.v3"
"github.com/fsnotify/fsnotify"
"github.com/open-feature/flagd/pkg/logger"
"github.com/open-feature/flagd/pkg/sync"
)
type Sync struct {
URI string
Logger *logger.Logger
// FileType indicates the file type e.g., json, yaml/yml etc.,
fileType string
watcher *fsnotify.Watcher
ready bool
Mux *msync.RWMutex
}
// default state is used to prevent EOF errors when handling filepath delete events + empty files
const defaultState = "{}"
func (fs *Sync) ReSync(ctx context.Context, dataSync chan<- sync.DataSync) error {
fs.sendDataSync(ctx, sync.ALL, dataSync)
return nil
}
func (fs *Sync) Init(ctx context.Context) error {
fs.Logger.Info("Starting filepath sync notifier")
w, err := fsnotify.NewWatcher()
if err != nil {
return err
}
fs.watcher = w
err = fs.watcher.Add(fs.URI)
if err != nil {
return err
}
return nil
}
func (fs *Sync) IsReady() bool {
fs.Mux.RLock()
defer fs.Mux.RUnlock()
return fs.ready
}
func (fs *Sync) setReady(val bool) {
fs.Mux.Lock()
defer fs.Mux.Unlock()
fs.ready = val
}
//nolint:funlen
func (fs *Sync) Sync(ctx context.Context, dataSync chan<- sync.DataSync) error {
defer fs.watcher.Close()
fs.sendDataSync(ctx, sync.ALL, dataSync)
fs.setReady(true)
fs.Logger.Info(fmt.Sprintf("watching filepath: %s", fs.URI))
for {
select {
case event, ok := <-fs.watcher.Events:
if !ok {
fs.Logger.Info("filepath notifier closed")
return errors.New("filepath notifier closed")
}
fs.Logger.Info(fmt.Sprintf("filepath event: %s %s", event.Name, event.Op.String()))
switch {
case event.Has(fsnotify.Create) || event.Has(fsnotify.Write):
fs.sendDataSync(ctx, sync.ALL, dataSync)
case event.Has(fsnotify.Remove):
// K8s exposes config maps as symlinks.
// Updates cause a remove event, we need to re-add the watcher in this case.
err := fs.watcher.Add(fs.URI)
if err != nil {
// the watcher could not be re-added, so the file must have been deleted
fs.Logger.Error(fmt.Sprintf("error restoring watcher, file may have been deleted: %s", err.Error()))
fs.sendDataSync(ctx, sync.DELETE, dataSync)
continue
}
// Counterintuitively, remove events are the only meaningful ones seen in K8s.
// K8s handles mounted ConfigMap updates by modifying symbolic links, which is an atomic operation.
// At the point the remove event is fired, we have our new data, so we can send it down the channel.
fs.sendDataSync(ctx, sync.ALL, dataSync)
case event.Has(fsnotify.Chmod):
// on linux the REMOVE event will not fire until all file descriptors are closed, this cannot happen
// while the file is being watched, os.Stat is used here to infer deletion
if _, err := os.Stat(fs.URI); errors.Is(err, os.ErrNotExist) {
fs.Logger.Error(fmt.Sprintf("file has been deleted: %s", err.Error()))
fs.sendDataSync(ctx, sync.DELETE, dataSync)
}
}
case err, ok := <-fs.watcher.Errors:
if !ok {
fs.setReady(false)
return errors.New("watcher error")
}
fs.Logger.Error(err.Error())
case <-ctx.Done():
fs.Logger.Debug("exiting file watcher")
return nil
}
}
}
func (fs *Sync) sendDataSync(ctx context.Context, syncType sync.Type, dataSync chan<- sync.DataSync) {
fs.Logger.Debug(fmt.Sprintf("Configuration %s: %s", fs.URI, syncType.String()))
msg := defaultState
if syncType != sync.DELETE {
m, err := fs.fetch(ctx)
if err != nil {
fs.Logger.Error(fmt.Sprintf("Error fetching %s: %s", fs.URI, err.Error()))
}
if m == "" {
fs.Logger.Warn(fmt.Sprintf("file %s is empty", fs.URI))
} else {
msg = m
}
}
dataSync <- sync.DataSync{FlagData: msg, Source: fs.URI, Type: syncType}
}
func (fs *Sync) fetch(_ context.Context) (string, error) {
if fs.URI == "" {
return "", errors.New("no filepath string set")
}
if fs.fileType == "" {
uriSplit := strings.Split(fs.URI, ".")
fs.fileType = uriSplit[len(uriSplit)-1]
}
rawFile, err := os.ReadFile(fs.URI)
if err != nil {
return "", err
}
switch fs.fileType {
case "yaml", "yml":
return yamlToJSON(rawFile)
case "json":
return string(rawFile), nil
default:
return "", fmt.Errorf("filepath extension for URI: '%s' is not supported", fs.URI)
}
}
// yamlToJSON is a generic helper function to convert
// yaml to json
func yamlToJSON(rawFile []byte) (string, error) {
var ms map[string]interface{}
// yaml.Unmarshal unmarshals to map[interface]interface{}
if err := yaml.Unmarshal(rawFile, &ms); err != nil {
return "", fmt.Errorf("unmarshal yaml: %w", err)
}
r, err := json.Marshal(ms)
if err != nil {
return "", fmt.Errorf("convert yaml to json: %w", err)
}
return string(r), err
}