Repository navigation
refactor: refactor sync providers #291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
james-milligan
merged 9 commits into
open-feature:main
from
Kavindu-Dodan:refactor/refactor-sync-providers
Jan 23, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2e41696
remove unused code
Kavindu-Dodan ed42642
structure sync impls in dedicated packages
Kavindu-Dodan cd95948
unify eventing for all syncs
Kavindu-Dodan a8987c5
lint fix
Kavindu-Dodan 4a85352
Refactor isync with simple interface
Kavindu-Dodan b136ebc
Review changes and adding simple sync tests
Kavindu-Dodan 1022a45
Use correct context
Kavindu-Dodan 021eca2
Remove logs when error is returned
Kavindu-Dodan 764255f
Remove subtest from simple tests
Kavindu-Dodan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| package file | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/open-feature/flagd/pkg/sync" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
|
|
||
| "github.com/fsnotify/fsnotify" | ||
| "github.com/open-feature/flagd/pkg/logger" | ||
| ) | ||
|
|
||
| type Sync struct { | ||
| URI string | ||
| Logger *logger.Logger | ||
| ProviderArgs sync.ProviderArgs | ||
| // FileType indicates the file type e.g., json, yaml/yml etc., | ||
| fileType string | ||
| } | ||
|
|
||
| //nolint:funlen | ||
| func (fs *Sync) Sync(ctx context.Context, dataSync chan<- sync.DataSync) error { | ||
| fs.Logger.Info("Starting filepath sync notifier") | ||
|
Kavindu-Dodan marked this conversation as resolved.
Resolved
|
||
| watcher, err := fsnotify.NewWatcher() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer watcher.Close() | ||
|
|
||
| err = watcher.Add(fs.URI) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // file watcher is ready(and stable), fetch and emit the initial results | ||
| fetch, err := fs.fetch(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| dataSync <- sync.DataSync{FlagData: fetch, Source: fs.URI} | ||
|
|
||
| fs.Logger.Info(fmt.Sprintf("Watching filepath: %s", fs.URI)) | ||
| for { | ||
| select { | ||
| case event, ok := <-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 event.Op { | ||
| case fsnotify.Create: | ||
| fs.Logger.Debug("New configuration created") | ||
| msg, err := fs.fetch(ctx) | ||
| if err != nil { | ||
| fs.Logger.Error(fmt.Sprintf("Error fetching after Create notification: %s", err.Error())) | ||
| continue | ||
| } | ||
|
|
||
| dataSync <- sync.DataSync{FlagData: msg, Source: fs.URI} | ||
| case fsnotify.Write: | ||
| fs.Logger.Debug("Configuration modified") | ||
| msg, err := fs.fetch(ctx) | ||
| if err != nil { | ||
| fs.Logger.Error(fmt.Sprintf("Error fetching after Write notification: %s", err.Error())) | ||
| continue | ||
| } | ||
|
|
||
| dataSync <- sync.DataSync{FlagData: msg, Source: fs.URI} | ||
| case fsnotify.Remove: | ||
| // K8s exposes config maps as symlinks. | ||
| // Updates cause a remove event, we need to re-add the watcher in this case. | ||
| err = watcher.Add(fs.URI) | ||
| if err != nil { | ||
| fs.Logger.Error(fmt.Sprintf("Error restoring watcher, file may have been deleted: %s", err.Error())) | ||
| } | ||
| } | ||
| case err, ok := <-watcher.Errors: | ||
| if !ok { | ||
| return errors.New("watcher error") | ||
| } | ||
|
|
||
| fs.Logger.Error(err.Error()) | ||
| case <-ctx.Done(): | ||
| fs.Logger.Debug("Exiting file watcher") | ||
| return nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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{} | ||
|
Kavindu-Dodan marked this conversation as resolved.
Resolved
|
||
| 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 | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.